[英]subsetting raster stack by index using cell values of another raster
I have a 12-layer raster stack consisting of temperature data - one layer for each month. 我有一个由温度数据组成的12层光栅堆栈 - 每个月一层。 I also have one raster each for planting and harvest dates of a certain crop.
我还有一个光栅用于种植和收获某种作物的日期。 I would like to make some calculations of various climate parameters (eg temperature standard deviation), but only within the cropping period between the planting/harvest dates, excluding months outside of this period.
我想对各种气候参数(例如温度标准偏差)进行一些计算,但仅限于种植/收获日期之间的种植期,不包括此期间之外的月份。 All months within the temperature raster stack that do not fall within the cropping period I would like to assign as
NA
for a given cell. 温度光栅堆栈中的所有月份都不在裁剪期内,我希望将其指定为给定单元格的
NA
。 More specifically, I would like to use the cell values within my planting/harvest rasters to subset the cropping months by their index (ie layers 1 through 12) and NOT by their cell values. 更具体地说,我想使用我的种植/收获栅格中的细胞值来按照它们的指数(即第1层到第12层)对种植月份进行子集,而不是通过它们的细胞值。 I am at my wit's end with this one.
我对这个问题已经结束了。 Below is the code I attempted to use on an example dataset, but does not give me the output I want:
下面是我试图在示例数据集上使用的代码,但是没有给出我想要的输出:
set.seed(150)
t1<- raster(ncol=10, nrow=5) #Create rasters
values(t1) = round(runif(ncell(t1),1,20))
t2<- raster(ncol=10, nrow=5)
values(t2) = round(runif(ncell(t2),1,20))
t3<- raster(ncol=10, nrow=5)
values(t3) = round(runif(ncell(t3),1,20))
t4<- raster(ncol=10, nrow=5)
values(t4) = round(runif(ncell(t4),1,20))
t5<- raster(ncol=10, nrow=5)
values(t5) = round(runif(ncell(t5),1,20))
t6<- raster(ncol=10, nrow=5)
values(t6) = round(runif(ncell(t6),1,20))
Planting<- raster(ncol=10, nrow=5) #Create Planting date raster
values(Planting) = round(runif(ncell(Planting),3,5)) # All planting dates are between 3 and 5
t.stack<-stack(t1,t2,t3,t4,t5,t6) #Create raster stack
layer.names<-c(1:6) # Rename raster stack layers from 1 to 6
names(t.stack)<-as.numeric(as.character(layer.names)) # Attempt to coerce raster stack layer names to numeric
names(t.stack) #View new raster stack layer names (they don't seem to be numberic)
t.stack[t.stack[["X1"]]< Planting] <- NA #Attempt to use Planting raster cell values to coerce raster layer index X1 (1st layer) to NA because it lies outside of the range of 3 to 5
head(t.stack[["X1"]]) #View new values of raster layer X1
The output from the 1st raster stack layer ( X1
) is: 第一个栅格堆栈层(
X1
)的输出是:
# 1 2 3 4 5 6 7 8 9 10
# 1 NA 9 10 17 6 8 8 8 9 17
# 2 12 19 16 9 14 19 12 6 5 7
# 3 10 NA NA NA 10 15 6 15 6 12
# 4 20 16 12 5 18 13 13 5 NA 10
# 5 13 14 18 10 16 8 10 NA 20 7
As you can see, I attempted to change the layer names to numeric values and use conditional statements to query them against the cell values in t.stack
. 如您所见,我尝试将图层名称更改为数值,并使用条件语句根据
t.stack
中的单元格值查询它们。 Instead, what happened was that specific cell values within the X1
layer were replaced if they were less than the corresponding cell values in the Planting
raster. 相反,如果它们小于
Planting
栅格中的相应单元格值,则会替换X1
层内的特定单元格值。 However, I want the whole X1
layer in this dataset to be designated NA
because the planting dates only span from 3 to 5, and do not include 1 (ie the first layer). 但是,我希望将此数据集中的整个
X1
图层指定为NA
因为种植日期仅跨越3到5,并且不包括1(即第一层)。 Any thoughts would be appreciated. 任何想法将不胜感激。
Not sure, if I understood you correctly, maybe this helps to get you started: 不确定,如果我理解正确,也许这有助于你开始:
for (i in 1:nlayers(t.stack)) {
tt <- t.stack[[i]][]
tt[i < Planting[]] <- NA
t.stack[[i]][] <- tt
}
Here is a more formal (safer, memory wise) approach: 这是一种更正式(更安全,更记忆)的方法:
set.seed(150)
r <- raster(ncol=10, nrow=5)
tt <- sapply(1:6, function(x) setValues(r, round(runif(ncell(r),1,20))))
t.stack <- stack(tt)
Planting <- setValues(r, round(runif(ncell(r), 3, 5)))
f <- function(p) {
x <- matrix(0, nrow=length(p), ncol=12)
x[cbind(1:nrow(x), p)] <- 1
x <- t(apply(x, 1, cumsum))
x[x==0] <- NA
x
}
p <- calc(Planting, f, filename='planting12.grd')
tp <- t.stack * p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.