1

I am very new to R and to raster package and I was hoping that someone could provide some guidance. I am trying to run a function that averages a series of rasters, 300 to be exact, they are in geotif format and each is of them has 44 megabytes. For this I am using a raster stack and some parallelization tool in R to make it faster. The problem I run is that when I try this on a stack that has 70 or more rasters R stops responding, I left it overnight and the still no response. Are there any limitations of the number of rasters/size I can include in a stack? The computer I am using is a Windows server with 40 cores and 384 gigabytes of ram.

Below the code I am using:

library(spatial.tools)
library(snow)
library(parallel)
library(gdalUtils)
library(raster)

# Funciones
ff <- function(x) mean(x, na.rm=T)

# ......

setwd('S:/Miguel_Fernandez/MODIS_GEOTIFF')

lista_1.tif <- list.files(pattern="_1.tif$")


# test with tiffs
beginCluster(n=40)
for (i in 1:12){
  nn <- (i*25)
  test50_stack_1 <- stack(lista_1.tif[1:nn])
  t.ini <- Sys.time()

  res <- clusterR(test50_stack_1, fun = calc, args=list(fun=ff))

  print(paste ('loop', nn, Sys.time()-t.ini))
}
endCluster()
  • While not an actual answer, you could work your way around the issue by simplifying your approach. Simply make a empty raster with the same size as your MODIS images, and then add all your MODIS images to that by simply using +, and then in the end, divide it by the number of rasters added giving your the average. No need for all the fancy bits. You can add functionality to deal with NAs if you want. – Mikkel Lydholm Rasmussen Apr 18 '16 at 9:16
  • Thank you Mikkel for the comment, however the function that calculates the mean is only an example. Ideally I would like to run more complex functions using a stack, but if there are limitations to the number/size of rasters then I will have to look for alternatives besides raster package – Miguel Apr 20 '16 at 7:45

Your Answer

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

Browse other questions tagged or ask your own question.