Skip to content
Merged
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 NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# canClimateData (development version)

- `outputDir` may be a `reproducible::asPath(dir, nParentDirs)`: the event cache keys then hold only the last `nParentDirs + 1` components of the folder, so a cache built on one machine matches on another where the folder has a different prefix, while a different folder (e.g. another hindcast) still gives a different key. A plain path is keyed in full, as before. Nothing in the module changed; the parameter description says how to pass it.
- Rerunning this module on a finished run whose years are not calendar years (NRV), with that run's `climateYearRecord` (from the climateYear module), now prepares the climate years the record holds for the simulation years the rerun serves (the load times of its inputs, `inputs(sim)`, which `alreadyRun` sets to the finished run's save times), not the years in `climateVariables` or `historicalClimateYears`/`projectedClimateYears`. Variables by period, and `calcCumMDC` ones (which need consecutive years), keep their years. `times` are not checked as calendar years then. `climateYearRecord` is a new optional input; in a normal run climateYear writes it after this module's `init`, and `loadOrder` keeps that order.
- `climateData` is now required from its modsForFireSense branch (>= 2.2.3.9011). `checkCalendarYears()` is only there; climateData development reached 2.2.3.9011 without it, so the old floor installed a climateData whose `init` failed with "could not find function".
- `projectedClimateYears` now defaults to 2017:2100 instead of 2011:2100. The projected archive for tile 39 (CNRM-ESM2-1 ssp370) has no 2013-2016 files, and observed climate now covers 1901-2024, so projections before 2017 are not needed.
Expand Down
8 changes: 7 additions & 1 deletion canClimateData.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ defineModule(sim, list(
"(e.g., when running multiple simulation replicates)",
"to avoid multiple copies of the climate data.",
"If not specified by the user, a subdirectory of the simulation output",
"directory will be used (i.e., `file.path(outputPath(sim), 'climate')`.")),
"directory will be used (i.e., `file.path(outputPath(sim), 'climate')`.",
"Event cache keys hold this value as given, so a different folder",
"is a different key, and a machine-specific prefix is part of it.",
"To let the same folder match on another machine, pass",
"`reproducible::asPath(dir, nParentDirs)`: the key then holds only",
"`dir`'s last `nParentDirs + 1` components, e.g.",
"`asPath(dir, 2)` for `<hindcast>/<ELF>/climate`.")),
defineParameter("projectedClimatePeriod", "character", NULL, NA, NA,
paste("period to use for projected climate normals",
"if `climateVariables` not supplied.")),
Expand Down
49 changes: 49 additions & 0 deletions tests/testthat/test-outputDirKey.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## `outputDir` is part of the event cache keys, so an absolute path with a machine-specific prefix
## (the hindcast climate folder, <basePath>/outputs/hindcast<tag>/<ELF>/climate) made a cache built
## on one machine miss on another. `outputDir` may be `reproducible::asPath(dir, nParentDirs)`, which
## the key holds as only `dir`'s last `nParentDirs + 1` components. A different folder must still miss:
## a hit writes nothing into the new folder.

test_that("Init() uses a Path outputDir as the folder the climate files go to", {
withr::local_options(reproducible.useCache = FALSE, reproducible.useNewDigestAlgorithm = 2)
root <- withr::local_tempdir()
dir <- file.path(root, "outputs", "hindcastX", "9.1", "climate")
sim <- runInit(path = withr::local_tempdir(), params = list(outputDir = reproducible::asPath(dir, 2)))
expect_true(all(startsWith(normalizePath(terra::sources(sim$historicalClimateRasters[[1]])),
normalizePath(dir))))
})

## a module with this module's parameter and a cached init, to read the key from the cache
writeKeyModule <- function(modulePath) {
dir.create(file.path(modulePath, "m"), recursive = TRUE)
cat(file = file.path(modulePath, "m", "m.R"), sep = "", '
defineModule(sim, list(name = "m", description = "", keywords = "", authors = person("A", "B"),
childModules = character(0), version = list(m = "0.0.1"), timeframe = as.POSIXlt(c(NA, NA)),
timeunit = "year", citation = list(), documentation = list(), reqdPkgs = list(),
parameters = rbind(
defineParameter("outputDir", "character", NA_character_, NA, NA, "d"),
defineParameter(".useCache", "character", "init", NA, NA, "c")),
inputObjects = bindrows(), outputObjects = bindrows()))
doEvent.m <- function(sim, eventTime, eventType, debug = FALSE) invisible(sim)
')
}

test_that("a Path outputDir keys the event on its last components: same under another base, different for another folder", {
skip_if_not_installed("SpaDES.core")
withr::local_options(spades.loadReqdPkgs = FALSE, spades.moduleCodeChecks = FALSE, spades.useRequire = FALSE,
reproducible.useMemoise = FALSE, spades.saveSimOnExit = FALSE)
modulePath <- withr::local_tempdir(); cachePath <- withr::local_tempdir()
writeKeyModule(modulePath)
nKeys <- function(outputDir) {
s <- suppressMessages(SpaDES.core::simInit(modules = "m", times = list(start = 0, end = 1),
params = list(m = list(outputDir = outputDir)),
paths = list(modulePath = modulePath, cachePath = cachePath)))
suppressMessages(SpaDES.core::spades(s, debug = FALSE))
length(unique(reproducible::showCache(cachePath, verbose = -2)$cacheId))
}
asP <- function(base, hindcast) reproducible::asPath(file.path(base, "outputs", hindcast, "9.1", "climate"), 2)
expect_identical(nKeys(asP("/machineA", "hindcastX")), 1L)
expect_identical(nKeys(asP("/other/machineB", "hindcastX")), 1L) # same relative folder: a hit
expect_identical(nKeys(asP("/machineA", "hindcastY")), 2L) # another folder: a miss
expect_identical(nKeys(file.path("/machineA", "outputs", "hindcastX", "9.1", "climate")), 3L) # a plain path is keyed in full
})
Loading