Quick Start with CDER

Michael Koohafkan

2019-08-07

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")
#> Warning: 'cder_query' is deprecated.
#> Use 'cdec_query' instead.
#> See help("Deprecated")
#> # A tibble: 578 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 NSL       E                   1 RIV STG    2019-08-06 21:30:00
#>  2 NSL       E                   1 RIV STG    2019-08-06 21:45:00
#>  3 NSL       E                   1 RIV STG    2019-08-06 22:00:00
#>  4 NSL       E                   1 RIV STG    2019-08-06 22:15:00
#>  5 NSL       E                   1 RIV STG    2019-08-06 22:30:00
#>  6 NSL       E                   1 RIV STG    2019-08-06 22:45:00
#>  7 NSL       E                   1 RIV STG    2019-08-06 23:00:00
#>  8 NSL       E                   1 RIV STG    2019-08-06 23:15:00
#>  9 NSL       E                   1 RIV STG    2019-08-06 23:30:00
#> 10 NSL       E                   1 RIV STG    2019-08-06 23:45: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)
#> Warning: 'cder_query' is deprecated.
#> Use 'cdec_query' instead.
#> See help("Deprecated")
#> # A tibble: 1,345 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 NSL       E                 100 EL COND    2019-07-24 00:00:00
#>  2 NSL       E                 100 EL COND    2019-07-24 00:15:00
#>  3 NSL       E                 100 EL COND    2019-07-24 00:30:00
#>  4 NSL       E                 100 EL COND    2019-07-24 00:45:00
#>  5 NSL       E                 100 EL COND    2019-07-24 01:00:00
#>  6 NSL       E                 100 EL COND    2019-07-24 01:15:00
#>  7 NSL       E                 100 EL COND    2019-07-24 01:30:00
#>  8 NSL       E                 100 EL COND    2019-07-24 01:45:00
#>  9 NSL       E                 100 EL COND    2019-07-24 02:00:00
#> 10 NSL       E                 100 EL COND    2019-07-24 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)
#> Warning: 'cder_query' is deprecated.
#> Use 'cdec_query' instead.
#> See help("Deprecated")
#> # A tibble: 240 x 9
#>    StationID Duration SensorNumber SensorType DateTime           
#>    <chr>     <chr>           <int> <chr>      <dttm>             
#>  1 HUN       E                   1 RIV STG    2019-08-06 21:30:00
#>  2 HUN       E                   1 RIV STG    2019-08-06 21:45:00
#>  3 HUN       E                   1 RIV STG    2019-08-06 22:00:00
#>  4 HUN       E                   1 RIV STG    2019-08-06 22:15:00
#>  5 HUN       E                   1 RIV STG    2019-08-06 22:30:00
#>  6 HUN       E                   1 RIV STG    2019-08-06 22:45:00
#>  7 HUN       E                   1 RIV STG    2019-08-06 23:00:00
#>  8 HUN       E                   1 RIV STG    2019-08-06 23:15:00
#>  9 HUN       E                   1 RIV STG    2019-08-06 23:30:00
#> 10 HUN       E                   1 RIV STG    2019-08-06 23: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().