Skip to content

A community-contributed tool box for managing, analyzing, and visualizing WRF Hydro (and HydroDART) input and output files in R.

License

Notifications You must be signed in to change notification settings

TimLahmers/rwrfhydro

 
 

Repository files navigation

[![Travis-CI Build Status](https://travis-ci.org/mccreigh/rwrfhydro.png?branch=master)](https://travis-ci.org/mccreigh/rwrfhydro)

# rwrfhydro
## A CRAN-like overview

<h4 style="color: rgb(40%, 40%, 40%); font-family: monospace; font-size: large;">rwrfhydro: R tools for the WRF Hydro Model</h4>
<p>A community-contributed tool box for managing, analyzing, and visualizing WRF Hydro input and output files in R. See the <a href="https://github.com/mccreigh/rwrfhydro/blob/master/README.Rmd">github repository README file</a> for more information. </p>
<table summary="Package rwrfhydro summary">
<tr>
<td style="vertical-align: top;">Version:</td>
<td>1.0.0.9000</td>
</tr>
<tr>
<td style="vertical-align: top;">Depends:</td>
<td>R (&ge; 3.1.0), stats, methods</td>
</tr>
<tr>
<td style="vertical-align: top;">Imports:</td>
<td> plyr (>= 1.8.1), grid (>= 3.1.2), lubridate (>= 1.3.3), ncdf4
        (>= 1.13), ggplot2 (>= 1.0.0), ggmap (>= 2.3), reshape2 (>=
        1.4.1), doMC (>= 1.3.3), foreach (>= 1.4.2), curl (>= 0.5),
        dataRetrieval (>= 2.1.2), raster (>= 2.3), httr (>= 0.6.1),
        devtools (>= 1.7.0), jsonlite (>= 0.9.14)
</td>
</tr>
<tr>
<td style="vertical-align: top;">Suggests:</td>
<td>testthat, knitr, rgdal, pander, ptw</td>
</tr>
<tr>
<td style="vertical-align: top;">Published:</td>
<td>2015-05-01</td>
</tr>
<tr>
<td style="vertical-align: top;">Author:</td>
<td>James McCreight [aut, cre],
Aubrey Dugger[aut]</td>
</tr>
<tr>
<td style="vertical-align: top;">Maintainer:</td>
<td>James McCreight</td>
</tr>
<tr>
<td style="vertical-align: top;">BugReports:</td>
<td><a href="https://github.com/mccreigh/rwrfhydro/issues">https://github.com/mccreigh/rwrfhydro/issues</a></td>
</tr>
<tr>
<td style="vertical-align: top;">License:</td>
<td><a href="https://github.com/mccreigh/rwrfhydro/blob/master/LICENSE.md">Terms of use.</a></td>
</tr>
<tr>
<td style="vertical-align: top;">URL:</td>
<td><a href="https://github.com/mccreigh/rwrfhydro">https://github.com/mccreigh/rwrfhydro</a>, <a href="http://www.ral.ucar.edu/projects/wrf_hydro/download.php">http://www.ral.ucar.edu/projects/wrf_hydro/download.php</a></td>
</tr>
<tr>
<td style="vertical-align: top;">NeedsCompilation:</td>
<td>no</td>
</tr>
<tr>
<td style="vertical-align: top;">Citation:</td>
<td><em>McCreight, J.L., Dugger, A., Gochis, D.J., 2015: The rwrfhydro R package. Available
at http://www.ral.ucar.edu/projects/wrf_hydro/rwrfhydro.php.</em>
</a> </td>
</tr>
<tr>
<td style="vertical-align: top;">Materials:</td>
<td><a href="https://github.com/mccreigh/rwrfhydro/blob/master/README.Rmd">README</a> <a href="https://github.com/mccreigh/rwrfhydro/blob/master/NEWS.Rmd">NEWS</a> </td>
</tr>
</table>


<h4 style="color: rgb(40%, 40%, 40%); font-family: monospace;">Downloads:</h4>
<table summary="Package rwrfydro downloads", width="80%">
  
<table width="100%">
  <col style="width: 6%;" />
  <col style="width: 94%;" />
  <td style="vertical-align: top;"> Package&nbsp;source: </td>
  <td>
    Git repositry available on github at <a href="https://github.com/mccreigh/rwrfhydro/">https://github.com/mccreigh/rwrfhydro.</a> <br />
    See the <a href="https://github.com/mccreigh/rwrfhydro/blob/master/README.Rmd#developing-and-bug-reports">  README </a> to contribute to the source code.
  </td>
</table>
</tr>

<table width="100%">
  <col style="width: 6%;" />
  <col style="width: 94%;" />
  <td style="vertical-align: top;"> Installation: </td>
  <td>
    <a href="https://github.com/mccreigh/rwrfhydro/blob/master/README.Rmd#installing"> See installation instructions. </a>
  </td>
</table>
</tr>

  
<tr>
<table width="100%">
  <col style="width: 6%;" />
  <col style="width: 94%;" />
  <td style="vertical-align: top;"> Reference&nbsp;manual: </td>
  <td>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/rwrfhydro-manual.pdf">rwrfhydro.pdf </a>
  </td>
</table>
</tr>

<tr>
<table width="100%">
  <col style="width: 6%;" />
  <col style="width: 94%;" />
  <td style="vertical-align: top;">Vignettes:</td>
  
  <td style="text-align: left; vertical-align: top;">
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/overview.html">An overview of rwrfhydro functionality</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/streamflowEval.html">Streamflow Evaluation</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/domainChannelVis.html">WRF Hydro Domain and Channel Visualization</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/getMultiNetcdf.html">Get data from multiple netcdf files</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/usgsObsDb.html">Collect USGS stream observations and build a local database</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/waterBudget.html">Water Budget</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/evapotranspirationEval.html">ET Evaluation</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/streamflowEvalMulti.html">Streamflow Evaluation - Multi-Basin</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/modisProcessing.html">MODIS Processing</a> <br>
    <a href="http://www.ral.ucar.edu/projects/wrf_hydro/images/v3.0/rwrfhydro/vignettes/snodas.html">Collect the SNODAS product and build a local database</a> <br>
  </td>

</table>
</tr>

</div>



# Purpose
A community-contributed tool box for managing, analyzing, and visualizing WRF Hydro (and HydroDART) input and output files in R.

Intentionally, “rwrfhydro” can be read as “our wrf hydro”. The purpose of this R package is to focus community development of 
tools for working with and analyzing data related to the WRF Hydro model. These tools are both free and open-source, just like
R, which should help make them accessible and popular. For users new to R, several introductory resources are listed below. 

The purposes of this README are 1) to get you started using rwrfhydro and 2) to explain the basics (and then some) of how we 
develop the package so you can quickly start adding your contributions. 

Table of Contents
=================
  * [rwrfhydro](#rwrfhydro)
    * [A CRAN-like overview](#a-cran-like-overview)
        * [rwrfhydro: R tools for the WRF Hydro Model](#rwrfhydro-r-tools-for-the-wrf-hydro-model)
        * [Downloads:](#downloads)
  * [Purpose](#purpose)
  * [Table of Contents](#table-of-contents)
  * [Installing](#installing)
  * [Using](#using)
  * [Developing and bug reports](#developing-and-bug-reports)
    * [Version control for collaboration: Github](#version-control-for-collaboration-github)
      * [Forking and cloning](#forking-and-cloning)
      * [devBranch and pull requests](#devbranch-and-pull-requests)
      * [Not using Github.](#not-using-github)
      * [Travis-CI and `R CMD check`](#travis-ci-and-r-cmd-check)
    * [Workflow: git, R Packaging, and you](#workflow-git-r-packaging-and-you)
    * [Our best practices](#our-best-practices)
      * [R package best practices and code style](#r-package-best-practices-and-code-style)
        * [Organizing functions](#organizing-functions)
        * [R code style](#r-code-style)
        * [Packages are NOT scripts](#packages-are-not-scripts)
        * [Documentation with roxygen](#documentation-with-roxygen)
      * [Objects in rwrfhydro](#objects-in-rwrfhydro)
      * [Graphics](#graphics)
  * [R Package development resources](#r-package-development-resources)
  * [Introductory R resources (somewhat random)](#introductory-r-resources-somewhat-random)

# Installing
Installing the development package (not on [CRAN](http://cran.r-project.org/)) is facilitated by the devtools package (on CRAN),
so devtools is installed in this process. The following is done for the initial install or to update the rwrfhydro package.
``` R
install.packages("devtools")
devtools::install_github("mccreigh/rwrfhydro")
```
The very first time this is run, it can take a while to install all the package dependencies listed as "Imports" in the
[DESCRIPTION](https://github.com/mccreigh/rwrfhydro/blob/master/DESCRIPTION) file. 

To check for updates once rwrfhydro is loaded run `CheckForUpdates()`. 

To install other branches than master and perhaps from your own fork:
``` R
devtools::install_github("username/rwrfhydro", ref='myBranch')
```

# Using
After the one-time install or any subsequent re-installs/updates, simply load the rwrfhydro library in an R session:
```
library(rwrfhydro)
```
and now the package (namespace) is available. 

[*Online vignettes*](https://github.com/mccreigh/rwrfhydro/blob/master/vignettes.Rmd) (or in R `browseVignettes("rwrfhydro")`) are probably the easiest way to get in-depth, thematic overviews of rwrfhydro functionality. 

To get package metadata and a listing of functions:  `library(help=rwrfhydro)`. Just the functions: `ls('package:rwrfhydro')`.
For specific functionality see function help (e.g. `?VisualizeDomain` or `help(VisualizeDomain)`). 

# Developing and bug reports

Bugs are to be reported [here](https://github.com/mccreigh/rwrfhydro/issues). If you want to solve your bugs and get them back into the code, please continue reading about developing. 

There are three main aspects of developing the code base and contributing:

* Version control for collaboration: Again, not terribly interesting but incredibly useful. For those new to the Git/Github process, it can be a bit daunting. Please contact us for some help, we do want to get your useful code into the main repository!

* R Packaging: Not very interesting, but critical for creating the extrememly useful nature of R packages. Fortunately, the `devtools` package simplifies life tremendously and so figures prominently in the development process we sketch below. The main details have been sorted out, contributing new functions is generally fairly easy. 

* Our best practices: This ranges from fundamental to fussy. 

All of these topics (minus some of our specific best practices) are treated by [Hadley Wickham’s book on R Packages](http://r-pkgs.had.co.nz/). Specific sections of this book are linked below. Further resources on R package development are listed below. 

## Version control for collaboration: Github

Instead of going straight to developing, we recommend that you install rwrfhydro using `devtools::install_github('mccreigh/rwrfhydro')` first, because this streamlines the installation of package dependencies. Note that `devtools::install_github('mccreigh/rwrfhydro')` installs rwrfhydro into you default library path (`.libPaths()[1]`) and that the source code is not included. 

The very best way to obtain and edit the source is to "fork" rwrfhydro on github and clone the repository to your machine(s) of choice. You edit your fork and, when it's ready, you submit a pull request to get the changes back to the main (upstream) fork of rwrfhydro. More details are provided below. Your cloned git repository is not in your default R library path (`.libPaths()[1]`), but somewhere else where you choose to keep your development code. However, devtools allows you to build your development package into your library path. This means that after you add some code locally, you can `library(rwrfhydro)` from other R sessions on that machine with your changes appearing in the package. The basic use of devtools is outlined below. It greatly stream lines all aspects of developing R packages and is highly recommended. Particularly, it make is easyt to go from github to an R package.

### Forking and cloning
Please fork the repository to contribute. A fork is a separate copy of the main repository. [Forking is trivial in Github](https://help.github.com/articles/fork-a-repo/). You have to have a free (for open-source repositories) account to fork on github.

Then you'll clone the fork to a local computer and you'll have to interact between your foked github version and the 'master' fork. You are free to merge changes on the master into your fork at any time. Keep your fork sync'd as much as possible to avoid painful merges. Getting changes back to the master fork requires a pull request on github. We give some tips below. 

### devBranch and pull requests
We maintain two main branches: master and devBranch. You should never work on the master branch. All changes have to pass through the devBranch before going in to the master and this is controlled by the package maintainer. Therefore, devBranch is where your [pull requests](https://help.github.com/articles/using-pull-requests/) will go. Other barnches on your fork are up to you, How you get your code into your fork's devBranch is your choice. The easiest way is to `git checkout devBranch` and edit that. Some more details on using git are provided in the [workflow](#workflow-git-r-packaging-and-you) overview below.

### Not using Github.
This may be possible for small amounts of codes over long periods of time. It is really only the "hub" part of github which may give pause, interfacing a git repo with github is described here and maybe an alternative if necessary: http://stackoverflow.com/questions/4728432/git-forking-without-github

### Travis-CI and `R CMD check`
The rwrfhydro repo is configured to build on a third-party virtual linux machine with every push or pull request to the master or devBranch branches. This service is known as Travis-CI (continuous integration). This means your pull requests are automatically checked by `R CMD check`, this keeps errors from creeping into the upstream code. There are a variety of hurdles to getting code to build on Travis-CI, including installing requisite system and R packages, which can be challenging but worth it for the debugging provided by automated builds in conjunction with `R CMD check`.

`R CMD check` accepts a variety of arguments. Ultimately, it 1) checks the source for consistency including across platforms (Windows, OSX, linux), 2) runs all specified code tests, essentially regression tests, 3) runs all the examples provided in the documentation, and 4) builds all the vignettes. Currently, we are skipping vignette building until we can streamline several of these. 

You can configure your own fork to build on Travis-CI and you can push frequently to check for errors. This is nearly identical to (slightly more stringent than) running `devtools::check()`, but all you have to do is push your commits. 

## Workflow: git, R Packaging, and you

Workflow is approximately this:
  
* Fork project on github
* Clone the github fork to your local machine(s)
* Checkout the devBranch (`git checkout devBranch`)

* Development cycles:
  * Optional: Create a topic branch off of devBranch in git (e.g. `git checkout topicBranch`) and push this to github (`git push origin topicBranch`). 
  * Write code (in these dirs: R/, NAMESPACE, src/, data/, inst/).
  * Write documentation (in these dirs: man/, vignettes/).
  * Write tests (in this dir: test/).
  * Document and check with devtools: `devtools::document(); devtools::check_doc(); devtools::check()`
  * Commit to your branch with git. (`git commit -am 'Some cool features were needed.'`)
  * Push to github (`git push origin branch`). If Travis-CI is configured, this triggers an `R CMD check` on Travis. 

To get code back to the main reposiory/fork:  
  * [If on topicBranch: merge it into devBranch.]
  * Merge your devBranch with the "upstream" devBranch, that is in the master fork: [See here.](https://help.github.com/articles/syncing-a-fork/) **Note** pushing back to github at the very bottom of that page.
  * [If on topicBranch: You probably want to mege devBranch into topicBranch.]
  * Submit a [pull request on github](https://help.github.com/articles/using-pull-requests/) on devBranch. 


## Our best practices

### R package best practices and code style
[http://r-pkgs.had.co.nz/r.html](http://r-pkgs.had.co.nz/r.html)

#### Organizing functions
[http://r-pkgs.had.co.nz/r.html#r-organising](http://r-pkgs.had.co.nz/r.html#r-organising)
* Do NOT put all functions in a single file, nor each in their own file. Functions should be grouped by files and may occasionally need moved to new or different files as new functions are written.
* File names end in .R and are all lowercase with _ used to separate words. (All lowercase (except the .R) helps ensure compatibility with Windows developers.)

#### R code style
[http://r-pkgs.had.co.nz/r.html#style](http://r-pkgs.had.co.nz/r.html#style)
* Generally follow Google’s R style guide with preference for variableName (first-lower camel case) over variable.name (period distinction). Note that functions are first-upper camel case, e.g. FunctionName.  [https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml](https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml)
* Variables are nouns. Functions are verbs.
* Lots of other style considerations to learn: indents, braces, line length, assignment, comment style.

#### Packages are NOT scripts
[http://r-pkgs.had.co.nz/r.html#r-differences](http://r-pkgs.had.co.nz/r.html#r-differences)
* Don’t use library() or require(). Use the DESCRIPTION to specify your package’s requirements.
* Use package::function() to use function from external packages. Make sure the package and version are listed in DESCRIPTION.
* Never use source() to load code from a file. Rely on devtools::load_all() to automatically source all files in R/.
* Don’t modify global options() or graphics par(). Put state changing operations in functions that the user can call when they want.
* Don’t save files to disk with write(), write.csv(), or saveRDS(). Use data/ to cache important data files.

#### Documentation with roxygen
[http://r-pkgs.had.co.nz/man.html](http://r-pkgs.had.co.nz/man.html)
Once you get used to this, you will love writing documentation as you go for your R functions. 

* Roxygen comments start with #' and come before a function. All the roxygen lines preceding a function are called ablock. Each line should be wrapped in the same way as your code, normally at 80 characters.
* Blocks are broken up into tags, which look like @tagName details. The content of a tag extends from the end of the tag name to the start of the next tag (or the end of the block). Because @ has a special meaning in roxygen, you need to write @@ if you want to add a literal @ to the documentation (this is mostly important for email addresses and for accessing slots of S4 objects).
* Each block includes some text before the first tag. This is called the introduction, and is parsed specially:
* The first sentence becomes the title of the documentation. That’s what you see when you look at help(package = mypackage) and is shown at the top of each help file. It should fit on one line, be written in sentence case, and end in a full stop.
* The second paragraph is the description: this comes first in the documentation and should briefly describe what the function does.
* The third and subsequent paragraphs go into the details: this is a (often long) section that is shown after the argument description and should go into detail about how the function works.
* All objects must have a title and description. Details are optional.

### Objects in rwrfhydro
We will probably need to develop some s3 classes or reuse some from other packages.
List of possible objects:
gaugePts object for organizing "frxst points", both locations and data.

### Graphics
We need to resolve if we are going to use base graphics or ggplot or both. 
I’m leaning towards both. Not all plotting routines have to always be available for a given function, but I think that both will probably develop over time. 

Because ggplot has a big learning curve, we can return closures which 1) provide tweakability for basic things to be adjusted in the plot make the plot when called, 2) which return the basic ggplot object which can then also be extended with ggplot commands. I made an example of this in VisualizeDomain.R for ggmap/ggplot objects.

# R Package development resources
* [http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/](http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/)
* [http://r-pkgs.had.co.nz/](http://r-pkgs.had.co.nz/)
* [http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf](http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf)
* [http://portal.stats.ox.ac.uk/userdata/ruth/APTS2012/Rcourse10.pdf](http://portal.stats.ox.ac.uk/userdata/ruth/APTS2012/Rcourse10.pdf)

# Introductory R resources (somewhat random)
* [My introduction to R: multiple resources but, sorry, the video link is broken.] (https://nex.nasa.gov/nex/resources/118/) 
* [My R cheat sheet (also availabile in LaTex inthe above link).] (https://nex.nasa.gov/nex/static/media/other/R-refcard_2.pdf)
* [The popular YouTube serires on R by Roger Peng.] (https://www.youtube.com/user/rdpeng)
* [https://www.datacamp.com/courses/free-introduction-to-r] (https://www.datacamp.com/courses/free-introduction-to-r)
* [http://cran.r-project.org/doc/contrib/Torfs+Brauer-Short-R-Intro.pdf] (http://cran.r-project.org/doc/contrib/Torfs+Brauer-Short-R-Intro.pdf)
* [http://cran.r-project.org/doc/manuals/R-intro.pdf] (http://cran.r-project.org/doc/manuals/R-intro.pdf)

About

A community-contributed tool box for managing, analyzing, and visualizing WRF Hydro (and HydroDART) input and output files in R.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • R 100.0%