This function subsets a datacube (RasterStack or RasterBrick) based on dates.
subset_datacube(r, from, to)
is the Raster layer to compare to the climatology.
string, starting date (e.g. "2018-12-30").
string, ending date (e.g. "2018-12-31").
The function returns a subset of r
, with layer's date in the
range starting with from
and ending with to
.
If the from
and to
strings are in the format
"YYYY-MM-DD", they are automatically converted into a date.
if (FALSE) {
r <- raster(nrows = 2, ncols = 2,
xmn = 0, xmx = 360, ymn = -90, ymx = 90, vals = 30)
# Generate dummy RasterBrick
b <- raster::brick(lapply(1:(365 * 3),
function(i) raster::setValues(r,
unif(n = raster::ncell(r), min = 0, max = 100))))
raster::setZ(b) <- seq.Date(from = as.Date("1993-01-01"),
to = as.Date("1995-12-31"),
by = "day")
subset_datacube(r = b, from = "1993-01-01", to = "1993-01-01")
}