sumSeries: Summarize climatic series to higher temporal resolution

View source: R/sumSeries.R

sumSeriesR Documentation

Summarize climatic series to higher temporal resolution

Description

Function to convert climatic series (provided as RasterStack) into a coarser time frequency series for a period of interest. This function transforms the RasterStack into an xts time series object to extract the values for the period of interest and apply some summary function. It is mainly a wrapper from the apply. function family in the package xts (Ryan and Ulrich 2017).

Usage

sumSeries(r, p, yr0, l, fun = function(x) colMeans(x, na.rm = TRUE), freqin = "months",
freqout = "years")

Arguments

r

RasterStack containing the time series of the climatic variable.

p

character string defining the period to extract for the calculation of the series (see examples).

yr0

character string specifying the first (yr0) year in the series (see examples).

l

integer length of the input time series.

fun

logical summary function to be computed. Summary functions need to be applied by cell (columns) so should have the structure 'function(x) apply(x, 2, function(y)...)'. For convinience, sumSeries imports colMaxs, and colMins from package ‘matrixStats’ (Bengtsson 2018) so they can be called in directly.

freqin

character string specifying the original time frequency of the series.

freqout

character string specifying the desired time frequency of the new series. Must be one of the following: "weeks", "months", "quarters", "years", "other". Argument "other" allows for user-defined functions to be applied on the 'xts' time series object over the period of interest (see examples).

Value

A RasterStack with the new series.

Author(s)

Jorge Garcia Molinos

References

Ray and Ulrich. 2017. xts: eXtensible Time Series. R package version 0.10-1.
Bengtsson 2018. matrixStats: Functions that Apply to Rows and Columns of Matrices (and to Vectors). R package version 0.53.1.

Examples

# Monthly mean SST (HadISST) data for Europe Jan-1950 to Dec-2010

?HSST

# Calculate mean annual monthly SST

yrSST <- sumSeries(HSST, p = "1969-01/2009-12", yr0 = "1955-01-01", l = nlayers(HSST),
fun = function(x) colMeans(x, na.rm = TRUE), freqin = "months", freqout = "years")

# Extract Jul Aug mean SST each year (xts months are indexed from 0 to 11)

myf = function(x, m = c(7,8)){
x[xts::.indexmon(x) %in% (m-1)]
}

JlAugSST <- sumSeries(HSST, p = "1969-01/2009-12", yr0 = "1950-01-01", l = raster::nlayers(HSST),
fun = myf, freqin = "months", freqout = "other")

# Same but calculating the annual variance of the two months

myf = function(x, m = c(7,8)){
x1 <- x[xts::.indexmon(x) %in% (m-1)]
xts::apply.yearly(x1, function(y) apply(y, 2, function(y){var(y, na.rm = TRUE)}))
}

meanJASST <- sumSeries(HSST, p = "1969-01/2009-12", yr0 = "1950-01-01", l = raster::nlayers(HSST),
fun = myf, freqin = "months", freqout = "other")


JorGarMol/VoCC documentation built on Aug. 17, 2022, 11:07 p.m.