我一直在使用R raster
软件包很长一段时间,但现在我真的无法理解这个clusterR问题。我必须计算netCDF栅格的SPI索引。这是针对每个单元格完成的,获取单元时间序列并返回该单元格的SPI索引时间序列。
可以找到示例输入文件(大约4MB){。{3}}。
见下面的代码:
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()
完全没用。
有什么问题?
答案 0 :(得分:1)
这对我有用:
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()