diff --git a/NEWS.md b/NEWS.md index 0c9e466..23e81ea 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/fireSense_dataPrepPredict.R b/fireSense_dataPrepPredict.R index 44aede7..dace378 100644 --- a/fireSense_dataPrepPredict.R +++ b/fireSense_dataPrepPredict.R @@ -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`, from before the", "first fit year. A hindcast needs it or `historicalFireRaster`: the fires after the data year make a", @@ -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 diff --git a/tests/testthat/test-latestVegetation.R b/tests/testthat/test-latestVegetation.R index 3644947..ac57195 100644 --- a/tests/testthat/test-latestVegetation.R +++ b/tests/testthat/test-latestVegetation.R @@ -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) @@ -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) }) diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index de7b3c0..9cff434 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -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",