-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code_IFR_Redflag_RCP4.5.Rmd
425 lines (280 loc) · 24.6 KB
/
Code_IFR_Redflag_RCP4.5.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
---
title: "Code_IFR_Redflag"
author: "Gustavo Facincani Dourado"
date: "6/26/2020"
output: html_document
---
```{r}
library(dplyr)
library(tidyverse)
library(lfstat)
library(reshape2)
```
```{r}
#Read data for Merced basin
#IFR actual flow
Livneh_Redflag_Mer <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/historical/Livneh/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Mer$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_Mer
CanESM2_RedFlag_Mer <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/CanESM2_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
CanESM2_RedFlag_Mer$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_Mer
CNRM_RedFlag_Mer <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
CNRM_RedFlag_Mer$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_Mer
HadGEM2_RedFlag_Mer <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
HadGEM2_RedFlag_Mer$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_Mer
MIROC5_RedFlag_Mer <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/MIROC5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
MIROC5_RedFlag_Mer$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_Mer
Flow_Mer <- rbind(Livneh_Redflag_Mer, CanESM2_RedFlag_Mer, CNRM_RedFlag_Mer,HadGEM2_RedFlag_Mer, MIROC5_RedFlag_Mer)
Flow_Mer
```
```{r}
Livneh_Redflag_Mer2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/historical/Livneh/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Mer2$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_Mer2
CanESM2_RedFlag_Mer2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/CanESM2_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
CanESM2_RedFlag_Mer2$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_Mer2
CNRM_RedFlag_Mer2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
CNRM_RedFlag_Mer2$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_Mer2
HadGEM2_RedFlag_Mer2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
HadGEM2_RedFlag_Mer2$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_Mer2
MIROC5_RedFlag_Mer2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/merced/gcms/MIROC5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2,6)]
MIROC5_RedFlag_Mer2$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_Mer2
MinFlow_Mer <- rbind(Livneh_Redflag_Mer2, CanESM2_RedFlag_Mer2, CNRM_RedFlag_Mer2,HadGEM2_RedFlag_Mer2, MIROC5_RedFlag_Mer2)
```
```{r}
#Read data for tuolumne basin
#IFR actual flow
Livneh_Redflag_Tuo <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/historical/Livneh/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Tuo$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_Tuo
CanESM2_RedFlag_Tuo <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/CanESM2_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18)]
CanESM2_RedFlag_Tuo$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_Tuo
CNRM_RedFlag_Tuo <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18)]
CNRM_RedFlag_Tuo$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_Tuo
HadGEM2_RedFlag_Tuo <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18)]
HadGEM2_RedFlag_Tuo$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_Tuo
MIROC5_RedFlag_Tuo <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/MIROC5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18)]
MIROC5_RedFlag_Tuo$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_Tuo
Flow_Tuo <- rbind(Livneh_Redflag_Tuo, CanESM2_RedFlag_Tuo, CNRM_RedFlag_Tuo,HadGEM2_RedFlag_Tuo, MIROC5_RedFlag_Tuo)
Flow_Tuo
```
```{r}
Livneh_Redflag_Tuo2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/historical/Livneh/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2, 6, 10, 14)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Tuo2$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_Tuo2
CanESM2_RedFlag_Tuo2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/CanESM2_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2, 6, 10, 14)]
CanESM2_RedFlag_Tuo2$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_Tuo2
CNRM_RedFlag_Tuo2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2, 6, 10, 14)]
CNRM_RedFlag_Tuo2$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_Tuo2
HadGEM2_RedFlag_Tuo2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2, 6, 10, 14)]
HadGEM2_RedFlag_Tuo2$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_Tuo2
MIROC5_RedFlag_Tuo2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/tuolumne/gcms/MIROC5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1,2, 6, 10, 14)]
MIROC5_RedFlag_Tuo2$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_Tuo2
MinFlow_Tuo <- rbind(Livneh_Redflag_Tuo2, CanESM2_RedFlag_Tuo2, CNRM_RedFlag_Tuo2,HadGEM2_RedFlag_Tuo2, MIROC5_RedFlag_Tuo2)
MinFlow_Tuo
```
```{r}
#Read data for Stanislaus basin
#IFR actual flow
Livneh_Redflag_Stn <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/historical/Livneh/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Stn$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_Stn
CanESM2_RedFlag_Stn <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/CanESM2_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78)]
CanESM2_RedFlag_Stn$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_Stn
CNRM_RedFlag_Stn <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78)]
CNRM_RedFlag_Stn$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_Stn
HadGEM2_RedFlag_Stn <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78)]
HadGEM2_RedFlag_Stn$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_Stn
MIROC5_RedFlag_Stn <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/MIROC5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78)]
MIROC5_RedFlag_Stn$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_Stn
Flow_Stn <- rbind(Livneh_Redflag_Stn, CanESM2_RedFlag_Stn, CNRM_RedFlag_Stn,HadGEM2_RedFlag_Stn, MIROC5_RedFlag_Stn)
Flow_Stn
```
```{r}
#MIFs for STN are in two different files
Livneh_Redflag_Stn2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/historical/Livneh/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18, 22,26,30,34,38,42,46,50)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Stn2
Livneh_Redflag_Stn3 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/historical/Livneh/InstreamFlowRequirement_Requirement_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 6, 10, 14, 18, 22)]%>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_Stn3
Livneh_Redflag_Stn2 <- cbind(Livneh_Redflag_Stn3, Livneh_Redflag_Stn2[-1]) %>%
mutate(GCM = as.factor("Livneh (Historical)"))
Livneh_Redflag_Stn2
CanESM2_RedFlag_Stn2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/CanESM2_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18, 22,26,30,34,38,42,46,50)]
CanESM2_RedFlag_Stn2
CanESM2_RedFlag_Stn3 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/CanESM2_rcp45/InstreamFlowRequirement_Requirement_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 6, 10, 14, 18, 22)]
CanESM2_RedFlag_Stn3
CanESM2_RedFlag_Stn2 <- cbind(CanESM2_RedFlag_Stn3[-1], CanESM2_RedFlag_Stn2) %>%
mutate(GCM = as.factor("CanESM2"))
CanESM2_RedFlag_Stn2
CNRM_RedFlag_Stn2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18, 22,26,30,34,38,42,46,50)]
CNRM_RedFlag_Stn3 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Requirement_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 6, 10, 14, 18, 22)]
CanESM2_RedFlag_Stn3
CNRM_RedFlag_Stn2 <- cbind(CNRM_RedFlag_Stn3[-1], CNRM_RedFlag_Stn2) %>%
mutate(GCM = as.factor("CNRM-CM5"))
CNRM_RedFlag_Stn2
HadGEM2_RedFlag_Stn2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18, 22,26,30,34,38,42,46,50)]
HadGEM2_RedFlag_Stn3 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Requirement_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 6, 10, 14, 18, 22)]
HadGEM2_RedFlag_Stn3
HadGEM2_RedFlag_Stn2 <- cbind(HadGEM2_RedFlag_Stn3[-1], HadGEM2_RedFlag_Stn2) %>%
mutate(GCM = as.factor("HadGEM2-ES"))
HadGEM2_RedFlag_Stn2
MIROC5_RedFlag_Stn2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/MIROC5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 6, 10, 14, 18, 22,26,30,34,38,42,46,50)]
MIROC5_RedFlag_Stn3 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/stanislaus/gcms/MIROC5_rcp45/InstreamFlowRequirement_Requirement_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 6, 10, 14, 18, 22)]
MIROC5_RedFlag_Stn3
MIROC5_RedFlag_Stn2 <- cbind(MIROC5_RedFlag_Stn3[-1], MIROC5_RedFlag_Stn2) %>%
mutate(GCM = as.factor("MIROC5"))
MIROC5_RedFlag_Stn2
MinFlow_Stn <- rbind(Livneh_Redflag_Stn2, CanESM2_RedFlag_Stn2, CNRM_RedFlag_Stn2,HadGEM2_RedFlag_Stn2, MIROC5_RedFlag_Stn2)
MinFlow_Stn
```
```{r}
#Read data for upper_san_joaquin basin
#IFR actual flow
Livneh_Redflag_USJ <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/historical/Livneh/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_USJ$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_USJ
CanESM2_RedFlag_USJ <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/CanESM2_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
CanESM2_RedFlag_USJ$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_USJ
CNRM_RedFlag_USJ <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
CNRM_RedFlag_USJ$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_USJ
HadGEM2_RedFlag_USJ <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
HadGEM2_RedFlag_USJ$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_USJ
MIROC5_RedFlag_USJ <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/MIROC5_rcp45/InstreamFlowRequirement_Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
MIROC5_RedFlag_USJ$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_USJ
Flow_USJ <- rbind(Livneh_Redflag_USJ, CanESM2_RedFlag_USJ, CNRM_RedFlag_USJ,HadGEM2_RedFlag_USJ, MIROC5_RedFlag_USJ)
Flow_USJ
```
```{r}
Livneh_Redflag_USJ2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/historical/Livneh/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)] %>%
filter(between(node, as.Date("1980-10-01"), as.Date("2010-09-30")))
Livneh_Redflag_USJ2$GCM <- as.factor("Livneh (Historical)")
Livneh_Redflag_USJ2
CanESM2_RedFlag_USJ2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/CanESM2_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
CanESM2_RedFlag_USJ2$GCM <- as.factor("CanESM2")
CanESM2_RedFlag_USJ2
CNRM_RedFlag_USJ2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/CNRM-CM5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
CNRM_RedFlag_USJ2$GCM <- as.factor("CNRM-CM5")
CNRM_RedFlag_USJ2
HadGEM2_RedFlag_USJ2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/HadGEM2-ES_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
HadGEM2_RedFlag_USJ2$GCM <- as.factor("HadGEM2-ES")
HadGEM2_RedFlag_USJ2
MIROC5_RedFlag_USJ2 <- read_csv("C:/Users/gusta/Box/VICE Lab/RESEARCH/PROJECTS/CERC-WET/Task7_San_Joaquin_Model/Pywr models/results/Binary IFRs x Prices/upper_san_joaquin/gcms/MIROC5_rcp45/InstreamFlowRequirement_Min Flow_mcm.csv", col_types = cols(node = col_date(), .default = col_double()))[-c(1:3), c(1, 2, 14, 18, 22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90)]
MIROC5_RedFlag_USJ2$GCM <- as.factor("MIROC5")
MIROC5_RedFlag_USJ2
MinFlow_USJ <- rbind(Livneh_Redflag_USJ2, CanESM2_RedFlag_USJ2, CNRM_RedFlag_USJ2,HadGEM2_RedFlag_USJ2, MIROC5_RedFlag_USJ2)
```
```{r}
#Function to create the tibble containing the redflags
IFR_Redflag <- function(Flow, MinFlow, Basin, Scenario) {
IFR <- merge(Flow, MinFlow,by=c("node", "GCM")) #merging Flows and MIFs
#as the columns have the same name, they receive and .x and .y at the end
IFR2 <- IFR[, grepl("*\\.x$",names(IFR))] - IFR[,grepl("*\\.y$",names(IFR))] #subtracting each column with its respective match in each data frame
IFR_redflag<- cbind(IFR[,1:2, drop=FALSE], IFR2) #bind Flows and MIFs
names(IFR_redflag) <- gsub(pattern = ".x", replacement = "", x = names(IFR_redflag)) #remove all ".x" at once
IFR_redflag[,3:ncol(IFR_redflag)] <- lapply(IFR_redflag[,3:ncol(IFR_redflag)], function(x) ifelse(x < -0.0123348, 1,0))
IFR_redflag <- IFR_redflag %>% #condition for considering the days with flow below 0
#below -5 cfs, as Josh recommended
dplyr::transmute(WaterYear = as.factor(lfstat::water_year(node, origin= "usgs")),
#column for WY
"Basin" = as.factor(Basin), #column for basin
"Scenario" = as.factor(Scenario),!!!.) #Column for scenario
IFR_redflag
IFR_redflag <- melt(IFR_redflag, id = c("WaterYear", "GCM", "Scenario","node", "Basin")) %>%
#Rearranging the columns
group_by(WaterYear, GCM, Scenario, Basin, variable) %>% #grouping to summarize
dplyr::summarize(Total = sum(value)) #summing up all the days in a year
#Orderding the Climate scenarios for plotting
IFR_redflag$GCM <- factor(IFR_redflag$GCM, levels = c("Livneh (Historical)","HadGEM2-ES", "CanESM2", "MIROC5", "CNRM-CM5"))
IFR_redflag #result
}
```
```{r}
IFR_redflag_Mer <- IFR_Redflag(Flow_Mer, MinFlow_Mer, "Merced River", "RCP 4.5")
IFR_redflag_Mer
IFR_redflag_Tuo <- IFR_Redflag(Flow_Tuo, MinFlow_Tuo, "Tuolumne River", "RCP 4.5")
IFR_redflag_Tuo
IFR_redflag_Stn <- IFR_Redflag(Flow_Stn, MinFlow_Stn, "Stanislaus River", "RCP 4.5")
IFR_redflag_Stn
IFR_redflag_USJ <- IFR_Redflag(Flow_USJ, MinFlow_USJ, "Upper San Joaquin River", "RCP 4.5")
IFR_redflag_USJ
```
```{r}
#Function for figures
IFR_Deficit <- function(data, title, subtitle) {
ggplot(data) +
theme_bw(base_size=18, base_family='Times New Roman') +
geom_bar(aes(x= WaterYear, y=Total, fill = variable), color = "black", position = "stack", stat = "identity") +
ggthemes::scale_colour_colorblind() +
scale_y_continuous(expand = c(0, NA)) +
scale_x_discrete(breaks = c("1981", "1986", "1991", "1996", "2001", "2006", "2010",
"2031", "2036", "2041", "2046", "2051", "2056", "2059"),
expand = c(0, NA))+
facet_wrap(~ GCM, ncol =1, scales = "free_x", strip.position = 'left') +
labs(title = title,
subtitle = subtitle,
# subtitle = "CanESM2",
x = element_blank(),
y = "Total Instream Flow Requirement Gross Deficit (days)") +
theme(legend.title = element_blank(),
legend.key.size = unit(0.75,"line"), #change the size of icons
legend.position = "bottom",
legend.text = element_text(size = 9),
legend.direction = "horizontal",
legend.box.margin = margin(t = -17),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
strip.placement = "outside",
strip.background = element_blank(),
panel.spacing.y = unit(0, "lines"))+
guides(fill = guide_legend(ncol = 2)) #change number of columns in the legend
}
```
```{r}
IFR_Deficit(IFR_redflag_Stn, "Stanislaus River", "RCP 4.5 Scenario") +
png("Stn_IFR_Deficit_RCP4.5.png", units ="in", width=6.1, height=13, res = 300)
```
```{r}
IFR_Deficit(IFR_redflag_Tuo, "Tuolumne River", "RCP 4.5 Scenario") +
png("Tuo_IFR_Deficit_RCP4.5.png", units ="in", width=6.1, height=13, res = 300)
```
```{r}
IFR_Deficit(IFR_redflag_Mer, "Merced River", "RCP 4.5 Scenario") +
png("Mer_IFR_Deficit_RCP4.5.png", units ="in", width=6.1, height=13, res = 300)
```
```{r}
IFR_Deficit(IFR_redflag_USJ, "Upper San Joaquin River", "RCP 4.5 Scenario") +
png("USJ_IFR_Deficit_RCP4.5.png", units ="in", width=6.1, height=13, res = 300)
```
```