From 341078bd4d91998a323d833086de47c599b2a491 Mon Sep 17 00:00:00 2001
From: unknown <ldecicco@usgs.gov>
Date: Tue, 7 Apr 2015 16:26:03 -0500
Subject: [PATCH] Added some logic to deal with incomplete dates. Fixes #106

---
 R/readNWISunit.r    | 20 +++++++++++++++++---
 man/readNWISpeak.Rd |  9 ++++++++-
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/R/readNWISunit.r b/R/readNWISunit.r
index 3c86e1aa..89865596 100644
--- a/R/readNWISunit.r
+++ b/R/readNWISunit.r
@@ -83,13 +83,19 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){
 
 #' 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 startDate character starting date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates
 #' retrieval for the earliest possible record.
 #' @param endDate character ending date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates
 #' 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:
 #' \tabular{lll}{
 #' Name \tab Type \tab Description \cr
@@ -123,14 +129,22 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){
 #' \dontrun{
 #' data <- readNWISpeak(siteNumbers)
 #' }
-readNWISpeak <- function (siteNumbers,startDate="",endDate=""){  
+readNWISpeak <- function (siteNumbers,startDate="",endDate="", asDateTime=TRUE){  
   
   # Doesn't seem to be a peak xml service
   url <- constructNWISURL(siteNumbers,NA,startDate,endDate,"peak")
   
   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)
   
   siteInfo <- readNWISsite(siteNumbers)
diff --git a/man/readNWISpeak.Rd b/man/readNWISpeak.Rd
index 8e01ddb6..3fbc574d 100644
--- a/man/readNWISpeak.Rd
+++ b/man/readNWISpeak.Rd
@@ -4,7 +4,7 @@
 \alias{readNWISpeak}
 \title{Peak flow data from USGS (NWIS)}
 \usage{
-readNWISpeak(siteNumbers, startDate = "", endDate = "")
+readNWISpeak(siteNumbers, startDate = "", endDate = "", asDateTime = TRUE)
 }
 \arguments{
 \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.}
 
 \item{endDate}{character ending date for data retrieval in the form YYYY-MM-DD. Default is "" which indicates
 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{
 A data frame with the following columns:
@@ -45,6 +48,10 @@ siteInfo \tab data.frame \tab A data frame containing information on the request
 }
 \description{
 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{
 siteNumbers <- c('01594440','040851325')
-- 
GitLab