Chapter 9 Demo: dataRetrieval and StreamStats
- This is a demo of some things we can do with dataRetrieval and StreamStats.
9.3 We can look for sites based on state, county, huc, site number, etc.
stream_gauges <- whatNWISsites(parameterCd = param_cd, service = service_cd, stateCd = state_cd, countyCd = county_cd, startDate = start, endDate = end)
stream_gauges_info <- whatNWISdata(parameterCd = param_cd, service = service_cd, stateCd = state_cd, countyCd = county_cd, startDate = start, endDate = end) %>%
filter(begin_date < start, end_date > end)
huc_cd <- "10020008" # This is our local hydrologic unit code.
huc_gauges <- whatNWISsites(parameterCd = param_cd, service = service_cd, huc = huc_cd, startDate = start, endDate = end)
9.4 We can use dataRetrieval to look for water quality data in the Water Quality Portal (WQP) and we can search for groundwater data.
#wq_sites <- whatWQPsites(huc = huc_cd, startDate = start, endDate = end, characteristicName = "Nitrate")
#wq_sites <- whatWQPsites(paste0("USGS-", site = "06043500"), startDate = start, endDate = end, characteristicName = "Nitrate")
#gw_sites <- whatNWISdata(parameterCd = "72019", stateCd = state_cd, countyCd = county_cd)
9.5 StreamStats
StreamStats isn’t available through the Comprehensive R Archive Network (CRAN), so we have to install from GitHub using devtools. Streamstats also requires the sf library.
#install.packages("devtools")
#devvtool::install_github("markwh/streamstats")
#install.packages("sf")
#library(sf)
#library(streamstats)
Here are some links to documentation about StreamStats:
https://github.com/markwh/streamstats
https://streamstats.usgs.gov/docs/streamstatsservices/#/
https://rdrr.io/github/markwh/streamstats/man/
https://www.usgs.gov/streamstats
And here is a link to a Stack Overflow about writing a function to delineate many watersheds using the StreamStats package.
9.5.1 Delineate one watershed in StreamStats
Here I am just getting the lat and long for the Gallatin Gateway USGS gauge. I only want one row of data so I’ve put in a parameter code and a service code. If I didn’t do that it would give me lots of rows (1 for every type of measurement made at this gauge).
gallatin_gauge_loc <- whatNWISdata(site = "06043500", parameterCd = "00060", service = "dv")
gall_gauge <- whatNWISdata(site = "06043500")
gall_x <- gallatin_gauge_loc$dec_long_va
gall_y <- gallatin_gauge_loc$dec_lat_va
The delineateWatershed function will do the work.
#gallatin_gauge_map <- delineateWatershed(xlocation = gall_x, ylocation = gall_y, crs = 4326, includeparameters = "true", includeflowtypes = "true")
#leafletWatershed(gallatin_gauge_map)
Other statistics can be found using the computeChars (for watershed characteristics such as basin area and land-use) and computeFlowStats (for statistics such as flow percentiles)
#chars <- computeChars(workspaceID = gallatin_gauge_map$workspaceID, rcode = "MT")
# chars is a list so we can make that easier to work with by grabbing some information from that list and putting it into a tibble (i.e., a dataframe)
#basin_chars <- chars$parameters %>%
#as_tibble()
# stats gives us flow statistics for the site.
#stats <- computeFlowStats(workspaceID = gallatin_gauge_map$workspaceID, rcode = "MT", simplify = TRUE)