Skip to content

How to perform cross-year date operations on rasters?

5 messages · Thiago V. dos Santos, Michael Sumner, Dominik Schneider +1 more

#
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


 Thanks in advance,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota
#
On Thu, 5 Nov 2015 at 09:38 Thiago V. dos Santos <thi_veloso at yahoo.com.br>
wrote:
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 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,
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,
#
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:

            

  
  
#
Hi Thiago,

Building on Michael's answer and your example, I think the following should work. The tricky part is to build the right by= argument for zApply().
You can see zApply as a wrapper of aggregate.zoo() for temporal raster bricks, and you're more likely to find answers for aggregate.zoo than for zApply when searching the internet.
This is where I found the answer: https://stat.ethz.ch/pipermail/r-help/2008-August/171366.html

Cheers,
Lo?c Dutrieux

library(raster)
library(zoo)
library(lubridate)

# 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=year(as.yearmon(getZ(s2)) - 1/12), fun = sum))
On 11/05/2015 07:17 PM, Thiago V. dos Santos wrote: