计算超过阈值的三个连续值的数量(在栅格堆栈中) [英] Calculating number of three consecutive values above a threshold (in raster stack)

查看:198
本文介绍了计算超过阈值的三个连续值的数量(在栅格堆栈中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当栅格堆栈(x)中每个像素的值超过给定阈值(由另一个栅格y定义)时,我需要计算连续三天的数量.在将xy一起堆叠到新栅格a中之后,我尝试将rlecalc一起使用,如下所示:

I need to compute number of three consecutive days when value of each pixel in a raster stack (x) is above a given threshold (defined by another raster y). I tried using rle for the purpose with calc as follows after stacking x and y together into new raster a:

library(raster)    
fn<-function(a) with(rle(a), sum(lengths>=3 & values>a[[nlayers(a)]]))
calc(b,fn)

但是,我得到了错误:

.calcTest(x [1:5],fun,na.rm,forcefun,forceapply)中的错误:
无法使用此功能

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function

可重现的样品:

x1 <- raster(nrows=10, ncols=10)
 x2=x3=x4=x5=x6=x1
 x1[]= runif(ncell(x1))
 x2[]= runif(ncell(x1))
 x3[]= runif(ncell(x1))
 x4[]= runif(ncell(x1))
 x5[]= runif(ncell(x1))
 x6[]= runif(ncell(x1))
 x=stack(x1,x2,x3,x4,x5,x6)
 y=x1
 y[]= runif(ncell(x1))
 a<-stack(x,y)

有人可以帮忙吗?

推荐答案

您可以尝试以下方法:

 x1 <- raster(nrows=10, ncols=10)
 x2=x3=x4=x5=x6=x1
 x1[]= runif(ncell(x1))
 x2[]= runif(ncell(x1))
 x3[]= runif(ncell(x1))
 x4[]= runif(ncell(x1))
 x5[]= runif(ncell(x1))
 x6[]= runif(ncell(x1))
 x=stack(x1,x2,x3,x4,x5,x6)*4
 y=x1
 y[]= runif(ncell(x1))*2
 a<-stack(x,y)


 library(raster)

fn<-function(x) {
  seq <- rle(as.numeric(x)>as.numeric(x)[[nlayers(a)]])
  n = length(seq$lengths > 3 & seq$values == TRUE)
  return(n)
}
calc(a,fn)

class       : RasterLayer 
dimensions  : 10, 10, 100  (nrow, ncol, ncell)
resolution  : 36, 18  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 1, 7  (min, max)

(请注意,我修改了示例数据集以获取一些良好的序列)

(note that I modified the example dataset to get some good sequences)

HTH!

这篇关于计算超过阈值的三个连续值的数量(在栅格堆栈中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆