From 5f8cf144d2cb951a5c8b126443c4253e585a03db Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 14 Oct 2026 17:04:46 -0700 Subject: [PATCH] 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") +})