---
title: "FIESTA - Data Tools"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{FIESTA - Data Tools}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include = F}
library(knitr)
knitr::opts_chunk$set(message = F, warning = F)
```

```{r, include=FALSE}
# Sets up output folding
hooks = knitr::knit_hooks$get()
hook_foldable = function(type) {
  force(type)
  function(x, options) {
    res = hooks[[type]](x, options)
    
    if (isFALSE(options[[paste0("fold.", type)]])) return(res)
    
    paste0(
      "<details><summary>", type, "</summary>\n\n",
      res,
      "\n\n</details>"
    )
  }
}
knitr::knit_hooks$set(
  output = hook_foldable("output"),
  plot = hook_foldable("plot")
)
```

## FIESTA Overview
The R package, FIESTA (Forest Inventory ESTimation and Analysis) is a research estimation tool for analysts that work with sample-based inventory data like that from the U.S. Department of Agriculture, Forest Service, Forest Inventory and Analysis (FIA) Program to accommodate: unique population boundaries, different evaluation time periods, customized stratification schemes, non-standard variance equations, integration of multi-scale remotely-sensed data and other auxiliary information, and interaction with other modeling and estimation tools from CRAN R's library of packages. FIESTA contains a collection of functions that can access FIA databases, summarize and compile plot and spatial data, and generate estimates with associated sampling errors. 


Functions are organized by type or objective and are named with a corresponding prefix: 

**Core Functions**

* Database tools (DB) - functions for querying and extracting data from FIA's national database.
* Data tools (dat) - functions for summarizing and exploring FIA data.
* Spatial tools (sp) - functions for manipulating and summarizing spatial data.

**Estimation Modules**

* Green-Book (GB) - functions for FIA’s standard ‘Green-Book’ estimators.
* Photo-Based (PB) - functions for supplementary photo-based estimators.
* Small Area (SA) - functions for integration with available small area estimators (SAE).
* Model-Assisted (MA) - functions for integration with available Model-Assisted estimators.

**Analysis Tools**

* Analysis tools (an) - wrapper functions for stream-lining estimation processes.



### Overview of FIESTA data (dat) tools
FIESTA's dat tools assist with customizing variables as well as summarizing and visualizing FIA data. The datLUTclass and datLUTnm functions help with merging look-up tables to support new variables, standard FIA code names or unique names, or groupings of discrete or continuous data values. The datSum* functions provide tools for aggregating FIA data for estimation, mapping, or exploratory data analyses. 

FIESTA's tree estimates require tree data summarized to the condition (base unit) domain level and extrapolated to an acre to account for trees sampled from different plot sizes. An adjustment factor is applied to adjust for nonsampled plots across different strata (adj='samp') within the area of interest for GB estimates or within each plot (adj='plot') for MA and SA estimates. 

### Objective of tutorial
The objective of this tutorial is to demonstrate the use of FIESTA's dat tools for customizing, summarizing, and visualizing FIA data. The examples use data from two inventory years of field measurements in the state of Wyoming, from FIADB_1.7.2.00, last updated June 20, 2018, downloaded on June 25, 2018 and stored as internal data objects in FIESTA.


FUNCTION  | DESCRIPTION
-------------- | ---------------------------------------------------------------
[datFilter()](#datFilter) | Subsets a data table by specified filter(s).
[datFreq()](#datFreq) | Generates frequency table.
[datPivot()](#datPivot) | Generates pivot table.
[datBarplot()](#datBarplot) | Generates bar plot.
[datBarplotStacked()](#datBarplotStacked) | Generates stacked bar plot.
[datPiechart()](#datPiechart) | Generates pie chart.
[datLUTclass()](#datLUTclass) | Merge look-up table for classifying continuous data (e.g., DIA).
[datLUTnm()](#datLUTnm) | Merge look-up table to append names or categories.
[datSumCond()](#datSumCond) | Aggregates numeric condition data to plot level.
[datSumTree()](#datSumTree) | Aggregates numeric tree data to the plot or condition-level.
[datSumTreeDom()](#datSumTreeDom) | Aggregates numeric tree data by tree domain to plot or condition level.


### Set up

We just load the `FIESTA` library to get started with the examples.
```{r, message = F, warning = F}
# Load library
library(FIESTA)
```

The following examples use FIA data from Wyoming for inventory years 2011-2013.

### <a name="datFilter"/>`datFilter()`

The `datFilter` function subsets a data table by specified filter(s) or logical statement and returns a list of the filtered data table and the filter.

#### Example 1: Subset National Forest Conditions in Wyoming
<details>
  <summary>View Example</summary>
  
```{r}

WYconddat.nfs <- datFilter(
        x = WYcond, 
        xfilter = "ADFORCD > 0"
        )

names(WYconddat.nfs)
WYcond.nfs <- WYconddat.nfs$xf
dim(WYcond)
dim(WYcond.nfs)
```

</details>

#### Example 2: Subset Trees That Are Standing Dead Aspen Trees in Wyoming
<details>
  <summary>View Example</summary>

```{r}
WYtreedat.dead <- datFilter(
        x = WYtree, 
        xfilter = "STATUSCD == 2 & STANDING_DEAD_CD == 1 & SPCD == 746"
        )

names(WYtreedat.dead)
WYtree.deadasp <- WYtreedat.dead$xf
dim(WYtree.deadasp)
dim(WYtree)
```

</details>


#### Example 3: Subset Trees That Are Standing Dead Aspens in Wyoming, and Subset Other Tables to Match
<details>
  <summary>View Example</summary>
  
```{r}
WYtreedat.dead2 <- datFilter(
        x = WYtree, 
        xfilter = "STATUSCD == 2 & STANDING_DEAD_CD == 1 & SPCD == 746",
        othertabnms = c("WYplt", "WYcond")
        )

names(WYtreedat.dead2)
WYtree.deadasp2 <- WYtreedat.dead2$xf
head(WYtree.deadasp2)
dim(WYtree.deadasp2)

WYtree.deadasptabs <- WYtreedat.dead2$cliptabs
names(WYtree.deadasptabs)
WYplt.deadasp <- WYtree.deadasptabs$clip_WYplt
WYcond.deadasp <- WYtree.deadasptabs$clip_WYcond

dim(WYplt.deadasp)
dim(WYcond.deadasp)
```

</details>


### <a name="datFreq"/>`datFreq()` 

The `datFreq` function generates a frequency table from a data frame, including number of records by a specified variable or variables in the data frame with optional totals and/or subtotals. 

#### Example: Multiple Uses
<details>
  <summary>View Example</summary>

```{r}
## Get number of plots by county
datFreq(
      x = WYplt, 
      xvar="COUNTYCD"
      )

## Get number of plots by county and plot status
datFreq(
      x = WYplt, 
      xvar=c("COUNTYCD", "PLOT_STATUS_CD")
      )

## Get number of plots by county and plot status with subtotals
datFreq(
      x = WYplt, 
      xvar = c("COUNTYCD", "PLOT_STATUS_CD"), 
      subtotal = TRUE
      )

## Get number of plots by county and plot status with subtotals
datFreq(
      x = WYplt, 
      xvar = c("COUNTYCD", "PLOT_STATUS_CD"), 
      subtotal = TRUE,
      subtotalcol = "COUNTYCD"
      )

```

</details>


### <a name="datPivot"/>`datPivot()`

The `datPivot` function generates a pivot table. 

#### Example: Multiple Uses
<details>
  <summary>View Example</summary>
  
```{r}
## Get summed condition proportions by forest type class and stand size class
datPivot(
      x = WYcond, 
      pvar = "CONDPROP_UNADJ", 
      xvar = "FORTYPCD", 
      yvar = "STDSZCD"
      )

## Get average height by species group code and status code
datPivot(
      x = WYtree, 
      pvar = "HT", 
      xvar = "SPGRPCD", 
      yvar = "TREECLCD", 
      pfun = mean
      )
```

</details>


### <a name="datLUTclass"/>`datLUTclass()`

The `datLUTclass` function merges a look-up table to define categories of continuous data in `x` (e.g., `DIACL`). Adds a variable to `x`, setting the variable to `VARCLNM` where: `xvar >= MIN` and `xvar < MAX`.

#### Example 1: Reference Table Stored in `FIESTA` with 2-inch Diameter Classes
<details>
  <summary>View Example</summary>
  
```{r}
FIESTAutils::ref_diacl2in

WYtreelut <- datLUTclass(
                x = WYtree, 
                xvar = "DIA",
                LUT = FIESTAutils::ref_diacl2in, 
                LUTclassnm = "DIACL2IN"
                )

names(WYtreelut)
WYtree2 <- WYtreelut$xLUT
head(WYtree2)
dim(WYtree)
dim(WYtree2)
```

</details>

#### Example 2: Create New Reference Table and Merge to Tree Table
<details>
  <summary>View Example</summary>

```{r}
diacl25 <- data.frame(
              MIN = c(5,25), 
              MAX = c(25, 100), 
              DIACL25 = c("5.0-24.9", "25.0+" )
              )
diacl25


WYtreelut2 <- datLUTclass(
                  x = WYtree, 
                  xvar = "DIA", 
                  LUT = diacl25, 
                  LUTclassnm = "DIACL25"
                  )

names(WYtreelut2)
WYtree2 <- WYtreelut2$xLUT
head(WYtree2)
dim(WYtree)
dim(WYtree2)
```

</details>

#### Example 3: Use Cutbreaks to Classify `LIVE_CANOPY_CVR_PCT` variable from `WYcond` Table
<details>
  <summary>View Example</summary>

```{r}
cutbreaks <- c(0,25,50,100)
WYcondlut <- datLUTclass(
                x = WYcond, 
                xvar = "LIVE_CANOPY_CVR_PCT", 
                cutbreaks = cutbreaks
                )

names(WYcondlut)

head(WYcondlut$xLUT)
WYcondlut$LUT
```

</details>

### <a name="datLUTnm"/>`datLUTnm()`

The `datLUTnm` function merges a look-up table to append new variables, names, or categories to `x`.

#### Example: Multiple Uses
<details>
  <summary>View Example</summary>
  
First, we can look at a reference table stored in `FIESTA` with code descriptions of common FIA attributes.

```{r}
head(FIESTAutils::ref_codes)
unique(FIESTAutils::ref_codes$VARIABLE)
```

Next, we set the code descriptions for FIA disturbance variable.

```{r}
ref_dstrbcd <- FIESTAutils::ref_codes[FIESTAutils::ref_codes$VARIABLE == "DSTRBCD",]
head(ref_dstrbcd)
```

Next, we can append forest type names using the reference table above.
```{r}
WYcondlut <- datLUTnm(
              x = WYcond, 
              xvar = "DSTRBCD1", 
              LUT = ref_dstrbcd, 
              LUTvar = "VALUE",
              LUTnewvar = "MEANING", 
              LUTnewvarnm = "DSTRB1NM"
              )

names(WYcondlut)
WYcond2 <- WYcondlut$xLUT
head(WYcond2[WYcond2$DSTRBCD1 > 0, ])
```

Now, we can append forest type names using `datLUTnm`. If the `xvar` is in the stored reference table, the name and values will automatically be appended.
```{r}
WYcondlut2 <- datLUTnm(
                x = WYcond, 
                xvar = "DSTRBCD1", 
                FIAname = TRUE
                )

names(WYcondlut2)
WYcond3 <- WYcondlut2$xLUT
head(WYcond3[WYcond3$DSTRBCD1 > 0, ])
```

</details>

### <a name="datSumCond"/>`datSumCond()`

The `datSumCond` functions aggregates the `CONDPROP_UNADJ` variable or other continuous condition variables to plot level with option to apply condition filters. If condition variable is not `CONDPROP_UNADJ` the variable is multiplied by `CONDPROP_UNADJ` for a weighted sum.

The function returns a list of the following:      
    `condsum` - plot-level table with aggregated condition attribute.      
    `cfilter` - condition filter
    
#### Example: Multiple Uses
<details>
  <summary>View Example</summary>

First, we aggregate `LIVE_CANOPY_CVR_PCT` to plot, weighted by `CONDPROP_UNADJ`.
```{r}
condsumdat <- datSumCond(
                cond = WYcond, 
                csumvar = "LIVE_CANOPY_CVR_PCT"
                )

names(condsumdat)

condsum <- condsumdat$condsum
head(condsum)
```

Next, we check results.
```{r}
condsum[condsum$PLT_CN == 40405596010690,]
WYcond[WYcond$PLT_CN == 40405596010690,]
```

Next, we can append results to plot table.
```{r}
condsum <- datSumCond(
              cond = WYcond, 
              plt = WYplt, 
              csumvar = "LIVE_CANOPY_CVR_PCT"
              )$condsum
head(condsum)
```


We can also add a filter.
```{r}
condsum <- datSumCond(
              cond = WYcond, 
              plt = WYplt, 
              csumvar = "LIVE_CANOPY_CVR_PCT",
              cfilter = "STDSZCD == 1" 
              )$condsum
head(condsum)

## Check results
condsum[condsum$CN == 40405596010690,]
WYcond[WYcond$PLT_CN == 40405596010690,]
```

Next, we look at summed nonforest condition proportions by plot.
```{r}
condnf <- datSumCond(
              cond = WYcond, 
              plt = WYplt, 
              csumvar = "CONDPROP_UNADJ", 
              csumvarnm = "cond_nf", 
              cfilter = "COND_STATUS_CD %in% c(2,3)"
              )$condsum

condnf[condnf$CN == 40404737010690,]
WYcond[WYcond$PLT_CN == 40404737010690,]
```

Finally, we can look at summed reserved condition proportions by plot. 
```{r}
condres <- datSumCond(
              cond = WYcond, 
              plt = WYplt, 
              csumvar = "CONDPROP_UNADJ",
              csumvarnm = "cond_reserved", 
              cfilter = "RESERVCD == 1"
              )$condsum

WYcond[WYcond$PLT_CN == 46792188020004,]
```

</details>

### <a name="datSumTree"/>`datSumTree()`

The `datSumTree` function aggregates tree-level data to plot or condition, including options for filtering tree data or extrapolating to plot acre by multiplying by trees per acre (`TPA*`).

Returns a list of the following:    
  treedat - plot or condition-level table with aggregated tree attribute(s).     
  sumvars - Name(s) of the output aggregated tree attributes.
  
#### Example 1: Sum of Basal Area and Net Cubic-foot Volume of Live Trees by Plot (`TPA=FALSE`)
<details>
  <summary>View Example</summary>
  
```{r}
treesumdat1 <- datSumTree(
                tree = WYtree, 
                plt = WYplt, 
                tsumvarlst = c("BA", "VOLCFNET"),
                TPA = FALSE, 
                tfilter = "STATUSCD == 1", 
                bycond = FALSE, 
                tround = 2
                )

names(treesumdat1)
treesum1 <- treesumdat1$treedat
head(treesum1)
treesumdat1$sumvars
```

</details>

#### Example 2: Sum of Basal Area Per Acre and Net Cubic-foot Volume Per Acre of Live Trees by Plot (`TPA=TRUE`)
<details>
  <summary>View Example</summary>
  
```{r}
treesumdat2 <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  tsumvarlst = c("BA", "VOLCFNET"),
                  TPA = TRUE, 
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  tround = 2
                  )

names(treesumdat2)
treesum2 <- treesumdat2$treedat
head(treesum2)
```


### Example 1 & 2 Check: Test BA Results for 1 Plot
<details>
  <summary>View Check</summary>
  
```{r}
checkvars <- c("PLT_CN", "CONDID", "SUBP", "TREE", "STATUSCD", "SPCD", "DIA", "HT", 
    "BA", "VOLCFNET", "TPA_UNADJ")
testplt <- WYtree[WYtree$PLT_CN == 40404758010690, checkvars]
testplt
sum(testplt[testplt$STATUSCD == 1, "BA"], na.rm=TRUE)
sum(testplt[testplt$STATUSCD == 1, "BA"] * testplt[testplt$STATUSCD == 1, "TPA_UNADJ"], na.rm=TRUE)

treesum1[treesum1$CN == 40404758010690,]
treesum2[treesum2$CN == 40404758010690,]
```

</details>

### Example 3: Average Diameter and Height of Live Trees by Plot
<details>
  <summary>View Example</summary>

```{r}
treesumdat3 <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  tsumvarlst = c("DIA", "HT"),
                  TPA = FALSE, 
                  tfun = mean, 
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  tround = 2
                  )

names(treesumdat3)
treesum3 <- treesumdat3$treedat
head(treesum3)

## Test DIA and HT results for 1 plot
testplt
mean(testplt[testplt$STATUSCD == 1, "DIA"], na.rm=TRUE)
mean(testplt[testplt$STATUSCD == 1, "HT"], na.rm=TRUE)

treesum3[treesum3$CN == 40404758010690,]
```

</details>

#### Example 4a: Number of Live Trees by Plot (`TPA=TRUE`)
<details>
  <summary>View Example</summary>

```{r}
treesumdat4a <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  tsumvarlst = "TPA_UNADJ",
                  TPA = TRUE, 
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  tround = 2
                  )

names(treesumdat4a)
treesum4a <- treesumdat4a$treedat
head(treesum4a)
```


</details>

#### Example 4b: Adjust Summed Live Tree Volume to Account for Nonsampled Conditions on Plot
<details>
  <summary>View Example</summary>

Note: Must include condition table for condition proportion variables
```{r}
treesumdat4b <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  cond = WYcond, 
                  tsumvarlst = "VOLCFNET",
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  getadjplot = TRUE
                  )

names(treesumdat4b)
treesum4b <- treesumdat4b$treedat
head(treesum4b)
```

</details>

#### Example 4c: Compare with Unadjusted Estimate for Plot Where 25% of Plot is Nonsampled (`COND_STATUS_CD = 5` and `CONDPROP_UNADJ = 0.25`)

<details>
  <summary>View Example</summary>

```{r}
treesumdat4c <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  cond = WYcond, 
                  tsumvarlst = "VOLCFNET",
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  getadjplot = FALSE
                  )

treesum4c <- treesumdat4c$treedat

cn <- 40407815010690
WYcond[WYcond$PLT_CN == cn, ]
treesum4b[treesum4b$CN == cn, ]
treesum4c[treesum4c$CN == cn, ]
```

</details>

#### Example 5a: Number of Live Trees Plus Seedlings by Plot (`TPA=TRUE`)

<details>
  <summary>View Example</summary>

```{r}
treesumdat5a <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  seed = WYseed, 
                  tsumvarlst = "TPA_UNADJ",
                  TPA = TRUE, 
                  addseed = TRUE, 
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  tround = 2
                  )

names(treesumdat5a)
treesum5a <- treesumdat5a$treedat
head(treesum5a)

treesum5a[treesum5a$CN %in% cn,]
WYseed[WYseed$PLT_CN == cn,]
```

</details>

#### Example 5b: Number of Live Trees Plus Seedlings by Plot (`TPA=TRUE`) - Adjusted

<details>
  <summary>View Example</summary>

```{r}
treesumdat5b <- datSumTree(
                  tree = WYtree, 
                  plt = WYplt, 
                  cond = WYcond, 
                  seed = WYseed, 
                  tsumvarlst = "TPA_UNADJ",
                  TPA = TRUE, 
                  addseed = TRUE, 
                  tfilter = "STATUSCD == 1", 
                  bycond = FALSE, 
                  tround = 2,
                  getadjplot  =TRUE
                  )

names(treesumdat5b)
treesum5b <- treesumdat5b$treedat
head(treesum5b)

treesum5a[treesum5a$CN %in% cn,]
treesum5b[treesum5b$CN %in% cn,]
WYcond[WYcond$PLT_CN %in% cn,]
```

</details>

### <a name="datSumTreeDom"/>`datSumTreeDom()`

The `datSumTreeDom` function aggregates tree-level data to plot or condition, including options for filtering tree data or extrapolating to plot acre by multiplying by `TPA`.

It returns a list of the following:     
    `tdomdat` - plot or condition-level table with aggregated tree domain attributes.    
    `tdomsum` - the tdom look-up table with data aggregated by species.    
    `tsumvar` - name of aggregated output variable.    
    `tdomlst` - list of the aggregated data in tdomdat.     
    `tdomdat.pres` - if presence=TRUE, plot or condition-level table with aggregated
        domain attributes represented as presence/absence (1/0).     
    `tdomdat.prop` - if presence=TRUE, plot or condition-level table with aggregated
        domain attributes represented as proportion of total by plot.      
    `tdomdat.pres` - if presence=TRUE, plot or condition-level table with aggregated
        domain attributes represented as percent cover, multiplying cover attribute
        by tdom proportion by plot.   
        
#### Example 1: Sum of Live Basal Area Per Acre by Species

<details>
  <summary>View Example</summary>
  
```{r}
treedomBA <- datSumTreeDom(
                tree = WYtree, 
                cond = WYcond, 
                plt = WYplt, 
                puniqueid = "CN", 
                bycond = FALSE, 
                tsumvar = "BA", 
                TPA = TRUE, 
                tdomtot = TRUE, 
                tdomtotnm = "BA_LIVE", 
                tdomprefix = "BA_LIVE", 
                tround = 2, 
                tfilter = "STATUSCD==1"
                )

names(treedomBA)
tdomdat <- treedomBA$tdomdat
tdomvarlut <- treedomBA$tdomvarlut
tdomlst <- treedomBA$tdomlst
tdomtotnm <- treedomBA$tdomtotnm

head(tdomdat)
tdomvarlut
tdomlst
tdomtotnm

dim(WYplt)
dim(tdomdat)
```

</details>

#### Example 2: Number of Live Trees by Species
<details>
  <summary>View Example</summary>

```{r}
treedomCNT <- datSumTreeDom(
                tree = WYtree, 
                cond = WYcond, 
                plt = WYplt, 
                puniqueid = "CN", 
                bycond = FALSE, 
                tsumvar = "PLT_CN", 
                TPA = TRUE, 
                tdomtot = TRUE, 
                tdomprefix = "CNT", 
                tround = 0, 
                tfilter = "STATUSCD==1"
                )

names(treedomCNT)
tdomdat.tree <- treedomCNT$tdomdat
tdomvarlut <- treedomCNT$tdomvarlut
tdomlst <- treedomCNT$tdomlst
tdomtotnm <- treedomCNT$tdomtotnm

head(tdomdat.tree)
```

</details>

#### Example 3: Number of Live Trees by Species - Seedlings
<details>
  <summary>View Example</summary>

```{r}
treedomCNTs <- datSumTreeDom(
                cond = WYcond, 
                plt = WYplt, 
                seed = WYseed, 
                puniqueid = "CN", 
                bycond = FALSE, 
                tsumvar = "PLT_CN", 
                TPA = TRUE, 
                tdomtot = TRUE, 
                tdomprefix = "CNT", 
                tround = 0
                )

names(treedomCNTs)
tdomdat.seed <- treedomCNTs$tdomdat
tdomvarlut <- treedomCNTs$tdomvarlut
tdomlst <- treedomCNTs$tdomlst
tdomtotnm <- treedomCNTs$tdomtotnm

head(tdomdat.seed)
```

</details>

#### Example 4: Number of Live Trees by Species, Including Seedlings
<details>
  <summary>View Example</summary>

```{r}
treedomCNTs <- datSumTreeDom(
                tree = WYtree, 
                cond = WYcond, 
                plt = WYplt, 
                seed = WYseed, 
                puniqueid = "CN", 
                bycond  =FALSE, 
                tsumvar = "PLT_CN", 
                TPA = TRUE, 
                tdomtot = TRUE, 
                tdomprefix = "CNT", 
                tround = 0, 
                tfilter = "STATUSCD==1", 
                addseed = TRUE
                )

names(treedomCNTs)
tdomdat.treeseed <- treedomCNTs$tdomdat
tdomvarlut <- treedomCNTs$tdomvarlut
tdomlst <- treedomCNTs$tdomlst
tdomtotnm <- treedomCNTs$tdomtotnm

head(tdomdat.treeseed)

cn <- 40404730010690
tdomdat.tree[tdomdat.tree$CN == cn,]
tdomdat.seed[tdomdat.seed$CN == cn,]
tdomdat.treeseed[tdomdat.treeseed$CN == cn,]
```

</details>


#### Example 5: Presence of Live Trees by Species, Including Seedlings
<details>
  <summary>View Example</summary>

```{r}
treedomCNTs <- datSumTreeDom(
                  tree = WYtree, 
                  cond = WYcond, 
                  plt = WYplt, 
                  seed = WYseed, 
                  puniqueid ="CN", 
                  bycond = FALSE, 
                  tsumvar = "PLT_CN", 
                  TPA = TRUE, 
                  tdomtot = TRUE, 
                  tdomprefix = "CNT", 
                  savedata = FALSE, 
                  tfilter = "STATUSCD==1", 
                  addseed = TRUE, 
                  presence = TRUE
                  )

names(treedomCNTs)
tdomdat.pres <- treedomCNTs$tdomdat.pres

head(tdomdat.pres)
```

</details>

#### Example 6: Presence and Proportion of Live Trees by Species, Including Seedlings
<details>
  <summary>View Example</summary>
  
```{r}
treedomCNTs <- datSumTreeDom(tree=WYtree, cond=WYcond, plt=WYplt, seed=WYseed, 
	    puniqueid="CN", bycond=FALSE, tsumvar="PLT_CN", TPA=TRUE, tdomtot=TRUE,
	    tdomprefix="CNT", savedata=FALSE, tround=0, tfilter="STATUSCD==1",
		  addseed=TRUE, presence=TRUE, proportion=TRUE)
names(treedomCNTs)
tdomdat.pres <- treedomCNTs$tdomdat.pres
tdomdat.prop <- treedomCNTs$tdomdat.prop

head(tdomdat.pres)
```

</details>

#### Example 7: Presence and Proportion and Cover of Live Trees by Species, Including Seedlings (Add Barplot)
<details>
  <summary>View Example</summary>
  
```{r}
treedomCNTs <- datSumTreeDom(
                tree = WYtree, 
                cond = WYcond, 
                plt = WYplt, 
                seed = WYseed, 
                puniqueid = "CN", 
                bycond = FALSE, 
                tsumvar = "PLT_CN", 
                TPA = TRUE, 
                tdomtot = TRUE, 
                tdomprefix = "CNT", 
                tround = 0, 
                tfilter = "STATUSCD==1",
                addseed = TRUE, 
                presence = TRUE, 
                proportion = TRUE, 
                cover = TRUE, 
                tdombarplot = TRUE
                )

names(treedomCNTs)
tdomdat.pres <- treedomCNTs$tdomdat.pres
tdomdat.prop <- treedomCNTs$tdomdat.prop
tdomdat.cov <- treedomCNTs$tdomdat.cov

cn=40404742010690
tdomdat.tree[tdomdat.tree$CN == cn,]
tdomdat.seed[tdomdat.seed$CN == cn,]
tdomdat.treeseed[tdomdat.treeseed$CN == cn,]
tdomdat.pres[tdomdat.pres$CN == cn,]
tdomdat.prop[tdomdat.prop$CN == cn,]
tdomdat.cov[tdomdat.cov$CN == cn,]
```

</details>

#### Example 8: Presence and Proportion of Live Trees by Species, Including Seedlings (Add Barplot)
<details>
  <summary>View Example</summary>
  
```{r}
treedomCNTs <- datSumTreeDom(
                tree = WYtree, 
                cond = WYcond, 
                plt = WYplt, 
                seed = WYseed, 
                puniqueid = "CN", 
                bycond = FALSE, 
                tsumvar = "PLT_CN", 
                TPA = TRUE, 
                tdomtot = TRUE, 
                tdomprefix = "CNT", 
                tround = 0, 
                tfilter = "STATUSCD==1", 
                addseed = TRUE, 
                presence = TRUE, 
                proportion = TRUE, 
                tdombarplot = TRUE
                )

names(treedomCNTs)
tdomdat.pres <- treedomCNTs$tdomdat.pres
tdomdat.prop <- treedomCNTs$tdomdat.prop

head(tdomdat.pres)
```

</details>

#### Example 9: Total Basal Area Per Acre of Limber Pine (`SPCD=113`) Trees >= 1.0 DIA
<details>
  <summary>View Example</summary>

```{r}
treedomBA <- datSumTreeDom(
                tree = WYtree, 
                cond = WYcond, 
                plt = WYplt, 
                puniqueid = "CN", 
                bycond = FALSE, 
                tsumvar = "BA", 
                TPA = TRUE, 
                tdomprefix = "BA", 
                tdomvarlst = 113, 
                tround = 2, 
                tfilter = "STATUSCD==1"
                )

names(treedomBA)
ba.limber <- treedomBA$tdomdat

head(ba.limber)
```

</details>

#### Example 10: Species and Diameter Class
<details>
  <summary>View Example</summary>

```{r}
## Total basal area per acre by species and diameter class
DIALUT <- FIESTAutils::ref_diacl2in[FIESTAutils::ref_diacl2in$MIN <= 37, ]
names(DIALUT)[names(DIALUT) == "MEANING"] <- "DIACL2IN"

## Append diameter classes to tree table
datlut <- datLUTclass(
                    x = WYtree, 
                    xvar = "DIA", 
                    LUT = DIALUT, 
                    LUTclassnm = "DIACL2IN"
                    )
WYtree2 <- datlut$xLUT

## Species and diameter class
treedomDIACNTs <- datSumTreeDom(
                    tree = WYtree2, 
                    cond = WYcond, 
                    plt = WYplt, 
                    puniqueid = "CN", 
                    bycond = FALSE, 
                    tsumvar = "PLT_CN", 
                    TPA = TRUE, 
                    tdomtot = TRUE, 
                    tdomprefix = "CNT", 
                    tround = 2, 
                    tfilter = "STATUSCD==1 & DIA > 30", 
                    presence = TRUE, 
                    proportion = TRUE, 
                    tdomvar2 = "DIACL2IN", 
                    tdombarplot = TRUE
                    )

names(treedomDIACNTs)
tdomdat.pres <- treedomDIACNTs$tdomdat.pres
tdomdat.prop <- treedomDIACNTs$tdomdat.prop

head(tdomdat.pres)
```

</details>
