0

我已经使用 Rraster包很长时间了,但现在我真的无法解决这个 clusterR 问题。我必须计算 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()在这种情况下完全没用。怎么了?

4

1 回答 1

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()
于 2017-05-26T18:30:27.093 回答