Scenarios

Set working directory and load packages

setwd("/Users/richpauloo/Google Drive/pyVIN/Process pyVIN")
suppressPackageStartupMessages({
  library(tidyverse) 
  library(ggplot2)
})

Pull in data

temp = list.files(pattern="*.csv")
list2env(
  lapply(setNames(temp, make.names(gsub("*.csv$", "", temp))), 
         read.csv, header=TRUE, stringsAsFactors=FALSE), envir = .GlobalEnv)

Winter Re-Operation

SR_FOL Storage Carryover Rule Curves for 5 winter months (November - March) were modified to allow more outflow from surface water and into GW_08.

pre <- data.frame(x = c(83,218.057,375.614,575),
                  y = c(1288.633,741.814,329.347,0),
                  policy = c(rep('historical CALVIN')))
post <- data.frame(x = c(83,218.057,375.614, 575),
                   y = c(959.285,412.466,0,0),
                   policy = c(rep('Re-operation')))
rbind(pre,post) %>% 
  ggplot() +
  geom_line(aes(x,y, color = policy)) +
  ylab('benefit ($K)') +
  xlab('carryover storage (TAF)') +
  ggtitle('Folsom Reservoir Re-Operation (1922/01/31 - 1922/02/28)') +
  theme_light()

Evaporation

# evaporation
glimpse(evaporation_0)
evaporation_0 <- evaporation_0 %>% rename(SR_FOL_0 = SR_FOL) %>% select(date, SR_FOL_0)
evaporation_0.1 <- evaporation_0.1 %>% rename(SR_FOL_0.1 = SR_FOL) %>% select(SR_FOL_0.1)
evaporation_4.1 <- evaporation_4.1 %>% rename(SR_FOL_4.1 = SR_FOL) %>% select(SR_FOL_4.1)
evaporation_4.2 <- evaporation_4.2 %>% rename(SR_FOL_4.2 = SR_FOL) %>% select(SR_FOL_4.2)

all_evaporation <- cbind(evaporation_0, evaporation_0.1, evaporation_4.1, evaporation_4.2)
scen_nam <- c("date", "NoLink, Overdraft", "NoLink, Si=Sf", "RO, Link, Overdraft", "RO, Link, Si=Sf")
colnames(all_evaporation) <- scen_nam

# evaporation (annual averages)
all_evaporation %>% 
  gather(key = scenario, value = evap, colnames(all_evaporation[2:5])) %>% 
  mutate(year = substring(date, 1, 4)) %>% 
  group_by(year, scenario) %>% 
  summarise(evap = mean(evap)) %>% 
ggplot() + 
  geom_line(aes(x = as.Date(year, format = '%Y'), y = evap, color = scenario, group = scenario)) + 
  scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
  xlab('date') +
  ylab('Evaporation (TAF)') +
  ggtitle('Annual Average Evaporation (TAF)') +
  theme_light()

Storage (GW and SW)

# storage
glimpse(storage_0)
storage_0 <- storage_0 %>% rename(SR_FOL_0 = SR_FOL, GW_08_0 = GW_08) %>% select(date, SR_FOL_0, GW_08_0)
storage_0.1 <- storage_0.1 %>% rename(SR_FOL_0.1 = SR_FOL, GW_08_0.1 = GW_08) %>% select(SR_FOL_0.1, GW_08_0.1)
storage_4.1 <- storage_4.1 %>% rename(SR_FOL_4.1 = SR_FOL, GW_08_4.1 = GW_08) %>% select(SR_FOL_4.1, GW_08_4.1) 
storage_4.2 <- storage_4.2 %>% rename(SR_FOL_4.2 = SR_FOL, GW_08_4.2 = GW_08) %>% select(SR_FOL_4.2, GW_08_4.2)

all_storage <- cbind(storage_0, storage_0.1, storage_4.1, storage_4.2)
GW_storage <- all_storage %>% select(date, starts_with('GW'))
SW_storage <- all_storage %>% select(date, starts_with('SR'))
colnames(GW_storage) <- scen_nam
colnames(SW_storage) <- scen_nam

# GW storage
GW_storage %>% 
  gather(key = scenario, value = storage, colnames(GW_storage[2:5])) %>% 
ggplot() + 
  geom_line(aes(x = as.Date(date), y = storage, color = scenario, group = scenario)) + 
  scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
  xlab('date') +
  ylab('storage (TAF)') +
  scale_color_discrete(breaks = c('NoLink, Si=Sf', 'RO, Link, Si=Sf', 'NoLink, Overdraft', 'RO, Link, Overdraft')) +
  ggtitle('GW Storage (TAF)') +
  theme_light()

# SW Storage
SW_storage %>% 
  gather(key = scenario, value = storage, colnames(SW_storage[2:5])) %>% 
  mutate(year = substring(date, 1, 4)) %>% 
  group_by(year, scenario) %>% 
  summarise(storage = mean(storage)) %>% 
ggplot() + 
  geom_line(aes(x = as.Date(year, format = '%Y'), y = storage, color = scenario, group = scenario)) +
  scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
  xlab('date') +
  ylab('Storage (TAF)') +
  ggtitle('Average Annual SR_FOL Storage (TAF)') +
  theme_light()

Recharge (along C173.GW_08)

# recharge
glimpse(flow_4.1)
flow_4.1 <- flow_4.1 %>% select(date, C173.GW_08) %>% 
  rename(C173.GW08_4.1 = C173.GW_08) 
flow_4.2 <- flow_4.2 %>% select(C173.GW_08) %>% 
  rename(C173.GW08_4.2 = C173.GW_08)

all_flow <- cbind(flow_4.1, flow_4.2)
colnames(all_flow) <- scen_nam[c(1,4:5)]


# GW recharge (annual average)
all_flow %>% 
  gather(key = scenario, value = recharge, colnames(all_flow[2:3])) %>% 
  mutate(year = substring(date, 1, 4)) %>% 
  group_by(year, scenario) %>% 
  summarise(recharge = mean(recharge)) %>% 
ggplot() + 
  geom_line(aes(x = as.Date(year, format = '%Y'), y = recharge, color = scenario, group = scenario)) +
  scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
  xlab('date') +
  ylab('Recharge (TAF)') +
  scale_color_discrete(breaks = c('RO, Link, Si=Sf', 'RO, Link, Overdraft')) +
  ggtitle('Average Annual Recharge (TAF)') +
  theme_light()

# total 82 yr recharge
all_flow %>% 
  gather(key = scenario, value = recharge, colnames(all_flow[2:3])) %>% 
  mutate(year = substring(date, 1, 4)) %>% 
  group_by(year, scenario) %>% 
  summarise(recharge = sum(recharge)) %>% 
ggplot() + 
  geom_bar(aes(x = factor(scenario), y = recharge, fill = scenario, group = scenario), stat = 'identity') + 
  xlab('scenario') +
  ylab('Recharge (TAF)') +
  scale_fill_discrete(breaks = c('RO, Link, Si=Sf', 'RO, Link, Overdraft')) +
  coord_flip() +
  ggtitle('82-year Total Recharge (TAF)') +
  theme_light()

# average annual recharge
all_flow %>% 
  gather(key = scenario, value = recharge, colnames(all_flow[2:3])) %>% 
  mutate(year = substring(date, 1, 4)) %>% 
  group_by(year, scenario) %>% 
  summarise(recharge = sum(recharge)) %>% 
ggplot() + 
  geom_bar(aes(x = factor(scenario), y = recharge/82, fill = scenario, group = scenario), stat = 'identity') + 
  xlab('scenario') +
  ylab('Recharge (TAF)') +
  scale_fill_discrete(breaks = c('RO, Link, Si=Sf', 'RO, Link, Overdraft')) +
  coord_flip() +
  ggtitle('Average Annual Recharge (TAF)') +
  theme_light()

Shortage Volume

# recharge
glimpse(shortage_volume_0)
shortage_volume_0 <- shortage_volume_0 %>% select(-date) %>% sum()
shortage_volume_0.1 <- shortage_volume_0.1 %>% select(-date) %>% sum()
shortage_volume_4.1 <- shortage_volume_4.1 %>% select(-date) %>% sum()
shortage_volume_4.2 <- shortage_volume_4.2 %>% select(-date) %>% sum()

all_shortage <- cbind(shortage_volume_0, shortage_volume_0.1, shortage_volume_4.1, shortage_volume_4.2)
colnames(all_shortage) <- scen_nam[2:5]
all_shortage <- all_shortage %>% as.data.frame() %>% 
  gather(key = scenario, value = all_shortage)

# 82 year shortage volume
all_shortage %>% 
ggplot() + 
  geom_bar(aes(x = factor(scenario), y = all_shortage, fill = scenario, group = scenario), stat = 'identity') + 
  xlab('scenario') +
  ylab('Shortage Volume (TAF)') +
  scale_fill_discrete(breaks = c('RO, Link, Si=Sf', 'RO, Link, Overdraft', 'NoLink, Si=Sf', 'NoLink, Overdraft')) +
  coord_flip() +
  ggtitle('82-year shortage volume (TAF)') +
  theme_light()

# average annual shortage volume
all_shortage %>% 
ggplot() + 
  geom_bar(aes(x = factor(scenario), y = all_shortage/82, fill = scenario, group = scenario), stat = 'identity') + 
  xlab('scenario') +
  ylab('Shortage Volume (TAF)') +
  scale_fill_discrete(breaks = c('RO, Link, Si=Sf', 'RO, Link, Overdraft', 'NoLink, Si=Sf', 'NoLink, Overdraft')) +
  coord_flip() +
  ggtitle('Average Annual Shortage Volume (TAF)') +
  theme_light()

devtools::session_info()
## Session info -------------------------------------------------------------
##  setting  value                       
##  version  R version 3.4.0 (2017-04-21)
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/Los_Angeles         
##  date     2017-08-30
## Packages -----------------------------------------------------------------
##  package    * version date       source         
##  assertthat   0.2.0   2017-04-11 CRAN (R 3.4.0) 
##  backports    1.1.0   2017-05-22 CRAN (R 3.4.0) 
##  base       * 3.4.0   2017-04-21 local          
##  bindr        0.1     2016-11-13 CRAN (R 3.4.0) 
##  bindrcpp   * 0.2     2017-06-17 CRAN (R 3.4.0) 
##  broom        0.4.2   2017-02-13 CRAN (R 3.4.0) 
##  cellranger   1.1.0   2016-07-27 CRAN (R 3.4.0) 
##  colorspace   1.3-2   2016-12-14 CRAN (R 3.4.0) 
##  compiler     3.4.0   2017-04-21 local          
##  datasets   * 3.4.0   2017-04-21 local          
##  devtools     1.13.3  2017-08-02 CRAN (R 3.4.1) 
##  digest       0.6.12  2017-01-27 CRAN (R 3.4.0) 
##  dplyr      * 0.7.2   2017-07-20 CRAN (R 3.4.0) 
##  evaluate     0.10.1  2017-06-24 CRAN (R 3.4.1) 
##  forcats      0.2.0   2017-01-23 CRAN (R 3.4.0) 
##  foreign      0.8-69  2017-06-22 CRAN (R 3.4.1) 
##  ggplot2    * 2.2.1   2016-12-30 CRAN (R 3.4.0) 
##  glue         1.1.1   2017-06-21 CRAN (R 3.4.1) 
##  graphics   * 3.4.0   2017-04-21 local          
##  grDevices  * 3.4.0   2017-04-21 local          
##  grid         3.4.0   2017-04-21 local          
##  gtable       0.2.0   2016-02-26 CRAN (R 3.4.0) 
##  haven        1.1.0   2017-07-09 CRAN (R 3.4.1) 
##  hms          0.3     2016-11-22 CRAN (R 3.4.0) 
##  htmltools    0.3.6   2017-04-28 CRAN (R 3.4.0) 
##  httr         1.3.0   2017-08-16 CRAN (R 3.4.0) 
##  jsonlite     1.5     2017-06-01 CRAN (R 3.4.0) 
##  knitr        1.17    2017-08-10 CRAN (R 3.4.1) 
##  labeling     0.3     2014-08-23 CRAN (R 3.4.0) 
##  lattice      0.20-35 2017-03-25 CRAN (R 3.4.0) 
##  lazyeval     0.2.0   2016-06-12 CRAN (R 3.4.0) 
##  lubridate    1.6.0   2016-09-13 CRAN (R 3.4.0) 
##  magrittr     1.5     2014-11-22 CRAN (R 3.4.0) 
##  memoise      1.1.0   2017-04-21 CRAN (R 3.4.0) 
##  methods    * 3.4.0   2017-04-21 local          
##  mnormt       1.5-5   2016-10-15 CRAN (R 3.4.0) 
##  modelr       0.1.1   2017-07-24 CRAN (R 3.4.1) 
##  munsell      0.4.3   2016-02-13 CRAN (R 3.4.0) 
##  nlme         3.1-131 2017-02-06 CRAN (R 3.4.0) 
##  parallel     3.4.0   2017-04-21 local          
##  pkgconfig    2.0.1   2017-03-21 CRAN (R 3.4.0) 
##  plyr         1.8.4   2016-06-08 CRAN (R 3.4.0) 
##  psych        1.7.5   2017-05-03 CRAN (R 3.4.0) 
##  purrr      * 0.2.3   2017-08-02 CRAN (R 3.4.1) 
##  R6           2.2.2   2017-06-17 CRAN (R 3.4.0) 
##  Rcpp         0.12.12 2017-07-15 cran (@0.12.12)
##  readr      * 1.1.1   2017-05-16 CRAN (R 3.4.0) 
##  readxl       1.0.0   2017-04-18 CRAN (R 3.4.0) 
##  reshape2     1.4.2   2016-10-22 CRAN (R 3.4.0) 
##  rlang        0.1.2   2017-08-09 CRAN (R 3.4.1) 
##  rmarkdown    1.6     2017-06-15 CRAN (R 3.4.0) 
##  rprojroot    1.2     2017-01-16 CRAN (R 3.4.0) 
##  rvest        0.3.2   2016-06-17 CRAN (R 3.4.0) 
##  scales       0.4.1   2016-11-09 CRAN (R 3.4.0) 
##  stats      * 3.4.0   2017-04-21 local          
##  stringi      1.1.5   2017-04-07 CRAN (R 3.4.0) 
##  stringr      1.2.0   2017-02-18 CRAN (R 3.4.0) 
##  tibble     * 1.3.3   2017-05-28 CRAN (R 3.4.0) 
##  tidyr      * 0.7.0   2017-08-16 CRAN (R 3.4.0) 
##  tidyselect   0.1.1   2017-07-24 CRAN (R 3.4.1) 
##  tidyverse  * 1.1.1   2017-01-27 CRAN (R 3.4.0) 
##  tools        3.4.0   2017-04-21 local          
##  utils      * 3.4.0   2017-04-21 local          
##  withr        2.0.0   2017-07-28 CRAN (R 3.4.1) 
##  xml2         1.1.1   2017-01-24 CRAN (R 3.4.0) 
##  yaml         2.1.14  2016-11-12 CRAN (R 3.4.0)