[R-sig-Geo] Convert rasters to data frame with time stamp
Thiago V. dos Santos
thi_veloso at yahoo.com.br
Fri Oct 16 22:39:51 CEST 2015
Dear list,
Generally speaking, I love R. However, one of the things I like least in R is the need to interchange between the various data formats required by different packages.
I am trying to apply a bias-correction function on some gridded climate data. The qmap package has functions to perform bias correction on climate data, but the problem I am grasping with is that it requires data to be organized as data.frames:
library(qmap)
data(obsprecip)
data(modprecip)
#Fit a quantile mapping function to observed and modeled data
qm.fit <- fitQmap(obsprecip,modprecip,
method="QUANT",qstep=0.01)
#Perform bias correction on modeled data
qm <- doQmap(modprecip, qm.fit, type="tricub")
And that's all. But notice that both observed and modeled data in this example are data frames for different locations (Moss, Geiranger and Barkestad):
> head(obsprecip)
MOSS GEIRANGER BARKESTAD
1-1-1961 0.1 0 0
2-1-1961 0.2 0 0
3-1-1961 0.9 0 0
4-1-1961 10.6 0 0
5-1-1961 1.5 0 0
6-1-1961 1.2 0 2
> head(modprecip)
MOSS GEIRANGER BARKESTAD
2-1-1961 2.283 0.0000 3.177000
3-1-1961 2.443 10.8600 1.719000
4-1-1961 3.099 12.7300 6.636000
5-1-1961 0.000 9.7720 9.676000
6-1-1961 0.140 0.6448 7.110000
7-1-1961 13.470 3.3570 0.001107
Now, let's back to my problem. I have monthly precip data to which I want to apply the same function above, but my data is gridded:
library(raster)
#Create a rasterStack similar to my data - same dimensions and layer names
r <- raster(ncol=60, nrow=60)
s <- stack(lapply(1:408, function(x) setValues(r, runif(ncell(r)))))
names(s) <- paste0('X', seq(as.Date("1980/1/1"), by = "month", length.out = 408))
s
Therefore, I need to load data as rasters and iterate through all individual gridcells to create a data frame containing:
date1, cell1, cell2, cell3, ..., cell3600
date2, cell1, cell2, cell3, ..., cell3600
date3, cell1, cell2, cell3, ..., cell3600...
date408, cell1, cell2, cell3, ..., cell3600
then apply the fit function and finally convert the data back to a raster.
Any ideas on how to efficiently convert rasters to data frames containing their time stamp and then back to a raster again??
Any hint is much appreciated.
Greetings,
-- Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
More information about the R-sig-Geo
mailing list