-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bias Correction - Other trial 2.Rmd
215 lines (168 loc) · 11 KB
/
Bias Correction - Other trial 2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: "GraniteCreekTest"
author: "Gustavo Facincani Dourado"
date: "10/24/2020"
output: html_document
---
```{r}
library(readr)
library(tidyverse)
library(hyfo)
library(lubridate)
load('utilityFunctions.R')
```
1. import the files
make a function (a) to import observed
and (b) to import simulated
2. calculate bias factor individually use a function
3.
dataObs = getObserb(path, id)
dataSimul = getObserb(path, id)
```{r}
USJpath <- "C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/pywr_models/data/Upper San Joaquin River/"
path1 <- "C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/pywr_models/data/Upper San Joaquin River/hydrology/historical/Livneh/runoff/"
Sub22_Livneh <- read_csv(paste(path1,"tot_runoff_sb22_mcm.csv",sep="")) %>%
rename(Inflow = tot_runoff_sb22)
Sub22_PreBC <- Sub22_Livneh %>% #Granite Creek
filter(between(Date, as.Date("1965-10-01"), as.Date("1986-09-29")))
Reservoir_flow <- read_csv(paste0(USJpath, "gauges/streamflow_cfs.csv", sep = ""), col_types = cols(Date = col_date(), .default = col_double()))
#Subbasin 22
Sub22 <- Reservoir_flow[c(1,3)] %>% #Granite Creek
filter(between(Date, as.Date("1965-10-01"), as.Date("1986-09-29"))) %>%
rename(flow = 2)
Sub22
```
```{r}
path <- "C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/pywr_models/bias correction/USJR/SubCatchment_RO_BC2"
#BiasCorrection <- function(Observed, Simulated, ToBeCorrected, Subbasin){
Observed <- Sub22
Simulated <- Sub22_PreBC
ToBeCorrected <- Sub22_Livneh
Subbasin <-"22"
bc_df <- list()
fin_df <- list()
for (i in (1:366)){
obs <- filter(Observed, paste0(lubridate::month(Observed$Date), "/", lubridate::day(Observed$Date)) == unique(paste0(lubridate::month(Observed$Date), "/", lubridate::day(Observed$Date)))[i])
#filter(Observed, paste0(lubridate::day(Observed$Date), "/", lubridate::month(Observed$Date)) == i)
hind <- filter(Simulated, paste0(lubridate::month(Simulated$Date), "/", lubridate::day(Simulated$Date)) == unique(paste0(lubridate::month(Simulated$Date), "/", lubridate::day(Simulated$Date)))[i])
#filter(Simulated,yday(Simulated$Date)== i)
bF <- getBiasFactor(as.data.frame(hind),as.data.frame(obs),method = "scaling", scaleType = "multi")
bF
# print(paste(i," bias factor is ", bF@biasFactor, sep = ""))
new_df <- filter(ToBeCorrected, paste0(lubridate::month(ToBeCorrected$Date), "/", lubridate::day(ToBeCorrected$Date)) == unique(paste0(lubridate::month(ToBeCorrected$Date), "/", lubridate::day(ToBeCorrected$Date)))[i])
#filter(ToBeCorrected,yday(as.Date(ToBeCorrected$Date, format = "%Y-%m-%d")) ==i)
bc_df <- applyBiasFactor(new_df,bF)
if (i==1){
fin_df <- bc_df
bF_list <- data.frame(unique(paste0(lubridate::month(Observed$Date), "/", lubridate::day(Observed$Date)))[i], `Bias Factor` = bF@biasFactor[[1]])
}
else {
fin_df <- bind_rows(fin_df,bc_df)
bF_list <- bind_rows(bF_list, data.frame(unique(paste0(lubridate::month(Observed$Date), "/", lubridate::day(Observed$Date)))[i]unique(paste0(lubridate::month(Observed$Date), "/", lubridate::day(Observed$Date)))[i], `Bias Factor` = bF@biasFactor[[1]]))
}
}
fin_df2 <- arrange(fin_df, Date) #bias corrected Florence Lake inflow
head(fin_df2)
write_csv(fin_df2, paste(path,"/tot_runoff_sb",Subbasin,"_mcm.csv", sep=""))
bF_list <- bF_list %>%
rename(`Day` = i)
write_csv(bF_list, paste(path,"/Bias Correction Factors_sb",Subbasin,".csv", sep=""))
}
```
```{r}
BiasCorrection(Sub22, Sub22_PreBC, Sub22_Livneh, "22")
```
```{r}
Make_Figures <- function(Observed, Simulated, Subbasin, StartDate, EndDate, Area){
#Area <- "Florence Lake"
#Observed <- Sub38
#Simulated<- Sub38_PreBC
#Subbasin <- "sb38"
#StartDate <- "1950-01-01"
#EndDate <- "1980-09-29"
path <- "C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/pywr_models/bias correction/USJR/SubCatchment_RO_BC2/sub22"
Pre_BC <- Simulated %>% #getting the data pre-bias correction
mutate(Data = "Pre-Bias Correction (Livneh)", #getting the numbers back into cms to plot
Month = as.Date(cut(Date, breaks = "month"))) %>%
rename(`Inflow (cfs)` = 2)
Pre_BC
Pre_BC2 <- Pre_BC %>% #getting the data pre-bias correction
group_by(yr = year(Date), mon = month(Date), Data) %>%
summarise(`Inflow (cfs)` = sum(`Inflow (cfs)`)) %>%
ungroup() %>%
mutate(Date = as.Date(ymd(paste(yr,'-', mon,'-01', sep="")))) %>%
select(Date, `Inflow (cfs)`, Data)
Pre_BC2
BC_daily <- read_csv(paste("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/pywr_models/bias correction/USJR/SubCatchment_RO_BC2/tot_runoff_",Subbasin,"_mcm.csv", sep="")) %>% #Getting the Bias Corrected data we generated
mutate(Data = "Post-Bias Correction (Livneh)",
Month = as.Date(cut(Date, breaks = "month"))) %>%
rename(`Inflow (cfs)` = 2) %>%
filter(between(Date, as.Date(StartDate), as.Date(EndDate)))
BC_daily
BC_daily2 <- BC_daily %>%
group_by(yr = year(Date), mon = month(Date), Data) %>%
summarise(`Inflow (cfs)` = sum(`Inflow (cfs)`)) %>%
ungroup() %>%
mutate(Date = as.Date(ymd(paste(yr,'-', mon,'-01', sep="")))) %>%
select(Date, `Inflow (cfs)`, Data)
BC_daily2
Reference <- Observed %>% #getting the observed data from USGS
mutate(Data = "USGS Reference (Observed)",
Month = as.Date(cut(Date, breaks = "month"))) %>%
rename(`Inflow (cfs)` = 2)
Reference
Reference2 <- Reference %>%
group_by(yr = year(Date), mon = month(Date), Data) %>%
summarise(`Inflow (cfs)` = sum(`Inflow (cfs)`)) %>%
ungroup() %>%
mutate(Date = as.Date(ymd(paste(yr,'-', mon,'-01', sep="")))) %>%
select(Date, `Inflow (cfs)`, Data)
Reference2
Final <- rbind(Pre_BC, BC_daily, Reference)
Final
Final$Data <- factor(Final$Data, levels = c("USGS Reference (Observed)", "Pre-Bias Correction (Livneh)","Post-Bias Correction (Livneh)"))
Final2 <- rbind(Pre_BC2, BC_daily2, Reference2)
Final2
Final2$Data <- factor(Final2$Data, levels = c("USGS Reference (Observed)", "Pre-Bias Correction (Livneh)","Post-Bias Correction (Livneh)"))
##Line Curve
#Final2 <- Final %>%
# mutate(Yearmon = zoo::as.yearmon(Date)) %>%
# group_by(Yearmon, Data) %>%
# summarise(Inflow = sum(Inflow)) %>%
# ungroup() %>%
# mutate(Date = as.Date(Yearmon))
ggplot(Final,aes(x=Date, y=`Inflow (cfs)`*35314666.21266/86400 , color=Data))+ geom_line(size = 0.3, alpha = 0.9)+ xlab("Date")+ ylab(expression("Inflow ("*cfs*")"))+ scale_x_date(date_labels = "%Y-%m")+
ggtitle(paste0(Area)) +theme(plot.title = element_text(hjust = 0.5))+ scale_y_continuous(breaks = scales::pretty_breaks())+scale_colour_manual(values = c("USGS Reference (Observed)" = "springgreen4", "Pre-Bias Correction (Livneh)" = "tomato2", "Post-Bias Correction (Livneh)" = "dodgerblue2"))+ theme(legend.position="bottom")+
ggsave(filename=paste(path,"/",Subbasin,"_line_daily.png",sep=""), units="in",width=7,height=5)#,res=360)
ggplot(Final2,aes(x=Date, y=`Inflow (cfs)`*810.71318210885, color=Data))+ ylab(expression("Inflow ("*ACF/month*")"))+ geom_line(size = 0.3, alpha = 0.9)+ scale_y_continuous(breaks = scales::pretty_breaks())+ scale_x_date(date_labels = "%Y-%m") +ggtitle(paste0(Area)) + theme(plot.title = element_text(hjust = 0.5)) +scale_colour_manual(values = c("USGS Reference (Observed)" = "springgreen4", "Pre-Bias Correction (Livneh)" = "tomato2", "Post-Bias Correction (Livneh)" = "dodgerblue2")) + theme(legend.position="bottom")+
ggsave(filename=paste(path,"/",Subbasin,"_line_monthly.png",sep=""), units="in",width=7,height=5)
ggplot(Final,aes(x=Date, y=`Inflow (cfs)`*35314666.21266/86400 +1, color=Data))+ geom_line(size = 0.3, alpha = 0.9)+ xlab("Date")+ ylab(expression("Inflow ("*cfs*")"))+ scale_x_date(date_labels = "%Y-%m")+
ggtitle(paste0(Area)) +theme(plot.title = element_text(hjust = 0.5))+ scale_y_log10() +scale_colour_manual(values = c("USGS Reference (Observed)" = "springgreen4", "Pre-Bias Correction (Livneh)" = "tomato2", "Post-Bias Correction (Livneh)" = "dodgerblue2")) + theme(legend.position="bottom")+
ggsave(filename=paste(path,"/",Subbasin,"_line_daily_logscale.png",sep=""), units="in",width=7,height=5)#,res=360)
ggplot(Final2,aes(x=Date, y=`Inflow (cfs)`*810.71318210885, color=Data))+ ylab(expression("Inflow ("*ACF/month*")"))+ geom_line(geom = "line", size = 0.3, alpha = 0.9)+ scale_colour_manual(values = c("USGS Reference (Observed)" = "springgreen4", "Pre-Bias Correction (Livneh)" = "tomato2", "Post-Bias Correction (Livneh)" = "dodgerblue2"))+ scale_x_date(date_labels = "%Y-%m") +ggtitle(paste0(Area)) + theme(plot.title = element_text(hjust = 0.5)) + scale_y_log10() + theme(legend.position="bottom")+
ggsave(filename=paste(path,"/",Subbasin,"_line_monthly_logscale.png",sep=""), units="in",width=7,height=5)
##FDC Curve
data.nm <- unique(Final$Data)
Final$FDC <- NA
for (i in (1:length(data.nm))){
vls <- Final$`Inflow (cfs)`[Final$Data==data.nm[i]]+0.00001
Fn <- ecdf(vls)
Final$FDC[Final$Data==data.nm[i]] <- 1-Fn(vls) # exceedance probabilities
}
ggplot(Final, aes(x=FDC, y=`Inflow (cfs)`*35314666.21266/86400 , color=Data)) + geom_line(size = 0.6, alpha = 0.5) + #geom_point(shape=21, size=0.05, alpha=0.25) + #scale_y_log10(limits = c(min(Final$`Inflow (cfs)`+0.00001),NA))+
ylab(expression("Inflow ("*cfs*")")) + xlab("Exceedance probability") + ggtitle(paste0(Area)) + theme(plot.title = element_text(hjust = 0.5))+scale_colour_manual(values = c("USGS Reference (Observed)" = "springgreen4", "Pre-Bias Correction (Livneh)" = "tomato2", "Post-Bias Correction (Livneh)" = "dodgerblue2")) + theme(legend.position="bottom") +
ggsave(filename=paste(path,"/",Subbasin,"_fdc.png",sep=""), units="in",width=7,height=5)#,res=360)
## Box&whisker
ggplot(Final, aes(x=Data, y=`Inflow (cfs)`*35314666.21266/86400 )) + geom_boxplot() +
# scale_y_log10(limits=c(3e-1,3e3)) +
ylab(expression("Inflow ("*cfs*")")) + xlab("Data") +ggtitle(paste0(Area)) +theme(plot.title = element_text(hjust = 0.5)) + theme(legend.position="bottom")+
ggsave(filename=paste(path,"/",Subbasin,"_box.png",sep=""), units="in",width=7,height=5)#,res=360)
## Q-Q plot
ggplot(Final, aes(sample=`Inflow (cfs)`*35314666.21266/86400 , color=Data)) + stat_qq(shape=21, size=0.75)+ylab(expression("Sample Quantiles ("*cfs*")"))+ xlab("Theorical Quantiles (Normal Distribution)")+ggtitle(paste0(Area)) + theme(plot.title = element_text(hjust = 0.5))+ scale_colour_manual(values = c("USGS Reference (Observed)" = "springgreen4", "Pre-Bias Correction (Livneh)" = "tomato2", "Post-Bias Correction (Livneh)" = "dodgerblue2")) + theme(legend.position="bottom")+
png(filename=paste(path,"/",Subbasin,"_qq.png",sep=""), units="in",width=7,height=5,res=360)
}
```
```{r}
#Make_Figures(Sub21, Sub21_PreBC, "sb21", "1980-10-01", "2013-12-31")
Make_Figures(Sub22, Sub22_PreBC, "sb22", "1965-10-01" ,"1986-09-29", "Granite Creek")
```