[R-sig-Geo] Faster way of calculating the mean of multiple ascii files?

Swen Meyer s.meyer at lmu.de
Mon Nov 26 16:51:15 CET 2012


Dear All,
I´m trying to process a couple of ascii grids (daily grids): size of one 
grid (344kb). the header of the ascii grids look like this:
ncols 288
nrows 199
xllcorner 506255
yllcorner 4.35899e+006
cellsize 30

I have daily grids over 30 years and I like to find a fast way of 
calculating the monthly means for every month of those 30 years:
the file structure is like this: grid1: "sb05_dem30m_20410101.024"
grid2: "sb05_dem30m_20410102.024"
grid3: "sb05_dem30m_20410103.024"
...
gridn: "sb05_dem30m_YYYYMMdd.024"
...
last grid: "sb05_dem30m_20701231.024"

At the moment I´using R Vers. 2.15.1 on a Intel PC i5 CPU 760@ 2.80GHz 
2.80GHz with 4GB RAM Windows7, and the raster package and this is my 
script:
library(raster)

setwd("G:\\hydro_modelling\\Sardinia\\Wasim\\RioCostara30m\\KNMI_REF\\output30m\\RUN2\\UNSATZON\\") 

Path="G:\\hydro_modelling\\Sardinia\\Wasim\\RioCostara30m\\KNMI_REF\\output30m\\RUN2\\SOILMOIST\\" 



x="sb05_dem30m_"
#list files of folder with string x
Files=list.files(pattern=x)
# select files to process
Files=Files[which((substring(Files,nchar(Files)-3,nchar(Files)))==".024")]

for( i in unique(substring(Files,(nchar(Files)-9),nchar(Files)-6))){
print(i)
#stack files
X_01 <- 
stack(Files[which(substring(Files,(nchar(Files)-9),nchar(Files)-6)==i)])
#mean the grid with raster package
X_01_sum <- mean((X_01))
#Write monthly mean asci grid
writeRaster(X_01_sum, 
filename=paste(Path,"SoilMoisture_RM_Mean_",i,sep=""), "ascii")
}

The script works, but it takes more than 30 min until the grids are 
precessed. Does anyone know of a faster way of meaning (summing) those 
grids and exporting them as a asci again?
Best regards and thank you in advance,
Swen

-- 
--------------------------------------------------------------
Dipl. - Geogr. Swen Meyer
Department of Geography
Physical Geography and Environmental Modeling Ludwig-Maximilian University of Munich

Luisenstrasse 37
80333 Munich/Germany
fon: +49 (0)89 2180-6665
fax: +49 (0)89 2180-6675
email: s.meyer at lmu.de
web: www.geographie.uni-muenchen.de



More information about the R-sig-Geo mailing list