knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) library(magrittr) library(ggplot2) library(ggrepel) library(kableExtra) library(tidyverse) library(AOI) library(nhdplusTools)
knitr::opts_chunk$set(dev.args = list(png = list(type = "cairo")),fig.width = 10, fig.height = 5, warning = FALSE, message = FALSE, error = FALSE) thematic::thematic_rmd(font = 'Montserrat') ggplot2::theme_set(ggplot2::theme_bw(base_size = 16)) library(wildlandhydRo) library(microbenchmark) library(dataRetrieval) library(tidyverse)
The goal of wildlandhydRo is to create wrapper functions around commonly used packages like streamstats, snotelr and dataRetrieval and prepare them for chunk-based parallelism, data munging and reporting. Basically, a package for me but worth sharing with others.
As of 2-24-2022 the lfstat package is not available on CRAN. You'll need to download yourself, sorry for the inconvenience! I'll be working on taking that out and implementing into the package.
You can install the development version from GitHub with:
``` {r, eval = F}
install.packages("lmomRFA") install.packages("lfstat", repos="http://R-Forge.R-project.org")
install.packages('devtools') devtools::install_github("joshualerickson/wildlandhydRo")
## Examples Below are some common examples that I routinely use with the package. ### Parallel With {wildlandhydRo} it is really easy to get station data in parallel. The functions use the [{furrr}](https://github.com/DavisVaughan/furrr) framework and will work by planning a session prior to calling the function. Below is a quick benchmark between the options; regular (np) or parallel (para). ```r library(wildlandhydRo) library(microbenchmark) library(dataRetrieval) sites <- dataRetrieval::whatNWISsites(stateCd='MT', parameterCd = '00060') %>% filter(nchar(site_no) <= 8) tm <- microbenchmark( 'np' = { batch_USGSdv(sites = sites[1:50,]$site_no, parallel = FALSE)}, 'para' = { future::plan('multisession') batch_USGSdv(sites = sites[1:50,]$site_no, parallel = TRUE)}, times = 10 ) tm autoplot(tm)
knitr::include_graphics('man/figures/para_vs_np.png')
You can do this for most functions in the package, e.g. batch_*(), wym*(), ym*(), get_Basin()
, etc.
With the package you can get monthly, annual and monthly-annual statistics for a snotel or usgs sites. You can provide a previously create batch_*()
data.frame or the station id. This creates/mungs the results of the daily values to generate mean, maximum, median and standard deviation per water year or per water year per month. It also includes peaks from dataRetrieval::readNWISpeak(); annual base-flow (tp = 0.9, window = 5) and Base-flow Index (BFI) (total-flow/base-flow) from baseflow; annual coefficient of variance (sd of flow/mean of flow); and normalization methods Flow/drainage area, Flow/(all time mean flow) and Flow/log(standard deviation). The window for all the methods are annual, e.g. 1. This leaves it up to the user to explore different windows if inclined. Monthly stats
water_year <- wyUSGS(sites = '12304500') ggplot(water_year, aes(peak_dt, Peak)) + geom_line() + labs(title = 'Peak Flow per Water Year', subtitle = paste0(water_year %>% slice(n=1) %>% .$Station)) monthly <- wymUSGS(sites = '12304500') ggplot(monthly, aes(year_month, coef_var)) + geom_line() + labs(title = 'Monthly Coefficient of Variance', subtitle = paste0(monthly %>% slice(n=1) %>% .$Station))
You can also just generate percentiles for daily values or months.
yaak_dv <- batch_USGSdv(sites = '12304500') usgs_rep <- reportUSGSdv(procDV = yaak_dv, days = 60) plot_reportUSGS(usgs_rep)
usgs_rep_month <- reportUSGSmv(procDV = yaak_dv) plot_reportUSGS(usgs_rep_month %>% filter(year_nu >2020), time = 'month')
Sometimes you just want to get the drainage area above a point and then do some other things. With get_Basin()
function you just have to provide a sf point and it will return the basin. If you want zonal stats associated with that drainage area then just use get_BasinStats()
.
pt <- tibble(lon = -114.36, lat = 48.92) pt <- sf::st_as_sf(pt, coords = c('lon', 'lat')) basin <- get_Basin(pt) ggplot() + geom_sf(data = basin)
basin_stats <- get_BasinStats(basin) basin_stats %>% sf::st_drop_geometry() %>% pivot_longer(cols = c('TOT_PET', 'TOT_BFI', 'TOT_TWI')) %>% select(name, value, comid) %>% right_join(basin_stats, by ='comid') %>% sf::st_as_sf() %>% ggplot() + geom_sf(aes(fill = value)) + facet_wrap(~name, nrow = 3) + theme_void()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.