Evan Kwityn
Menu

All Active USGS Gages in the Delaware River Basin Utilizing dataRetrevial Package in R

11/8/2019

0 Comments

 
Deriving all present, active, and historic USGS gage data across multiple state boundaries can be achieved by utilizing a sub-region approach filtering through HUC8 conditions. 
R Source Code: All Active USGS Gages in the Delaware River Basin 
#The following code should be run in its own Rstudio Project Folder
library(dataRetrieval)
library(tidyverse)
library(openxlsx)
Huc<-read.xlsx("USGSSiteNoDRB_Baseflow.xlsx", sheet = 2) . #data found at https://water.usgs.gov/GIS/huc_name.html

#Filtering out all USGS gages by Huc8 level throughout the Delaware River Basin
for (j in 1:nrow(Huc)){
  sqw_mindailyT <- readNWISdata(huc=Huc$Cataloging.Units[j])
  form=sprintf('SQW/sqw_mindailyT.%s.csv', Huc$Cataloging.Units[j])
  write.csv(sqw_mindailyT, file = form,  row.names = FALSE ) }

##Scrubbing through each Huc8 output and inputting them into one master list
FilePath<-c('SQW/')
files <- list.files(path = FilePath, pattern = "*.csv", full.names = T)
tbl <- sapply(files, read_csv, simplify=FALSE) %>% bind_rows(.id = "id")

idsplit<-strsplit(tbl$id, ".", fixed=TRUE)
mat  <- matrix(unlist(idsplit), ncol=3, byrow=TRUE)
df   <- as.data.frame(mat)
df   <- cbind(df, tbl)
colnames(df) <- c("Directory","HUC","FileExt",paste0(colnames(tbl)))
df
write.csv(df,"SQW/Output/df.csv", row.names = FALSE)

##Reading site_no input for all USGS gages pulled from the HUC8 level and obtaining
#dec_lat_va
#dec_long_va
#site_tp_cd
#begin_date/end_date

datalist = list() #creating blank data list
for (j in 1:nrow(df)){
  dailyDataAvailable <- readNWISsite(siteNumbers=df$site_no[j])
  print(dailyDataAvailable)
datalist[[j]]<- dailyDataAvailable    }

big_data = do.call('rbind', datalist)
big_data<-distinct(big_data,site_no, .keep_all = TRUE) #Removing duplicate records
write.csv(big_data,"SQW/Output/USGSBigDataDRB.csv", row.names = FALSE)
usgssitenodrb_baseflow.xlsx
File Size: 36 kb
File Type: xlsx
Download File

0 Comments



Leave a Reply.

    Archives

    February 2020
    December 2019
    November 2019
    October 2018
    December 2017
    November 2017
    May 2017
    August 2016
    March 2016

    Categories

    All

    RSS Feed

Picture
@ 2016 Evan Kwityn. All Rights Reserved
  • Home
  • Blog
  • Gallery
  • Contact
  • Home
  • Blog
  • Gallery
  • Contact