I've been using the R raster
package for a long time, but now I really can't get my head around this clusterR problem. I have to compute the SPI index for a netCDF raster. This is done for each cell, taking the cell timeseries and returning the SPI index timeseries for that cell.
The example input file (about 4MB) can be found here.
See the below code:
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()
is completely useless in this case.
What is wrong?
This worked for me :
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()