diff --git a/NEWS.md b/NEWS.md
index 2347a24..9180d66 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,6 @@
# fireSense_dataPrepFit 1.3.0.9011 (development version)
+* Biomass_borealDataPrep now gets the stand age map and land cover of its own `dataYear` from `dataPrepBuild` (`vegStartYearName()`), not those of the last of `dataYears`. A run whose vegetation starts before the last data year (the attribution hindcasts, `dataYear` 1985) got the 2020 stand ages on 1985 biomass and species: stands burned in 1980-1985 started at about 38 years, and stands that burn in 1986-2020 at about 11. Fits, hindcast validation and NRV runs are unchanged: they read the per-year lists, or start from the last data year.
* The per-year stand age maps (`standAgeMaps`, which the fits' vegetation and the hindcasts use) reset age at the last stand-replacing disturbance from `LandR::prepInputsDisturbanceYear()` (NFDB, NBAC and CanLaD fire and harvest) with the new parameter `ageDisturbanceSources = "all"` (opt in; same parameter as Biomass_borealDataPrep's, checked to match, so set it in `.globals`), so they match the simulation's own starting ages; `youngAge` then follows the same fires and cuts (new `disturbancePixelsByYear`). It is this module's own parameter because a hindcast runs without Biomass_borealDataPrep. With `"NFDB"` (the default) nothing changes. Opting in changes the vegetation the fits are made with.
* New parameter `saveFitTables` (default `character(0)`: nothing is written). `"ignition"` and/or `"escape"` write `fireSense_ignitionCovariates` and `fireSense_escapeCovariates`, the exact tables the fits are trained on, to `outputPath(sim)` as `
_.qs2` (qs2) and list them in `outputs(sim)`, so an analysis outside the fit can use them. They are written by a new event `saveFitTables` after the prep events: it is never cached (`.neverCache`) and reads the tables from the simList, so a cached `prepIgnitionFitData` still writes them. No table changes, and the parameter is not in the cache keys (`cacheKey = FALSE`).
diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R
index 09d9864..14fca27 100644
--- a/fireSense_dataPrepFit.R
+++ b/fireSense_dataPrepFit.R
@@ -541,13 +541,15 @@ defineModule(sim, list(
createsOutput("propFlammable", "SpatRaster",
"Last element of `propFlammables`.",
events = "dataPrepBuild"),
- createsOutput("standAgeMap", "SpatRaster", "Last element of `standAgeMaps`.",
+ createsOutput("standAgeMap", "SpatRaster",
+ "The element of `standAgeMaps` for Biomass_borealDataPrep's `dataYear` (see `vegStartYearName()`).",
events = "dataPrepBuild"),
createsOutput("rstLCC_RTM", "SpatRaster",
paste0("Last element of the output `rstLCCs`, i.e., on `rasterToMatch`."),
events = "dataPrepBuild"),
createsOutput("rstLCC", "SpatRaster",
- paste0("Last element of the input `rstLCCs`, i.e., on `rasterToMatch_biomassParam`."),
+ paste0("The element of the input `rstLCCs` for Biomass_borealDataPrep's `dataYear` (see ",
+ "`vegStartYearName()`), on `rasterToMatch_biomassParam`."),
events = "dataPrepBuild"),
createsOutput("flammableRTM", "SpatRaster", "Last element of `flammableRTMs`.",
events = "dataPrepBuild"),
@@ -648,6 +650,9 @@ Init <- function(sim) {
escapeDefinition = fireSenseUtils::escapeDefinitionOf(sim))
}
mod$haveSpreadFit <- is(sim$spreadFitPreRun, "sf") || is(sim$spreadFitPreRun, "data.frame")
+ ## the year Biomass_borealDataPrep builds the simulation's starting vegetation for; set here, not in the
+ ## cached dataPrepBuild, so that event's cache key (which covers mod$ but not other modules' parameters) sees it
+ mod$vegStartYear <- params(sim)$Biomass_borealDataPrep$dataYear
## doFit FALSE (the default) never fits: without a ledger fit for this ELF, stop now, before hours of data
## preparation for a fit that fireSense_spreadFit would refuse. A held-out fold fits without the ledger.
## Over a studyArea_fireSense, `.ELFind` names several ELFs (fireSense_ELFs), and each needs its fit.
@@ -760,7 +765,9 @@ dataPrepBuild <- function(sim) {
#crop rstLCC and standAgeMap to create smaller objects before their derived objects
# (landcoverDT/flammableMap and nonForest_timeSinceDisturbance, respectively).
# This is at rasterToMatch_biomassParam which is needed by Biomass_borealDataPrep
- sim$rstLCC <- tail(sim$rstLCCs, 1)[[1]]
+ ## Biomass_borealDataPrep reads rstLCC and standAgeMap for its own dataYear (see vegStartYearName())
+ vegYear <- vegStartYearName(sim$rstLCCs, mod$vegStartYear)
+ sim$rstLCC <- sim$rstLCCs[[vegYear]]
objs <- sim$rstLCCs
if (!LandR::.compareRas(sim$rasterToMatch, sim$rasterToMatch_biomassParam, stopOnError = FALSE)) {
@@ -789,7 +796,7 @@ dataPrepBuild <- function(sim) {
# Create the "objects for prediction cases
#this object is still at biomassParam size
- sim$standAgeMap <- tail(sim$standAgeMaps, 1)[[1]]
+ sim$standAgeMap <- sim$standAgeMaps[[vegYear]]
sim$propFlammable <- tail(sim$propFlammables, 1)[[1]]
# This is now RTM
@@ -1327,6 +1334,23 @@ lccShareForYear <- function(sim, year) {
fireSenseUtils::lccFlammableShare(sim$rstLCCs[[el]], sim$flammableRTMs[[el]])
}
+#' The data year of the simulation's starting vegetation
+#'
+#' Biomass_borealDataPrep builds the simulation's starting vegetation for its own `dataYear`, from the
+#' `rstLCC` and `standAgeMap` this module hands it, so those must be that year's elements of `rstLCCs` and
+#' `standAgeMaps`. Handing it the last of `dataYears` gave a hindcast that starts from SCANFI 1985 the
+#' 2020 stand ages on 1985 biomass and species.
+#'
+#' @param rstLCCs the per-year list (named `year`).
+#' @param dataYear Biomass_borealDataPrep's `dataYear`; `NULL` or `NA` (module not in the simulation)
+#' gives the last element.
+#' @return the element name: that of the latest data year at or before `dataYear`
+#' (`fireSenseUtils::dataYearElement()`).
+vegStartYearName <- function(rstLCCs, dataYear) {
+ if (!length(dataYear) || is.na(dataYear)) return(tail(names(rstLCCs), 1))
+ fireSenseUtils::dataYearElement(rstLCCs, dataYear)
+}
+
#' The year a fit's structure decisions are for
#'
#' The structure of the spread model -- whether treed wetland is a term (`minCovariateProp`), how the
diff --git a/tests/testthat/test-vegStartYear.R b/tests/testthat/test-vegStartYear.R
new file mode 100644
index 0000000..c15741b
--- /dev/null
+++ b/tests/testthat/test-vegStartYear.R
@@ -0,0 +1,27 @@
+## Biomass_borealDataPrep builds the simulation's starting vegetation for its own dataYear from the rstLCC
+## and standAgeMap dataPrepBuild hands it. Handing it the last of dataYears gave a hindcast that starts from
+## SCANFI 1985 (dataYear 1985) the 2020 stand ages on 1985 biomass and species.
+
+test_that("vegStartYearName picks Biomass_borealDataPrep's dataYear, or the latest data year before it", {
+ ages <- list(year1985 = 1L, year1990 = 2L, year2020 = 3L)
+ expect_identical(vegStartYearName(ages, 1985L), "year1985")
+ expect_identical(vegStartYearName(ages, 1989), "year1985")
+ expect_identical(vegStartYearName(ages, 2020L), "year2020")
+ expect_identical(vegStartYearName(ages, 2025L), "year2020")
+ ## without Biomass_borealDataPrep in the simulation: the last data year, as before
+ expect_identical(vegStartYearName(ages, NULL), "year2020")
+ expect_identical(vegStartYearName(ages, NA), "year2020")
+ expect_error(vegStartYearName(ages, 1980L), "no vegetation data")
+})
+
+test_that("dataPrepBuild hands on rstLCC and standAgeMap for that year, set in the uncached Init", {
+ defs <- moduleDefs()
+ build <- paste(deparse(defs[["dataPrepBuild"]]), collapse = "\n")
+ expect_match(build, "vegYear <- vegStartYearName(sim$rstLCCs, mod$vegStartYear)", fixed = TRUE)
+ expect_match(build, "sim$rstLCC <- sim$rstLCCs[[vegYear]]", fixed = TRUE)
+ expect_match(build, "sim$standAgeMap <- sim$standAgeMaps[[vegYear]]", fixed = TRUE)
+ expect_no_match(build, "tail(sim$standAgeMaps", fixed = TRUE)
+ ## the cached event's key covers mod$ but not other modules' parameters, so the year is read in Init
+ expect_match(paste(deparse(defs[["Init"]]), collapse = "\n"),
+ "mod$vegStartYear <- params(sim)$Biomass_borealDataPrep$dataYear", fixed = TRUE)
+})