R/readCache.R

Defines functions readCache

### readCache function
# checks if the data is already available
# has to check whether arguments agree as well

readCache <- function(){
  
  # use the arguments of the original function as file name (e.g. 0.5_0.5 for the resolution)
  
  # only certain parts of the arguments should be used for creating the file name
  # only those that are arguments of the function
  arg <- names(as.list(sys.function(1))[-length(as.list(sys.function(1)))])
  
  # rCache and wCache should not be considered
  arg <- arg[!arg %in% c("rCache", "wCache")]
  
  filename <- paste(as.list(sys.frame(1))[arg], collapse="_")
  
  ## get the name of the original function (e.g. readFF)
  funcname <- strsplit(as.character(rev(sys.calls())[[2]]), split = "(", fixed=T)[[1]]
  
  # the filename is constructed out of these
  filedir <- paste0(geodata$config$mainfolder, "/cache/", funcname, "/")
  
  x <- NULL
  if ( file.exists(paste0(filedir,filename, ".tif")) ) {
    print(paste0("Loading cache file from: ", filedir,filename, ".tif"))
    x <- raster::raster(paste0(filedir,filename, ".tif"))
  # } else if ( file.exists(paste0(filedir,filename, ".shp")) ) {
  #   out <- rgdal::readOGR(dsn=paste0(filedir,filename, ".shp"), layer=filename) 
  # }
    } else if ( file.exists(paste0(filedir,filename, ".rda")) ) {
      print(paste0("Loading cache file from: ", filedir,filename, ".rda"))
      load(file=paste0(filedir,filename, ".rda"))
    }
  
return(x)
}
pik-piam/geodata documentation built on Nov. 5, 2019, 12:21 a.m.