I have about 50.000 small glacier rasters (rectangular shape) that need to be clipped to the glacier's extent. I have the glacier polygons stored in a large SpatialPolygonsDataFrame.
I want to use parallel computing or else it would take forever, I think. For previous tasks I've successfully used the mcmapply
function, but I am open for other approaches.
My (admittedly rudimentary) code so far is:
filenames <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=F)
filelocations <- list.files("/.../RGI60-13_reproj/", pattern="*.tif", full.names=T)
glaciers <- readOGR("/.../13_rgi60_CentralAsia.shp",verbose=TRUE)
fun_clip <- function(filelocations, filenames, glaciers){
r <- raster(filelocations)
r <- crop(r,glaciers) # here I need to clarify the corresponding shp in the SPDF
writeRaster(r, paste0("/.../RGI60-13_crop/",filenames))
}
mcmapply(fun_proj, filelocations, filenames, mc.cores = 50)
How can I give the crop
-function the right iterative arguments? filelocation
is of the same length as glaciers
, so in a for-loop
I would use something like
r <- crop(r,glaciers[1]glaciers[i])
, but how do I pass the iteration in my kind of function?
What would be the way to introduce the i
, so to speak?