From 342eecbd1be419dc9c0e2c6294b634ea07d0e8fe Mon Sep 17 00:00:00 2001
From: Laura DeCicco <ldecicco@usgs.gov>
Date: Wed, 13 Mar 2013 13:45:15 -0500
Subject: [PATCH] Added new function 'getDataAvailability' to find what
 parameters, stats, and period of record for an individual site.

---
 DESCRIPTION             |  1 +
 R/constructNWISURL.r    |  2 +-
 R/getDataAvailability.r | 60 +++++++++++++++++++++++++++++++++++++++++
 R/getSiteFileData.r     | 12 ++++-----
 4 files changed, 68 insertions(+), 7 deletions(-)
 create mode 100644 R/getDataAvailability.r

diff --git a/DESCRIPTION b/DESCRIPTION
index c3c806d8..417f6ce7 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -51,6 +51,7 @@ Collate:
     'retrieveNWISqwData.r'
     'processQWData.r'
     'constructNWISURL.r'
+    'getSiteFileData.r'
 Depends:
     R (>= 2.15.0)
 Imports:
diff --git a/R/constructNWISURL.r b/R/constructNWISURL.r
index be326745..8651134b 100644
--- a/R/constructNWISURL.r
+++ b/R/constructNWISURL.r
@@ -11,7 +11,7 @@
 #' @param statCd string USGS statistic code only used for daily value service. This is usually 5 digits.  Daily mean (00003) is the default.
 #' @param service string USGS service to call. Possible values are "dv" (daily values), "iv" (unit/instantaneous values), "qwdata" (water quality data), and "wqp" (water quality portal, which can include STORET).
 #' @keywords data import USGS web service
-#' @return data dataframe with agency, site, dateTime, value, and code columns
+#' @return url string
 #' @export
 #' @examples
 #' siteNumber <- '04085427'
diff --git a/R/getDataAvailability.r b/R/getDataAvailability.r
new file mode 100644
index 00000000..2d8927b0
--- /dev/null
+++ b/R/getDataAvailability.r
@@ -0,0 +1,60 @@
+#' USGS data availability
+#'
+#' Imports a table of available parameters, period of record, and count.
+#'
+#' @param siteNumber string USGS site number.  This is usually an 8 digit number
+#' @param interactive logical Option for interactive mode.  If true, there is user interaction for error handling and data checks.
+#' @param longNames logical
+#' @keywords data import USGS web service
+#' @return retval dataframe with all information found in the expanded site file
+#' @export
+#' @examples
+#' # These examples require an internet connection to run
+#' siteINFO <- getSiteFileData('05114000',interactive=FALSE)
+getSiteFileData <- function(siteNumber="",interactive=TRUE){
+  
+  # Checking for 8 digit site ID:
+  siteNumber <- formatCheckSiteNumber(siteNumber, longNames = FALSE,interactive=interactive)
+  
+  urlSitefile <- paste("http://waterservices.usgs.gov/nwis/site?format=rdb&siteOutput=Expanded&sites=",siteNumber,sep = "")
+  
+  SiteFile <- read.delim(  
+    urlSitefile, 
+    header = TRUE, 
+    quote="\"", 
+    dec=".", 
+    sep='\t',
+    colClasses=c('character'),
+    fill = TRUE, 
+    comment.char="#")
+  
+  SiteFile <- SiteFile[-1,]
+  
+  SiteFile <- with(SiteFile, data.frame(parameter_cd=parm_cd, statCd=stat_cd, startDate=begin_date,endDate=end_date, count=count_nu,service=data_type_cd,stringsAsFactors = FALSE))
+  
+  SiteFile <- SiteFile[!is.na(SiteFile$parameter_cd),]
+  SiteFile <- SiteFile["" != SiteFile$parameter_cd,]
+  SiteFile$startDate <- as.Date(SiteFile$startDate)
+  SiteFile$endDate <- as.Date(SiteFile$endDate)
+  SiteFile$count <- as.numeric(SiteFile$count)
+  
+  if(longNames){
+    pCodes <- unique(SiteFile$pCode)
+    numObs <- length(pCodes)
+    printUpdate <- floor(seq(1,numObs,numObs/100))
+    for (i in 1:numObs){
+      if (1 == i) {
+        pcodeINFO <- getParameterInfo(pCodes[i])
+      } else {
+        pcodeINFO <- rbind(pcodeINFO, getParameterInfo(pCodes[i]))
+      }
+      if(interactive) {
+        cat("Percent complete: \n")
+        if(i %in% printUpdate) cat(floor(i*100/numObs),"\t")
+      }
+    }
+    SiteFile <- merge(SiteFile,pcodeINFO,by="parameter_cd")
+  }
+  
+  return(SiteFile)
+}
\ No newline at end of file
diff --git a/R/getSiteFileData.r b/R/getSiteFileData.r
index 48e3bb66..12353364 100644
--- a/R/getSiteFileData.r
+++ b/R/getSiteFileData.r
@@ -17,14 +17,14 @@ getSiteFileData <- function(siteNumber="",interactive=TRUE){
   
   urlSitefile <- paste("http://waterservices.usgs.gov/nwis/site?format=rdb&siteOutput=Expanded&sites=",siteNumber,sep = "")
   
-  SiteFile <- read.delim(  
-    urlSitefile, 
-    header = TRUE, 
-    quote="\"", 
-    dec=".", 
+  SiteFile <- read.delim(
+    urlSitefile,
+    header = TRUE,
+    quote="\"",
+    dec=".",
     sep='\t',
     colClasses=c('character'),
-    fill = TRUE, 
+    fill = TRUE,
     comment.char="#")
   
   INFO <- SiteFile[-1,]
-- 
GitLab