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 @@
# 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 `<table>_<ELF>.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`).
Expand Down
32 changes: 28 additions & 4 deletions fireSense_dataPrepFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<dataYear>`).
#' @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
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-vegStartYear.R
Original file line number Diff line number Diff line change
@@ -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)
})