clusterR returns "argument is of length zero"

clusterR returns "argument is of length zero"

我已经使用 R raster 包很长时间了,但现在我真的无法解决这个 clusterR 问题。我必须计算 netCDF 栅格的 SPI 索引。这是为每个单元完成的,获取单元时间序列并返回该单元的 SPI 索引时间序列。

可以找到示例输入文件(大约 4MB)here

见以下代码:

library(raster)
library(SPEI)

calcspi <- function(pr) { #this function calculates the SPI index for each timeseries of values
    pr <- as.numeric(pr)
    if (all(is.na(pr[1:20]))) { #Check that this is not an NA cell
        outspi <- rep(NA, length(pr))
    } else {
        outspi <- fitted(spi(pr, 12, na.rm=TRUE))
    }
    return(outspi)
}

b <- brick("input_crop.nc", varname="pr")
readAll(b) #As requested in the comments

###THIS WORKS BUT IS SLOW:
bc <- calc(b, calcspi)

###THIS DOES NOT:
beginCluster(n=4)

bc <- clusterR(b, calc, args=list(fun="calcspi"))
#[1] "argument is of length zero"
#attr(,"class")
#[1] "snow-try-error" "try-error"
#Error in clusterR(b, calc, args = list(fun = "calcspi")) : cluster error

endCluster()

###THIS DOESN'T EITHER:
beginCluster(n=4)

f <- function(x) calc(x, calcspi)
bc <- clusterR(b, f)
#[1] "argument is of length zero"
#attr(,"class")
#[1] "snow-try-error" "try-error"
#Error in clusterR(b, f) : cluster error

endCluster()

traceback() 在这种情况下完全没用。 怎么了?

这对我有用:

b <- mybrick
#readAll(b) #As requested in the comments
#parallel processing 
ff <- function(x) calc(x, calcspi)
beginCluster(8)
bc <- clusterR(b, fun = ff,export='calcspi')
endCluster()