Quick Start with CDER

Michael Koohafkan

2019-08-02

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 cder_query:

library(cder)
# get data for CDEC station NSL 
cder_query("NSL")
#> # A tibble: 578 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 NSL       E                   1 RIV STG    2019-08-01 19:15:00
#>  2 NSL       E                   1 RIV STG    2019-08-01 19:30:00
#>  3 NSL       E                   1 RIV STG    2019-08-01 19:45:00
#>  4 NSL       E                   1 RIV STG    2019-08-01 20:00:00
#>  5 NSL       E                   1 RIV STG    2019-08-01 20:15:00
#>  6 NSL       E                   1 RIV STG    2019-08-01 20:30:00
#>  7 NSL       E                   1 RIV STG    2019-08-01 20:45:00
#>  8 NSL       E                   1 RIV STG    2019-08-01 21:00:00
#>  9 NSL       E                   1 RIV STG    2019-08-01 21:15:00
#> 10 NSL       E                   1 RIV STG    2019-08-01 21:30:00
#> # ... with 568 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()

cder_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-07-19 00:00:00
#>  2 NSL       E                 100 EL COND    2019-07-19 00:15:00
#>  3 NSL       E                 100 EL COND    2019-07-19 00:30:00
#>  4 NSL       E                 100 EL COND    2019-07-19 00:45:00
#>  5 NSL       E                 100 EL COND    2019-07-19 01:00:00
#>  6 NSL       E                 100 EL COND    2019-07-19 01:15:00
#>  7 NSL       E                 100 EL COND    2019-07-19 01:30:00
#>  8 NSL       E                 100 EL COND    2019-07-19 01:45:00
#>  9 NSL       E                 100 EL COND    2019-07-19 02:00:00
#> 10 NSL       E                 100 EL COND    2019-07-19 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")

cder_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-08-01 19:15:00
#>  2 HUN       E                   1 RIV STG    2019-08-01 19:30:00
#>  3 HUN       E                   1 RIV STG    2019-08-01 19:45:00
#>  4 HUN       E                   1 RIV STG    2019-08-01 20:00:00
#>  5 HUN       E                   1 RIV STG    2019-08-01 20:15:00
#>  6 HUN       E                   1 RIV STG    2019-08-01 20:30:00
#>  7 HUN       E                   1 RIV STG    2019-08-01 20:45:00
#>  8 HUN       E                   1 RIV STG    2019-08-01 21:00:00
#>  9 HUN       E                   1 RIV STG    2019-08-01 21:15:00
#> 10 HUN       E                   1 RIV STG    2019-08-01 21:30: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.