1 Initial setup

1.1 Packages

library(dataRetrieval)
library(plotly)
library(flexdashboard)

1.2 120 day date range

gauge_date_range=c(Sys.Date()-120, Sys.Date())

1.3 Colors list

cols=list(
    list('rgba(200, 30, 30, 0.5)','rgb(200, 30, 30)'),      #red 1
    list('rgba(245, 121, 58, 0.5)','rgb(245, 121, 58)'),    #orange 2
    list('rgba(230, 200, 50, 0.5)','rgb(230, 200, 50)'),    #yellow 3
    list('rgba(60, 230, 70, 0.5)','rgb(60, 230, 70)'),      #green 4
    list('rgba(15, 32, 128, 0.5)','rgb(15, 32, 128)'),      #blue 5
    list('rgba(169, 90, 161, 0.5)','rgb(169, 90, 161)')     #purple 6
    )
gauge_cols=c(cols[[4]][[2]],cols[[2]][[2]],cols[[1]][[2]])

1.4 Data imports

1.4.1 Sites

site_list=read.csv(file="site_list.csv")

1.4.2 Elevation

elevation=dataRetrieval::readNWISdv(siteNumbers=site_list$SiteNumber[site_list$SiteType=="Elevation gauge"],parameterCd="62614")
elevation=dplyr::rename(elevation, elev_ft=X_62614_00003)
elevation=merge(elevation, site_list, by.x='site_no', by.y='SiteNumber')

1.4.3 Breach discharge

breach_data=dataRetrieval::readNWISdv(siteNumbers=subset(site_list, SiteType=="Causeway breach")$SiteNumber, parameterCd="00060", startDate = "2010-01-01", endDate = Sys.Date())
breach_data=dplyr::rename(breach_data, discharge_cfs=X_00060_00003)
breach_data_wide=reshape2::dcast(breach_data, Date~site_no, value.var='discharge_cfs')
breach_data_wide = breach_data_wide %>% mutate(Net=`10010025`-`10010026`) %>% rename("N to S"=`10010025`, "S to N" =`10010026`)
breach_data=reshape2::melt(breach_data_wide, id.vars='Date', measure.vars=c('N to S','S to N','Net'), value.name='discharge_cfs', variable.name='site')

1.4.4 Salinity (water quality) - Gilbert

wq_data=dataRetrieval::readNWISqw(expanded=F,tz="America/Denver",startDate="01-01-2010", endDate=Sys.Date(),
        siteNumbers=site_list$SiteNumber[site_list$SiteType %in% c("Lake", "Lake, DBL", "Discharge gauge", "Causeway breach","Elevation gauge")],
        parameterCd=c(
            "00010",
            "00098",            
            "70305",
            "72263",
            "00095",
            "72012",
            "72013")
)

wq_data=dplyr::rename(wq_data,
    Temperature_c=p00010,
    Sampling_depth_m=p00098,
    Salinity_gL=p70305,
    Density_gcm3=p72263,
    SpecCond_uScm=p00095,
    SG=p72013,
    SG_temp=p72012)

wq_data=within(wq_data, {
    sg_20c=SG/((SG_temp-20)*0.0002+1)
    sal_pct=(((Density_gcm3-1)*1000)/0.63)/(Density_gcm3*10)
    sal_pct=ifelse(is.na(sal_pct) & site_no=='10010100', (((sg_20c-1)*1000)/0.63)/(sg_20c*10), sal_pct)
    rel_depth=NA
    rel_depth[Sampling_depth_m<=1]="Surface"
    rel_depth[Sampling_depth_m>=4]="Bottom"
    month=lubridate::month(sample_dt)
})

wq_data=merge(wq_data, site_list, by.x='site_no', by.y='SiteNumber')