This function subsets a datacube (RasterStack or RasterBrick) based on dates.

subset_datacube(r, from, to)

Arguments

r

is the Raster layer to compare to the climatology.

from

string, starting date (e.g. "2018-12-30").

to

string, ending date (e.g. "2018-12-31").

Value

The function returns a subset of r, with layer's date in the range starting with from and ending with to.

Details

If the from and to strings are in the format "YYYY-MM-DD", they are automatically converted into a date.

Examples

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")
}