Package: tidyhydat 0.7.1

Sam Albers

tidyhydat: Extract and Tidy Canadian 'Hydrometric' Data

Provides functions to access historical and real-time national 'hydrometric' data from Water Survey of Canada data sources (<https://dd.weather.gc.ca/hydrometric/csv/> and <https://collaboration.cmc.ec.gc.ca/cmc/hydrometrics/www/>) and then applies tidy data principles.

Authors:Sam Albers [aut, cre], David Hutchinson [ctb], Dewey Dunnington [ctb], Ryan Whaley [ctb], Province of British Columbia [cph], Government of Canada [dtc], Luke Winslow [rev], Laura DeCicco [rev]

tidyhydat_0.7.1.tar.gz
tidyhydat_0.7.1.zip(r-4.5)tidyhydat_0.7.1.zip(r-4.4)tidyhydat_0.7.1.zip(r-4.3)
tidyhydat_0.7.1.tgz(r-4.5-any)tidyhydat_0.7.1.tgz(r-4.4-any)tidyhydat_0.7.1.tgz(r-4.3-any)
tidyhydat_0.7.1.tar.gz(r-4.5-noble)tidyhydat_0.7.1.tar.gz(r-4.4-noble)
tidyhydat_0.7.1.tgz(r-4.4-emscripten)tidyhydat_0.7.1.tgz(r-4.3-emscripten)
tidyhydat.pdf |tidyhydat.html
tidyhydat/json (API)
NEWS

# Install 'tidyhydat' in R:
install.packages('tidyhydat', repos = c('https://ropensci.r-universe.dev', 'https://cloud.r-project.org'))

Reviews:rOpenSci Software Review #152

Bug tracker:https://github.com/ropensci/tidyhydat/issues9 issues

Pkgdown site:https://docs.ropensci.org

Datasets:

On CRAN:tidyhydat-0.7.1(2025-03-11)

Conda:

citzgovernment-datahydrologyhydrometricstidy-datawater-resources

9.59 score 71 stars 3 packages 202 scripts 838 downloads 54 exports 47 dependencies

Last updated 11 days agofrom:ab0dd61c10 (on main). Checks:4 OK, 4 NOTE, 1 ERROR. Indexed: yes.

TargetResultLatest binary
Doc / VignettesOKMar 11 2025
R-4.5-winOKMar 11 2025
R-4.5-macOKMar 11 2025
R-4.5-linuxOKMar 11 2025
R-4.4-winNOTEMar 11 2025
R-4.4-macNOTEMar 11 2025
R-4.4-linuxNOTEMar 11 2025
R-4.3-winNOTEMar 11 2025
R-4.3-macERRORMar 11 2025

Exports:download_hydatenexprenquoensymexprexprshy_agency_listhy_annual_instant_peakshy_annual_statshy_dailyhy_daily_flowshy_daily_levelshy_datum_listhy_default_dbhy_dirhy_downloaded_dbhy_monthly_flowshy_monthly_levelshy_plothy_reg_office_listhy_remotehy_sed_daily_loadshy_sed_daily_susconhy_sed_monthly_loadshy_sed_monthly_susconhy_sed_sampleshy_sed_samples_psdhy_set_default_dbhy_srchy_src_disconnecthy_stationshy_stn_data_collhy_stn_data_rangehy_stn_datum_convhy_stn_datum_unrelatedhy_stn_op_schedulehy_stn_regulationhy_stn_remarkshy_test_dbhy_versionpull_station_numberquoquo_namequosrealtime_add_local_datetimerealtime_daily_meanrealtime_ddrealtime_plotrealtime_stationsrealtime_wssearch_stn_namesearch_stn_numbersymsyms

Dependencies:askpassbitbit64blobcachemclicliprcpp11crayoncurlDBIdbplyrdplyrfansifastmapgenericsgluehmshttr2lifecyclelubridatemagrittrmemoiseopensslpillarpkgconfigplogrprettyunitsprogresspurrrR6rappdirsreadrrlangRSQLitestringistringrsystibbletidyrtidyselecttimechangetzdbutf8vctrsvroomwithr

Stepping into the HYDAT Database

Rendered fromtidyhydat_hydat_db.Rmdusingknitr::rmarkdownon Mar 11 2025.

Last update: 2025-03-11
Started: 2018-01-17

tidyhydat: An Introduction

Rendered fromtidyhydat_an_introduction.Rmdusingknitr::rmarkdownon Mar 11 2025.

Last update: 2025-03-11
Started: 2017-11-06

Two examples of using tidyhydat

Rendered fromtidyhydat_example_analysis.Rmdusingknitr::rmarkdownon Mar 11 2025.

Last update: 2025-03-11
Started: 2017-11-06

Citation

Albers S (2017). “tidyhydat: Extract and Tidy Canadian Hydrometric Data.” The Journal of Open Source Software, 2(20). doi:10.21105/joss.00511, http://dx.doi.org/10.21105/joss.00511.

Corresponding BibTeX entry:

  @Article{,
    title = {tidyhydat: Extract and Tidy Canadian Hydrometric Data},
    author = {Sam Albers},
    doi = {10.21105/joss.00511},
    url = {http://dx.doi.org/10.21105/joss.00511},
    year = {2017},
    publisher = {The Open Journal},
    volume = {2},
    number = {20},
    journal = {The Journal of Open Source Software},
  }

Readme and manuals

tidyhydat

What does tidyhydat do?tidyhydat

  • Provides functions (hy_*) that access hydrometric data from the HYDAT database, a national archive of Canadian hydrometric data and return tidy data.
  • Provides functions (realtime_*) that access Environment and Climate Change Canada’s real-time hydrometric data source.
  • Provides functions (search_*) that can search through the approximately 7000 stations in the database and aid in generating station vectors
  • Keep functions as simple as possible. For example, for daily flows, the hy_daily_flows() function queries the database, tidies the data and returns a tibble of daily flows.

Installation

You can install tidyhydat from CRAN:

install.packages("tidyhydat")

To install the development version of the tidyhydat package, you can install directly from the rOpenSci development server:

install.packages("tidyhydat", repos = "https://dev.ropensci.org")

Usage

More documentation on tidyhydat can found at the rOpenSci doc page: https://docs.ropensci.org/tidyhydat/

When you install tidyhydat, several other packages will be installed as well. One of those packages, dplyr, is useful for data manipulations and is used regularly here. To use actually use dplyr in a session you must explicitly load it. A helpful dplyr tutorial can be found here.

library(tidyhydat)
library(dplyr)
HYDAT download

To use many of the functions in the tidyhydat package you will need to download a version of the HYDAT database, Environment and Climate Change Canada’s database of historical hydrometric data then tell R where to find the database. Conveniently tidyhydat does all this for you via:

download_hydat()

This downloads (with your permission) the most recent version of HYDAT and then saves it in a location on your computer where tidyhydat’s function will look for it. Do be patient though as this can take a long time! To see where HYDAT was saved you can run hy_default_db(). Now that you have HYDAT downloaded and ready to go, you are all set to begin looking at Canadian hydrometric data.

Real-time

To download real-time data using the datamart we can use approximately the same conventions discussed above. Using realtime_dd() we can easily select specific stations by supplying a station of interest:

realtime_dd(station_number = "08MF005")
#>   Queried on: 2025-03-11 19:53:39.321743 (UTC)
#>   Date range: 2025-02-09 to 2025-03-11 
#> # A tibble: 17,520 × 8
#>    STATION_NUMBER PROV_TERR_STATE_LOC Date                Parameter Value Grade
#>    <chr>          <chr>               <dttm>              <chr>     <dbl> <chr>
#>  1 08MF005        BC                  2025-02-09 08:00:00 Flow        612 <NA> 
#>  2 08MF005        BC                  2025-02-09 08:05:00 Flow        612 <NA> 
#>  3 08MF005        BC                  2025-02-09 08:10:00 Flow        613 <NA> 
#>  4 08MF005        BC                  2025-02-09 08:15:00 Flow        611 <NA> 
#>  5 08MF005        BC                  2025-02-09 08:20:00 Flow        611 <NA> 
#>  6 08MF005        BC                  2025-02-09 08:25:00 Flow        611 <NA> 
#>  7 08MF005        BC                  2025-02-09 08:30:00 Flow        610 <NA> 
#>  8 08MF005        BC                  2025-02-09 08:35:00 Flow        611 <NA> 
#>  9 08MF005        BC                  2025-02-09 08:40:00 Flow        611 <NA> 
#> 10 08MF005        BC                  2025-02-09 08:45:00 Flow        611 <NA> 
#> # ℹ 17,510 more rows
#> # ℹ 2 more variables: Symbol <chr>, Code <chr>

Or we can use realtime_ws:

realtime_ws(
  station_number = "08MF005",
  parameters = c(46, 5), ## see param_id for a list of codes
  start_date = Sys.Date() - 14,
  end_date = Sys.Date()
)
#> All station successfully retrieved
#> All parameters successfully retrieved
#> # A tibble: 4,602 × 12
#>    STATION_NUMBER Date                Name_En  Value Unit  Grade Symbol Approval
#>    <chr>          <dttm>              <chr>    <dbl> <chr> <lgl> <chr>  <chr>   
#>  1 08MF005        2025-02-25 00:00:00 Water t…  4.46 °C    NA    <NA>   Provisi…
#>  2 08MF005        2025-02-25 01:00:00 Water t…  4.48 °C    NA    <NA>   Provisi…
#>  3 08MF005        2025-02-25 02:00:00 Water t…  4.49 °C    NA    <NA>   Provisi…
#>  4 08MF005        2025-02-25 03:00:00 Water t…  4.48 °C    NA    <NA>   Provisi…
#>  5 08MF005        2025-02-25 04:00:00 Water t…  4.5  °C    NA    <NA>   Provisi…
#>  6 08MF005        2025-02-25 05:00:00 Water t…  4.51 °C    NA    <NA>   Provisi…
#>  7 08MF005        2025-02-25 06:00:00 Water t…  4.52 °C    NA    <NA>   Provisi…
#>  8 08MF005        2025-02-25 07:00:00 Water t…  4.52 °C    NA    <NA>   Provisi…
#>  9 08MF005        2025-02-25 08:00:00 Water t…  4.55 °C    NA    <NA>   Provisi…
#> 10 08MF005        2025-02-25 09:00:00 Water t…  4.54 °C    NA    <NA>   Provisi…
#> # ℹ 4,592 more rows
#> # ℹ 4 more variables: Parameter <dbl>, Code <chr>, Qualifier <chr>,
#> #   Qualifiers <lgl>

Compare realtime_ws and realtime_dd

tidyhydat provides two methods to download realtime data. realtime_dd() provides a function to import .csv files from here. realtime_ws() is an client for a web service hosted by ECCC. realtime_ws() has several difference to realtime_dd(). These include:

  • Speed: The realtime_ws() is much faster for larger queries (i.e. many stations). For single station queries to realtime_dd() is more appropriate.
  • Length of record: realtime_ws() records goes back further in time.
  • Type of parameters: realtime_dd() are restricted to river flow (either flow and level) data. In contrast realtime_ws() can download several different parameters depending on what is available for that station. See data("param_id") for a list and explanation of the parameters.
  • Date/Time filtering: realtime_ws() provides argument to select a date range. Selecting a data range with realtime_dd() is not possible until after all files have been downloaded.
Plotting

Plot methods are also provided to quickly visualize realtime data:

realtime_ex <- realtime_dd(station_number = "08MF005")

plot(realtime_ex)

and also historical data:

hy_ex <- hy_daily_flows(station_number = "08MF005", start_date = "2013-01-01")

plot(hy_ex)

Getting Help or Reporting an Issue

To report bugs/issues/feature requests, please file an issue.

These are very welcome!

How to Contribute

If you would like to contribute to the package, please see our CONTRIBUTING guidelines.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Citation

Get citation information for tidyhydat in R by running:

To cite package 'tidyhydat' in publications use:

  Albers S (2017). "tidyhydat: Extract and Tidy Canadian Hydrometric
  Data." _The Journal of Open Source Software_, *2*(20).
  doi:10.21105/joss.00511 <https://doi.org/10.21105/joss.00511>,
  <http://dx.doi.org/10.21105/joss.00511>.

A BibTeX entry for LaTeX users is

  @Article{,
    title = {tidyhydat: Extract and Tidy Canadian Hydrometric Data},
    author = {Sam Albers},
    doi = {10.21105/joss.00511},
    url = {http://dx.doi.org/10.21105/joss.00511},
    year = {2017},
    publisher = {The Open Journal},
    volume = {2},
    number = {20},
    journal = {The Journal of Open Source Software},
  }

ropensci_footer

License

Copyright 2017 Province of British Columbia

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Help Manual

Help pageTopics
All Canadian stationsallstations
Download and set the path to HYDATdownload_hydat
hy_agency_list functionhy_agency_list
Extract annual max/min instantaneous flows and water levels from HYDAT databasehy_annual_instant_peaks
Extract annual statistics information from the HYDAT databasehy_annual_stats
Extract all daily water level and flow measurementshy_daily
Extract daily flows information from the HYDAT databasehy_daily_flows
Extract daily levels information from the HYDAT databasehy_daily_levels
DATA SYMBOLS look-up tablehy_data_symbols
DATA TYPES look-up tablehy_data_types
Extract datum list from HYDAT databasehy_datum_list
Output OS-independent path to the HYDAT sqlite databasehy_dir
Extract monthly flows information from the HYDAT databasehy_monthly_flows
Extract monthly levels information from the HYDAT databasehy_monthly_levels
This function is deprecated in favour of generic plot methodshy_plot
Extract regional office list from HYDAT databasehy_reg_office_list
Get the version date of HYDAT that is current on the ECCC websitehy_remote
Extract daily sediment load information from the HYDAT databasehy_sed_daily_loads
Extract daily suspended sediment concentration information from the HYDAT databasehy_sed_daily_suscon
Extract monthly flows information from the HYDAT databasehy_sed_monthly_loads
Extract monthly flows information from the HYDAT databasehy_sed_monthly_suscon
Extract instantaneous sediment sample information from the HYDAT databasehy_sed_samples
Extract instantaneous sediment sample particle size distribution information from the HYDAT databasehy_sed_samples_psd
Set the default database pathhy_set_default_db
Open a connection to the HYDAT databasehy_src hy_src_disconnect
Extract station information from the HYDAT databasehy_stations
Extract station data collection from HYDAT databasehy_stn_data_coll
Extract station data range from HYDAT databasehy_stn_data_range
Extract station datum conversions from HYDAT databasehy_stn_datum_conv
Extract station datum unrelated from HYDAT databasehy_stn_datum_unrelated
Extract station operation schedule from HYDAT databasehy_stn_op_schedule
Extract station regulation from the HYDAT databasehy_stn_regulation
Extract station remarks from HYDAT databasehy_stn_remarks
Get the location of the HYDAT databasehy_default_db hy_downloaded_db hy_test_db
Extract version number from HYDAT databasehy_version
Parameter IDparam_id
Plot historical and realtime dataplot plot.hy plot.realtime
Convenience function to pull station number from tidyhydat functionspull_station_number
Add local datetime column to realtime tibblerealtime_add_local_datetime
Calculate daily means from higher resolution realtime datarealtime_daily_mean
Download a tibble of realtime river data from the last 30 days from the Meteorological Service of Canada datamartrealtime_dd
Convenience function to plot realtime datarealtime_plot
Download a tibble of active realtime stationsrealtime_stations
Download realtime data from the ECCC web servicerealtime_ws
A search function for hydrometric station name or numbersearch_stn_name search_stn_number