我正在处理.nc文件中组织的气候数据的日常观察.我使用光栅包的stack命令读取它们.每个文件(对应一年)都是具有以下特征的RasterStack元素:
class : RasterStack
dimensions : 360, 720, 259200, 365 (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
Run Code Online (Sandbox Code Playgroud)
每一层都是一天价值的栅格.
我想总结各层,以计算月度值.我相信解决方案应该使用calc或stackApply {raster},但我找不到从层x到层y求和的方法或者在求和之前将RasterStack子集化的方法.
我准备了一个只有12层的示例文件(以减小大小).
我不知道如何提出代码,对不起,但它应该是这样的:
library(raster)
setwd("myfolder")
data<-stack(mydata.nc)
datasum<- stackApply(data, ??? ,fun=sum)
Run Code Online (Sandbox Code Playgroud)
谢谢
你可以stackApply用来做这件事.使用示例数据,看起来每个栅格图层的名称都是日期.您可以使用它来构建需要传递给的索引stackApply.
1月份的指数清单需要31个1天等.
你可以这样做:
#get the date from the names of the layers and extract the month
indices <- format(as.Date(names(data), format = "X%Y.%m.%d"), format = "%m")
indices <- as.numeric(indices)
#sum the layers
datasum<- stackApply(data, indices, fun = sum)
Run Code Online (Sandbox Code Playgroud)
结果将是12层的光栅堆栈.
要从堆栈中对栅格图层进行子集,您可以这样做 data[[c(1,2]]
| 归档时间: |
|
| 查看次数: |
5942 次 |
| 最近记录: |