First look: USGS Stage/Discharge Gages

Overview: Download Data using the USGS Data Retrieval (R Package )

USGS Package Overview

GitHub Repo

CRAN

In [1]:
require(dataRetrieval)
require(curl)
library(repr)
library(maps)
library(ggplot2)
library(leaflet)
#library(skimr)
library(htmlwidgets)
options(repr.matrix.max.cols = 40)
Loading required package: dataRetrieval
Loading required package: curl

Select states of interest & search for available USGS gages

  • Pull metadata from stations with flow & stage records
  • Plot
In [2]:
# Select states to refine search 
states <- c("TX")

#--- Assign parameter of interest
stageCode <- "00065"
flowCode <- "00060"
#bBox not working directly
#stage_sites <- whatNWISsites(bBox= bbox, parameterCd=stageCode)

stage_sites <- data.frame()
flow_sites <- data.frame()

for(state in states){
    print(state)
    tmp <- whatNWISsites(stateCd=state ,parameterCd=stageCode)
    stage_sites <- rbind(tmp, stage_sites)

    tmp <- whatNWISsites(stateCd=state ,parameterCd=flowCode)
    flow_sites <- rbind(tmp, flow_sites)   
}

stage_sites$gtype = stageCode
flow_sites$gtype = flowCode

map('state', region = c('texas'))
#map('state', region = c('mississippi'))
title(main="USGS Gauges found in the NWIS")
points(x=flow_sites$dec_long_va, 
       y=flow_sites$dec_lat_va,
      col='black')

points(x=stage_sites$dec_long_va, 
       y=stage_sites$dec_lat_va,
      col='blue')

head(flow_sites)
[1] "TX"
agency_cdsite_nostation_nmsite_tp_cddec_lat_vadec_long_vacolocatedqueryTimegtype
USGS 08364000 RIO GRANDE AT EL PASO, TX ST 31.80288 -106.5408 FALSE 2019-02-11 13:59:30 00060
USGS 08365600 McKelligon Canyon at El Paso, TX ST 31.82233 -106.4697 FALSE 2019-02-11 13:59:30 00060
USGS 08365800 Government Ditch at El Paso, TX ST-DCH 31.78400 -106.4453 FALSE 2019-02-11 13:59:30 00060
USGS 08366400 Riverside Canal nr Socorro, TX ST-CA 31.65845 -106.3261 FALSE 2019-02-11 13:59:30 00060
USGS 08368900 Hudspeth Feeder Canal nr Tornillo, TX ST-CA 31.40845 -106.0914 FALSE 2019-02-11 13:59:30 00060
USGS 08368000 Tornillo Drain at mouth nr Tornillo, TXST 31.40040 -106.0266 FALSE 2019-02-11 13:59:30 00060

Refine gage list using bounding box

  • Select gages within a refined areas of interest
  • Filter out gages with limited data
In [3]:
# Bounding box
bbox <- c(29.15, -96.00, 30.5, -94.5)

df <- rbind(stage_sites, flow_sites)
df <- df[df$dec_lat_va <= bbox[3] & df$dec_lat_va >= bbox[1],]
df <- df[df$dec_long_va >= bbox[2] & df$dec_long_va <= bbox[4],]

s <- df[df['gtype'] == stageCode,]
q <- df[df['gtype'] == flowCode,]


head(q, 5)
agency_cdsite_nostation_nmsite_tp_cddec_lat_vadec_long_vacolocatedqueryTimegtype
1695USGS 08117500 San Bernard Rv nr Boling, TX ST 29.31358 -95.89384 FALSE 2019-02-11 13:59:30 00060
1697USGS 08112500 Brazos Rv Authority Canal A nr Fulshear, TX ST-CA 29.64135 -95.88523 FALSE 2019-02-11 13:59:30 00060
1698USGS 08117600 San Bernard Rv nr New Gulf, TX ST 29.26608 -95.87829 FALSE 2019-02-11 13:59:30 00060
1701USGS 08067700 Caney Ck nr Dobbin, TX ST 30.35382 -95.80995 FALSE 2019-02-11 13:59:30 00060
1702USGS 08068720 Cypress Ck at Katy-Hockley Rd nr Hockley, TXST 29.95022 -95.80828 FALSE 2019-02-11 13:59:30 00060

Filter by # of records available

In [4]:
nrecords = 10000
s_meta <- whatNWISdata(siteNumber=q$site_no, parameterCd=stageCode)
q_meta <- whatNWISdata(siteNumber=q$site_no, parameterCd=flowCode)

mysdata <- s_meta[s_meta['count_nu'] > nrecords,]
myqdata <- q_meta[q_meta['count_nu'] > nrecords,]
head(myqdata)
agency_cdsite_nostation_nmsite_tp_cddec_lat_vadec_long_vacoord_acy_cddec_coord_datum_cdalt_vaalt_acy_vaalt_datum_cdhuc_cddata_type_cdparm_cdstat_cdts_idloc_web_dsmedium_grp_cdparm_grp_cdsrs_idaccess_cdbegin_dateend_datecount_nu
227USGS 08066300 Menard Ck nr Rye, TX ST 30.48139 -94.77972 S NAD83 62.32 .1 NGVD29 12030202 dv 00060 00003 133978 NA wat NA 1645423 0 1965-12-01 2019-02-10 19430
300USGS 08066500 Trinity Rv at Romayor, TXST 30.42521 -94.85076 F NAD83 25.92 .1 NGVD29 12030202 dv 00060 00003 133980 NA wat NA 1645423 0 1924-05-01 2019-02-10 34619
1067USGS 08066500 Trinity Rv at Romayor, TXST 30.42521 -94.85076 F NAD83 25.92 .1 NGVD29 12030202 uv 00060 NA 140189 NA wat NA 1645423 0 1988-10-01 2019-02-11 11090
1105USGS 08067000 Trinity Rv at Liberty, TXST 30.05772 -94.81826 F NAD83 -2.22 .5 NGVD29 12030203 dv 00060 00003 133986 NA wat NA 1645423 0 1940-11-25 2019-02-10 10237
1298USGS 08067000 Trinity Rv at Liberty, TXST 30.05772 -94.81826 F NAD83 -2.22 .5 NGVD29 12030203 uv 00060 NA 140193 NA wat NA 1645423 0 1991-01-05 2019-02-11 10264
1303USGS 08067070 CWA Canal nr Dayton, TX ST-CA 29.96133 -94.81020 F NAD83 50 5 NGVD29 12030203 dv 00060 00003 133988 Upstream of Flume wat NA 1645423 0 1981-04-08 2019-02-10 13817

Let's look at data by location

In [5]:
pal <- colorFactor(c("black","blue"), domain = c("00060", "00065"))

m <- leaflet(
    
     data=na.omit(df), height=500, width=1000) %>%  
     addTiles('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') %>%
     addCircleMarkers(~dec_long_va,~dec_lat_va, color = ~pal(gtype),
                      radius=5, stroke=FALSE,fillOpacity = 0.9, opacity = 0.8,
                    popup=~c(station_nm)) %>%
     addLegend("topright", pal = pal, values = ~gtype,
                title = "USGS Gages in Study Area",
                labFormat = labelFormat(prefix = "")
      )

head(df,2)
saveWidget(m, 'HoustonGages.html')
m
agency_cdsite_nostation_nmsite_tp_cddec_lat_vadec_long_vacolocatedqueryTimegtype
627USGS 08117500 San Bernard Rv nr Boling, TX ST 29.31358 -95.89384 FALSE 2019-02-11 13:59:29 00065
630USGS 08068700 Cypress Ck at Sharp Rd nr Hockley, TXST 29.92106 -95.84023 FALSE 2019-02-11 13:59:29 00065

Click HERE if points don't plot

Isolate Bayous

In [10]:
regex <- 'Bayou'

idx <- grep(regex,q_meta$station_nm)
dc_q_gages <- q_meta[idx,]#$station_nm

idx <- grep(regex,s_meta$station_nm)
dc_s_gages <- s_meta[idx,]#$station_nm

m2 <-   (leaflet(data=dc_s_gages, height=500, width=1000) %>%  
        addTiles('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') %>%
        addCircleMarkers(~dec_long_va,~dec_lat_va, color = "red",
                          radius=8, stroke=FALSE, fillOpacity = 0.9, 
                          opacity = 0.8, popup=~c(site_no)) )

head(dc_q_gages,3)
saveWidget(m2, 'BayouGages.html')
m2
agency_cdsite_nostation_nmsite_tp_cddec_lat_vadec_long_vacoord_acy_cddec_coord_datum_cdalt_vaalt_acy_vaalt_datum_cdhuc_cddata_type_cdparm_cdstat_cdts_idloc_web_dsmedium_grp_cdparm_grp_cdsrs_idaccess_cdbegin_dateend_datecount_nu
4USGS 08042550 W Fk Double Bayou nr Anahuac, TX ST 29.76102 -94.63344 S NAD83 10 2 NAVD88 12040202 qw 00060 NA 0 NA wat PHY 1645423 0 1967-12-15 1972-08-31 25
220USGS 08042558 W Fk Double Bayou at Eagle Ferry Rd nr Anahuac, TXST 29.67556 -94.66611 S NAD83 3 2.5 NAVD88 12040202 uv 00060 NA 139733 NA wat NA 1645423 0 2016-02-12 2019-02-11 1095
1310USGS 08067240 Cotton Bayou nr Cove, TX ST 29.80606 -94.83798 F NAD83 12 20 NAVD88 12030203 qw 00060 NA 0 NA wat PHY 1645423 0 1968-03-25 1968-09-17 5

Click HERE if points don't plot

Pull Metadata

In [7]:
s_meta <- whatNWISdata(siteNumber=s$site_no, parameterCd=stageCode)
q_meta <- whatNWISdata(siteNumber=q$site_no, parameterCd=flowCode)

options(repr.plot.width=7, repr.plot.height=2)# Set plotsize
qplot(s_meta$begin_date, geom="histogram", binwidth = 50)  # Plot stage counts by starting year
qplot(q_meta$begin_date, geom="histogram", binwidth = 75) # Plot flow counts by starting year

Download Flow Data & Plot

In [8]:
sites <- q$site_no
print(sites)
siteNo <- sites[3]

gage_data <- readNWISdv(siteNumbers=siteNo, parameterCd=stageCode, startDate = "1920-01-01", endDate = "2018-01-01")
gage_data <- renameNWISColumns(gage_data)
head(gage_data,2)
  [1] "08117500" "08112500" "08117600" "08067700" "08068720" "08072300"
  [7] "08115000" "08113500" "08117800" "08067690" "08117700" "08115500"
 [13] "08114000" "08116400" "08068740" "08117850" "08116500" "08116000"
 [19] "08072680" "08068780" "08072700" "08072730" "08068750" "08072760"
 [25] "08068275" "08072600" "08073100" "08073500" "08068305" "08068800"
 [31] "08074780" "08074760" "08116650" "08067900" "08067920" "08074800"
 [37] "08067610" "08073600" "08068325" "08067650" "08068310" "08073630"
 [43] "08114500" "08074810" "08073700" "08075780" "08068900" "08068390"
 [49] "08074150" "08074900" "08068400" "08074020" "08068450" "08079000"
 [55] "08074250" "08068000" "08075400" "08068500" "08069000" "08075900"
 [61] "08075000" "08074000" "08068520" "08074500" "08074550" "08074540"
 [67] "08078700" "08075100" "08075110" "08068090" "08076500" "08078400"
 [73] "08075760" "08069200" "08078000" "08075763" "08076000" "08070500"
 [79] "08076997" "08075500" "08077000" "08075720" "08075770" "08069500"
 [85] "08075650" "08076180" "08076700" "08075605" "08075730" "08075740"
 [91] "08077540" "08077900" "08077600" "08077800" "08077640" "08071000"
 [97] "08076800" "08076900" "08072000" "08071500" "08077850" "08070200"
[103] "08076850" "08077620" "08070000" "08072050" "08071280" "08077680"
[109] "08067520" "08071200" "08067525" "08067500" "08066800" "08066500"
[115] "08067240" "08067000" "08067070" "08066300" "08067252" "08067285"
[121] "08042558" "08067270" "08042550"
In [9]:
siteNo <- sites[15]

gage_data <- readNWISdv(siteNumbers=siteNo, parameterCd=flowCode, startDate = "1920-01-01", endDate = "2018-01-01")
gage_data <- renameNWISColumns(gage_data)
head(gage_data,2)

parameterInfo <- attr(gage_data, "variableInfo")
siteInfo <- attr(gage_data, "siteInfo")

options(repr.plot.width=8, repr.plot.height=3)
ts <- ggplot(data = gage_data,
             aes(Date, Flow)) +
      geom_line()+
      xlab(siteInfo$station_nm) +
      ylab(parameterInfo$parameter_desc) +
      ggtitle(siteNo)
ts 
#skim(gage_data) %>% skimr::kable()
agency_cdsite_noDateFlowFlow_cd
USGS 08068740 1975-06-01750 A
USGS 08068740 1975-06-02500 A