From d01f887ae8a5809a057b56b6de0004938395e6f6 Mon Sep 17 00:00:00 2001 From: unknown <ldecicco@usgs.gov> Date: Tue, 28 Oct 2014 17:00:29 -0500 Subject: [PATCH] Adding peak, rating, and meas NWIS functions. --- NAMESPACE | 3 +++ R/importRDB1.r | 6 ++++- R/importWQP.R | 13 +++++----- R/readNWISunit.r | 56 +++++++++++++++++++++++++++++++++++++++++ R/readWQPdata.R | 4 +-- man/constructNWISURL.Rd | 13 +++++++--- man/importWQP.Rd | 9 +++---- man/readNWISmeas.Rd | 22 ++++++++++++++++ man/readNWISpeak.Rd | 22 ++++++++++++++++ man/readNWISrating.Rd | 20 +++++++++++++++ 10 files changed, 150 insertions(+), 18 deletions(-) create mode 100644 man/readNWISmeas.Rd create mode 100644 man/readNWISpeak.Rd create mode 100644 man/readNWISrating.Rd diff --git a/NAMESPACE b/NAMESPACE index d3cb2db5..adb6a318 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,8 +9,11 @@ export(importWaterML2) export(padVariable) export(readNWISdata) export(readNWISdv) +export(readNWISmeas) export(readNWISpCode) +export(readNWISpeak) export(readNWISqw) +export(readNWISrating) export(readNWISsite) export(readNWISunit) export(readWQPdata) diff --git a/R/importRDB1.r b/R/importRDB1.r index 77930c85..2d5cf6c9 100644 --- a/R/importRDB1.r +++ b/R/importRDB1.r @@ -1,3 +1,4 @@ + #' Function to return data from the NWIS RDB 1.0 format #' #' This function accepts a url parameter that already contains the desired @@ -166,7 +167,10 @@ importRDB1 <- function(obs_url,asDateTime=FALSE, qw=FALSE){ } else { for (i in grep('d$', dataType)){ - data[,i] <- as.Date(data[,i]) + if (all(data[,i] != "")){ + data[,i] <- as.Date(data[,i]) + } + } } } diff --git a/R/importWQP.R b/R/importWQP.R index aa42c5c3..7e5d721a 100644 --- a/R/importWQP.R +++ b/R/importWQP.R @@ -15,12 +15,11 @@ #' \dontrun{ #' ## Examples take longer than 5 seconds: #' rawSampleURL <- constructWQPURL('USGS-01594440','01075', '', '') -#' rawSample <- importWQP(rawSampleURL, TRUE) -#' url2 <- paste0("http://www.waterqualitydata.us/Result/search?", -#' "siteid=USGS-01594440&pCode=01075&mimeType=tsv") -#' rawSample2 <- importWQP(url2, FALSE) +#' rawSample <- importWQP(rawSampleURL) +#' url2 <- paste0(rawSampleURL,"&zip=yes") +#' rawSample2 <- importWQP(url2, TRUE) #' } -importWQP <- function(url, zip=TRUE){ +importWQP <- function(url, zip=FALSE){ h <- basicHeaderGatherer() @@ -28,11 +27,11 @@ importWQP <- function(url, zip=TRUE){ if(zip){ headerInfo <- HEAD(url)$headers temp <- tempfile() - origTimeout <- getOption("timeout") +# origTimeout <- getOption("timeout") options(timeout = 120) download.file(url,temp, quiet=TRUE, mode='wb') doc <- unzip(temp) - options(timeout = origTimeout) +# options(timeout = origTimeout) } else { doc <- getURL(url, headerfunction = h$update) headerInfo <- h$value() diff --git a/R/readNWISunit.r b/R/readNWISunit.r index 2f6cf288..c22e434b 100644 --- a/R/readNWISunit.r +++ b/R/readNWISunit.r @@ -35,3 +35,59 @@ readNWISunit <- function (siteNumber,parameterCd,startDate,endDate,format="xml") return (data) } + +#' Reads peak flow data from NWISweb. +#' +#' +#' +#' @param siteNumber string USGS site number. This is usually an 8 digit number +#' @param startDate string starting date for data retrieval in the form YYYY-MM-DD. +#' @param endDate string ending date for data retrieval in the form YYYY-MM-DD. +#' @export +#' @examples +#' siteNumber <- '01594440' +#' data <- readNWISpeak(siteNumber, '','') +readNWISpeak <- function (siteNumber,startDate,endDate){ + + url <- constructNWISURL(siteNumber,NA,startDate,endDate,"peak") + data <- importRDB1(url) + + return (data) +} + +#' Reads the current rating table for an active USGS streamgage. +#' +#' +#' +#' @param siteNumber string USGS site number. This is usually an 8 digit number +#' @param type string can be "base", "corr", or "exsa" +#' @export +#' @examples +#' siteNumber <- '01594440' +#' data <- readNWISrating(siteNumber, "base") +readNWISrating <- function (siteNumber,type){ + + url <- constructNWISURL(siteNumber,service="rating",ratingType = type) + data <- importRDB1(url) + + return (data) +} + +#'Reads surface-water measurement data from NWISweb. +#' +#' +#' +#' @param siteNumber string USGS site number. This is usually an 8 digit number +#' @param startDate string starting date for data retrieval in the form YYYY-MM-DD. +#' @param endDate string ending date for data retrieval in the form YYYY-MM-DD. +#' @export +#' @examples +#' siteNumber <- '01594440' +#' data <- readNWISmeas(siteNumber, '','') +readNWISmeas <- function (siteNumber,startDate,endDate){ + + url <- constructNWISURL(siteNumber,NA,startDate,endDate,"meas") + data <- importRDB1(url) + + return (data) +} diff --git a/R/readWQPdata.R b/R/readWQPdata.R index 3a113a7c..6ef6b17c 100644 --- a/R/readWQPdata.R +++ b/R/readWQPdata.R @@ -34,9 +34,9 @@ readWQPdata <- function(...){ baseURL <- "http://www.waterqualitydata.us/Result/search?" urlCall <- paste0(baseURL, urlCall, - "&mimeType=tsv&zip=yes") + "&mimeType=tsv") - retVal <- importWQP(urlCall,TRUE) + retVal <- importWQP(urlCall,FALSE) return(retVal) } \ No newline at end of file diff --git a/man/constructNWISURL.Rd b/man/constructNWISURL.Rd index 76a9d861..8187563f 100644 --- a/man/constructNWISURL.Rd +++ b/man/constructNWISURL.Rd @@ -3,8 +3,9 @@ \alias{constructNWISURL} \title{Construct NWIS url for data retrieval} \usage{ -constructNWISURL(siteNumber, parameterCd, startDate, endDate, service, - statCd = "00003", format = "xml", expanded = FALSE) +constructNWISURL(siteNumber, parameterCd, startDate = "", endDate = "", + service, statCd = "00003", format = "xml", expanded = FALSE, + ratingType = "base") } \arguments{ \item{siteNumber}{string or vector of strings USGS site number. This is usually an 8 digit number} @@ -15,7 +16,8 @@ constructNWISURL(siteNumber, parameterCd, startDate, endDate, service, \item{endDate}{string ending date for data retrieval in the form YYYY-MM-DD.} -\item{service}{string USGS service to call. Possible values are "dv" (daily values), "uv" (unit/instantaneous values), "qw" (water quality data), "gwlevels" (groundwater),and "wqp" (water quality portal, which can include STORET).} +\item{service}{string USGS service to call. Possible values are "dv" (daily values), "uv" (unit/instantaneous values), +"qw" (water quality data), "gwlevels" (groundwater),and "rating" (rating curve), "peak", "meas" (discrete streamflow measurements).} \item{statCd}{string or vector USGS statistic code only used for daily value service. This is usually 5 digits. Daily mean (00003) is the default.} @@ -24,6 +26,8 @@ but will offer a warning if the file was incomplete (for example, if there was a but the user must carefully check the results to see if the data returns matches what is expected. The default is therefore "xml".} \item{expanded}{logical defaults to FALSE. If TRUE, retrieves additional information, only applicable for qw data.} + +\item{ratingType}{can be "base", "corr", or "exsa". Only applies to rating curve data.} } \value{ url string @@ -47,6 +51,9 @@ url_qw <- constructNWISURL(siteNumber,c('01075','00029','00453'), startDate,endDate,'qw') url_daily_tsv <- constructNWISURL(siteNumber,pCode,startDate,endDate,'dv', statCd=c("00003","00001"),format="tsv") +url_rating <- constructNWISURL(siteNumber,service="rating",ratingType="base") +url_peak <- constructNWISURL(siteNumber, service="peak") +url_meas <- constructNWISURL(siteNumber, service="meas") } } \keyword{USGS} diff --git a/man/importWQP.Rd b/man/importWQP.Rd index d37ea5da..8b9da71e 100644 --- a/man/importWQP.Rd +++ b/man/importWQP.Rd @@ -3,7 +3,7 @@ \alias{importWQP} \title{Basic Water Quality Portal Data grabber} \usage{ -importWQP(url, zip = TRUE) +importWQP(url, zip = FALSE) } \arguments{ \item{url}{string URL to Water Quality Portal#'} @@ -22,10 +22,9 @@ Imports data from the Water Quality Portal based on a specified url. \dontrun{ ## Examples take longer than 5 seconds: rawSampleURL <- constructWQPURL('USGS-01594440','01075', '', '') -rawSample <- importWQP(rawSampleURL, TRUE) -url2 <- paste0("http://www.waterqualitydata.us/Result/search?", -"siteid=USGS-01594440&pCode=01075&mimeType=tsv") -rawSample2 <- importWQP(url2, FALSE) +rawSample <- importWQP(rawSampleURL) +url2 <- paste0(rawSampleURL,"&zip=yes") +rawSample2 <- importWQP(url2, TRUE) } } \keyword{USGS} diff --git a/man/readNWISmeas.Rd b/man/readNWISmeas.Rd new file mode 100644 index 00000000..efac8331 --- /dev/null +++ b/man/readNWISmeas.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{readNWISmeas} +\alias{readNWISmeas} +\title{Reads surface-water measurement data from NWISweb.} +\usage{ +readNWISmeas(siteNumber, startDate, endDate) +} +\arguments{ +\item{siteNumber}{string USGS site number. This is usually an 8 digit number} + +\item{startDate}{string starting date for data retrieval in the form YYYY-MM-DD.} + +\item{endDate}{string ending date for data retrieval in the form YYYY-MM-DD.} +} +\description{ +Reads surface-water measurement data from NWISweb. +} +\examples{ +siteNumber <- '01594440' +data <- readNWISmeas(siteNumber, '','') +} + diff --git a/man/readNWISpeak.Rd b/man/readNWISpeak.Rd new file mode 100644 index 00000000..45c0eceb --- /dev/null +++ b/man/readNWISpeak.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{readNWISpeak} +\alias{readNWISpeak} +\title{Reads peak flow data from NWISweb.} +\usage{ +readNWISpeak(siteNumber, startDate, endDate) +} +\arguments{ +\item{siteNumber}{string USGS site number. This is usually an 8 digit number} + +\item{startDate}{string starting date for data retrieval in the form YYYY-MM-DD.} + +\item{endDate}{string ending date for data retrieval in the form YYYY-MM-DD.} +} +\description{ +Reads peak flow data from NWISweb. +} +\examples{ +siteNumber <- '01594440' +data <- readNWISpeak(siteNumber, '','') +} + diff --git a/man/readNWISrating.Rd b/man/readNWISrating.Rd new file mode 100644 index 00000000..b93d20bf --- /dev/null +++ b/man/readNWISrating.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{readNWISrating} +\alias{readNWISrating} +\title{Reads the current rating table for an active USGS streamgage.} +\usage{ +readNWISrating(siteNumber, type) +} +\arguments{ +\item{siteNumber}{string USGS site number. This is usually an 8 digit number} + +\item{type}{string can be "base", "corr", or "exsa"} +} +\description{ +Reads the current rating table for an active USGS streamgage. +} +\examples{ +siteNumber <- '01594440' +data <- readNWISrating(siteNumber, "base") +} + -- GitLab