diff --git a/NEWS.md b/NEWS.md index 91acc87..cf78c1f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # fireSense_dataPrepPredict (development version) +- `studyArea` is now a declared input. `.inputObjects` used it to mask the fire polygons, land cover and stand age it + makes, but an undeclared object is not visible there, so it was always `NULL` and nothing was masked. It also made + an unset `.studyAreaName` come from the extent of `rasterToMatch` in `.inputObjects` but from `studyArea` in `Init`. + That `rasterToMatch` fallback is removed: without a `studyArea`, an unset `.studyAreaName` stays `NA`. - New parameter `.studyAreaName` (default `NA`). The module already read `P(sim)$.studyAreaName` without defining it, so it was always `NULL`, whatever the user set. Left `NA`, it becomes a hash of `studyArea` (or of the extent of `rasterToMatch`), as in Biomass_borealDataPrep. It names the study area in the landcover file diff --git a/fireSense_dataPrepPredict.R b/fireSense_dataPrepPredict.R index adb573a..084cc50 100644 --- a/fireSense_dataPrepPredict.R +++ b/fireSense_dataPrepPredict.R @@ -10,7 +10,7 @@ defineModule(sim, list( person("Alex M", "Chubaty", role = "ctb", email = "achubaty@for-cast.ca") ), childModules = character(0), - version = list(fireSense_dataPrepPredict = "1.0.4.9002"), + version = list(fireSense_dataPrepPredict = "1.0.4.9003"), timeframe = as.POSIXlt(c(NA, NA)), timeunit = "year", citation = list("citation.bib"), @@ -133,6 +133,9 @@ defineModule(sim, list( desc = "Table of LandR species equivalencies; must have columns `sppEquivCol` and `fuelClassCol`."), expectsInput("standAgeMap", "SpatRaster", sourceURL = NA, desc = "Stand age (years) at `start(sim)`."), + expectsInput("studyArea", "SpatVector", sourceURL = NA, + desc = paste("Polygon of the study area. The fire polygons, land cover and stand age made in", + "`.inputObjects` are masked to it.")), expectsInput("landcoverDT", "data.table", sourceURL = NA, desc = paste( "`pixelID` plus one binary column per non-forest landcover group, for flammable pixels,", @@ -576,20 +579,6 @@ unionLCCGroups <- function(fuelSets) { #' @param sim A `simList`. #' #' @return The `simList`, invisibly. -## An NA `.studyAreaName` becomes a hash of the study area (as in Biomass_borealDataPrep), so NA never -## reaches a file name or cache tag. Without `studyArea`, the extent of `rasterToMatch` stands in for it. -resolveStudyAreaName <- function(sim) { - if (is.na(P(sim)$.studyAreaName)) { - sa <- sim$studyArea - if (is.null(sa)) - sa <- terra::as.polygons(terra::ext(sim$rasterToMatch), crs = terra::crs(sim$rasterToMatch)) - params(sim)[[currentModule(sim)]][[".studyAreaName"]] <- reproducible::studyAreaName(sa) - message("The .studyAreaName is not supplied; derived name from the study area: ", - params(sim)[[currentModule(sim)]][[".studyAreaName"]]) - } - sim -} - .inputObjects <- function(sim) { cacheTags <- c(currentModule(sim), "otherFunctions:.inputObjects") dPath <- asPath(inputPath(sim), 1) @@ -661,3 +650,14 @@ resolveStudyAreaName <- function(sim) { return(invisible(sim)) } + +## An NA `.studyAreaName` becomes a hash of `studyArea`, as in Biomass_borealDataPrep. Without a +## `studyArea` it stays NA (see PredictiveEcology/LandR#246). +resolveStudyAreaName <- function(sim) { + if (is.na(P(sim)$.studyAreaName) && !is.null(sim$studyArea)) { + params(sim)[[currentModule(sim)]][[".studyAreaName"]] <- reproducible::studyAreaName(sim$studyArea) + message("The .studyAreaName is not supplied; derived name from the study area: ", + params(sim)[[currentModule(sim)]][[".studyAreaName"]]) + } + sim +} diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index aeae8d4..1ef1f1a 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -36,7 +36,8 @@ test_that("inputs are the expected names and classes", { rstLCCs = "list", sppEquiv = "data.table", sppEquivs = "list", - standAgeMap = "SpatRaster") + standAgeMap = "SpatRaster", + studyArea = "SpatVector") ) }) diff --git a/tests/testthat/test-studyAreaName.R b/tests/testthat/test-studyAreaName.R index 309079d..6c890c3 100644 --- a/tests/testthat/test-studyAreaName.R +++ b/tests/testthat/test-studyAreaName.R @@ -9,13 +9,11 @@ test_that("an unset .studyAreaName becomes a hash of the study area", { expect_identical(nm, reproducible::studyAreaName(objs$studyArea)) }) -test_that("without studyArea, the extent of rasterToMatch names it", { +test_that("without studyArea, an unset .studyAreaName stays NA", { objs <- toyObjects() objs$studyArea <- NULL sim <- SpaDES.core::spades(toyPrepSim(objs), events = "init", debug = FALSE) - nm <- SpaDES.core::P(sim, module = moduleName)$.studyAreaName - expect_false(is.na(nm)) - expect_type(nm, "character") + expect_true(is.na(SpaDES.core::P(sim, module = moduleName)$.studyAreaName)) }) test_that("a supplied .studyAreaName is kept", {