diff --git a/DESCRIPTION b/DESCRIPTION index 888c8d4..9b46a67 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Utilities for Working With the 'fireSense' Group of 'SpaDES' Modules Description: Utilities for working with the 'fireSense' group of 'SpaDES' modules. Date: 2024-12-20 -Version: 0.0.5.9078 +Version: 0.0.5.9079 Authors@R: c( person("Jean", "Marchal", email = "jean.d.marchal@gmail.com", role = c("aut")), diff --git a/NAMESPACE b/NAMESPACE index 3d895bc..b1acf04 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,6 +28,7 @@ export(cohortsToFuelClasses) export(compareMDC) export(dtReplaceNAwith0) export(extractSpecial) +export(fuelClassPrep) export(getFirePoints_NFDB) export(getFirePoints_NFDB_V2) export(getFirePolygons) diff --git a/R/fuelClassPrep.R b/R/fuelClassPrep.R new file mode 100644 index 0000000..a840282 --- /dev/null +++ b/R/fuelClassPrep.R @@ -0,0 +1,37 @@ +#' Calculate proportional burn of landcover and tree species +#' +#' @template pixelGroupMap +#' @template cohortData +#' @param rstLCC a landcover map +#' @param nonflammableLCC nonflammable landcover in rstLCC +#' @param fires a single sf or SpatVector object of fire polygons containing a YEAR coumn +#' @param yearRange the range of years represented by this landscape +#' +#' @return a data.table with cell, biomass, tree species or lcc for non-forest, and year of fire +#' @export +#' +#' @examples +#' fuelclassPrep(sim$pixelGroupMap2011, sim$cohortData2011, sim$rstLCC2011, +#' nonflammableLCC = P(sim)$nonflammableLCC< yearRange = c(2012, 2020)) +fuelClassPrep <- function(pixelGroupMap, cohortData, rstLCC, nonflammableLCC, fires, yearRange) { + # Filter fires by year range + firesSubset <- fires[fires$YEAR >= yearRange[1] & fires$YEAR <= yearRange[2], ] |> + rasterize(pixelGroupMap, field = "YEAR", fun = "min") |> + as.data.frame(cells = TRUE) |> as.data.table() + + # Add pixels to cohort data + speciesData <- addPixels2CohortData(cohortData = cohortData, pixelGroupMap) + + # Prepare landscape data excluding non-flammable land cover classes + landscapeData <- as.data.table(as.data.frame(rstLCC, cells = TRUE)) |> + setnames(c("cell", "lcc")) + landscapeData <- landscapeData[!lcc %in% nonflammableLCC] + + # Merge species data with landscape data + landscapeData <- speciesData[landscapeData, on = c("pixelIndex" = "cell")] + + # Merge fires data with landscape data + landscapeData <- firesSubset[landscapeData, on = c("cell" = "pixelIndex")] + + return(landscapeData) +} diff --git a/man/fuelClassPrep.Rd b/man/fuelClassPrep.Rd new file mode 100644 index 0000000..deba2cc --- /dev/null +++ b/man/fuelClassPrep.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fuelClassPrep.R +\name{fuelClassPrep} +\alias{fuelClassPrep} +\title{Calculate proportional burn of landcover and tree species} +\usage{ +fuelClassPrep( + pixelGroupMap, + cohortData, + rstLCC, + nonflammableLCC, + fires, + yearRange +) +} +\arguments{ +\item{pixelGroupMap}{A \code{RasterLayer} with pixel values equal to a pixel group +number that corresponds exactly to \code{pixelGroup} column in \code{cohortData}.} + +\item{cohortData}{A \code{data.table} with columns: \code{pixelGroup}, \code{ecoregionGroup}, +\code{speciesCode}, and optionally \code{age}, \code{B}, \code{mortality}, \code{aNPPAct}, +and \code{sumB}.} + +\item{rstLCC}{a landcover map} + +\item{nonflammableLCC}{nonflammable landcover in rstLCC} + +\item{fires}{a single sf or SpatVector object of fire polygons containing a YEAR coumn} + +\item{yearRange}{the range of years represented by this landscape} +} +\value{ +a data.table with cell, biomass, tree species or lcc for non-forest, and year of fire +} +\description{ +Calculate proportional burn of landcover and tree species +} +\examples{ +fuelclassPrep(sim$pixelGroupMap2011, sim$cohortData2011, sim$rstLCC2011, + nonflammableLCC = P(sim)$nonflammableLCC< yearRange = c(2012, 2020)) +}