0

I am working with two global raster stacks, each containing 60 layers using the R Software. The layers are global raster files (in GTiff format) at a resolution of 5 min of arc. The first stack contains cultivated areas for 60 crops, while the second stack contains the grid-cell yield of the same 60 crops.

my objective is to multiply the two raster stacks, in order to obtain the production for the 60 crops for each grid-cell. I am running this calculation using the raster package and using its parallelizing function clusterR together with the fuction calc. I am running the process using 24 cores. The code looks like this:

rasters_production <- clusterR(rasters_land_use_hectars, calc, args=list(fun=function(x){x*rasters_yield}),
                        export="rasters_yield", exclude="parallel", filename=paste0("Script_generated_files/", "organic_rasters_production_1.tif"), format="GTiff", overwrite=T, cl=cluster)

When I run it, the code generate the following error:

Error in clusterR(rasters_land_use_hectars, calc, args = list(fun = function(x) { : cluster error

Nevertheless, if I run the same multiplication but multiplying a raster stack for a vector of 60 coefficient (and not for a second raster stack), the calculation works correctly?

Does somebody know why the calculation between the two raster stacks does not work instead?

1

if you look at the documentation for ClusterR you'll see you need a couple extra lines of code, like:

beginCluster(cpus = n)
x <- clusterR(stuff)
endCluster()

you may also need forceapply = TRUE in your calc function, unless I'm misreading what you're trying to do.

  • Thank you for the hints. Nevertheless, also including forceapply = TRUE the code does not work. To my understanding, I think there should be a problem with the master branch when coming to summaring the results of the different chunks. Any idea about any reason why that might happen? – PietroB Apr 19 '17 at 8:06
0

You can also use mclapply in parallel package, where NUM_CORES is the total number of cores you wanna use.

 rasters_production <- stack(mclapply(1:nlayers(rasters_yield)), function(i){ 
        rasters_land_use_hectars[[i]] * rasters_yield[[i]]
    }, mc.cores = NUM_CORES))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.