Thiago,

One approach to speed things up is to avoid reading and writing of files by
combining computations in a single function.

# define a function
fun <- function(x) {
x[x>249] <- NA
rowSums(x / 10, na.rm=FALSE)
}

# stack the files needed
s <- stack('lai200101.tif', 'lai200109.tif', 'lai200117.tif',
'lai200125.tif')
# compute
lai_jan_2001 <- calc(s, fun, filename="/home/thiago/Desktop/lai_jan_2001.nc",
overwrite=TRUE)


Now it should be relatively straightforward to loop over years & months

files <- list.files(pattern='.tif',  full.names=TRUE)
years <- substr(files, 1, 4)
days <- substr(files, 5, 2)
for (y in 2001:2011) {
  for (m in 1:12) {
      f <- files[ years == y]
     # and now some lookup to get the days for the month and extract the
corresponding files
     f <- f[   ]
     # stack these
     s <- stack(f)
     fout  <- paste('/home/thiago/Desktop/lai_', y,  '_', m, '.nc', sep='')
     x <- calc(s, fun, filename=fout)
  }
}


Hth, Robert

On Tue, May 1, 2012 at 9:50 AM, Thiago Veloso <thi_veloso@yahoo.com.br>wrote:

> Sorry for the two last messages: I have no ideia why the text arrives
> unformatted and scrambled! Hope it is fixed this time!
>
> Dear R experts,
>
> Recently, I have prepared a comprehensive MODIS imagery database. Ten
> years of LAI images (50GB of data) encompassing South America were
> carefully acquired and resampled/reprojected with MRT tool.
>
> In the end, I came up with 8-day composition images like this:
>
> lai200101.tif
> lai200109.tif
> lai200117.tif
> lai200125.tif
>        .
>        .
>        .
> lai2011361.tif
>
> following the naming logic of "lai{year}{julian day}.tif".
>
> As a final step, I need to multiply each image by its respective
> calibration factor. This can be done in R:
>
> lai[lai>249]<-NA # this eliminates everything which is tagged as
> non-vegetation
> lai_cal<-lai*0.1
>
> After that, I would like to perform monthly integrations and save a netcdf
> file, something like this:
>
> lai_jan_2001 <- lai200101.tif + lai200109.tif + lai200117.tif +
> lai200125.tif
> lai_feb_2001 <- lai200133.tif + lai200141.tif + lai200149.tif +
> lai200157.tif
> if(require(ncdf)){
> writeRaster(lai_jan_2001,filename="/home/thiago/Desktop/lai_jan_2001.nc
> ",format="CDF",overwrite=TRUE)
> writeRaster(lai_feb_2001,filename="/home/thiago/Desktop/lai_feb_2001.nc
> ",format="CDF",overwrite=TRUE)
> }
>
> and so on, for each month in the 2001~2011 period. I work on a HP
> Workstation with 6GB RAM and running Ubuntu 11.04 64bits and R 2.15.0. My
> current attempt to load and process one single image takes long instants:
>
> require(raster)
> require(rgdal)
> lai<-raster("/home/thiago/MODIS/LAI/200101.tif")
> lai[lai>249]<-NA
> lai_cal<-lai*0.1
> plot(lai) # optional step, but this takes LOTS of seconds to process
> #Writing out calibrated image to a netcdf file
> if(require(ncdf)){
> writeRaster(lai_cal,filename="/home/thiago/Desktop/lai_cal.nc
> ",format="CDF",overwrite=TRUE)
> }
>
> Also, I run out of ideas to process all the images in batch. I wonder if
> anyone can suggest the most effective approach, in terms of reducing both
> disk usage and time consumption, to process the entire database.
>
> All the best,
> Thiago Veloso.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

