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
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)
}
Finally, I found that using raster::removeTmpFiles(h = 1) worked well for me.