Search code examples
rplyrstring-matchingraster

use string matching to read files and apply function


I have a folder filled with geotiffs that I'd like to load with raster and extract values from. The .tif are 01APR2010.tif, 02APR2010.tif, 01MAR2010.tif etc. I'd like to read the files in chronological order (by day) so I can stay organized with the values. I have 01MAR to 30AUG for 2000-2011.

I have SpatialPointsDataFrame called lsa, usa, and alp with coordinates that I want corresponding values for. I've gotten as far as this for reading the files (thanks to other questions/answers on SO)

library(raster)
library(plyr)
year=2010
site=list(lsa,usa,alp)

rcn.extract.swe<-function(r,site){
projection(r)=CRS('+proj=longlat +datum=NAD83')
return(mean(extract(r,coordinates(site)[,1:2])))
}    

temp_geotiffs=getwd()#location of geotiffs
filenames <- list.files(temp_geotiffs, pattern="*.tif", full.names=TRUE)
ldf <- llply(filenames, raster)
res <- outer(ldf,site,Vectorize(rcn.extract.swe)) #this gives a matrix

maybe there is a better way? I also tried mapply but outer does all combinations

here are some variables to use:

lsa=structure(c(-105.593648807562, -105.593721469436, -105.593667442221, 
        -105.593755471559, -105.593796239505, -105.59393253876, -105.593864787348, 
        -105.594020998966, -105.593758049064, 40.0538437274748, 40.0538836912761, 
        40.0540185900867, 40.0540081923506, 40.0541962757639, 40.0542507971756, 
        40.0544078670555, 40.054427734347, 40.0540699483592), .Dim = c(9L, 
        2L), .Dimnames = list(NULL, c("coords.x1", "coords.x2")))

        usa=structure(c(-105.580580448185, -105.580661572731, -105.58071127033, 
        -105.580955908821, -105.580938632289, 40.0514730866613, 40.0513509958902, 
        40.0512184221927, 40.0513010575266, 40.0514244681361, 3437.769, 
        3434.92, 3431.876, 3434.075, 3436.987), .Dim = c(5L, 3L), .Dimnames = list(
        NULL, c("coords.x1", "coords.x2", "coords.x3")))

    alp=structure(c(-105.593648807562, -105.593721469436, -105.593667442221, 
    -105.593755471559, -105.593796239505, -105.59393253876, -105.593864787348, 
    -105.594020998966, -105.593758049064, 40.0538437274748, 40.0538836912761, 
    40.0540185900867, 40.0540081923506, 40.0541962757639, 40.0542507971756, 
    40.0544078670555, 40.054427734347, 40.0540699483592, 3538.319, 
    3539.285, 3541.733, 3541.962, 3545.753, 3547.899, 3550.574, 3552.051, 
    0), .Dim = c(9L, 3L), .Dimnames = list(NULL, c("coords.x1", "coords.x2", 
    "coords.x3")))

r=raster(matrix(runif(100,0,3),nrow=20),xmn=-112.25,xmx=-104.125,ymn=33,ymx=43.75)
writeRaster(r,'01APR2010.tif')
writeRaster(r,'02APR2010.tif')
writeRaster(r,'03APR2010.tif')
writeRaster(r,'04APR2010.tif')
writeRaster(r,'01MAR2010.tif')
writeRaster(r,'02MAR2010.tif')
writeRaster(r,'03MAR2010.tif')

your help is appreciated!


Solution

  • It is not clear why you want to order your data since the outer will do all the combinations. You can order your files names by extracting the date part from and converting it to correct date. For example:

    full.filenames <- list.files(pattern="*.tif", full.names=TRUE)
    filenames <- list.files(pattern="*.tif")
    full.filenames[order(as.Date(tolower(gsub('[.].*','',filenames)),
                                                  '%d%b%Y'))]
    
    "./01MAR2010.tif" "./02MAR2010.tif" "./03MAR2010.tif" "./01APR2010.tif"
    [5] "./02APR2010.tif" "./03APR2010.tif" "./04APR2010.tif"