From bb1caa2089cab8eb4b77982443adf08baf276b99 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 14 Oct 2026 16:31:16 -0700 Subject: [PATCH 1/2] init fails early when times are not calendar years Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 4 ++++ canClimateData.R | 19 +++++++++++++++++++ tests/testthat/test-calendarYears.R | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/testthat/test-calendarYears.R diff --git a/NEWS.md b/NEWS.md index ab016e3..8750962 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# canClimateData (development version) + +- `init` now stops, before preparing any climate data, when the run's start is not a calendar year from 1901 to 2100 (for example the model years 0-10 that `setupProject()` gives without `times`). The message says to set `times`, e.g. `times = list(start = 2020, end = 2120)`. The run may end after 2100. + # canClimateData 1.1.0 canClimateData now uses the current climateData package to prepare its climate layers. It can run hindcasts as well as forecasts, it returns historical and projected climate as named lists, and a shared output folder can be set for climate files. The manual was updated, with links for exploring and comparing climate scenarios. diff --git a/canClimateData.R b/canClimateData.R index d84d294..e684de3 100644 --- a/canClimateData.R +++ b/canClimateData.R @@ -141,7 +141,26 @@ climateRastersToRewrite <- function(projectedType) { if (identical(projectedType, "hindcast")) "projected" else character(0) } +#' Check that the run's times are calendar years the climate data can serve +#' +#' The climate data are by calendar year: historical from 1901, projections to 2100. A run +#' whose start is outside 1901-2100 (e.g. model years 0-10, which `setupProject()` gives when +#' `times` is not set) cannot be matched to any climate year. The run may end after 2100. +#' +#' @param start,end Start and end times of the simulation. +#' +#' @return `NULL` invisibly; an error otherwise. +checkCalendarYears <- function(start, end) { + if (start < 1901 || start > 2100) { + stop("canClimateData: times are model years ", start, "-", end, "; this module needs calendar years ", + "(the climate data cover 1901-2100, so `start` must be in that range), ", + "e.g. times = list(start = 2020, end = 2120).", call. = FALSE) + } + invisible(NULL) +} + Init <- function(sim) { + checkCalendarYears(start(sim), end(sim)) stopifnot(P(sim)$projectedType %in% c("forecast", "hindcast")) ## separate intermediate outputs from raw inputs, to reduce file conflicts on shared drives diff --git a/tests/testthat/test-calendarYears.R b/tests/testthat/test-calendarYears.R new file mode 100644 index 0000000..c52bc11 --- /dev/null +++ b/tests/testthat/test-calendarYears.R @@ -0,0 +1,25 @@ +## The climate data are by calendar year (historical from 1901, projections to 2100), so a run +## whose times are model years (setupProject() without `times` gives 0 to 10) cannot be served. + +test_that("calendar-year start times are accepted", { + expect_silent(checkCalendarYears(2020, 2120)) + expect_silent(checkCalendarYears(1901, 1950)) + expect_silent(checkCalendarYears(2100, 2100)) + expect_silent(checkCalendarYears(1985.5, 2020)) +}) + +test_that("model-year times fail with a message that says what to supply", { + expect_error(checkCalendarYears(0, 10), "model years 0-10.*calendar years.*times = list\\(start = 2020") + expect_error(checkCalendarYears(1900, 2000), "1901") + expect_error(checkCalendarYears(2101, 2200), "2100") +}) + +test_that("init stops on model-year times before touching any climate data", { + rtm <- terra::rast(nrows = 2, ncols = 2, vals = 1L, crs = "EPSG:3978") + sa <- sf::st_as_sf(terra::as.polygons(rtm)) + sim <- suppressMessages(SpaDES.core::simInit( + times = list(start = 0, end = 10), modules = moduleName, paths = testPaths, + objects = list(studyArea = sa, rasterToMatch = rtm, climateVariables = list(dummy = list())))) + expect_error(suppressMessages(SpaDES.core::spades(sim, debug = FALSE)), + "model years 0-10.*calendar years") +}) From 97f7e4e7642ffe6de6e3a90836ce74bdc09ef4ae Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 14 Oct 2026 17:05:22 -0700 Subject: [PATCH 2/2] Use climateData::checkCalendarYears(); canClimateData follow-ups Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 2 +- canClimateData.R | 37 +++++++++++++---------------- tests/testthat/test-calendarYears.R | 33 +++++++++++++------------ 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8750962..39f4e55 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # canClimateData (development version) -- `init` now stops, before preparing any climate data, when the run's start is not a calendar year from 1901 to 2100 (for example the model years 0-10 that `setupProject()` gives without `times`). The message says to set `times`, e.g. `times = list(start = 2020, end = 2120)`. The run may end after 2100. +- `init` now stops, before preparing any climate data, when the run's start is outside the climate years this module is asked for (`historicalClimateYears` and `projectedClimateYears`, 1991 to 2100 by default), for example the model years 0-10 that `setupProject()` gives without `times`. With a supplied `climateVariables`, whose years are not known here, it stops when the start is not a calendar year from 1901 to 2100. The check is `climateData::checkCalendarYears()` (climateData >= 2.2.3.9009); the message says how to proceed. The run may end after the last year. # canClimateData 1.1.0 diff --git a/canClimateData.R b/canClimateData.R index e684de3..b8fc530 100644 --- a/canClimateData.R +++ b/canClimateData.R @@ -20,7 +20,7 @@ defineModule(sim, list( reqdPkgs = list("archive", "digest", "geodata", "googledrive", "purrr", "R.utils", "sf", "spatialEco", "terra", "dbplyr", # needed for prepClimateLayers - "PredictiveEcology/climateData@development (>= 2.2.3.9008)", + "PredictiveEcology/climateData@development (>= 2.2.3.9009)", "PredictiveEcology/fireSenseUtils@development (>= 0.0.5.9046)", "PredictiveEcology/LandR@development (>= 1.1.0.9064)", "PredictiveEcology/reproducible@development (>= 3.2.1.9060)", @@ -141,26 +141,20 @@ climateRastersToRewrite <- function(projectedType) { if (identical(projectedType, "hindcast")) "projected" else character(0) } -#' Check that the run's times are calendar years the climate data can serve -#' -#' The climate data are by calendar year: historical from 1901, projections to 2100. A run -#' whose start is outside 1901-2100 (e.g. model years 0-10, which `setupProject()` gives when -#' `times` is not set) cannot be matched to any climate year. The run may end after 2100. -#' -#' @param start,end Start and end times of the simulation. -#' -#' @return `NULL` invisibly; an error otherwise. -checkCalendarYears <- function(start, end) { - if (start < 1901 || start > 2100) { - stop("canClimateData: times are model years ", start, "-", end, "; this module needs calendar years ", - "(the climate data cover 1901-2100, so `start` must be in that range), ", - "e.g. times = list(start = 2020, end = 2120).", call. = FALSE) - } - invisible(NULL) -} - Init <- function(sim) { - checkCalendarYears(start(sim), end(sim)) + ## When this module builds `climateVariables` it fetches `historicalClimateYears` and + ## `projectedClimateYears`, so the run's start must fall within those. A supplied + ## `climateVariables` has years this module cannot read, so only calendar years are required. + ## (`.inputObjects` sets the flag; it does not run when every input was supplied.) + fromParams <- isTRUE(mod$climateVariablesFromParams) + checkCalendarYears(start(sim), end(sim), module = "canClimateData", + years = if (fromParams) + c(P(sim)$historicalClimateYears, P(sim)$projectedClimateYears), + hint = if (fromParams) { + "Set `times` inside those years, or change `historicalClimateYears`/`projectedClimateYears`." + } else { + "`climateVariables` was supplied, so its years are not known here: set `times` to calendar years." + }) stopifnot(P(sim)$projectedType %in% c("forecast", "hindcast")) ## separate intermediate outputs from raw inputs, to reduce file conflicts on shared drives @@ -309,7 +303,8 @@ Init <- function(sim) { datatype = "INT1U", overwrite = TRUE) } - if (!suppliedElsewhere("climateVariables", sim)) { + mod$climateVariablesFromParams <- !suppliedElsewhere("climateVariables", sim) + if (mod$climateVariablesFromParams) { historical_prd <- P(sim)$historicalClimatePeriod historical_yrs <- P(sim)$historicalClimateYears projected_yrs <- P(sim)$projectedClimateYears diff --git a/tests/testthat/test-calendarYears.R b/tests/testthat/test-calendarYears.R index c52bc11..8226b4d 100644 --- a/tests/testthat/test-calendarYears.R +++ b/tests/testthat/test-calendarYears.R @@ -1,25 +1,24 @@ -## The climate data are by calendar year (historical from 1901, projections to 2100), so a run -## whose times are model years (setupProject() without `times` gives 0 to 10) cannot be served. +## The climate data are by calendar year (see climateData::checkCalendarYears(), which has the rule +## and its unit tests), so a run whose times are model years (setupProject() without `times` gives +## 0 to 10) cannot be served. This module must stop before preparing any climate data. -test_that("calendar-year start times are accepted", { - expect_silent(checkCalendarYears(2020, 2120)) - expect_silent(checkCalendarYears(1901, 1950)) - expect_silent(checkCalendarYears(2100, 2100)) - expect_silent(checkCalendarYears(1985.5, 2020)) -}) - -test_that("model-year times fail with a message that says what to supply", { - expect_error(checkCalendarYears(0, 10), "model years 0-10.*calendar years.*times = list\\(start = 2020") - expect_error(checkCalendarYears(1900, 2000), "1901") - expect_error(checkCalendarYears(2101, 2200), "2100") -}) - -test_that("init stops on model-year times before touching any climate data", { +test_that("init stops, with a supplied climateVariables, on model-year times before touching any climate data", { rtm <- terra::rast(nrows = 2, ncols = 2, vals = 1L, crs = "EPSG:3978") sa <- sf::st_as_sf(terra::as.polygons(rtm)) sim <- suppressMessages(SpaDES.core::simInit( times = list(start = 0, end = 10), modules = moduleName, paths = testPaths, objects = list(studyArea = sa, rasterToMatch = rtm, climateVariables = list(dummy = list())))) expect_error(suppressMessages(SpaDES.core::spades(sim, debug = FALSE)), - "model years 0-10.*calendar years") + "canClimateData: times are model years 0-10.*calendar years.*climateVariables. was supplied") +}) + +test_that("init stops when the run starts before the climate years this module is asked for", { + rtm <- terra::rast(nrows = 2, ncols = 2, vals = 1L, crs = "EPSG:3978") + sa <- sf::st_as_sf(terra::as.polygons(rtm)) + sim <- suppressMessages(SpaDES.core::simInit( + times = list(start = 1950, end = 2020), modules = moduleName, paths = testPaths, + params = list(canClimateData = list(historicalClimateYears = 1991:2022, projectedClimateYears = 2011:2100)), + objects = list(studyArea = sa, rasterToMatch = rtm))) + expect_error(suppressMessages(SpaDES.core::spades(sim, debug = FALSE)), + "times are model years 1950-2020.*cover 1991-2100.*historicalClimateYears") })