---
title: "fireSense LCC flammability analysis"
author: "Ian Eddy, Eliot McIntire, Steve Cumming, Alex Chubaty"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{fireSense LCC flammability analysis}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE,
  eval = FALSE ## TODO: re-enable, but note that vignette takes a while to build
)
```

```{r packages}
library(data.table)
library(ggplot2)
library(reproducible)
library(sf)
library(terra)
```

## Compare LCC and fire data

```{r compare_LCC_and_fire_data}
dPath <- tempdir()
lccUrl <- paste0(
  "ftp://ftp.ccrs.nrcan.gc.ca/ad/NLCCLandCover/",
  "LandcoverCanada2005_250m/LandCoverOfCanada2005_V1_4.zip"
)
lcc <- prepInputs(
  url = lccUrl, destinationPath = dPath,
  targetFile = "LCC2005_V1_4a.tif", alsoExtract = NA
)

fireUrl <- "https://cwfis.cfs.nrcan.gc.ca/downloads/nfdb/fire_poly/current_version/NFDB_poly.zip"
firePolygons <- prepInputs(url = fireUrl, destinationPath = dPath, fun = "sf::read_sf")
firePolygons <- sf::st_transform(firePolygons, crs = crs(lcc))

# rasterize firePolygons
firePolygons$dummyVar <- 1
firePolygons <- firePolygons[firePolygons$YEAR > 2004, ]
fireRaster <- fasterize(sf = firePolygons, raster = lcc, field = "dummyVar")
fireLoc <- 1:ncell(lcc)
fireLoc <- fireLoc[!is.na(getValues(fireRaster))]

burned <- data.table(pixelID = 1:ncell(lcc), lcc = getValues(lcc))
burned[pixelID %in% fireLoc, burn := 1]
burnCalc <- burned[, .(available = .N, burned = sum(burn, na.rm = TRUE)), .(lcc)]

burnCalc[, "percentBurned" := round(burned / available * 100, digits = 3)]
setkey(burnCalc, lcc)
burnCalc
```

## Plot

```{r fire, echo=FALSE}
ggplot(data = burnCalc, aes(x = lcc, y = percentBurned)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  ylab("percent burned (%)") +
  scale_x_continuous("LCC", labels = as.character(burnCalc$lcc), breaks = burnCalc$lcc) +
  theme_bw()
```

From the plot above (and based on descripitons of each class) we determine the following non-flammable classes:

```{r eval = FALSE}
## original fireSense_dataPrepFit defaults
LCC2005_nonFlam <- c(0, 25, 30, 33, 36, 37, 38, 39)
```

And for fireSense categories:

- **non-forest high-flammability:**
- **non-forest low-flammability:**

