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

* A hindcast or forecast with a `~ 1` spread fit (no spread climate variable, so `climateVariablesForFire$spread` is empty) stopped when the covariates were built (`terra` cannot select no layer). The spread covariate table now has `pixelID` and `youngAge` and the fuels, with no climate column; the ignition covariates are unchanged. Together with `fireSenseUtils::climateRasterToDataTable()` returning a table for no raster (PredictiveEcology/fireSenseUtils, null-arm hindcasts of `fireSense_dataPrepFit`).

* A hindcast of year Y built `youngAge` differently from the fit, for the spread and the ignition covariates: it used the cohort ages and the data year's time since disturbance as they were, and did not see a fire between the data year and Y (youngAge was 2.8% of burned pixels in ELF 6.6.2 in 2010, 1.5% in the fit). It now resolves it as `fireSense_dataPrepFit` does: the data year's time since disturbance aged to Y and every fire after it, from `fireSenseUtils::youngAgeAtYear()` (spread) and `fireSenseUtils::prepare_FuelCovsCoarseByYear()` (ignition), with the fires from `historicalFireRaster`, else `firePolysForAge` (`fireSenseUtils::firePixelsByYear()`, as the fit chooses). Fuel, non-forest land cover and treed wetland are cleared where `youngAge` is 1 in the covariate table itself (`fireSenseUtils::clearYoungAge()`, which the ignition fit uses), so a fit on log-scale fuel, for which `fireSense_spreadPredict` clears only the indicators, is right too. A hindcast with `nonForestCanBeYoungAge = FALSE` stops, as the fit does (`fireSenseUtils::stopIfNonForestCannotBeYoung()`). New inputs `firePolysForAge` and `historicalFireRaster` (from `fireSense_dataPrepFit`); a hindcast with neither stops. Hindcast covariates, and so their cache keys, change. Forecasts (a module simulates `cohortData`) are unchanged. Needs the fireSenseUtils version after PredictiveEcology/fireSenseUtils#239 merges; the `reqdPkgs` floor is set after that merge's version bump.

* New parameters `replaySave` (default `FALSE`) and `replayFrom` (default `NA`): no change unless set. `replaySave = TRUE` writes, once after the first covariate build, the spread-prediction inputs that do not depend on the spread parameters to `outputPath(sim)` through SpaDES.core's `outputs()` (its `save` event): `fireSense_SpreadCovariates`, `flammableRTM`, `rasterToMatch`, `rasterToMatchELF`, `rasterToMatchLargeELF`, `studyAreaWithSpreadParams`, `nonEscapedFireSizesHa`, `ignitionFirePoints` and `firePolys` (those present), as `replay_<name>_year<time>.<ext>`: `.tif` (`terra::writeRaster()`, keeps categories), `.gpkg` for vectors (`terra::writeVector()`; `firePolys` stacked into one, element names in a column), `.qs2` for tables and `studyAreaWithSpreadParams` (its list columns survive). `replayFrom = <that folder>` reads them back in `.inputObjects` through `inputs()`/`loadFiles()` and schedules no covariate event, so a job needs no climate, vegetation, fit or ELF module. The module declares the objects other modules make as inputs only. `qs2` is now in `reqdPkgs`.
Expand Down
31 changes: 22 additions & 9 deletions fireSense_dataPrepPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ ageNonForest <- function(TSD, rstCurrentBurn, yearsElapsed = 1L) {
#' @return The `simList`, invisibly.
prepare_IgnitionAndEscapePredict <- function(sim) {
## get climate
ignitionClimate <- sim$currentClimateRasters[[sim$climateVariablesForFire$ignition]]
ignitionClimate <- climateLayers(sim, "ignition")
if (is.null(ignitionClimate))
stop("ignitionClimate is NULL; there is a problem to debug")

Expand Down Expand Up @@ -740,6 +740,19 @@ hindcastYoungAgeInputs <- function(sim) {
inputs
}

#' The layers of `currentClimateRasters` that the ignition or spread covariates use
#'
#' @param sim A `simList`.
#' @param what `"ignition"` or `"spread"`.
#' @return A `SpatRaster`, or `NULL` when the variables are `character(0)` (a spread model `~ 1` has none).
#' Stops if `climateVariablesForFire[[what]]` is `NULL` (absent): that is a malformed `sim`, not a model without one.
climateLayers <- function(sim, what) {
vars <- sim$climateVariablesForFire[[what]]
if (is.null(vars))
stop("climateVariablesForFire$", what, " is NULL; use character(0) for a model with no climate variable")
if (length(vars)) sim$currentClimateRasters[[vars]]
}

#' Build this year's spread covariates
#'
#' Creates `sim$fireSense_SpreadCovariates` from fuel classes, non-forest landcover and
Expand All @@ -749,9 +762,7 @@ hindcastYoungAgeInputs <- function(sim) {
#'
#' @return The `simList`, invisibly.
prepare_SpreadPredict <- function(sim) {
spreadClimate <- sim$currentClimateRasters[[sim$climateVariablesForFire$spread]]
if (is.null(spreadClimate))
stop("spreadClimate is NULL; there is a problem to debug")
spreadClimate <- climateLayers(sim, "spread")

sim <- useOwnELFRows(sim)
## one fuel set per fitted ELF (one, as before, when there is one ELF). Every ELF's covariates are made for
Expand Down Expand Up @@ -827,11 +838,13 @@ prepare_SpreadPredict <- function(sim) {
}))

# TODO: this chunk is untested 18/12/2024
climateCovariates <- spreadClimate |> as.data.frame(cells = TRUE)
climateCovariates <- na.omit(climateCovariates) |> as.data.table()
if (!is.null(spreadClimate)) { # a '~ 1' spread fit has no climate variable
climateCovariates <- spreadClimate |> as.data.frame(cells = TRUE)
climateCovariates <- na.omit(climateCovariates) |> as.data.table()

setnames(climateCovariates, new = c("pixelID", names(spreadClimate)))
spreadCovariates <- climateCovariates[spreadCovariates, on = c("pixelID")]
setnames(climateCovariates, new = c("pixelID", names(spreadClimate)))
spreadCovariates <- climateCovariates[spreadCovariates, on = c("pixelID")]
}

nonFuelNames <- c("pixelID", names(spreadClimate), "youngAge")
setcolorder(spreadCovariates, neworder = nonFuelNames)
Expand Down Expand Up @@ -884,7 +897,7 @@ covariatesBuilt <- function(sim, what, extra = list()) {
sa <- sim$studyAreaWithSpreadParams
rows <- list(as.character(sim$.ELFind), as.character(sa[[fireSenseUtils::polygonIDTxt]]),
if (!is.null(sa$params)) lapply(sa$params, colnames))
climate <- sim$currentClimateRasters[[sim$climateVariablesForFire[[what]]]]
climate <- climateLayers(sim, what)
stamp <- list(floor(as.numeric(time(sim))), sharedInputStamp(sim), lapply(c(list(climate), extra), inputDigest), rows)
built <- sim[[c(ignition = "fireSense_igAndEscapePred_Covariates", spread = "fireSense_SpreadCovariates")[[what]]]]
if (!is.null(built) && identical(mod$covariateStamps[[what]], stamp)) return(TRUE)
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-covariates.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,28 @@ test_that("ageNonForest runs as an event and ages the TSD raster each year", {
expect_identical(as.vector(terra::values(sim$nonForest_timeSinceDisturbance)),
c(6, rep(31, 13), 6, 31))
})

test_that("a '~ 1' spread fit (no spread climate variable) builds the spread covariates without climate", {
objs <- toyObjects()
objs$climateVariablesForFire <- list(ignition = "MDC", spread = character(0))
sim <- toyPrepRun(objs)
sp <- covDF(sim$fireSense_SpreadCovariates)
expect_identical(sp$pixelID, 1:15)
expect_identical(names(sp)[1:2], c("pixelID", "youngAge"))
expect_false("MDC" %in% names(sp))
## ignition climate is unaffected
ig <- covDF(sim$fireSense_igAndEscapePred_Covariates)
expect_true("MDC" %in% names(ig))
})

test_that("a NULL spread climate variable (a malformed sim) stops", {
objs <- toyObjects()
objs$climateVariablesForFire <- list(ignition = "MDC", spread = NULL)
expect_error(toyPrepRun(objs), "climateVariablesForFire\\$spread is NULL")
})

test_that("ignition always needs climate: character(0) ignition variables stop", {
objs <- toyObjects()
objs$climateVariablesForFire <- list(ignition = character(0), spread = "MDC")
expect_error(toyPrepRun(objs), "ignitionClimate is NULL")
})
Loading