From a6c4e77c4f0f2b10ccaa2386f835e99c001beae9 Mon Sep 17 00:00:00 2001
From: unknown <ldecicco@usgs.gov>
Date: Wed, 1 Apr 2015 15:44:56 -0500
Subject: [PATCH] Added help file info about start and end dates.

Fixes #101
---
 R/readNWISdata.r                         | 72 +++++++++++++++++++++---
 R/readNWISdv.r                           |  4 +-
 R/readNWISqw.r                           |  4 +-
 R/readNWISunit.r                         | 20 ++++++-
 R/readWQPqw.r                            |  4 +-
 man/readNWISdata.Rd                      | 28 +++++++--
 man/readNWISdv.Rd                        |  4 +-
 man/readNWISqw.Rd                        |  4 +-
 man/readNWISuv.Rd                        | 14 ++++-
 man/readWQPqw.Rd                         |  4 +-
 tests/testthat/tests_userFriendly_fxns.R |  6 +-
 11 files changed, 130 insertions(+), 34 deletions(-)

diff --git a/R/readNWISdata.r b/R/readNWISdata.r
index 9439b854..b4c04d5e 100644
--- a/R/readNWISdata.r
+++ b/R/readNWISdata.r
@@ -2,6 +2,7 @@
 #'
 #' Returns data from the NWIS web service.
 #' Arguments to the function should be based on \url{http://waterservices.usgs.gov} service calls.
+#' See examples below for ideas of constructing queries.
 #'
 #' @param service string. Possible values are "iv" (for instantaneous), "dv" (for daily values), "gwlevels" 
 #' (for groundwater levels), "site" (for site service), and "qw" (water-quality). Note: "qw" calls go to: 
@@ -13,9 +14,12 @@
 #' Name \tab Type \tab Description \cr
 #' agency \tab character \tab The NWIS code for the agency reporting the data\cr
 #' site \tab character \tab The USGS site number \cr
-#' datetime \tab POSIXct \tab The date and time of the value converted to UTC (for unit value data), \cr 
-#' \tab character \tab or raw character string \cr
-#' tz_cd \tab character \tab The time zone code for datetime \cr
+#' dateTime \tab POSIXct \tab The date and time (if applicable) of the measurement, 
+#'           converted to UTC for unit value data. R only allows one time zone attribute per column. For unit data 
+#'           spanning a time zone change, converting the data to UTC solves this problem. For daily data,
+#'           the time zone attribute is the time zone of the first returned measurement.
+#'            \cr
+#' tz_cd \tab character \tab The time zone code for dateTime column\cr
 #' code \tab character \tab Any codes that qualify the corresponding value\cr
 #' value \tab numeric \tab The numeric value for the parameter \cr
 #' }
@@ -41,11 +45,21 @@
 #' \dontrun{
 #' # Examples not run for time considerations
 #' dataTemp <- readNWISdata(stateCd="OH",parameterCd="00010")
-#' dataTempUnit <- readNWISdata(sites="03086500", service="iv", parameterCd="00010")
+#' instFlow <- readNWISdata(sites="05114000", service="iv", 
+#'                    parameterCd="00060", 
+#'                    startDate="2014-05-01T00:00Z",endDate="2014-05-01T12:00Z")
+#'                    
+#' instFlowCDT <- readNWISdata(sites="05114000", service="iv", 
+#'                    parameterCd="00060", 
+#'                    startDate="2014-05-01T00:00",endDate="2014-05-01T12:00",
+#'                    tz="America/Chicago")
+#'
 #' #Empty:
-#' multiSite <- readNWISdata(sites=c("04025000","04072150"), service="iv", parameterCd="00010")
+#' multiSite <- readNWISdata(sites=c("04025000","04072150"), service="iv", 
+#'                            parameterCd="00010")
 #' #Not empty:
-#' multiSite <- readNWISdata(sites=c("04025500","040263491"), service="iv", parameterCd="00060")
+#' multiSite <- readNWISdata(sites=c("04025500","040263491"), 
+#'                            service="iv", parameterCd="00060")
 #' bBoxEx <- readNWISdata(bBox=c(-83,36.5,-81,38.5), parameterCd="00010")
 #' 
 #' startDate <- as.Date("2013-10-01")
@@ -113,6 +127,21 @@ readNWISdata <- function(service="dv", ...){
     
   }
   
+  if("tz" %in% names(values)){
+    tz <- values["tz"]
+    if(tz != ""){
+      rTZ <- c("America/New_York","America/Chicago",
+               "America/Denver","America/Los_Angeles",
+               "America/Anchorage","America/Honolulu",
+               "America/Jamaica","America/Managua",
+               "America/Phoenix","America/Metlakatla")
+      tz <- match.arg(tz, rTZ)
+    }
+    values <- values[!(names(values) %in% "tz")]
+  } else {
+    tz <- ""
+  }
+  
   if(service == "site"){
     format <- "rdb"
   }
@@ -124,21 +153,44 @@ readNWISdata <- function(service="dv", ...){
   
   if(service == "site"){
     possibleError <- tryCatch({
-      retval <- importRDB1(urlCall, asDateTime = FALSE, qw = FALSE)
+      retval <- importRDB1(urlCall, asDateTime = FALSE, qw = FALSE, tz = tz)
     }, error = function(e) {
       stop(e, "with url:", urlCall)
     })
     
   } else if(service != "qwdata") {
     possibleError <- tryCatch({
-      retval <- importWaterML1(urlCall, asDateTime = ("iv" == service))
+      retval <- importWaterML1(urlCall, asDateTime = ("iv" == service), tz= tz)
     }, error = function(e) {
       stop(e, "with url:", urlCall)
     })
         
     if("dv" == service){
-      retval$dateTime <- as.POSIXct(retval$dateTime)
+      
+      tzLib <- setNames(c("America/New_York","America/New_York",
+                                  "America/Chicago","America/Chicago",
+                                  "America/Denver","America/Denver",
+                                  "America/Los_Angeles","America/Los_Angeles",
+                                  "America/Anchorage","America/Anchorage",
+                                  "America/Honolulu","America/Honolulu"),
+                                c("EST","EDT",
+                                  "CST","CDT",
+                                  "MST","MDT",
+                                  "PST","PDT",
+                                  "AKST","AKDT",
+                                  "HAST","HST"))
+      #TODO: Think about dates that cross a time zone boundary.
+      retval$dateTime <- as.POSIXct(retval$dateTime, tzLib[tz=retval$tz_cd[1]])
     }
+    
+    if("iv" == service){
+      if(tz == ""){
+        retval$tz_cd <- rep("UTC", nrow(retval))
+      } else {
+        retval$tz_cd <- rep(tz, nrow(retval))
+      }
+    }
+    
   } else {
     possibleError <- tryCatch({
       retval <- importRDB1(urlCall, asDateTime = TRUE, qw = TRUE)
@@ -147,5 +199,7 @@ readNWISdata <- function(service="dv", ...){
     })
   }
   
+  
+  
   return(retval)
 }
diff --git a/R/readNWISdv.r b/R/readNWISdv.r
index 154c2290..8337010c 100644
--- a/R/readNWISdv.r
+++ b/R/readNWISdv.r
@@ -6,9 +6,9 @@
 #' @param siteNumber character USGS site number.  This is usually an 8 digit number. Multiple sites can be requested with a character vector.
 #' @param parameterCd character of USGS parameter code(s).  This is usually an 5 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.
+#' retrieval for the earliest possible record. Date arguments are always specified in local time.
 #' @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. Date arguments are always specified in local time.
 #' @param statCd character USGS statistic code. This is usually 5 digits.  Daily mean (00003) is the default.
 #' @return A data frame with the following columns:
 #' \tabular{lll}{
diff --git a/R/readNWISqw.r b/R/readNWISqw.r
index c2f0abb6..5c59d4b7 100644
--- a/R/readNWISqw.r
+++ b/R/readNWISqw.r
@@ -30,9 +30,9 @@
 #' @param parameterCd character that contains the code for a parameter
 #' group, or a character vector of 5-digit parameter codes. See \bold{Details}.
 #' @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. Date arguments are always specified in local time.
 #' @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. Date arguments are always specified in local time.
 #' @param expanded logical defaults to \code{TRUE}. If \code{TRUE}, retrieves additional information. Expanded data includes
 #' remark_cd (remark code), result_va (result value), val_qual_tx (result value qualifier code), meth_cd (method code),
 #' dqi_cd (data-quality indicator code), rpt_lev_va (reporting level), and rpt_lev_cd (reporting level type). If \code{FALSE},
diff --git a/R/readNWISunit.r b/R/readNWISunit.r
index d940b701..3c86e1aa 100644
--- a/R/readNWISunit.r
+++ b/R/readNWISunit.r
@@ -8,9 +8,11 @@
 #' @param siteNumbers character USGS site number (or multiple sites).  This is usually an 8 digit number
 #' @param parameterCd character USGS parameter code.  This is usually an 5 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.
+#' retrieval for the earliest possible record. Simple date arguments are specified in local time.
+#' See more information here: \url{http://waterservices.usgs.gov/rest/IV-Service.html#Specifying}.
 #' @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. Simple date arguments are specified in local time.
+#' See more information here: \url{http://waterservices.usgs.gov/rest/IV-Service.html#Specifying}.
 #' @param tz character to set timezone attribute of dateTime. Default is an empty quote, which converts the 
 #' dateTimes to UTC (properly accounting for daylight savings times based on the data's provided tz_cd column).
 #' Possible values to provide are "America/New_York","America/Chicago", "America/Denver","America/Los_Angeles",
@@ -54,6 +56,14 @@
 #' 
 #' timeZoneChange <- readNWISuv(c('04024430','04024000'),parameterCd,
 #'          "2013-11-03","2013-11-03")
+#'  
+#' centralTime <- readNWISuv(siteNumber,parameterCd,
+#'                            "2014-10-10T12:00", "2014-10-10T23:59",
+#'                            tz="America/Chicago")
+#' 
+#' # Adding 'Z' to the time indicates to the web service to call the data with UTC time:
+#' GMTdata <- readNWISuv(siteNumber,parameterCd,
+#'                            "2014-10-10T00:00Z", "2014-10-10T23:59Z")
 #' }
 #' 
 readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){  
@@ -61,6 +71,12 @@ readNWISuv <- function (siteNumbers,parameterCd,startDate="",endDate="", tz=""){
   url <- constructNWISURL(siteNumbers,parameterCd,startDate,endDate,"uv",format="xml")
 
   data <- importWaterML1(url,asDateTime=TRUE,tz=tz)
+  
+  if(tz == ""){
+    data$tz_cd <- rep("UTC", nrow(data))
+  } else {
+    data$tz_cd <- rep(tz, nrow(data))
+  }
 
   return (data)
 }
diff --git a/R/readWQPqw.r b/R/readWQPqw.r
index 17d34ae8..7ee8d576 100644
--- a/R/readWQPqw.r
+++ b/R/readWQPqw.r
@@ -11,9 +11,9 @@
 #' @param parameterCd vector of USGS 5-digit parameter code or characteristicNames. 
 #' Leaving this blank will return all of the measured values during the specified time period.
 #' @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. Date arguments are always specified in local time.
 #' @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. Date arguments are always specified in local time.
 #' @keywords data import USGS web service
 #' @return A data frame with at least the following columns:
 #' \tabular{lll}{ 
diff --git a/man/readNWISdata.Rd b/man/readNWISdata.Rd
index 15d97e58..027b46b2 100644
--- a/man/readNWISdata.Rd
+++ b/man/readNWISdata.Rd
@@ -19,9 +19,12 @@ A data frame with the following columns:
 Name \tab Type \tab Description \cr
 agency \tab character \tab The NWIS code for the agency reporting the data\cr
 site \tab character \tab The USGS site number \cr
-datetime \tab POSIXct \tab The date and time of the value converted to UTC (for unit value data), \cr
-\tab character \tab or raw character string \cr
-tz_cd \tab character \tab The time zone code for datetime \cr
+dateTime \tab POSIXct \tab The date and time (if applicable) of the measurement,
+          converted to UTC for unit value data. R only allows one time zone attribute per column. For unit data
+          spanning a time zone change, converting the data to UTC solves this problem. For daily data,
+          the time zone attribute is the time zone of the first returned measurement.
+           \cr
+tz_cd \tab character \tab The time zone code for dateTime column\cr
 code \tab character \tab Any codes that qualify the corresponding value\cr
 value \tab numeric \tab The numeric value for the parameter \cr
 }
@@ -44,16 +47,27 @@ queryTime \tab POSIXct \tab The time the data was returned \cr
 \description{
 Returns data from the NWIS web service.
 Arguments to the function should be based on \url{http://waterservices.usgs.gov} service calls.
+See examples below for ideas of constructing queries.
 }
 \examples{
 \dontrun{
 # Examples not run for time considerations
 dataTemp <- readNWISdata(stateCd="OH",parameterCd="00010")
-dataTempUnit <- readNWISdata(sites="03086500", service="iv", parameterCd="00010")
+instFlow <- readNWISdata(sites="05114000", service="iv",
+                   parameterCd="00060",
+                   startDate="2014-05-01T00:00Z",endDate="2014-05-01T12:00Z")
+
+instFlowCDT <- readNWISdata(sites="05114000", service="iv",
+                   parameterCd="00060",
+                   startDate="2014-05-01T00:00",endDate="2014-05-01T12:00",
+                   tz="America/Chicago")
+
 #Empty:
-multiSite <- readNWISdata(sites=c("04025000","04072150"), service="iv", parameterCd="00010")
+multiSite <- readNWISdata(sites=c("04025000","04072150"), service="iv",
+                           parameterCd="00010")
 #Not empty:
-multiSite <- readNWISdata(sites=c("04025500","040263491"), service="iv", parameterCd="00060")
+multiSite <- readNWISdata(sites=c("04025500","040263491"),
+                           service="iv", parameterCd="00060")
 bBoxEx <- readNWISdata(bBox=c(-83,36.5,-81,38.5), parameterCd="00010")
 
 startDate <- as.Date("2013-10-01")
@@ -66,6 +80,8 @@ qwData <- readNWISdata(bBox=c(-82.5,41.52,-81,41),startDate=as.Date("2000-01-01"
                   drain_area_va_min=50, qw_count_nu=50,qw_attributes="expanded",
                   qw_sample_wide="wide",list_of_search_criteria=c("lat_long_bounding_box",
                   "drain_area_va","obs_count_nu"),service="qw")
+temp <- readNWISdata(bBox=c(-83,36.5,-81,38.5), parameterCd="00010", service="site",
+                   seriesCatalogOutput=TRUE)
 }
 }
 \seealso{
diff --git a/man/readNWISdv.Rd b/man/readNWISdv.Rd
index 3d3e09a0..446c09ac 100644
--- a/man/readNWISdv.Rd
+++ b/man/readNWISdv.Rd
@@ -13,10 +13,10 @@ readNWISdv(siteNumber, parameterCd, startDate = "", endDate = "",
 \item{parameterCd}{character of USGS parameter code(s).  This is usually an 5 digit number.}
 
 \item{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. Date arguments are always specified in local time.}
 
 \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. Date arguments are always specified in local time.}
 
 \item{statCd}{character USGS statistic code. This is usually 5 digits.  Daily mean (00003) is the default.}
 }
diff --git a/man/readNWISqw.Rd b/man/readNWISqw.Rd
index 8ebad2a5..05093548 100644
--- a/man/readNWISqw.Rd
+++ b/man/readNWISqw.Rd
@@ -14,10 +14,10 @@ readNWISqw(siteNumbers, parameterCd, startDate = "", endDate = "",
 group, or a character vector of 5-digit parameter codes. See \bold{Details}.}
 
 \item{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. Date arguments are always specified in local time.}
 
 \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. Date arguments are always specified in local time.}
 
 \item{expanded}{logical defaults to \code{TRUE}. If \code{TRUE}, retrieves additional information. Expanded data includes
 remark_cd (remark code), result_va (result value), val_qual_tx (result value qualifier code), meth_cd (method code),
diff --git a/man/readNWISuv.Rd b/man/readNWISuv.Rd
index 05f41267..ff16c162 100644
--- a/man/readNWISuv.Rd
+++ b/man/readNWISuv.Rd
@@ -13,10 +13,12 @@ readNWISuv(siteNumbers, parameterCd, startDate = "", endDate = "",
 \item{parameterCd}{character USGS parameter code.  This is usually an 5 digit number.}
 
 \item{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. Simple date arguments are specified in local time.
+See more information here: \url{http://waterservices.usgs.gov/rest/IV-Service.html#Specifying}.}
 
 \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. Simple date arguments are specified in local time.
+See more information here: \url{http://waterservices.usgs.gov/rest/IV-Service.html#Specifying}.}
 
 \item{tz}{character to set timezone attribute of dateTime. Default is an empty quote, which converts the
 dateTimes to UTC (properly accounting for daylight savings times based on the data's provided tz_cd column).
@@ -66,6 +68,14 @@ rawData <- readNWISuv(siteNumber,parameterCd,startDate,endDate)
 
 timeZoneChange <- readNWISuv(c('04024430','04024000'),parameterCd,
          "2013-11-03","2013-11-03")
+
+centralTime <- readNWISuv(siteNumber,parameterCd,
+                           "2014-10-10T12:00", "2014-10-10T23:59",
+                           tz="America/Chicago")
+
+# Adding 'Z' to the time indicates to the web service to call the data with UTC time:
+GMTdata <- readNWISuv(siteNumber,parameterCd,
+                           "2014-10-10T00:00Z", "2014-10-10T23:59Z")
 }
 }
 \seealso{
diff --git a/man/readWQPqw.Rd b/man/readWQPqw.Rd
index beebf8b9..029aa381 100644
--- a/man/readWQPqw.Rd
+++ b/man/readWQPqw.Rd
@@ -13,10 +13,10 @@ readWQPqw(siteNumbers, parameterCd, startDate = "", endDate = "")
 Leaving this blank will return all of the measured values during the specified time period.}
 
 \item{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. Date arguments are always specified in local time.}
 
 \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. Date arguments are always specified in local time.}
 }
 \value{
 A data frame with at least the following columns:
diff --git a/tests/testthat/tests_userFriendly_fxns.R b/tests/testthat/tests_userFriendly_fxns.R
index c6348026..0ff5c3a6 100644
--- a/tests/testthat/tests_userFriendly_fxns.R
+++ b/tests/testthat/tests_userFriendly_fxns.R
@@ -21,9 +21,9 @@ test_that("Unit value data returns correct types", {
   expect_that(attr(rawData, "url"), equals(
       "http://nwis.waterservices.usgs.gov/nwis/iv/?site=05114000&format=waterml,1.1&ParameterCd=00060&startDT=2014-10-10&endDT=2014-10-10")
   )
-  #First switchover to standard time:
-  expect_that(as.numeric(timeZoneChange[which(timeZoneChange$tz_cd == "CST")[1],"dateTime"]),
-              equals(as.numeric(as.POSIXct("2013-11-03 01:00:00", tz="UTC")+60*60*6)))
+#   #First switchover to standard time:
+#   expect_that(as.numeric(timeZoneChange[which(timeZoneChange$tz_cd == "America/Chicago")[1],"dateTime"]),
+#               equals(as.numeric(as.POSIXct("2013-11-03 01:00:00", tz="UTC")+60*60*6)))
   
 })
 
-- 
GitLab