[R-sig-Geo] Problem creating raster brick

Lyndon Estes lyndon.estes at gmail.com
Mon Nov 21 04:38:17 CET 2011


Dear List,

I am having trouble creating a brick out of a raster stack with 9
grids of 2.5 mb each.  My goal in creating the brick is to hopefully
speed up calculation of quantiles for each grid location, which runs
very slowly as follows:

q.10 <- calc(stack1, function(x) quantile(x, 0.1))

I was hoping this would be faster (and I will appreciate any advice on
speeding this up)

q.10 <- calc(brick1, function(x) quantile(x, 0.1))

But I can't get to it, and I am not sure why.

For instance, I have no problem bricking these dummy rasters, which
are a bit bigger than my own data.

library(raster)
library(rgdal)

r.list <- lapply(1:9, function(x) {
  r <- raster(nrow = 800, ncol = 800)
  r[] <- rnorm(ncell(r))
  r <- writeRaster(r, filename = paste("test", x, ".tif", sep = ""),
overwrite = T)  # Using geotiffs
  return(r)
})
r.stack <- stack(lapply(1:9, function(x) r.list[[x]]))
r.brick <- brick(r.stack)  # Works fine, takes about 30 seconds - 1 minute

# However, when I try the same basic thing with my own rasters,
# my system goes into a hang with and R seems to chew up all
# remaining memory (~2.5 GB).

# Here's an example using one of my rasters
link <- "http://www.princeton.edu/~lestes/mri.A2.yld.a.r.tif"
download.file(url = link, destfile = "r.tif", method = "wget")
r2 <- raster("r.tif")
r.list2 <- c(r2, lapply(1:8, function(x) {
  rst <- writeRaster(r2, filename = paste("r.", x + 1, ".tif", sep = ""))
}))  # Duplicate the raster 8 times and make a list out of 9
r2.s <- stack(lapply(1:9, function(x) r.list2[[x]]))  # Stack them
r2.brick <- brick(r2.s)  # This hangs the system

I am not sure why this is happening, but maybe it has to do with the
file itself (assuming I am not coding something incorrectly).  Might
it have to do with the NA values in it?

I will appreciate any pointers on fixing this and on the quantile
calculation method.

Thanks in advance.  My sessionInfo follows the signoff.

Cheers, Lyndon

R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C/en_US.UTF-8/C/C/C/C

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets
methods   base

other attached packages:
 [1] raster_1.9-33    doBy_4.4.2       MASS_7.3-16      snow_0.3-7
  lme4_0.999375-42 Matrix_1.0-1     lattice_0.20-0   multcomp_1.2-8
 [9] mvtnorm_0.9-9991 R2HTML_2.2       survival_2.36-10 rgdal_0.7-1
  sp_0.9-91

loaded via a namespace (and not attached):
[1] grid_2.14.0   nlme_3.1-102  stats4_2.14.0 tools_2.14.0



More information about the R-sig-Geo mailing list