diff --git a/NAMESPACE b/NAMESPACE index e31d86ebec5519a1e7d4cb2cff0571fb2ca7728c..9d2fec6ac6c646d667c54ef1485b5ff89316ba8a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2 (4.1.0): do not edit by hand +export(checkWQPdates) export(constructNWISURL) export(constructWQPURL) export(getWebServiceData) diff --git a/R/checkWQPdates.r b/R/checkWQPdates.r new file mode 100644 index 0000000000000000000000000000000000000000..637783ab68afc9bc8b53c6394c51f164c2919f9b --- /dev/null +++ b/R/checkWQPdates.r @@ -0,0 +1,51 @@ +#' Date Check for Water Quality Portal +#' +#' Checks date format for inputs to the Water Quality Portal. Used in \code{readWQPqw} +#' and \code{readWQPdata}. +#' +#' @param values named list with arguments to send to the Water Quality Portal +#' @return values named list with corrected arguments to send to the Water Quality Portal +#' @export +#' @examples +#' values <- list(startDateLo="01-01-2002", characteristicName="Phosphorous", +#' endDate=as.Date("2014-01-01")) +#' values <- checkWQPdates(values) +checkWQPdates <- function(values){ + dateNames <- c("startDateLo","startDateHi","startDate","endDate") + + if(any(names(values) %in% dateNames)){ + index <- which(names(values) %in% dateNames) + + if("" %in% values[index]){ + values <- values[-index[values[index] == ""]] + index <- which(names(values) %in% dateNames) + } + + if(length(index) > 0){ + # If a valid R date was put in, the format needs to be changed to mm-dd-yyyy for the WQP: + for(i in index){ + dateInput <- as.character(values[[i]]) + splitDates <- unlist(strsplit(dateInput, "-")) + if(length(splitDates) == 3){ + if(nchar(splitDates[1]) == 4){ #R object + dates <- as.Date(dateInput, format="%Y-%m-%d") + dates <- format(as.Date(dates), format="%m-%d-%Y") + values[i] <- dates + } else if (nchar(splitDates[3]) != 4){ #The way WQP wants it == 4, so this is probably a 2 digit year or something + warning("Please check the date format for the arguments: ", + paste(names(values)[i],values[i], collapse=", ")) + } + } else { # Probably something wrong + warning("Please check the date format for the arguments: ", + paste(names(values)[i],values[i], collapse=", ")) + } + } + + names(values)[names(values) == 'startDate'] <- 'startDateLo' + names(values)[names(values) == 'endDate'] <- 'startDateHi' + } + + } + + return(values) +} \ No newline at end of file diff --git a/R/readWQPdata.R b/R/readWQPdata.R index cc5831841746660c78bb1a73f5e3b19d925d49ba..9b8ecffdf23563f74dc36cf7640cc6977c452511 100644 --- a/R/readWQPdata.R +++ b/R/readWQPdata.R @@ -93,6 +93,8 @@ #' startDate <- as.Date("2013-01-01") #' nutrientDaneCounty <- readWQPdata(countycode="US:55:025",startDate=startDate, #' characteristicType="Nutrient") +#' nutrientDaneCounty <- readWQPdata(countycode="US:55:025",startDate="", +#' characteristicType="Nutrient") #' } readWQPdata <- function(...){ @@ -108,31 +110,7 @@ readWQPdata <- function(...){ values['bBox'] <- gsub(pattern = ";", replacement = ",", x = values['bBox']) } - dateNames <- c("startDateLo","startDateHi","startDate","endDate") - - if(any(names(values) %in% dateNames)){ - index <- which(names(values) %in% dateNames) - - if("" %in% values[index]){ - values <- values[-index[values[index] == ""]] - index <- which(names(values) %in% dateNames) - } - - if(length(index) > 0){ - # If a valid R date was put in, the format needs to be changed to mm-dd-yyyy for the WQP: - if(any(!is.na(as.Date(values[index], format="%Y-%m-%d")))){ - dates <- as.Date(values[index[!is.na(as.Date(values[index], format="%Y-%m-%d"))]]) - dates <- format(as.Date(dates), format="%m-%d-%Y") - values[index] <- dates - } else if (any(is.na(as.Date(values[index], format="%m-%d-%Y")))){ - warning("Please check the date format for the arguments: ", paste(names(values)[index], collapse=", ")) - } - - names(values)[names(values) == 'startDate'] <- 'startDateLo' - names(values)[names(values) == 'endDate'] <- 'startDateHi' - } - - } + values <- checkWQPdates(values) urlCall <- paste(paste(names(values),values,sep="="),collapse="&") diff --git a/R/whatWQPsites.R b/R/whatWQPsites.R index 70f01708f237b6a2b9fa4076fe30c294861720cb..eb21094e43418ae8b843f5cc316bcb6029bcde1b 100644 --- a/R/whatWQPsites.R +++ b/R/whatWQPsites.R @@ -69,31 +69,7 @@ whatWQPsites <- function(...){ values['bBox'] <- gsub(pattern = ";", replacement = ",", x = values['bBox']) } - dateNames <- c("startDateLo","startDateHi","startDate","endDate") - - if(any(names(values) %in% dateNames)){ - index <- which(names(values) %in% dateNames) - - if("" %in% values[index]){ - values <- values[-index[values[index] == ""]] - index <- which(names(values) %in% dateNames) - } - - if(length(index) > 0){ - # If a valid R date was put in, the format needs to be changed to mm-dd-yyyy for the WQP: - if(any(!is.na(as.Date(values[index], format="%Y-%m-%d")))){ - dates <- as.Date(values[index[!is.na(as.Date(values[index], format="%Y-%m-%d"))]]) - dates <- format(as.Date(dates), format="%m-%d-%Y") - values[index] <- dates - } else if (any(is.na(as.Date(values[index], format="%m-%d-%Y")))){ - warning("Please check the date format for the arguments: ", paste(names(values)[index], collapse=", ")) - } - - names(values)[names(values) == 'startDate'] <- 'startDateLo' - names(values)[names(values) == 'endDate'] <- 'startDateHi' - } - - } + values <- checkWQPdates(values) urlCall <- paste(paste(names(values),values,sep="="),collapse="&") diff --git a/man/checkWQPdates.Rd b/man/checkWQPdates.Rd new file mode 100644 index 0000000000000000000000000000000000000000..f239655d3de874a1333ac4063405148d2f39f07b --- /dev/null +++ b/man/checkWQPdates.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/checkWQPdates.r +\name{checkWQPdates} +\alias{checkWQPdates} +\title{Date Check for Water Quality Portal} +\usage{ +checkWQPdates(values) +} +\arguments{ +\item{values}{named list with arguments to send to the Water Quality Portal} +} +\value{ +values named list with corrected arguments to send to the Water Quality Portal +} +\description{ +Checks date format for inputs to the Water Quality Portal. Used in \code{readWQPqw} +and \code{readWQPdata}. +} +\examples{ +values <- list(startDateLo="01-01-2002", characteristicName="Phosphorous", + endDate=as.Date("2014-01-01")) +values <- checkWQPdates(values) +} + diff --git a/man/readWQPdata.Rd b/man/readWQPdata.Rd index 3d71d35db9f0bc56cf9e184cb1c2a0bc1883d61b..f898a5344918c0b1ea06f1c5add325072c71d54c 100644 --- a/man/readWQPdata.Rd +++ b/man/readWQPdata.Rd @@ -102,6 +102,8 @@ pHDataExpanded2 <- readWQPdata(bBox=c(-90.10,42.67,-88.64,43.35),characteristicN startDate <- as.Date("2013-01-01") nutrientDaneCounty <- readWQPdata(countycode="US:55:025",startDate=startDate, characteristicType="Nutrient") +nutrientDaneCounty <- readWQPdata(countycode="US:55:025",startDate="", + characteristicType="Nutrient") } } \keyword{WQP}