在RasterBrick中的分组RasterLayers上运行stackApply

时间:2019-08-07 11:01:07

标签: r raster

我有一个RasterBrick,一年中的每个月都包含一个RasterLayer。我需要添加每个连续三个月(Jan + Feb + March,Feb + March + April等)的值。我曾考虑过使用Raster::stackApply,但还没有弄清楚如何获得正确的indices

可以下载数据here,并且我已经完成了以下数据准备:

data <- raster::brick(".../precip.mon.total.1x1.v2018.nc")

data <- data[[which(raster::getZ(data) >= as.Date("1946-01-01") 
                    & raster::getZ(data) <= as.Date("2016-12-01"))]]

indices <- format(as.Date(names(data), format = "X%Y.%m.%d"), format = "%m")
indices <- as.numeric(indices)

# Count number of times max value occurs for each month in each cell 
data <- raster::stackApply(data, indices, 
                           fun = function(x, na.rm = na.rm {sum(raster::which.max(x))}) 
names(data) <- month.abb

> data
class      : RasterBrick 
dimensions : 360, 720, 259200, 12  (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec 
min values :   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0 
max values :  71,  71,  71,  71,  71,  71,  71,  71,  71,  71,  71,  71 

每三个连续月份正确地加总的任何帮助将不胜感激!我认为输出应该是这样的:

> outputData
class      : RasterBrick 
dimensions : 360, 720, 259200, 12  (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : Jan-Mar, Feb-Apr, Mar-May, Apr-Jun, May-Jul, Jun-Aug, Jul-Sep, Aug-Oct, Sep-Nov, Oct-Dec, Nov-Jan, Dec-Feb 

0 个答案:

没有答案