Skip to content

Instantly share code, notes, and snippets.

@jkreft-usgs
Created March 29, 2018 21:37
Show Gist options
  • Save jkreft-usgs/e020e7a4b59cc64333403a6612807788 to your computer and use it in GitHub Desktop.
Save jkreft-usgs/e020e7a4b59cc64333403a6612807788 to your computer and use it in GitHub Desktop.
Data Retrieval Example
library(dataRetrieval)
library(dplyr)
site <- "07144100"
pCode <- "00060"
service <- "uv"
this_week <- readNWISdata(siteNumbers = site,
parameterCd = pCode,
startDate = Sys.Date()-7,
endDate = Sys.Date(),
service = service)
median_data <- readNWISdata(siteNumbers = site,
parameterCd = pCode,
service = "stat")
this_week <- this_week %>%
rename(data_col = !! paste("X",pCode,"00000",sep = "_"))
stat_data <- median_data %>%
mutate(date_this_year = as.POSIXct(as.Date(paste(format(Sys.Date(), "%Y"),month_nu,day_nu, sep = "-")))) %>%
filter(date_this_year >= Sys.Date()-7,
date_this_year <= Sys.Date())
data_limits <- range(c(this_week$data_col, stat_data$p50_va),na.rm = TRUE)
plot(this_week$dateTime, this_week$data_col,
xlab = "", type = "l",
ylab = attr(this_week, "variableInfo")[["variableDescription"]],
main = attr(this_week, "siteInfo")[["station_nm"]],
ylim = data_limits,las = 1)
points(stat_data$date_this_year, stat_data$p50_va,
col = "orange", pch=20, cex = 3)
###################################
# if the stat service doesn't exist:
median_data <- readNWISdata(siteNumbers = site,
parameterCd = pCode,
startDate = "1890-01-01",
service = "dv")
median_by_date <- median_data %>%
mutate(day_of_year = as.integer(format(dateTime, format = "%j"))) %>%
rename(data_col = !! paste("X",pCode,"00003",sep = "_")) %>%
group_by(day_of_year) %>%
summarize(median = median(data_col , na.rm = TRUE)) %>%
mutate(date_this_year = as.POSIXct(strptime(paste(format(Sys.Date(), "%Y"), day_of_year, sep = "-"), format = "%Y-%j"))) %>%
filter(date_this_year >= Sys.Date()-7,
date_this_year <= Sys.Date())
plot(this_week$dateTime, this_week$data_col,
xlab = "", type = "l",
ylab = attr(this_week, "variableInfo")[["variableDescription"]],
main = attr(this_week, "siteInfo")[["station_nm"]],
ylim = data_limits,las = 1)
points(median_by_date$date_this_year, median_by_date$median,
col = "orange", pch=20, cex = 3)
@jkreft-usgs
Copy link
Author

Example
image 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment