简体   繁体   English

raster :: stackApply仅返回NA值

[英]raster::stackApply returns only NA values

I have a RasterBrick that contains mean values one layer for each month in a 72-year interval. 我有一个RasterBrick ,其中包含一个RasterBrick ,该平均值在72年的间隔中为每个月一层。 I want to get the mean value for each year - ie return a 72-layer RasterBrick . 我想获取每年的平均值,即返回72层RasterBrick

The following code has worked on other similar rasters, yielding the expected results (found here ): 以下代码已在其他类似的栅格上工作,产生了预期的结果(在此处找到):

data <- raster::brick(".../air.mon.mean.nc", varname = "air")

index <- format(as.Date(raster::getZ(data), format = "%Y-%m-%d %H:%M:%S"), format = "%Y")
index <- as.numeric(index)

yearly <- raster::stackApply(data, index, fun = mean) 

> yearly
class      : RasterBrick 
dimensions : 360, 720, 259200, 72  (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5  (x, y)
extent     : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
source     : C:/Users/villar/AppData/Local/Temp/RtmpAbUQQT/raster/r_tmp_2019-08-05_102157_18368_64365.grd 
names      : index_1948, index_1949, index_1950, index_1951, index_1952, index_1953, index_1954, index_1955, index_1956, index_1957, index_1958, index_1959, index_1960, index_1961, index_1962, ... 
min values :         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA, ... 
max values :         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA,         NA, ... 

However, when running on this data, it returns only NA values. 但是,对这些数据运行时,它仅返回NA值。

fun = function(x, na.rm) {sum(x)/12} does not work, nor does adding na.rm = TRUE . fun = function(x, na.rm) {sum(x)/12}不起作用,也不能添加na.rm = TRUE

Any help would be greatly appreciated! 任何帮助将不胜感激!

The data is downloaded from here (air.mon.mean.nc). 数据从此处下载(air.mon.mean.nc)。

I have no idea why raster behaves like this. 我不知道为什么raster会像这样。 I can reproduce it. 我可以复制它。 However, if you can use CDO , you could use the operator yearmean to get the output you want: cdo yearmean input.grb output.grb . 但是,如果可以使用CDO ,则可以使用运算符yearmean获取所需的输出: cdo yearmean input.grb output.grb This will also be faster than any R implementation using a single core. 这也将比使用单个内核的任何R实现更快。

If you prefer to stay inside R, I suggest you take a look at the new-but-awesome stars package. 如果您喜欢留在R内,建议您看看新的但很棒的stars套票。

You could do something like this: 您可以执行以下操作:

library(stars)

s = read_stars(s)

yrs = st_get_dimension_values(s, 'time') %>% format('%Y') %>% as.numeric
mymean = function(v, indices, fun = mean, na.rm = FALSE) {
    sapply(unique(indices), function(i) fun(v[indices == i], na.rm = na.rm))
}

yearly = st_apply(s, 1:2, mymean, indices = yrs, na.rm = TRUE)

Optionally you could also do this multicore (see ?st_apply ) but I doubt it would be faster than barebones CDO anyway. 可选地,您也可以执行此多核操作(请参阅?st_apply ),但是我怀疑它会比准系统CDO更快。

I had the same problem but this It worked for me calc(data,fun=function(x) { by(x, index, sum)}) . 我有同样的问题,但是它对我calc(data,fun=function(x) { by(x, index, sum)}) Could you solve it with the stackApply function? 您可以使用stackApply函数解决它吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM