From c136736611e235940a132c91dc05f6bf6430af97 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 30 Sep 2026 19:00:37 -0700 Subject: [PATCH 1/5] prepClimateLayers: write multi-layer stacks band-interleaved and tiled Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CwcjqqK59FmTJscyi7xUqv --- NEWS.md | 1 + R/prepClimateLayers.R | 45 ++++++++++++++++-- tests/testthat/test-writeClimateStack.R | 62 +++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-writeClimateStack.R diff --git a/NEWS.md b/NEWS.md index 2f3a740..55fd74f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # climateData (development version) +- `prepClimateLayers()` now writes multi-layer climate stacks band-interleaved in 256 x 256 tiles (`INTERLEAVE=BAND`, `TILED=YES`) instead of pixel-interleaved 1-row strips, so reading one year's layer no longer decompresses every layer in the file (about 8.8 s down to 0.14 s for a 90-layer stack). Values are unchanged. The creation options are part of the cache key, so existing stacks are rebuilt the next time `prepClimateLayers()` runs. - When a seasonal variable is needed, monthly and yearly variables now come from the same MSY ("all") archive, which contains all their files. Before, they were also downloaded from their own M and Y archives unless M, S and Y were all needed. Some M archives on the server are incomplete (tile 46's future 2080s diff --git a/R/prepClimateLayers.R b/R/prepClimateLayers.R index 089b6e6..7178f93 100644 --- a/R/prepClimateLayers.R +++ b/R/prepClimateLayers.R @@ -471,6 +471,7 @@ prepClimateLayers <- function( "studyAreaName" ) |> mget(envir = environment()) |> + c(list(gdalOptions = .climateStackGdalOptions)) |> ## so older pixel-interleaved files are rebuilt .robustDigest(object = _) climData <- lapply(names(climDataFun), function(nm) { @@ -479,13 +480,11 @@ prepClimateLayers <- function( var <- stringr::str_remove(nm, paste0(type, "_")) fname <- paste(var, type, studyAreaName, sep = "_") climRast <- - postProcessTo( + .postProcessAndWriteClimate( from = newClimRast, to = rasterToMatch, maskTo = studyArea, - writeTo = file.path(climatePathOut, paste0(fname, ".tif")), - useCache = FALSE, ## use internal cache for postProcessTo - overwrite = TRUE + writeTo = file.path(climatePathOut, paste0(fname, ".tif")) ) |> Cache( omitArgs = c("to", "maskTo", "overwrite"), # don't digest these each time @@ -517,3 +516,41 @@ prepClimateLayers <- function( } unique(types) } + +## GDAL creation options for the multi-layer climate stacks. A yearly layer is read on its own +## (one `year` layer out of ~90), which is slow if the layers are pixel-interleaved in +## 1-row strips (GDAL's default): every read decompresses all layers. Band-interleaved 256 x 256 +## tiles (GDAL's usual tile size: small enough that one layer touches few tiles, big enough to +## compress well) let a single layer be read without touching the others. +## Compression is left to terra's default (LZW), as before. +.climateStackGdalOptions <- c("INTERLEAVE=BAND", "TILED=YES", "BLOCKXSIZE=256", "BLOCKYSIZE=256") + +#' Post-process a climate raster and write it +#' +#' Same as `postProcessTo(writeTo = )`, except that stacks with more than one layer are written +#' with `.climateStackGdalOptions`. `reproducible::postProcessTo(writeTo = )` does not pass +#' `gdal` creation options on to `terra::writeRaster()`, so the write is done here. +#' @noRd +.postProcessAndWriteClimate <- function(from, to, maskTo, writeTo) { + out <- postProcessTo( + from = from, + to = to, + maskTo = maskTo, + useCache = FALSE, ## use internal cache for postProcessTo + overwrite = TRUE + ) + .writeClimateStack(out, writeTo) +} + +.writeClimateStack <- function(x, filename) { + if (file.exists(filename)) { + unlink(filename) + } + ## NUM_THREADS=1 as in reproducible::writeTo(): a default write leaves a GDAL thread pool + ## that makes a later fork() deadlock + gdal <- "NUM_THREADS=1" + if (terra::nlyr(x) > 1) { + gdal <- c(.climateStackGdalOptions, gdal) + } + terra::writeRaster(x, filename = filename, overwrite = TRUE, gdal = gdal) +} diff --git a/tests/testthat/test-writeClimateStack.R b/tests/testthat/test-writeClimateStack.R new file mode 100644 index 0000000..946e3f3 --- /dev/null +++ b/tests/testthat/test-writeClimateStack.R @@ -0,0 +1,62 @@ +test_that(".writeClimateStack writes band-interleaved tiles with identical values", { + skip_if_not_installed("terra") + r <- terra::rast(nrows = 300, ncols = 400, nlyrs = 6, xmin = 0, xmax = 400, ymin = 0, ymax = 300) + set.seed(1) + terra::values(r) <- round(stats::rnorm(terra::ncell(r) * 6, 100, 30), 1) + names(r) <- paste0("year", 2001:2006) + + oldFile <- withr::local_tempfile(fileext = ".tif") + newFile <- withr::local_tempfile(fileext = ".tif") + ## how the stacks were written before: terra default, pixel-interleaved 1-row strips + terra::writeRaster(r, oldFile, gdal = "INTERLEAVE=PIXEL", overwrite = TRUE) + expect_match( + sf::gdal_utils("info", oldFile, quiet = TRUE), + "INTERLEAVE=PIXEL" + ) + + out <- climateData:::.writeClimateStack(r, newFile) + info <- sf::gdal_utils("info", newFile, quiet = TRUE) + expect_match(info, "Block=256x256") + expect_match(info, "Block=256x256") + expect_match(info, "COMPRESSION=LZW") + + expect_identical(names(terra::rast(newFile)), names(r)) + expect_equal(terra::values(terra::rast(newFile)), terra::values(terra::rast(oldFile)), + tolerance = 0) + expect_equal(terra::values(out), terra::values(terra::rast(oldFile)), tolerance = 0) + ## one layer on its own is the same too + expect_equal(terra::values(terra::rast(newFile, lyrs = "year2004")), + terra::values(terra::rast(oldFile, lyrs = "year2004")), tolerance = 0) + + ## a single layer is not given tiling + oneFile <- withr::local_tempfile(fileext = ".tif") + climateData:::.writeClimateStack(r[[1]], oneFile) + expect_no_match(sf::gdal_utils("info", oneFile, quiet = TRUE), "Block=256x256") + + ## overwriting an existing file works + climateData:::.writeClimateStack(r, newFile) + expect_equal(terra::nlyr(terra::rast(newFile)), 6L) +}) + +test_that(".postProcessAndWriteClimate gives the same layers as postProcessTo(writeTo =)", { + skip_if_not_installed("terra") + skip_if_not_installed("reproducible") + r <- terra::rast(nrows = 120, ncols = 160, nlyrs = 4, xmin = 0, xmax = 160, ymin = 0, ymax = 120, + crs = "EPSG:3857") + set.seed(2) + terra::values(r) <- round(stats::rnorm(terra::ncell(r) * 4, 50, 10), 2) + names(r) <- paste0("year", 2001:2004) + to <- terra::rast(terra::ext(20, 120, 10, 90), res = 2, crs = "EPSG:3857") + mask <- terra::as.polygons(terra::ext(30, 100, 20, 80), crs = "EPSG:3857") + + oldFile <- withr::local_tempfile(fileext = ".tif") + newFile <- withr::local_tempfile(fileext = ".tif") + old <- reproducible::postProcessTo(r, to = to, maskTo = mask, writeTo = oldFile, + useCache = FALSE, overwrite = TRUE) + new <- climateData:::.postProcessAndWriteClimate(r, to = to, maskTo = mask, writeTo = newFile) + expect_equal(terra::values(new), terra::values(old), tolerance = 0) + expect_identical(names(new), names(old)) + expect_equal(terra::values(terra::rast(newFile)), terra::values(terra::rast(oldFile)), + tolerance = 0) + expect_match(sf::gdal_utils("info", newFile, quiet = TRUE), "Block=256x256") +}) From 5fb6049585f0664a7dd3744928e84fed7c0d8c69 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 30 Sep 2026 19:12:17 -0700 Subject: [PATCH 2/5] prepClimateLayers: pass stack creation options through postProcessTo(gdal = ) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CwcjqqK59FmTJscyi7xUqv --- NEWS.md | 2 +- R/prepClimateLayers.R | 37 ++----------- tests/testthat/test-writeClimateStack.R | 70 +++++++++---------------- 3 files changed, 31 insertions(+), 78 deletions(-) diff --git a/NEWS.md b/NEWS.md index 55fd74f..df28afe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # climateData (development version) -- `prepClimateLayers()` now writes multi-layer climate stacks band-interleaved in 256 x 256 tiles (`INTERLEAVE=BAND`, `TILED=YES`) instead of pixel-interleaved 1-row strips, so reading one year's layer no longer decompresses every layer in the file (about 8.8 s down to 0.14 s for a 90-layer stack). Values are unchanged. The creation options are part of the cache key, so existing stacks are rebuilt the next time `prepClimateLayers()` runs. +- `prepClimateLayers()` now writes multi-layer climate stacks band-interleaved in 256 x 256 tiles (`INTERLEAVE=BAND`, `TILED=YES`) through `postProcessTo(writeTo = , gdal = )`, instead of pixel-interleaved 1-row strips, so reading one year's layer no longer decompresses every layer in the file (about 8.8 s down to 0.14 s for a 90-layer stack). Values are unchanged. This needs a `reproducible` in which `writeTo()` honours `gdal` (older versions ignore it and write the old layout). The creation options are part of the cache key, so existing stacks are rebuilt the next time `prepClimateLayers()` runs. - When a seasonal variable is needed, monthly and yearly variables now come from the same MSY ("all") archive, which contains all their files. Before, they were also downloaded from their own M and Y archives unless M, S and Y were all needed. Some M archives on the server are incomplete (tile 46's future 2080s diff --git a/R/prepClimateLayers.R b/R/prepClimateLayers.R index 7178f93..28de8c2 100644 --- a/R/prepClimateLayers.R +++ b/R/prepClimateLayers.R @@ -480,11 +480,14 @@ prepClimateLayers <- function( var <- stringr::str_remove(nm, paste0(type, "_")) fname <- paste(var, type, studyAreaName, sep = "_") climRast <- - .postProcessAndWriteClimate( + postProcessTo( from = newClimRast, to = rasterToMatch, maskTo = studyArea, - writeTo = file.path(climatePathOut, paste0(fname, ".tif")) + writeTo = file.path(climatePathOut, paste0(fname, ".tif")), + gdal = if (terra::nlyr(newClimRast) > 1) .climateStackGdalOptions, ## needs reproducible >= the version with `writeTo(gdal = )` + useCache = FALSE, ## use internal cache for postProcessTo + overwrite = TRUE ) |> Cache( omitArgs = c("to", "maskTo", "overwrite"), # don't digest these each time @@ -524,33 +527,3 @@ prepClimateLayers <- function( ## compress well) let a single layer be read without touching the others. ## Compression is left to terra's default (LZW), as before. .climateStackGdalOptions <- c("INTERLEAVE=BAND", "TILED=YES", "BLOCKXSIZE=256", "BLOCKYSIZE=256") - -#' Post-process a climate raster and write it -#' -#' Same as `postProcessTo(writeTo = )`, except that stacks with more than one layer are written -#' with `.climateStackGdalOptions`. `reproducible::postProcessTo(writeTo = )` does not pass -#' `gdal` creation options on to `terra::writeRaster()`, so the write is done here. -#' @noRd -.postProcessAndWriteClimate <- function(from, to, maskTo, writeTo) { - out <- postProcessTo( - from = from, - to = to, - maskTo = maskTo, - useCache = FALSE, ## use internal cache for postProcessTo - overwrite = TRUE - ) - .writeClimateStack(out, writeTo) -} - -.writeClimateStack <- function(x, filename) { - if (file.exists(filename)) { - unlink(filename) - } - ## NUM_THREADS=1 as in reproducible::writeTo(): a default write leaves a GDAL thread pool - ## that makes a later fork() deadlock - gdal <- "NUM_THREADS=1" - if (terra::nlyr(x) > 1) { - gdal <- c(.climateStackGdalOptions, gdal) - } - terra::writeRaster(x, filename = filename, overwrite = TRUE, gdal = gdal) -} diff --git a/tests/testthat/test-writeClimateStack.R b/tests/testthat/test-writeClimateStack.R index 946e3f3..2ac03f9 100644 --- a/tests/testthat/test-writeClimateStack.R +++ b/tests/testthat/test-writeClimateStack.R @@ -1,62 +1,42 @@ -test_that(".writeClimateStack writes band-interleaved tiles with identical values", { +## needs the `reproducible` version in which `writeTo()` honours `gdal` +test_that("postProcessTo(writeTo, gdal = .climateStackGdalOptions) writes band-interleaved tiles with identical values", { skip_if_not_installed("terra") - r <- terra::rast(nrows = 300, ncols = 400, nlyrs = 6, xmin = 0, xmax = 400, ymin = 0, ymax = 300) + skip_if_not_installed("sf") + skip_if_not_installed("withr") + ## skip with a `reproducible` whose writeTo() ignores `gdal` + probe <- withr::local_tempfile(fileext = ".tif") + p <- terra::rast(nrows = 300, ncols = 400, nlyrs = 2, vals = 1) + reproducible::postProcessTo(p, writeTo = probe, gdal = "TILED=YES", useCache = FALSE) + skip_if_not(grepl("Block=256x256", sf::gdal_utils("info", probe, quiet = TRUE)), + "reproducible::writeTo() ignores `gdal`") + + r <- terra::rast(nrows = 300, ncols = 400, nlyrs = 6, xmin = 0, xmax = 400, ymin = 0, ymax = 300, + crs = "EPSG:3857") set.seed(1) terra::values(r) <- round(stats::rnorm(terra::ncell(r) * 6, 100, 30), 1) names(r) <- paste0("year", 2001:2006) + to <- terra::rast(terra::ext(20, 380, 10, 280), resolution = 1, crs = "EPSG:3857") + mask <- terra::as.polygons(terra::ext(30, 300, 20, 250), crs = "EPSG:3857") oldFile <- withr::local_tempfile(fileext = ".tif") newFile <- withr::local_tempfile(fileext = ".tif") - ## how the stacks were written before: terra default, pixel-interleaved 1-row strips - terra::writeRaster(r, oldFile, gdal = "INTERLEAVE=PIXEL", overwrite = TRUE) - expect_match( - sf::gdal_utils("info", oldFile, quiet = TRUE), - "INTERLEAVE=PIXEL" - ) - - out <- climateData:::.writeClimateStack(r, newFile) + ## how the stacks were written before: no creation options + old <- reproducible::postProcessTo(r, to = to, maskTo = mask, writeTo = oldFile, + useCache = FALSE, overwrite = TRUE) + new <- reproducible::postProcessTo(r, to = to, maskTo = mask, writeTo = newFile, + gdal = climateData:::.climateStackGdalOptions, + useCache = FALSE, overwrite = TRUE) + expect_match(sf::gdal_utils("info", oldFile, quiet = TRUE), "INTERLEAVE=PIXEL") info <- sf::gdal_utils("info", newFile, quiet = TRUE) - expect_match(info, "Block=256x256") + expect_match(info, "INTERLEAVE=BAND") expect_match(info, "Block=256x256") expect_match(info, "COMPRESSION=LZW") - expect_identical(names(terra::rast(newFile)), names(r)) + expect_identical(names(terra::rast(newFile)), names(terra::rast(oldFile))) expect_equal(terra::values(terra::rast(newFile)), terra::values(terra::rast(oldFile)), tolerance = 0) - expect_equal(terra::values(out), terra::values(terra::rast(oldFile)), tolerance = 0) + expect_equal(terra::values(new), terra::values(old), tolerance = 0) ## one layer on its own is the same too expect_equal(terra::values(terra::rast(newFile, lyrs = "year2004")), terra::values(terra::rast(oldFile, lyrs = "year2004")), tolerance = 0) - - ## a single layer is not given tiling - oneFile <- withr::local_tempfile(fileext = ".tif") - climateData:::.writeClimateStack(r[[1]], oneFile) - expect_no_match(sf::gdal_utils("info", oneFile, quiet = TRUE), "Block=256x256") - - ## overwriting an existing file works - climateData:::.writeClimateStack(r, newFile) - expect_equal(terra::nlyr(terra::rast(newFile)), 6L) -}) - -test_that(".postProcessAndWriteClimate gives the same layers as postProcessTo(writeTo =)", { - skip_if_not_installed("terra") - skip_if_not_installed("reproducible") - r <- terra::rast(nrows = 120, ncols = 160, nlyrs = 4, xmin = 0, xmax = 160, ymin = 0, ymax = 120, - crs = "EPSG:3857") - set.seed(2) - terra::values(r) <- round(stats::rnorm(terra::ncell(r) * 4, 50, 10), 2) - names(r) <- paste0("year", 2001:2004) - to <- terra::rast(terra::ext(20, 120, 10, 90), res = 2, crs = "EPSG:3857") - mask <- terra::as.polygons(terra::ext(30, 100, 20, 80), crs = "EPSG:3857") - - oldFile <- withr::local_tempfile(fileext = ".tif") - newFile <- withr::local_tempfile(fileext = ".tif") - old <- reproducible::postProcessTo(r, to = to, maskTo = mask, writeTo = oldFile, - useCache = FALSE, overwrite = TRUE) - new <- climateData:::.postProcessAndWriteClimate(r, to = to, maskTo = mask, writeTo = newFile) - expect_equal(terra::values(new), terra::values(old), tolerance = 0) - expect_identical(names(new), names(old)) - expect_equal(terra::values(terra::rast(newFile)), terra::values(terra::rast(oldFile)), - tolerance = 0) - expect_match(sf::gdal_utils("info", newFile, quiet = TRUE), "Block=256x256") }) From e7f2b01875d4374bf8c938ef10cc8a82c8049c51 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 30 Sep 2026 21:13:01 -0700 Subject: [PATCH 3/5] Imports reproducible (>= 3.2.1.9057), where writeTo() honours gdal; drop the test's skip-if-ignored probe Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CwcjqqK59FmTJscyi7xUqv --- DESCRIPTION | 2 +- tests/testthat/test-writeClimateStack.R | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1a9e461..38e5daf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,7 @@ Imports: parallel, parallelly, purrr, - reproducible (>= 2.0.8), + reproducible (>= 3.2.1.9057), RSQLite, sf, stats, diff --git a/tests/testthat/test-writeClimateStack.R b/tests/testthat/test-writeClimateStack.R index 2ac03f9..ec742f4 100644 --- a/tests/testthat/test-writeClimateStack.R +++ b/tests/testthat/test-writeClimateStack.R @@ -1,15 +1,8 @@ -## needs the `reproducible` version in which `writeTo()` honours `gdal` +## needs reproducible (>= 3.2.1.9057), in which `writeTo()` honours `gdal` (DESCRIPTION Imports) test_that("postProcessTo(writeTo, gdal = .climateStackGdalOptions) writes band-interleaved tiles with identical values", { skip_if_not_installed("terra") skip_if_not_installed("sf") skip_if_not_installed("withr") - ## skip with a `reproducible` whose writeTo() ignores `gdal` - probe <- withr::local_tempfile(fileext = ".tif") - p <- terra::rast(nrows = 300, ncols = 400, nlyrs = 2, vals = 1) - reproducible::postProcessTo(p, writeTo = probe, gdal = "TILED=YES", useCache = FALSE) - skip_if_not(grepl("Block=256x256", sf::gdal_utils("info", probe, quiet = TRUE)), - "reproducible::writeTo() ignores `gdal`") - r <- terra::rast(nrows = 300, ncols = 400, nlyrs = 6, xmin = 0, xmax = 400, ymin = 0, ymax = 300, crs = "EPSG:3857") set.seed(1) From 5f8cf144d2cb951a5c8b126443c4253e585a03db Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 14 Oct 2026 17:04:46 -0700 Subject: [PATCH 4/5] checkCalendarYears(): stop early when a run's start is outside the climate years Co-Authored-By: Claude Opus 5.5 --- NAMESPACE | 1 + NEWS.md | 1 + R/checkCalendarYears.R | 31 ++++++++++++++++++++ man/checkCalendarYears.Rd | 36 ++++++++++++++++++++++++ tests/testthat/test-checkCalendarYears.R | 27 ++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 R/checkCalendarYears.R create mode 100644 man/checkCalendarYears.Rd create mode 100644 tests/testthat/test-checkCalendarYears.R diff --git a/NAMESPACE b/NAMESPACE index 23e8fc2..8e7a285 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(calcCumMDC) export(calcMDC) export(calcStackLayersType) export(checkCalcStackLayers) +export(checkCalendarYears) export(checksums_sql) export(climateLayers) export(climateStacksByPeriod) diff --git a/NEWS.md b/NEWS.md index 4944f42..86a7afc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # climateData (development version) +- New exported `checkCalendarYears(start, end, module, hint, years)`: stops with a message saying what to set when a run's start is not a calendar year the climate data cover (1901 to 2100, from `available()`, or the range of `years` when given). For modules such as canClimateData and climateYear, to fail early on model-year times (0-10, as `setupProject()` gives without `times`). - The `prepClimateLayers()` cache key now includes the bodies of the functions that build the layers (`maskInvalidPPT()`, `climateMosaicsParallel()`, the `calc*()` functions ...). Before, layers cached before the PPT guard or before the `reproducible` 3.2.1.9060 `postProcessTo()` smoothing fix kept diff --git a/R/checkCalendarYears.R b/R/checkCalendarYears.R new file mode 100644 index 0000000..aeee7c8 --- /dev/null +++ b/R/checkCalendarYears.R @@ -0,0 +1,31 @@ +#' Check that a run's times are calendar years the climate data can serve +#' +#' Climate data are by calendar year, from the first historical year to the last projected year +#' that [available()] reports (1901 to 2100). A run whose start is outside that span, such as +#' the model years 0-10 that `SpaDES.project::setupProject()` gives when `times` is not set, +#' cannot be matched to any climate year. A run may end after the last year. +#' +#' @param start,end Start and end times of the simulation. +#' @param years Optional years the caller will load (e.g. a module's climate-year parameters); the span is then +#' their range instead of the span [available()] reports. +#' @param module Name of the calling module, for the message. +#' @param hint Optional text added to the end of the message, saying how to proceed. +#' +#' @return `NULL`, invisibly; an error if `start` is outside the span of the climate data. +#' +#' @export +#' +#' @examples +#' checkCalendarYears(2020, 2120, module = "myModule") +checkCalendarYears <- function(start, end, module = "this module", hint = NULL, years = NULL) { + if (is.null(years)) { + years <- unlist(lapply(c("historical", "future"), function(type) available(type)[["years"]])) + } + span <- range(years) + if (start < span[1] || start > span[2]) { + stop(module, ": times are model years ", start, "-", end, "; this module needs calendar years ", + "(the climate data cover ", span[1], "-", span[2], ", so `start` must be in that range), ", + "e.g. times = list(start = 2020, end = 2120). ", hint, call. = FALSE) + } + invisible(NULL) +} diff --git a/man/checkCalendarYears.Rd b/man/checkCalendarYears.Rd new file mode 100644 index 0000000..6355ade --- /dev/null +++ b/man/checkCalendarYears.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/checkCalendarYears.R +\name{checkCalendarYears} +\alias{checkCalendarYears} +\title{Check that a run's times are calendar years the climate data can serve} +\usage{ +checkCalendarYears( + start, + end, + module = "this module", + hint = NULL, + years = NULL +) +} +\arguments{ +\item{start, end}{Start and end times of the simulation.} + +\item{module}{Name of the calling module, for the message.} + +\item{hint}{Optional text added to the end of the message, saying how to proceed.} + +\item{years}{Optional years the caller will load (e.g. a module's climate-year parameters); the span is then +their range instead of the span \code{\link[=available]{available()}} reports.} +} +\value{ +\code{NULL}, invisibly; an error if \code{start} is outside the span of the climate data. +} +\description{ +Climate data are by calendar year, from the first historical year to the last projected year +that \code{\link[=available]{available()}} reports (1901 to 2100). A run whose start is outside that span, such as +the model years 0-10 that \code{SpaDES.project::setupProject()} gives when \code{times} is not set, +cannot be matched to any climate year. A run may end after the last year. +} +\examples{ +checkCalendarYears(2020, 2120, module = "myModule") +} diff --git a/tests/testthat/test-checkCalendarYears.R b/tests/testthat/test-checkCalendarYears.R new file mode 100644 index 0000000..eb99552 --- /dev/null +++ b/tests/testthat/test-checkCalendarYears.R @@ -0,0 +1,27 @@ +test_that("a start inside the span of the climate data is accepted", { + expect_silent(checkCalendarYears(2020, 2120, module = "m")) + expect_silent(checkCalendarYears(1901, 1950, module = "m")) + expect_silent(checkCalendarYears(2100, 2100, module = "m")) + expect_silent(checkCalendarYears(1985.5, 2020, module = "m")) +}) + +test_that("the span is the one available() reports", { + yrs <- range(unlist(lapply(c("historical", "future"), function(t) available(t)[["years"]]))) + expect_error(checkCalendarYears(yrs[1] - 1, 2000, module = "m"), as.character(yrs[1])) + expect_error(checkCalendarYears(yrs[2] + 1, 2200, module = "m"), as.character(yrs[2])) +}) + +test_that("model-year times fail with the module, both times and an example", { + expect_error(checkCalendarYears(0, 10, module = "canClimateData"), + "canClimateData: times are model years 0-10.*calendar years.*times = list\\(start = 2020, end = 2120\\)") +}) + +test_that("a hint is appended to the message", { + expect_error(checkCalendarYears(0, 10, module = "m", hint = "Do this instead."), "Do this instead\\.") +}) + +test_that("`years` replaces the span of available()", { + expect_silent(checkCalendarYears(1995, 2050, module = "m", years = 1991:2100)) + expect_error(checkCalendarYears(1950, 2050, module = "m", years = 1991:2100), "1991-2100") + expect_error(checkCalendarYears(2101, 2200, module = "m", years = 1991:2100), "1991-2100") +}) From ed7948d9c6072cb40b5a632e0f70e98d89eb9b72 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 21 Oct 2026 09:51:07 -0700 Subject: [PATCH 5/5] CI: run on modsForFireSense (our integration branch; fixes are PR'd into it) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- .github/workflows/R-CMD-check.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 10c6c79..775eade 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -10,9 +10,9 @@ name: R-CMD-check on: push: - branches: [main, master, development] + branches: [main, master, development, modsForFireSense] pull_request: - branches: [main, master, development] + branches: [main, master, development, modsForFireSense] jobs: R-CMD-check: