get_cdec | R Documentation |
This function takes a station and sensor along with a duration and start and end date. It returns associated CDEC sensor data in a dataframe. This function can download data for a single station or a list of multiple stations at once using purrr.
get_cdec(station, sensor, duration, start, end)
station |
Station is 3 letter abbreviation (see https://info.water.ca.gov/staMeta.html) |
sensor |
Sensor is number. |
duration |
Duration is E=event, D=Daily, H=Hourly. |
start |
A start date formatted as "YYYY-MM-DD". |
end |
A end date formatted as "YYYY-MM-DD". |
Commonly used sensors include:
1 stage (ft)
20 flow (cfs)
2 rain accum (in)
16 precip tippingbucket (in)
45 ppt incremental (in)
3 snow water content (in)
18 snow depth (in)
6 reservoir elevation (ft)
15 reservoir storage (ac-ft)
76 reservoir inflow
25 water temp
4 air temp
Will return a dataframe with station ID and associated sensor data.
To see a list of Real-Time Stations: http://cdec.water.ca.gov/misc/realStations.html
To see a list of Daily Stations: http://cdec.water.ca.gov/misc/dailyStations.html
To see a list of sensors: http://cdec.water.ca.gov/misc/senslist.html
# set up the parameters to use in function: library(dplyr) library(purrr) sens <- c(20) startT <- c("2013-12-01") endT <- c("2019-03-01") stations <- c("CLW", "MLW", "TIS") dur <- c("H") # hourly here, try this with "H" "D" or "E" # combine into one argument list (note, order matches function order) varList <- list(stations, sens, dur, startT, endT) # now run function: # now map each argument in parallel, then combine cdec_hrly <- pmap(varList, get_cdec) %>% bind_rows() # check records by station table(cdec_hrly$station_id) # check date range cdec_hrly %>% group_by(station_id) %>% summarize(mx=max(datetime), mn=min(datetime)) # Format to Daily Data (requires dplyr) cdec <- cdec_hrly %>% mutate(date = as.Date(datetime)) %>% group_by(station_id, date) %>% summarize("daily_cfs" = mean(value, na.rm = TRUE)) %>% filter(!is.na(daily_cfs)) # here we can add Water Year and plot library(lubridate) cdec <- cdec %>% add_WYD("date") %>% mutate(month = lubridate::month(date), mday = lubridate::mday(date), md = format(date, "%m/%d")) # now make datasets for each overtopping weir # Tisdale overtops at 45.45 feet: tis<- cdec %>% filter(station_id == "TIS") %>% mutate(WY_rev = as.numeric(as.character(WY))+.3) # Colusa Weir overtopping col<- cdec %>% filter(station_id == "CLW") %>% mutate(WY_rev = as.numeric(as.character(WY))+.6) # Moulton Weir overtopping mol<- cdec %>% filter(station_id == "MLW") %>% mutate(WY_rev = as.numeric(as.character(WY))+.9) # now plot! library(ggplot2) ggplot() + geom_point(data=tis, aes(x=DOWY, y=WY_rev, shape=station_id, color=station_id), size=2.5, alpha=0.9) + geom_point(data=col, aes(x=DOWY, y=WY_rev, shape=station_id, color=station_id), size=2, alpha=0.9) + geom_point(data=mol, aes(x=DOWY, y=WY_rev, shape=station_id, color=station_id), size=1, alpha=0.9) + theme_bw(base_size = 10) + scale_shape_manual("Station", values = c(16,17,15))+ labs(x= "", y= "Water Year") + scale_color_viridis_d("Station", direction = -1, option = "D") + scale_y_continuous(breaks= seq(2012.5, 2020.5, 1), labels=c(seq(2012, 2020, 1))) + scale_x_continuous(breaks = seq(0, 365, 61), labels = c("Oct", "Dec", "Feb", "Apr", "Jun", "Aug"), limits = c(0, 340)) + theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.9, 0.7)) # single plot: (tis.plot<-ggplot() + geom_tile(data=tis, aes(x=DOWY, y=WY, fill=station_id), show.legend = FALSE) + labs(x= "", y= "Water Year", title = "Tisdale Overtopping") + scale_fill_viridis_d("Station", direction = -1, option = "C") + scale_y_continuous(breaks= seq(2012.5, 2020.5, 1), labels=c(seq(2012, 2020, 1))) + scale_x_continuous(breaks = seq(0, 365, 61), labels = c("Oct", "Dec", "Feb", "Apr", "Jun", "Aug"), limits = c(0, 340)) + theme(plot.title = element_text(hjust = 0.5)) + coord_cartesian(expand = FALSE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.