Quick Start with CDER

Michael Koohafkan

2019-12-20

The R package cder provides a simple interface to the CDEC Webservice. Getting CDEC data with cder is easy, just pass a station code to cdec_query():

library(cder)
# get data for CDEC station NSL 
cdec_query("NSL")
#> # A tibble: 577 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 NSL       E                   1 RIV STG    2019-12-20 02:30:00
#>  2 NSL       E                   1 RIV STG    2019-12-20 02:45:00
#>  3 NSL       E                   1 RIV STG    2019-12-20 03:00:00
#>  4 NSL       E                   1 RIV STG    2019-12-20 03:15:00
#>  5 NSL       E                   1 RIV STG    2019-12-20 03:30:00
#>  6 NSL       E                   1 RIV STG    2019-12-20 03:45:00
#>  7 NSL       E                   1 RIV STG    2019-12-20 04:00:00
#>  8 NSL       E                   1 RIV STG    2019-12-20 04:15:00
#>  9 NSL       E                   1 RIV STG    2019-12-20 04:30:00
#> 10 NSL       E                   1 RIV STG    2019-12-20 04:45:00
#> # ... with 567 more rows, and 4 more variables: ObsDate <dttm>, Value <dbl>,
#> #   DataFlag <chr>, SensorUnits <chr>

The CDEC web service uses some default values for the duration code, sensor number, and start/end dates. You can also specify these yourself:

station = "NSL"
duration = "event"
sensor = 100 # electrical conductivity
start.date = Sys.Date() - 14
end.date = Sys.Date()

cdec_query(station, sensor, duration, start.date, end.date)
#> # A tibble: 1,345 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 NSL       E                 100 EL COND    2019-12-06 00:00:00
#>  2 NSL       E                 100 EL COND    2019-12-06 00:15:00
#>  3 NSL       E                 100 EL COND    2019-12-06 00:30:00
#>  4 NSL       E                 100 EL COND    2019-12-06 00:45:00
#>  5 NSL       E                 100 EL COND    2019-12-06 01:00:00
#>  6 NSL       E                 100 EL COND    2019-12-06 01:15:00
#>  7 NSL       E                 100 EL COND    2019-12-06 01:30:00
#>  8 NSL       E                 100 EL COND    2019-12-06 01:45:00
#>  9 NSL       E                 100 EL COND    2019-12-06 02:00:00
#> 10 NSL       E                 100 EL COND    2019-12-06 02:15:00
#> # ... with 1,335 more rows, and 4 more variables: ObsDate <dttm>,
#> #   Value <dbl>, DataFlag <chr>, SensorUnits <chr>

The web service supports multiple stations, sensors, and even duration codes:

# get data for CDEC stations NSL and HUN
stations = c("NSL", "HUN")
# get electrical conductivity and stage
sensors = c(100, 1)
# get event data and hourly averages (where available)
durations = c("event", "hourly")

cdec_query(stations, sensors, durations)
#> # A tibble: 240 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 HUN       E                   1 RIV STG    2019-12-20 02:30:00
#>  2 HUN       E                   1 RIV STG    2019-12-20 02:45:00
#>  3 HUN       E                   1 RIV STG    2019-12-20 03:00:00
#>  4 HUN       E                   1 RIV STG    2019-12-20 03:15:00
#>  5 HUN       E                   1 RIV STG    2019-12-20 03:30:00
#>  6 HUN       E                   1 RIV STG    2019-12-20 03:45:00
#>  7 HUN       E                   1 RIV STG    2019-12-20 04:00:00
#>  8 HUN       E                   1 RIV STG    2019-12-20 04:15:00
#>  9 HUN       E                   1 RIV STG    2019-12-20 04:30:00
#> 10 HUN       E                   1 RIV STG    2019-12-20 04:45:00
#> # ... with 230 more rows, and 4 more variables: ObsDate <dttm>, Value <dbl>,
#> #   DataFlag <chr>, SensorUnits <chr>

That’s it! The CDEC Webservice currently does not support querying station metadata. To browse station data, use the Station Search tool or Locator Map. These URLs can also be accessed by calling cdec_search_stations() and cdec_map(), respectively. See help("cdec-search") for more information.To access the metadata page for a particular station, use cdec_meta().