Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(calcCumMDC)
export(calcMDC)
export(calcStackLayersType)
export(checkCalcStackLayers)
export(checkCalendarYears)
export(checksums_sql)
export(climateLayers)
export(climateStacksByPeriod)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 31 additions & 0 deletions R/checkCalendarYears.R
Original file line number Diff line number Diff line change
@@ -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)
}
36 changes: 36 additions & 0 deletions man/checkCalendarYears.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/testthat/test-checkCalendarYears.R
Original file line number Diff line number Diff line change
@@ -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")
})