From ba992d8ea0a3d340743607909afa3e80335511b2 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 30 Nov 2026 14:23:48 -0700 Subject: [PATCH] dataPrepBuild hands Biomass_borealDataPrep its own dataYear's stand age and land cover dataPrepBuild set sim$standAgeMap and sim$rstLCC to the last of dataYears (2020). Biomass_borealDataPrep uses them as given, so a run that starts its vegetation in 1985 got 2020 stand ages on 1985 biomass and species. The year is read in Init (mod$vegStartYear), because the cached dataPrepBuild's key covers mod$ but not other modules' parameters. New test-vegStartYear.R. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXfVqAugcusGHygZGJ3 --- NEWS.md | 2 ++ fireSense_dataPrepFit.R | 32 ++++++++++++++++++++++++++---- tests/testthat/test-vegStartYear.R | 27 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-vegStartYear.R diff --git a/NEWS.md b/NEWS.md index e7be3e4..3677017 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # 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. + * 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`). * New parameter `spreadClimateCurve` (`FALSE` by default: nothing changes). `TRUE` adds a covariate `_curve = softplus(C - k, s)` (smooth, no corner) for each spread climate variable `C`, with its own coefficient, so the climate response can steepen in dry years; `k` and `s` are the ELF's median and sd of `C` over the burnable pixels of all the fit years. They are in `sim$climateCurvePars`, which `fireSense_spreadFit` writes to the ledger row, and `fireSense_spreadPredict` builds the term from them. A negative coefficient flattens the dry side. A reused ledger fit keeps its own curve terms and their `k` and `s`. The code is `fireSenseUtils::climateCurvePars()`, `addClimateCurve()`, `softplus()` and `climateTermCols()`, in the `prepSpreadFitData` cache key. Needs `fireSense_spreadFit`, `fireSense_spreadPredict` and fireSenseUtils with the curve (fireSenseUtils >= 0.2.3.9196). diff --git a/fireSense_dataPrepFit.R b/fireSense_dataPrepFit.R index b4e5f41..230f9be 100644 --- a/fireSense_dataPrepFit.R +++ b/fireSense_dataPrepFit.R @@ -526,13 +526,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"), @@ -633,6 +635,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. @@ -745,7 +750,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)) { @@ -774,7 +781,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 @@ -1312,6 +1319,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) +})