Skip to content
Snippets Groups Projects
Commit 341078bd authored by Laura A DeCicco's avatar Laura A DeCicco
Browse files

Added some logic to deal with incomplete dates.

Fixes #106
parent 73d9321e
No related branches found
No related tags found
1 merge request!114Time zone code and bug fixes.
...@@ -83,13 +83,19 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){ ...@@ -83,13 +83,19 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){
#' Peak flow data from USGS (NWIS) #' Peak flow data from USGS (NWIS)
#' #'
#' Reads peak flow from NWISweb. Data is retrieved from \url{http://waterdata.usgs.gov/nwis}. #' Reads peak flow from NWISweb. Data is retrieved from \url{http://waterdata.usgs.gov/nwis}.
#' In some cases, the specific date of the peak data is not know. This function will default to
#' converting the complete dates, dropping rows with incomplete dates. If those incomplete dates are
#' needed, set the `asDateTime` argument to FALSE. No rows will be removed, and no dates will be converted
#' to R Date objects.
#' #'
#' @param siteNumbers character USGS site number(or multiple sites). This is usually an 8 digit number. #' @param siteNumbers character USGS site number(or multiple sites). This is usually an 8 digit number.
#' @param startDate character starting date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates #' @param startDate character starting date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates
#' retrieval for the earliest possible record. #' retrieval for the earliest possible record.
#' @param endDate character ending date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates #' @param endDate character ending date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates
#' retrieval for the latest possible record. #' retrieval for the latest possible record.
#' @param asDateTime logical default to \code{TRUE}. When \code{TRUE}, the peak_dt column is converted
#' to a Date object, and incomplete dates are removed. When \code{FALSE}, no columns are removed, but no dates are converted.
#' @return A data frame with the following columns: #' @return A data frame with the following columns:
#' \tabular{lll}{ #' \tabular{lll}{
#' Name \tab Type \tab Description \cr #' Name \tab Type \tab Description \cr
...@@ -123,14 +129,22 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){ ...@@ -123,14 +129,22 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){
#' \dontrun{ #' \dontrun{
#' data <- readNWISpeak(siteNumbers) #' data <- readNWISpeak(siteNumbers)
#' } #' }
readNWISpeak <- function (siteNumbers,startDate="",endDate=""){ readNWISpeak <- function (siteNumbers,startDate="",endDate="", asDateTime=TRUE){
# Doesn't seem to be a peak xml service # Doesn't seem to be a peak xml service
url <- constructNWISURL(siteNumbers,NA,startDate,endDate,"peak") url <- constructNWISURL(siteNumbers,NA,startDate,endDate,"peak")
data <- importRDB1(url, asDateTime=FALSE) data <- importRDB1(url, asDateTime=FALSE)
data$peak_dt <- as.Date(data$peak_dt) if(asDateTime){
badDates <- which(grepl("[0-9]*-[0-9]*-00",data$peak_dt))
data <- data[-badDates,]
if(length(badDates) > 0){
warning(length(badDates), " rows were thrown out due to incomplete dates")
}
data$peak_dt <- as.Date(data$peak_dt)
}
data$gage_ht <- as.numeric(data$gage_ht) data$gage_ht <- as.numeric(data$gage_ht)
siteInfo <- readNWISsite(siteNumbers) siteInfo <- readNWISsite(siteNumbers)
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
\alias{readNWISpeak} \alias{readNWISpeak}
\title{Peak flow data from USGS (NWIS)} \title{Peak flow data from USGS (NWIS)}
\usage{ \usage{
readNWISpeak(siteNumbers, startDate = "", endDate = "") readNWISpeak(siteNumbers, startDate = "", endDate = "", asDateTime = TRUE)
} }
\arguments{ \arguments{
\item{siteNumbers}{character USGS site number(or multiple sites). This is usually an 8 digit number.} \item{siteNumbers}{character USGS site number(or multiple sites). This is usually an 8 digit number.}
...@@ -14,6 +14,9 @@ retrieval for the earliest possible record.} ...@@ -14,6 +14,9 @@ retrieval for the earliest possible record.}
\item{endDate}{character ending date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates \item{endDate}{character ending date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates
retrieval for the latest possible record.} retrieval for the latest possible record.}
\item{asDateTime}{logical default to \code{TRUE}. When \code{TRUE}, the peak_dt column is converted
to a Date object, and incomplete dates are removed. When \code{FALSE}, no columns are removed, but no dates are converted.}
} }
\value{ \value{
A data frame with the following columns: A data frame with the following columns:
...@@ -45,6 +48,10 @@ siteInfo \tab data.frame \tab A data frame containing information on the request ...@@ -45,6 +48,10 @@ siteInfo \tab data.frame \tab A data frame containing information on the request
} }
\description{ \description{
Reads peak flow from NWISweb. Data is retrieved from \url{http://waterdata.usgs.gov/nwis}. Reads peak flow from NWISweb. Data is retrieved from \url{http://waterdata.usgs.gov/nwis}.
In some cases, the specific date of the peak data is not know. This function will default to
converting the complete dates, dropping rows with incomplete dates. If those incomplete dates are
needed, set the `asDateTime` argument to FALSE. No rows will be removed, and no dates will be converted
to R Date objects.
} }
\examples{ \examples{
siteNumbers <- c('01594440','040851325') siteNumbers <- c('01594440','040851325')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment