From 1263a437796a62e00cffcc6078ac9a315c978ae1 Mon Sep 17 00:00:00 2001
From: Laura DeCicco <ldecicco@usgs.gov>
Date: Fri, 25 Jul 2014 12:26:34 -0500
Subject: [PATCH] Added site and general wqp data function.

---
 R/getGeneralWQPData.R | 37 +++++++++++++++++++++++++++++++++++++
 R/getWQPSites.R       | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 R/getGeneralWQPData.R
 create mode 100644 R/getWQPSites.R

diff --git a/R/getGeneralWQPData.R b/R/getGeneralWQPData.R
new file mode 100644
index 00000000..9c178aa5
--- /dev/null
+++ b/R/getGeneralWQPData.R
@@ -0,0 +1,37 @@
+#' General Data Import from Water Quality Portal
+#'
+#' Imports data from Water Quality Portal web service. This function gets the data from here: \url{http://www.waterqualitydata.us}.
+#' because it allows for other agencies rather than the USGS.  
+#'
+#' @param \dots see \url{www.waterqualitydata.us/webservices_documentation.jsp} for a complete list of options
+#' @keywords data import WQP web service
+#' @return retval dataframe with first column dateTime, and at least one qualifier and value columns
+#' (subsequent qualifier/value columns could follow depending on requested parameter codes)
+#' @export
+#' @examples
+#' setInternet2(use=NA)
+#' options(timeout=120)
+#' pHData <- getGeneralWQPData(siteid="USGS-04024315",characteristicName="pH")
+getGeneralWQPData <- function(...){
+  
+  matchReturn <- match.call()
+  
+  options <- c("bBox","lat","long","within","countrycode","statecode","countycode","siteType","organization",
+               "siteid","huc","sampleMedia","characteristicType","characteristicName","pCode","activityId",
+               "startDateLo","startDateHi","mimeType","Zip","providers")
+  
+  if(!all(names(matchReturn[-1]) %in% options)) warning(matchReturn[!(names(matchReturn[-1]) %in% options)],"is not a valid query parameter to the Water Quality Portal")
+  
+  values <- sapply(matchReturn[-1], function(x) URLencode(as.character(paste(x,collapse="",sep=""))))
+  
+  urlCall <- paste(paste(names(values),values,sep="="),collapse="&")
+  
+  
+  baseURL <- "http://www.waterqualitydata.us/Result/search?"
+  urlCall <- paste(baseURL,
+                   urlCall,
+                   "&mimeType=tsv",sep = "")
+  
+  suppressWarnings(retval <- read.delim(urlCall, header = TRUE, quote="\"", dec=".", sep='\t', colClasses=c('character'), fill = TRUE))
+  return(retval)
+}
\ No newline at end of file
diff --git a/R/getWQPSites.R b/R/getWQPSites.R
new file mode 100644
index 00000000..190df9a2
--- /dev/null
+++ b/R/getWQPSites.R
@@ -0,0 +1,39 @@
+#' Site DAta Import from Water Quality Portal
+#'
+#' Imports site data from Water Quality Portal web service. This function gets the data from here: \url{http://www.waterqualitydata.us}. This function is more general than getQWData
+#' because it allows for other agencies rather than the USGS.  Therefore, the 5-digit parameter code cannot be used.
+#' Instead, this function uses characteristicName.  A complete list can be found here \url{http://www.waterqualitydata.us/Codes/Characteristicname}
+#'
+#' @param \dots see \url{www.waterqualitydata.us/webservices_documentation.jsp} for a complete list of options
+#' @keywords data import WQP web service
+#' @return retval dataframe with first column dateTime, and at least one qualifier and value columns
+#' (subsequent qualifier/value columns could follow depending on requested parameter codes)
+#' @export
+#' @examples
+#' setInternet2(use=NA)
+#' options(timeout=120)
+#' \dontrun{sitesList <- getWQPSites(within=10,lat=43.06932,long=-89.4444,characteristicName="pH")
+#' siteListPH <- getWQPSites(characteristicName="pH")}
+getWQPSites <- function(...){
+  
+  matchReturn <- match.call()
+  
+  options <- c("bBox","lat","long","within","countrycode","statecode","countycode","siteType","organization",
+    "siteid","huc","sampleMedia","characteristicType","characteristicName","pCode","activityId",
+    "startDateLo","startDateHi","mimeType","Zip","providers")
+  
+  if(!all(names(matchReturn[-1]) %in% options)) warning(matchReturn[!(names(matchReturn[-1]) %in% options)],"is not a valid query parameter to the Water Quality Portal")
+  
+  values <- sapply(matchReturn[-1], function(x) URLencode(as.character(paste(x,collapse="",sep=""))))
+  
+  urlCall <- paste(paste(names(values),values,sep="="),collapse="&")
+  
+  
+  baseURL <- "http://www.waterqualitydata.us/Station/search?"
+  urlCall <- paste(baseURL,
+               urlCall,
+               "&mimeType=tsv",sep = "")
+  
+  retval <- suppressWarnings(read.delim(urlCall, header = TRUE, quote="\"", dec=".", sep='\t', colClasses=c('character'), fill = TRUE))
+  return(retval)
+}
-- 
GitLab