我已经使用R raster
包很长时间了,但是现在我真的无法理解这个集群R问题。我必须为netCDF栅格计算SPI索引。对每个单元格执行此操作,获取单元格时间序列并返回该单元格的SPI索引时间序列。
示例输入文件(大约4MB)可以在这里找到。
请看下面的代码:
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()