library(tidyverse)
#R script to quickly find IMPLAN inputs or adjustment factors
YEAR <- 2027
#TAX_FILE <- FILES[grep("Taxes",FILES)]
GET_TRANSPORT_ADJ <- function(YEAR){
  YEAR <- ifelse(YEAR>2030,2030,YEAR)
  HEADER <- paste0('./Raw_Output/Prelim-Runs/')
  FILES <- list.files(HEADER)
  FILE <- paste0(HEADER,FILES[grep(paste0(YEAR,"-"),FILES)])
  DF <- read_csv(FILE)
  DF$County <-  gsub(" County, WY \\(2023\\)","",DF$DestinationRegion )
  DF$Impact_County <-  gsub(" County, WY Group","",DF$OriginRegion )
  TEMP <- DF %>% filter(ImpactType!="Induced",County==Impact_County,IndustryCode %in% c(397,399),TagName %in% c("Beet Purchase","SBS Purchase")) %>% group_by(County,IndustryCode,IndustryDescription) %>% summarize(Output=-sum(Output)) %>% arrange(Output)
  return(TEMP)
}

GET_INCOME_ADJ <- function(YEAR){
  PROFIT <- c(0,-1568776,3691597,9769730,26627999,52383510,55929633,56121706,56175248,56188791,56190869)
  TOTAL_PROFIT <- (PROFIT[YEAR-2023]-179917)
  YEAR <- ifelse(YEAR>2030,2030,YEAR)
  HEADER <- paste0('./Raw_Output/Prelim-Runs/')
  FILES <- list.files(HEADER)
  FILE <- paste0(HEADER,FILES[grep(paste0(YEAR,"-"),FILES)])
  DF <- read_csv(FILE)
  DF$County <-  gsub(" County, WY \\(2023\\)","",DF$DestinationRegion )
  DF$Impact_County <-  gsub(" County, WY Group","",DF$OriginRegion )
 REPORTED_PROFIT <- DF %>% filter(EventName=="Natural Gas Prod (Campbell)",ImpactType=="Direct",IndustryCode==20) %>% group_by(IndustryDescription) %>% summarize(TOTAL=OtherPropertyIncome+ProprietorIncome) %>% pull(TOTAL)
 PROFIT_ADJ <- TOTAL_PROFIT-REPORTED_PROFIT 
 return(PROFIT_ADJ)
}


