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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# canClimateData (development version)

- `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

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.
Expand Down
18 changes: 16 additions & 2 deletions canClimateData.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -142,6 +142,19 @@ climateRastersToRewrite <- function(projectedType) {
}

Init <- function(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
Expand Down Expand Up @@ -290,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
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-calendarYears.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## 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("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)),
"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")
})