#New scripts
GET_YEAR_DATA <- function(C_YEAR,DATA_PATH){
	FILE_LIST <-	list.files(paste0(DATA_PATH,C_YEAR))
	TAX_FILE <- paste0(DATA_PATH,C_YEAR,"/",FILE_LIST[grep("Taxes",FILE_LIST)])
	ECON_FILE <- 	paste0(DATA_PATH,C_YEAR,"/",FILE_LIST[grep("Economic",FILE_LIST)])
	DAT <- read_csv(ECON_FILE)[,-1] %>% select(-TagName)
	 colnames(DAT) <- c("Event","County_Name","Impact","Employment","Labor_Income","Value_Added","Output")
	DAT <- DAT[!is.na(DAT[,1]),]

	TAX <- read_csv(TAX_FILE)[,-1] %>% select(-TagName)
	  colnames(TAX) <- c("Event","County_Name","Impact","Subcounty","Special","County","State","Federal","Total")
	TAX <- TAX[!is.na(TAX[,1]),]
	TAX$STATE_TOTAL <- rowSums(TAX[,4:7])
	DAT <- full_join(DAT,TAX)
	#rm(TAX)
	  DAT$County_Name <- gsub(" County, WY \\(2023\\)","",DAT$County_Name)
	  DAT$Impact <- gsub(" - ","",gsub("1","",gsub("2","",gsub("3","",DAT$Impact))))
	DAT$Group <- NA
	DAT[grep('Skid',DAT$Event),"Group"] <- "Skid"
	DAT[grep('Purchase',DAT$Event),"Group"]  <- "Beet Purchase"
	DAT[grep('Truck',DAT$Event),"Group"] <- "Transportation"
	DAT[DAT$Event=="Gas Production (Campbell)","Group"] <- "Gas Production"
	DAT[is.na(DAT$Group),"Group"] <- "Processing Facilities"
	#Remove the direct transportation effects induced by by beet purchases, by adding in a negative impact of rail and truck equal to the direct effect found in the first run.
	DAT[grep('Adjustment',DAT$Event),"Group"]  <- "Beet Purchase"

	
	DAT <- DAT %>% select(Minor_Event=Event,Major_Event=Group,everything())
	DAT$Year <- C_YEAR
	DAT <-DAT  %>% select(Year,everything())
	return(DAT)
}
GET_ALL_DATA <- function(DATA_PATH){
	YEARS_OF_DATA  <- list.files(DATA_PATH)
	for(i in 1:length(YEARS_OF_DATA)){
		if(!exists("RES")){
			RES<- GET_YEAR_DATA(YEARS_OF_DATA[i],DATA_PATH)
		}else{
			RES<- rbind(RES,GET_YEAR_DATA(YEARS_OF_DATA[i],DATA_PATH)) %>% as_tibble
		}
	}
	return(RES)
}
