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).
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 convenience, 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).
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
if (FALSE) { # \dontrun{
# Monthly mean SST (HadISST) data for Europe Jan-1950 to Dec-2010
HSST <- VoCC_get_data("HSST.tif")
# Calculate mean annual monthly SST
yrSST <- sumSeries(HSST,
p = "1969-01/2009-12", yr0 = "1955-01-01", l = terra::nlyr(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 = terra::nlyr(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 = terra::nlyr(HSST),
fun = myf, freqin = "months", freqout = "other"
)
} # }