0

我从 NOAA (CPC Unified guage) 下载了 netcdf 格式的月降水量数据。数据以毫米/天为单位存储,我需要将每一层乘以它们各自月份的计数,以获得该月的总降水量。例如,1969 年 9 月的图层将乘以 30,而 2 月的图层将乘以 28。此外,当二月的图层必须乘以 29 时,存在闰年的问题。

我尝试了一些代码(见下文)但不工作。

prec <- brick(precip.V1.0.mon.mean.nc)                                                         
conv <- function(x, ...) {
  ifelse(x == c(01, 03, 05, 07, 08, 09, 10, 12), 
         31 * x, 
         x, 
         ifelse(x == c(04, 06, 11), 
                30 * x, 
                x, 
                ifelse(x == 02, 
                       28 * x, 
                       x)))
  } # function for the conversion

## pulling out date indices for each month
indices <- as.numeric(format(as.Date(names(prec), format = "X%Y.%m.%d"), format = "%m")) 

## applying the function using stackApply
new_prec <- stackApply(prec, indices, fun = conv) 

ifelse(x == c(1, 2, 3, 5, 7, 8, 9, 10, 12), 31 * x, x, ifelse(x == : 未使用的参数 (ifelse(x == c( 4, 6, 11), 30 * x, x, ifelse(x == 2, 28 * x, x)))

当我尝试使用简化版本(例如) ifelse(x == 12, 30*x, x) 传递函数时,出现以下错误

错误:(function (..., deparse.level = 1) 中的错误:矩阵的行数必须匹配(参见参数 2)

我想知道是否有人有转换的提示。

这是数据子集的链接:https ://fil.email/VahwQq8p

4

0 回答 0