Search code examples
rcachingraster

How can we clear the cache when raster processing


I use R raster and rLandsat8 package to process some Landsat 8 remote sensing image. When I wrote a loop to covert DN to Radiance for 10+ images, it takes more than 50G of harddisk.

So, How can we clear the cache when using raster after each image processing finished?

For example, In the loop can we do

  1. processing first image
  2. clear the cache
  3. processing second
  4. clear...

    for (i in l8.lst) {
        sceneName  <- i$metadata$landsat_scene_id
        if (!file.exists(file.path(dir.toaRad, sceneName))) {
             dir.create(file.path(dir.toaRad, sceneName), recursive = T)
        }
      for(j in bandnames){
         idx <- seq_along(bandnames)[sapply(bandnames, function(x) j %in% x)] # a number
         bandidx <- paste0("file_name_band_", idx)
         bandName <-  sapply(i, "[[", bandidx)[[1]]
         Rad.rst  <- ToTOARadiance(i, j)
         writeRaster(Rad.rst, filename = file.path(dir.toaRad, sceneName, bandName), overwrite = T)
        }
    

Solution

  • Finally, I found that using raster::removeTmpFiles(h = 1) worked well for me.