[R-sig-Geo] Calc function on a stack slow?
Els Ducheyne
educheyne at avia-gis.be
Fri Sep 9 12:45:28 CEST 2011
Dear r-sig-geo list
I have a number of rasters with dim = that I need to convert from Kelvin to Celsius. I though that stacking the layers and then perform a calculate would be the rrick. While this works, it is quite slow, so i wonder if there are faster ways in R to do this?
See below for code
Thanks for help
R.Version()
$platform
[1] "i386-apple-darwin9.8.0"
$arch
[1] "i386"
$os
[1] "darwin9.8.0"
$system
[1] "i386, darwin9.8.0"
$status
[1] "Patched"
$major
[1] "2"
$minor
[1] "11.1"
$year
[1] "2010"
$month
[1] "08"
$day
[1] "30"
$`svn rev`
[1] "52862"
$language
[1] "R"
$version.string
[1] "R version 2.11.1 Patched (2010-08-30 r52862)"
and raster package 1.7-29
require(raster)
#get all the inputdata
start <- Sys.time()
inputlist <- list.files(pattern="lstday")
datadisplay <- seq(1,length(inputlist),16)
datadisplay <- c(datadisplay,length(inputlist))
n <- length(datadisplay)-1
for (i in 1:n){
input <- inputlist[datadisplay[i]:datadisplay[i+1]-1]
images <- stack(input)
images.celsius <- calc(images,fun=function(x){x - 273.15})
plot(images.celsius, zlim=c(-20,80),asp=1)
hist(images.celsius,xlim=c(-20,80),n=100)
}
end <- Sys.time()
print(end-start)
More information about the R-sig-Geo
mailing list