I am trying to process several thousand GeoTIFF images in R with a simple calc operation (convert temperature from Kelvin to degree Celsius) from the raster package.
The calculation is working but I need the original input filenames as output to assign the images afterwards.
With my current code just the first filename in the list is taken and the output file is overwritten constantly.
Complete list of files with path to use 'calc':
library(raster)
files <- list.files(path="myInputPath",
pattern="1km.tif$",
full.names=T)
List of filenames without the path to name the output data:
filenames <- list.files(path="myInputPath",
pattern="1km.tif$",
full.names=F)
Two loops to process the data and name the output:
for(j in seq(filenames)){
for(i in seq(files)){
celsius <- calc(x=raster(files[i]), fun=function(x){x*0.02-273.15})
writeRaster(celsius,
filename=paste0("myOutputPath",#path where the data should be stored
filenames[j],#filenames from list
"_celsius",#suffix to distinguish from input data
".tif"),
overwrite=T)
}
}
Ouch, do you realize you are re-processing each of your files as many times as you have files? That is, the inside of the two loops executes i^j
times (which is i^2
), though you only need it to execute i
times.
How about this (not really tested):
infiles <- list.files(path="myInputPath",
pattern="1km.tif$",
full.names=TRUE)
outfiles <- file.path("myOutputPath", paste0(basename(infiles), "_celsius.tif"))
for (j in seq_along(infiles)) {
celsius <- calc(x=raster(infiles[j]), fun=function(x) x*0.02-273.15)
writeRaster(celsius, filename=outfiles[j], overwrite=TRUE)
}