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's `youngAge` takes the fires (and cuts) after the data year from `disturbancePixelsByYear` when `fireSense_dataPrepFit` made one (Biomass_borealDataPrep `ageDisturbanceSources = "all"`), as the fit does; otherwise from `historicalFireRaster` or `firePolysForAge`, as before.

* `ELFlabels()` reads the ELF of each cell with `fireSenseUtils::elfCellLabels()`, the lookup `fireSense_spreadPredict` uses too, instead of its own copy. The set it returns is now the labels of the cells (`unique()`, without `NA`): a category no cell has is no longer in it. Needs fireSenseUtils with `elfCellLabels()` (Depends-on PredictiveEcology/fireSenseUtils#244); the `reqdPkgs` floor is set after that merges.

* `fireSense_igAndEscapePred_Covariates` has a `coreFraction` column, the share of each coarse pixel's flammable cells that are core (`fireSenseUtils::addCoreFraction()` on `flammableRTM` and `rasterToMatchLargeELF`, with `igAggFactor`, which is forced to the fit module's value), as `fireSense_dataPrepFit` gives it to the fit. `fireSense_ignitionPredict`'s year climate term averages the core pixels only, as the fit does; without the column it would average the buffer too. It does not depend on the year, so it is kept in the module and remade only when `flammableRTM`, `rasterToMatchLargeELF` or `igAggFactor` change. A pixel with no flammable cell gets 0 with one warning. A multi-ELF categorical raster or none gives 1. Needs fireSenseUtils (>= 0.2.3.9183).
Expand Down
19 changes: 13 additions & 6 deletions fireSense_dataPrepPredict.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ defineModule(sim, list(
desc = "From `fireSense_dataPrepFit`. Only saved and replayed (`replaySave`, `replayFrom`)."),
expectsInput("firePolys", "list", sourceURL = NA,
desc = "Annual fire polygons, if supplied. Only saved and replayed (`replaySave`, `replayFrom`)."),
expectsInput("disturbancePixelsByYear", "list", sourceURL = NA,
desc = paste("From `fireSense_dataPrepFit`, optional: the pixels with a stand-replacing fire or harvest in",
"each year (fire and harvest records the stand ages use). Replaces `firePolysForAge` and",
"`historicalFireRaster` for a hindcast's `youngAge`, as in the fit.")),
expectsInput("firePolysForAge", "list", sourceURL = NA,
desc = paste("From `fireSense_dataPrepFit`: annual fire polygons, named `year<year>`, from before the",
"first fit year. A hindcast needs it or `historicalFireRaster`: the fires after the data year make a",
Expand Down Expand Up @@ -742,15 +746,18 @@ hindcastYoungAgeInputs <- function(sim) {
inputs <- NULL
if (isHindcast(sim) && any(grepl("[0-9]", names(tsds)))) {
fireSenseUtils::stopIfNonForestCannotBeYoung(P(sim)$nonForestCanBeYoungAge)
if (is.null(sim$firePolysForAge) && is.null(sim$historicalFireRaster))
if (is.null(sim$disturbancePixelsByYear) && is.null(sim$firePolysForAge) && is.null(sim$historicalFireRaster))
stop("fireSense_dataPrepPredict: a hindcast resolves youngAge as the fit does, from the fires after the data ",
"year: supply firePolysForAge or historicalFireRaster (made by fireSense_dataPrepFit)", call. = FALSE)
"year: supply disturbancePixelsByYear, firePolysForAge or historicalFireRaster (made by fireSense_dataPrepFit)",
call. = FALSE)
el <- fireSenseUtils::dataYearElement(tsds, year, before = TRUE)
## the fit's choice (fireSense_dataPrepFit allFirePixelsByYear()): the raster if there is one, else firePolysForAge
## the fit's choice (fireSense_dataPrepFit allFirePixelsByYear()): disturbancePixelsByYear if there is one,
## else the raster if there is one, else firePolysForAge
inputs <- list(tsd = tsds[[el]], dataYear = as.integer(gsub("[^0-9]", "", el)), year = year,
firePixels = fireSenseUtils::firePixelsByYear(firePolys = sim$firePolysForAge,
fireRaster = sim$historicalFireRaster,
template = sim$flammableRTM))
firePixels = if (!is.null(sim$disturbancePixelsByYear)) sim$disturbancePixelsByYear else
fireSenseUtils::firePixelsByYear(firePolys = sim$firePolysForAge,
fireRaster = sim$historicalFireRaster,
template = sim$flammableRTM))
}
mod$hindcastYoungAge <- list(year = year, inputs = inputs)
inputs
Expand Down
11 changes: 10 additions & 1 deletion tests/testthat/test-latestVegetation.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ test_that("fires are read from firePolysForAge when there is no historicalFireRa
expect_identical(df$pixelID[df$youngAge == 1], c(1L, 3L, 9L))
})

test_that("disturbancePixelsByYear, when there is one, gives the fires and cuts in place of firePolysForAge", {
objs <- yearLists()
cell3 <- terra::as.polygons(terra::ext(2, 3, 3, 4), crs = "EPSG:3005")
objs$firePolysForAge <- list(year2004 = cell3) # not used
objs$disturbancePixelsByYear <- list(`2004` = 4L) # cell 4 cut or burned in 2004
df <- covDF(hindcast2005(objs)$fireSense_SpreadCovariates)
expect_identical(df$pixelID[df$youngAge == 1], c(1L, 4L, 9L))
})

test_that("a forecast keeps the youngAge of the simulated cohorts", {
df <- covDF(forecast()$fireSense_SpreadCovariates)
expect_true(all(df$youngAge[df$pixelID %in% c(5L, 6L)] == 1)) # cohort age 10 (+ 0 years: not aged here)
Expand Down Expand Up @@ -281,7 +290,7 @@ test_that("a hindcast clears fuel and land cover on young pixels as the fit does
test_that("a hindcast with no fire data stops; the fire inputs are read once per year", {
objs <- yearLists()
objs$firePolysForAge <- NULL
expect_error(hindcast2005(objs), "firePolysForAge or historicalFireRaster")
expect_error(hindcast2005(objs), "disturbancePixelsByYear, firePolysForAge or historicalFireRaster")
sim <- hindcast2005(yearLists())
expect_identical(toyModP(sim)$hindcastYoungAge$year, 2005)
})
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test_that("inputs are the expected names and classes", {
cohortData = "data.table",
cohortDatas = "list",
currentClimateRasters = "SpatRaster",
disturbancePixelsByYear = "list",
firePolys = "list",
firePolysForAge = "list",
flammableRTM = "SpatRaster",
Expand Down