How to perform cross-year date operations on rasters?
Could you use water years for your dates? so Oct-Sep is the same year and then subset based on month and water year. On Thu, Nov 5, 2015 at 11:17 AM, Thiago V. dos Santos <
thi_veloso at yahoo.com.br> wrote:
Thanks for your input Michael. With slight modifications on your
suggestion, I almost got there:
library(raster)
library(zoo)
# Create a rasterStack similar to cmip5 - same dimensions and layer names
r <- raster(ncol=180, nrow=90)
s <- stack(lapply(1:1825, function(x) setValues(r, runif(ncell(r)))))
idx <- seq(as.Date("2010/1/1"), by = "day", length.out = 1825)
s <- setZ(s, idx)
# Separate layers with months of interest
ldates <- format(getZ(s), "%m") %in% c("10", "11", "12", "01")
s2 <- subset(s, which(ldates))
# Apply function
s3 <- zApply(s2, by=as.yearmon, fun = sum)
The problem now is that the "isolated" january in the first year is also
taken into account, and the "isolated" oct-nov-dec in the last year as well.
Ideally the function would run only on the contiguous period:
oct-nov-dec-jan, and not when at least one month is missing (for example in
the first and last years of the series).
Still possible to achieve this?
Greetings,
-- Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
On Thursday, November 5, 2015 12:15 AM, Michael Sumner <mdsumner at gmail.com>
wrote:
On Thu, 5 Nov 2015 at 09:38 Thiago V. dos Santos <thi_veloso at yahoo.com.br>
wrote:
Dear all,
Consider that I have a raster stack with daily values. How can I perform
date operations covering a time interval that crosses years?
For example, I want to sum the values from every october-to-january
period in this sample raster:
library(raster)
# Create a rasterStack similar to cmip5 - same dimensions and layer names
r <- raster(ncol=180, nrow=90)
s <- stack(lapply(1:1825, function(x) setValues(r, runif(ncell(r)))))
# Apply time stamps to raster
#x <- as.Date(c("2010-01-01","2014-12-31"),format="%Y-%m-%d")
#difftime(x[2], x[1], units="days")
idx <- seq(as.Date("2010/1/1"), by = "day", length.out = 1825)
s <- setZ(s, idx)
s
You can subset on the dates, by running a test on the date values:
ldates <- format(getZ(s), "%m") %in% c("10", "11", "01")
subsetting the object
subset(s, which(ldates))
and finally calculating what you want
calc(subset(s, which(ldates)), sum)
HTH
Thanks in advance,
-- Thiago V. dos Santos PhD student Land and Atmospheric Science University of Minnesota
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo