
#install FIA
devtools::install_github('hunter-stanke/rFIA')

#install other packages
library(rFIA)
library(dplyr)
library(dtplyr)
library(tidyr)
library(stringr)
library(sf)
library(parallel)
library(methods)
library(data.table)
library(bit64)
library(tidyselect)
library(rlang)
library(ggplot2)
library(lifecycle)
library(knitr)
library(rmarkdown)
library(coda) #recommended packages for rFIA
library(tidyverse)
library(raster)
library(rgdal)
library(sp)
library(raster)
library(rgeos)

# Setting working directory
wd <- "C:/Users/tangers.AFRC/OneDrive - University of Arkansas at Monticello/Desktop/rFIAprojects/"
setwd(wd)

#get data, sometimes this doenst work and you have to download the data directy
#download.file("https://apps.fs.usda.gov/fia/datamart/datamart.html", timeout = 600)
#https://apps.fs.usda.gov/fia/datamart/datamart.html, this is the datamart website, you can download each state you need here


States <- c("MS", "AL", "LA", "TN")
#df <- getFIA(states = States, dir=wd, common=TRUE)
df <- readFIA(dir=wd, common=TRUE, states=States, inMemory=TRUE) # inMemory=FALSE
plotFIA(df)

# Clip dataframe (creating harvest site) according to interest area
#Mills_buffer is a shapefile with the buffer from mills as seen in Dr. Crosby's recording

library(sf)
Mills_buffer <- st_read("C:/Users/tangers.AFRC/OneDrive - University of Arkansas at Monticello/Desktop/rFIAprojects/mills_buffer_new_02_19/mills_buffer_new_02_19.shp")
IntArea <- clipFIA(df, mask=Mills_buffer, mostRecent = FALSE)
plotFIA(IntArea)


#Now you have your area, but what do you want from the FIA plots in that area? 
# Lets get basic plot-level estimates
#FIA plots according to volume, harvest type and operability condition
vol_har <- volume(IntArea, byPlot=TRUE, bySpecies=TRUE, grpBy=c(MEASYEAR,OPERABILITY_SRS, HARVEST_TYPE1_SRS,STDORGCD), returnSpatial = TRUE, landType = "timber",
                  treeDomain = SPCD %in% c(131,110,121,111),treeType = "gs",volType = "net", method = "TI", nCores = 4)
vol_har1<-filter(vol_har, YEAR>=2010)
vol_har2 <- filter(vol_har1,HARVEST_TYPE1_SRS %in% c(11,12,13))
vol_har3 <- filter(vol_har2,OPERABILITY_SRS %in% c(0,1))
vol_har4<- vol_har3 %>% group_by(pltID, MEASYEAR) %>% dplyr::summarise(volume=sum(BOLE_CF_ACRE))

#print(vol_har4)

#Note for species Codes:
#110 = Shortleaf pine
#111 = Slash pine
#121 = longleaf pine
#131 = loblolly pine 

#Note for OPERABILITY_SRS:
#0 = No problems
#1 = Seasonal access due to water conditions in wet weather.

#Not for HARVEST_TYPE1_SRS
#11 = Clearcut harvest - The removal of the majority of the merchantable trees in a stand; residual stand stocking is under 50 percent.

#12 = Partial harvest - Removal primarily consisting of highest quality trees. Residual consists of lower quality trees because of high grading or selection harvest. (e.g., uneven aged, group selection, high grading, species selection).

#13 = Seed-tree/shelterwood harvest - Crop trees are harvested leaving seed source trees either in a shelterwood or seed tree. Also includes the final harvest of the seed trees.

# Create the data file as a csv file
# Specify the folder name within the working directory
output_folder <- "output"

# Check if the output folder exists, if not, create it
if (!dir.exists(output_folder)) {
  dir.create(output_folder, recursive = TRUE)
}

# Write vol_har4 data frame to a CSV file in the output folder
write.csv(vol_har4, file.path(output_folder, "harvestdata_02_19.csv"))

# Write vol_har3 data frame to a CSV file in the output folder
write.csv(vol_har3, file.path(output_folder, "harvestdata1_stand_org_02_019.csv"))





