Giter VIP home page Giter VIP logo

tidyhydat's Introduction

tidyhydat

Being designed and built, but in the lab. May change, disappear, or be buggy. Travis-CI Build Status

Project Status

This package is under active development. The package is currently undergoing a review by rOpenSci. You can see the progress of this review here: ropensci/software-review#152. As a result, breaking changes to tidyhydat functions will be made. To install the most recent version prior to these changes you can use this command:

devtools::install_github("bcgov/[email protected]")

However, you should strongly consider migrating to the updated tidyhydat once the review process is complete.

What does it do?

Here is a summary of what tidyhydat does:

  • 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

To install the tidyhydat package, you need to install the remotes package then the tidyhydat package

install.packages("remotes")
remotes::install_github("bcgov/tidyhydat")

Then to load the package you need to use the library() function. 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. Even though dplyr is installed alongside tidyhydat, it is helpful to load it by itself as there are many useful functions contained within dplyr. 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 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 takes a long time! To see where HYDAT was saved you can run hy_dir(). Now that you have HYDAT downloaded and ready to go, you are all set to begin some hydrologic analysis.

Usage

Most functions in tidyhydat follow a common argument structure. We will use the hy_daily_flows() function for the following examples though the same approach applies to most functions in the package (See ls("package:tidyhydat") for a list of exported objects). Much of the functionality of tidyhydat originates with the choice of hydrometric stations that you are interested in. A user will often find themselves creating vectors of station numbers. There are several ways to do this.

The simplest case is if you would like to extract only station. You can supply this directly to the station_number argument:

hy_daily_flows(station_number = "08LA001")
#> No start and end dates specified. All dates available will be returned.
#> All station successfully retrieved
#> # A tibble: 29,159 x 5
#>    STATION_NUMBER       Date Parameter Value Symbol
#>             <chr>     <date>     <chr> <dbl>  <chr>
#>  1        08LA001 1914-01-01      FLOW   144   <NA>
#>  2        08LA001 1914-01-02      FLOW   144   <NA>
#>  3        08LA001 1914-01-03      FLOW   144   <NA>
#>  4        08LA001 1914-01-04      FLOW   140   <NA>
#>  5        08LA001 1914-01-05      FLOW   140   <NA>
#>  6        08LA001 1914-01-06      FLOW   136   <NA>
#>  7        08LA001 1914-01-07      FLOW   136   <NA>
#>  8        08LA001 1914-01-08      FLOW   140   <NA>
#>  9        08LA001 1914-01-09      FLOW   140   <NA>
#> 10        08LA001 1914-01-10      FLOW   140   <NA>
#> # ... with 29,149 more rows

Another method is to use hy_stations() to generate your vector which is then given the station_number argument. For example, we could take a subset for only those active stations within Prince Edward Island (Province code: PE) and then create vector for hy_daily_flows():

PEI_stns <- hy_stations() %>%
  filter(HYD_STATUS == "ACTIVE") %>%
  filter(PROV_TERR_STATE_LOC == "PE") %>%
  pull(STATION_NUMBER)
#> All station successfully retrieved

PEI_stns
#> [1] "01CA003" "01CB002" "01CB004" "01CC002" "01CC005" "01CC010" "01CD005"

hy_daily_flows(station_number = PEI_stns)
#> No start and end dates specified. All dates available will be returned.
#> All station successfully retrieved
#> # A tibble: 98,562 x 5
#>    STATION_NUMBER       Date Parameter Value Symbol
#>             <chr>     <date>     <chr> <dbl>  <chr>
#>  1        01CA003 1961-08-01      FLOW    NA   <NA>
#>  2        01CB002 1961-08-01      FLOW    NA   <NA>
#>  3        01CA003 1961-08-02      FLOW    NA   <NA>
#>  4        01CB002 1961-08-02      FLOW    NA   <NA>
#>  5        01CA003 1961-08-03      FLOW    NA   <NA>
#>  6        01CB002 1961-08-03      FLOW    NA   <NA>
#>  7        01CA003 1961-08-04      FLOW    NA   <NA>
#>  8        01CB002 1961-08-04      FLOW    NA   <NA>
#>  9        01CA003 1961-08-05      FLOW    NA   <NA>
#> 10        01CB002 1961-08-05      FLOW    NA   <NA>
#> # ... with 98,552 more rows

We can also merge our station choice and data extraction into one unified pipe which accomplishes a single goal. For example, if for some reason we wanted all the stations in Canada that had the name "Canada" in them we could unify those selection and data extraction processes into a single pipe:

search_stn_name("canada") %>%
  pull(STATION_NUMBER) %>%
  hy_daily_flows()
#> No start and end dates specified. All dates available will be returned.
#> All station successfully retrieved
#> # A tibble: 76,679 x 5
#>    STATION_NUMBER       Date Parameter Value Symbol
#>             <chr>     <date>     <chr> <dbl>  <chr>
#>  1        01AK001 1918-08-01      FLOW    NA   <NA>
#>  2        01AK001 1918-08-02      FLOW    NA   <NA>
#>  3        01AK001 1918-08-03      FLOW    NA   <NA>
#>  4        01AK001 1918-08-04      FLOW    NA   <NA>
#>  5        01AK001 1918-08-05      FLOW    NA   <NA>
#>  6        01AK001 1918-08-06      FLOW    NA   <NA>
#>  7        01AK001 1918-08-07      FLOW  1.78   <NA>
#>  8        01AK001 1918-08-08      FLOW  1.78   <NA>
#>  9        01AK001 1918-08-09      FLOW  1.50   <NA>
#> 10        01AK001 1918-08-10      FLOW  1.78   <NA>
#> # ... with 76,669 more rows

These example illustrate a few ways that an vector can be generated and supplied to functions within tidyhydat.

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 = "08LG006")

Another option is to provide simply the province as an argument and download all stations from that province:

realtime_dd(prov_terr_state_loc = "PE")

Additionally download_realtime_ws() provides another means of acquiring real time data though that requires a username and password from Environment and Climate Change Canada.

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:

citation("tidyhydat")
#> Warning in citation("tidyhydat"): no date field in DESCRIPTION file of
#> package 'tidyhydat'
#> Warning in citation("tidyhydat"): could not determine year for 'tidyhydat'
#> from package DESCRIPTION file
#> 
#> To cite package 'tidyhydat' in publications use:
#> 
#>   Sam Albers (NA). tidyhydat: Extract and Tidy Canadian
#>   Hydrometric Data. R package version 0.3.0.
#>   https://github.com/bcgov/tidyhydat
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {tidyhydat: Extract and Tidy Canadian Hydrometric Data},
#>     author = {Sam Albers},
#>     note = {R package version 0.3.0},
#>     url = {https://github.com/bcgov/tidyhydat},
#>   }

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

http://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.

tidyhydat's People

Contributors

ateucher avatar boshek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.