WD  <- as.matrix(WD)
GET_CUTOFF  <- function(DIST){
	WD_MAT  <- as.matrix(WD)
	WD_PICK  <- as.matrix(WD)
	WD_PICK[WD_MAT<=DIST]  <- TRUE 
	WD_PICK[WD_MAT>DIST]  <- FALSE
	diag(WD_PICK)  <- FALSE
	return(WD_PICK)
}
COND_MAT  <- function(SEL_LIST,DIST){
	NUM_RES <- colSums(GET_CUTOFF(DIST)*(colnames(WD) %in% SEL_LIST))
	return(cbind(colnames(WD),NUM_RES)  %>% as_tibble%>% mutate(NUM_RES=as.numeric(NUM_RES),IN=ifelse(NUM_RES>0,TRUE,FALSE))  %>% rename(GW_wdid=V1))
}


GET_CREP_DAT  <- function(DIS){
	for(i in c(2014,2015,2016,2017,2018,2019,2020)){
		SEL_LIST  <- IN_CREP %>% filter(CREP_Year==i,PERM_CREP==0) %>% pull(GW_wdid)
		if(!exists("RES")){RES <- COND_MAT(SEL_LIST,DIS)%>% mutate(CREP_CLOSE_YEAR=i)}else{RES <- rbind(RES,COND_MAT(SEL_LIST,DIS)%>% mutate(CREP_CLOSE_YEAR=i))}
	}
	RES$PERM_CREP  <- 0
	for(i in c(2014,2015,2016,2018,2020,2021)){
		SEL_LIST  <- IN_CREP %>% filter(CREP_Year==i,PERM_CREP==1) %>% pull(GW_wdid)
		RES <- rbind(RES,COND_MAT(SEL_LIST,DIS)%>% mutate(CREP_CLOSE_YEAR=i,PERM_CREP=1))
	}
	for(i in 2014:2021){
		CREP  <-  RES %>% filter(CREP_CLOSE_YEAR<=i) %>% group_by(GW_wdid) %>% summarize(CREP_CLOSE_NUM=sum(NUM_RES),CREP_CLOSE_TREAT=max(IN),Year=i)
		TEMP <-  RES %>% filter(CREP_CLOSE_YEAR<=i,PERM_CREP==0) %>% group_by(GW_wdid) %>% summarize(TEMP_CLOSE_NUM=sum(NUM_RES),TEMP_CLOSE_TREAT=max(IN),Year=i) 
		PERM <-  RES %>% filter(CREP_CLOSE_YEAR<=i,PERM_CREP==1) %>% group_by(GW_wdid) %>% summarize(PERM_CLOSE_NUM=sum(NUM_RES),PERM_CLOSE_TREAT=max(IN),Year=i) 

		ALL  <- full_join(full_join(CREP,TEMP),PERM)
		if(!exists("NUM_RES")){NUM_RES  <- ALL}else{NUM_RES  <- rbind(NUM_RES,ALL)}
	}
	for(i in 2009:2013){
		temp  <- 	NUM_RES %>% filter(Year==2014) 
		temp[,-1]  <- 0
		temp$Year  <- i
		NUM_RES  <- rbind(NUM_RES,temp)
	}

	POLICY_SUM  <- full_join(RES %>% filter(PERM_CREP==0) %>% group_by(GW_wdid) %>% summarize(TEMP_CLOSE_YEAR=min(ifelse(IN,CREP_CLOSE_YEAR,Inf))),RES %>% filter(PERM_CREP==1) %>% group_by(GW_wdid) %>% summarize(PERM_CLOSE_YEAR=min(ifelse(IN,CREP_CLOSE_YEAR,Inf))))
	POLICY_SUM  <- POLICY_SUM %>% left_join(RES %>% group_by(GW_wdid) %>% summarize(CREP_CLOSE_YEAR=min(ifelse(IN,CREP_CLOSE_YEAR,Inf))))

	POLICY_SUM  <- POLICY_SUM %>% full_join(NUM_RES) %>% select(GW_wdid,Year,everything()) %>% arrange(GW_wdid,Year)
	return(POLICY_SUM)
}
#####
GET_CLOSE_FALLOW  <- function(YEAR,DIST){
	OR  <- colnames(WD)
	FALL <- well_df %>% filter(Year==YEAR) %>% arrange(GW_wdid==OR) %>% mutate(FALLOW=(AF==0)) %>% pull(FALLOW)
	WD  <- as.matrix(WD)
	CLOSE_FALLOW <- cbind(OR,rep(YEAR,nrow(WD)),rowSums(WD[,FALL]  <DIST)) %>% as_tibble
	colnames(CLOSE_FALLOW)  <-  c("GW_wdid","Year","NUM_CLOSE_FALLOW")
	CLOSE_FALLOW$Year  <- as.numeric(CLOSE_FALLOW$Year)
	CLOSE_FALLOW$NUM_CLOSE_FALLOW<- as.integer(CLOSE_FALLOW$NUM_CLOSE_FALLOW)
	return(CLOSE_FALLOW)
}
DF_CLOSE_FALL <- function(DIST){
	CLOSE_FAL  <- GET_CLOSE_FALLOW(2009,DIST)
	for(x in 2010:2021){
	CLOSE_FAL  <- rbind(CLOSE_FAL,GET_CLOSE_FALLOW(x,DIST))
	}
	CLOSE_FAL  <- CLOSE_FAL %>% mutate(HAS_CLOSE_FALL=ifelse(NUM_CLOSE_FALLOW>0,1,0))
	return(CLOSE_FAL)
}
###################################################################################
GET_FALLOW_PROG_DAT  <- function(DIS){
	for(i in c(2020,2021)){
		SEL_LIST  <- FALL_PROG %>% filter(FALL_PROG_YEAR==i) %>% pull(GW_wdid)
		if(!exists("RES")){RES <- COND_MAT(SEL_LIST,DIS)%>% mutate(FALL_PROG_CLOSE_YEAR=i)}else{RES <- rbind(RES,COND_MAT(SEL_LIST,DIS)%>% mutate(FALL_PROG_CLOSE_YEAR=i))}
	}
RES  <- RES %>% filter(IN) %>% select(GW_wdid,FALL_PROG_CLOSE_YEAR)
RES  <- RES %>% group_by(GW_wdid) %>% mutate(FALL_PROG_CLOSE_YEAR=min(FALL_PROG_CLOSE_YEAR)) %>%  unique
return(RES)
}
