| ck_apply_grid | R Documentation |
Compute a daily climatekit index function for every cell in a
SpatRaster x whose layers represent successive days, and return
the per-period results as a SpatRaster. The supplied function fun
must accept a numeric vector and a Date vector and return a data
frame with period and value columns (the standard climatekit
shape).
ck_apply_grid(x, fun, dates, ...)
x |
A SpatRaster. Layers correspond one-to-one with |
fun |
A |
dates |
Date vector of length |
... |
Additional named arguments forwarded to |
All cells must share the same dates. Cells that are entirely NA
are returned as NA. Run-time scales linearly with the number of
cells; for very large grids consider sub-setting first.
A SpatRaster with one layer per output period (layer names are the period labels).
if (requireNamespace("terra", quietly = TRUE)) {
dates <- seq(as.Date("2024-01-01"), as.Date("2024-12-31"), by = "day")
n <- length(dates)
# Tiny 2x2 SpatRaster of synthetic daily Tmax
r <- terra::rast(nrows = 2, ncols = 2, nlyrs = n,
xmin = 0, xmax = 2, ymin = 0, ymax = 2)
set.seed(1)
for (i in seq_len(n)) {
terra::values(r[[i]]) <- rnorm(4, 15, 5)
}
txx_r <- ck_apply_grid(r, ck_txx, dates = dates, period = "annual")
terra::nlyr(txx_r)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.