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

Getting rid of duplicate code. Using getRawQWData instead.

parent 776388de
No related branches found
No related tags found
1 merge request!2Updating naming convention + other minor changes.
#' Data Import from Water Quality Portal #' 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}. This function is more general than getQWData #' Imports data from Water Quality Portal web service. This function gets the data from: \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. #' 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} #' Instead, this function uses characteristicName. A complete list can be found here
#' #'
#' @param siteNumber string site number. If USGS, it should be in the form :'USGS-XXXXXXXXX...' #' @param siteNumber string site number. If USGS, it should be in the form :'USGS-XXXXXXXXX...'
#' @param characteristicName string #' @param characteristicName string
...@@ -16,77 +16,29 @@ ...@@ -16,77 +16,29 @@
#' @import RCurl #' @import RCurl
#' @examples #' @examples
#' # These examples require an internet connection to run #' # These examples require an internet connection to run
#' getWQPData('USGS-01594440','Chloride', '', '') #' Chloride <- getWQPData('USGS-01594440','Chloride', '', '')
#' getWQPData('WIDNR_WQX-10032762','Specific conductance', '', '') #' SC <- getWQPData('WIDNR_WQX-10032762','Specific conductance', '', '')
#' NWIS_Cl <- getWQPData('USGS-04024000','30234', '', '')
getWQPData <- function(siteNumber,characteristicName,StartDate,EndDate,interactive=TRUE){ getWQPData <- function(siteNumber,characteristicName,StartDate,EndDate,interactive=TRUE){
# require(RCurl)
StartDate <- formatCheckDate(StartDate, "StartDate", interactive=interactive)
EndDate <- formatCheckDate(EndDate, "EndDate", interactive=interactive)
dateReturn <- checkStartEndDate(StartDate, EndDate, interactive=interactive) retval <- getRawQWData(siteNumber=siteNumber,
StartDate <- dateReturn[1] ParameterCd=characteristicName,
EndDate <- dateReturn[2] StartDate=StartDate,
EndDate=EndDate,
if (nzchar(StartDate)){ interactive=interactive)
StartDate <- format(as.Date(StartDate), format="%m-%d-%Y") #Check for pcode:
} if(all(nchar(characteristicName) == 5)){
if (nzchar(EndDate)){ suppressWarnings(pCodeLogic <- all(!is.na(as.numeric(characteristicName))))
EndDate <- format(as.Date(EndDate), format="%m-%d-%Y") } else {
pCodeLogic <- FALSE
} }
characteristicName <- URLencode(characteristicName) if(nrow(retval) > 0){
data <- processQWData(retval,pCodeLogic)
baseURL <- "http://www.waterqualitydata.us/Result/search?siteid="
url <- paste(baseURL,
siteNumber,
"&characteristicName=",
characteristicName, # to get multi-parameters, use a semicolen
"&startDateLo=",
StartDate,
"&startDateHi=",
EndDate,
"&countrycode=US&mimeType=tsv",sep = "")
h <- basicHeaderGatherer()
doc <- getURI(url, headerfunction = h$update)
numToBeReturned <- as.numeric(h$value()["Total-Result-Count"])
suppressWarnings(retval <- read.delim(url, header = TRUE, quote="\"", dec=".", sep='\t', colClasses=c('character'), fill = TRUE))
qualifier <- ifelse((retval$ResultDetectionConditionText == "Not Detected" |
retval$ResultDetectionConditionText == "Detected Not Quantified" |
retval$ResultMeasureValue < retval$DetectionQuantitationLimitMeasure.MeasureValue),"<","")
correctedData<-ifelse((nchar(qualifier)==0),retval$ResultMeasureValue,retval$DetectionQuantitationLimitMeasure.MeasureValue)
test <- data.frame(retval$CharacteristicName)
# test$dateTime <- as.POSIXct(strptime(paste(retval$ActivityStartDate,retval$ActivityStartTime.Time,sep=" "), "%Y-%m-%d %H:%M:%S"))
test$dateTime <- as.Date(retval$ActivityStartDate, "%Y-%m-%d")
originalLength <- nrow(test)
if (!is.na(numToBeReturned)){
if(originalLength != numToBeReturned) warning(numToBeReturned, " sample results were expected, ", originalLength, " were returned")
test$qualifier <- qualifier
test$value <- as.numeric(correctedData)
test <- test[!is.na(test$dateTime),]
newLength <- nrow(test)
if (originalLength != newLength){
numberRemoved <- originalLength - newLength
warningMessage <- paste(numberRemoved, " rows removed because no date was specified", sep="")
warning(warningMessage)
}
colnames(test)<- c("CharacteristicName","dateTime","qualifier","value")
data <- reshape(test, idvar="dateTime", timevar = "CharacteristicName", direction="wide")
data$dateTime <- format(data$dateTime, "%Y-%m-%d")
data$dateTime <- as.Date(data$dateTime)
return(data)
} else { } else {
warning("No data retrieved") data <- NULL
} }
return(data)
} }
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