From 73251ecc5cfe3bc7761910c243c271d6ae1ddd81 Mon Sep 17 00:00:00 2001 From: Laura A DeCicco Date: Thu, 25 Jul 2024 16:01:47 -0500 Subject: [PATCH] groundwater updates --- R/constructNWISURL.R | 412 ++++++++++++++++++++-------------------- R/readNWISdata.R | 12 +- man/constructNWISURL.Rd | 6 +- man/readNWISdata.Rd | 12 +- 4 files changed, 232 insertions(+), 210 deletions(-) diff --git a/R/constructNWISURL.R b/R/constructNWISURL.R index 5a1f558e..b10f79ab 100644 --- a/R/constructNWISURL.R +++ b/R/constructNWISURL.R @@ -68,10 +68,8 @@ #' url_rating <- constructNWISURL(site_id, service = "rating", ratingType = "base") #' url_peak <- constructNWISURL(site_id, service = "peak") #' url_meas <- constructNWISURL(site_id, service = "meas") -#' urlQW <- constructNWISURL("450456092225801", "70300", -#' startDate = "", endDate = "", -#' "qw", expanded = TRUE -#' ) +#' url_gwl <- constructNWISURL(site_id, service = "gwlevels", +#' startDate = "2024-05-01", endDate = "2024-05-30") constructNWISURL <- function(siteNumbers, parameterCd = "00060", startDate = "", @@ -87,214 +85,224 @@ constructNWISURL <- function(siteNumbers, "dv", "uv", "iv", "iv_recent", "qw", "gwlevels", "rating", "peak", "meas", "stat", "qwdata" )) - + service[service == "qw"] <- "qwdata" service[service == "meas"] <- "measurements" service[service == "uv"] <- "iv" - + if (any(!is.na(parameterCd) & parameterCd != "all")) { pcodeCheck <- all(nchar(parameterCd) == 5) & all(!is.na(suppressWarnings(as.numeric(parameterCd)))) - + if (!pcodeCheck) { badIndex <- which(nchar(parameterCd) != 5 | is.na(suppressWarnings(as.numeric(parameterCd)))) stop("The following pCodes appear mistyped:", paste(parameterCd[badIndex], collapse = ", ")) } - + if (length(parameterCd) > 200) { stop("Maximum parameter codes allowed is 200, please adjust data request.") } } - + multipleSites <- length(siteNumbers) > 1 - + siteNumbers <- paste(siteNumbers, collapse = ",") - + baseURL <- drURL(service, Access = pkg.env$access) - + switch(service, - qwdata = { - if (multipleSites) { - searchCriteria <- "multiple_site_no" - url <- appendDrURL(baseURL, multiple_site_no = siteNumbers) - } else { - searchCriteria <- "search_site_no" - url <- appendDrURL(baseURL, - search_site_no = siteNumbers, - search_site_no_match_type = "exact" - ) - } - - multiplePcodes <- length(parameterCd) > 1 - - if (multiplePcodes) { - pCodes <- paste(parameterCd, collapse = ",") - url <- appendDrURL(url, - multiple_parameter_cds = pCodes, - param_cd_operator = "OR" - ) - } else { - url <- appendDrURL(url, - multiple_parameter_cds = parameterCd, - param_cd_operator = "AND" - ) - } - - searchCriteria <- paste(searchCriteria, "multiple_parameter_cds", sep = ",") - url <- appendDrURL(url, list_of_search_criteria = searchCriteria) - - - url <- paste(url, "group_key=NONE&sitefile_output_format=html_table&column_name=agency_cd", - "column_name=site_no&column_name=station_nm&inventory_output=0&rdb_inventory_output=file", - "TZoutput=0&pm_cd_compare=Greater%20than&radio_parm_cds=previous_parm_cds&qw_attributes=0", - "format=rdb&rdb_qw_attributes=0&date_format=YYYY-MM-DD", - "rdb_compression=value", - sep = "&" - ) - if (expanded) { - url <- appendDrURL(url, qw_sample_wide = "0") - url <- gsub("rdb_qw_attributes=0", "rdb_qw_attributes=expanded", url) - } else { - url <- appendDrURL(url, qw_sample_wide = "separated_wide") - } - - if (nzchar(startDate)) { - url <- appendDrURL(url, begin_date = startDate) - } - - if (nzchar(endDate)) { - url <- appendDrURL(url, end_date = endDate) - } - }, - rating = { - ratingType <- match.arg(ratingType, c("base", "corr", "exsa")) - url <- appendDrURL(baseURL, site_no = siteNumbers, file_type = ratingType) - }, - peak = { - url <- appendDrURL(baseURL, - site_no = siteNumbers, - range_selection = "date_range", - format = "rdb" - ) - if (nzchar(startDate)) { - url <- appendDrURL(url, begin_date = startDate) - } - if (nzchar(endDate)) { - url <- appendDrURL(url, end_date = endDate) - } - }, - measurements = { - url <- appendDrURL(baseURL, - site_no = siteNumbers, - range_selection = "date_range" - ) - if (nzchar(startDate)) { - url <- appendDrURL(url, - begin_date = startDate - ) - } - if (nzchar(endDate)) { - url <- appendDrURL(url, end_date = endDate) - } - if (expanded) { - url <- appendDrURL(url, format = "rdb_expanded") - } else { - url <- appendDrURL(url, format = "rdb") - } - }, - stat = { # for statistics service - - message("Please be aware the NWIS data service feeding this function is in BETA.\n + qwdata = { + if (multipleSites) { + searchCriteria <- "multiple_site_no" + url <- appendDrURL(baseURL, multiple_site_no = siteNumbers) + } else { + searchCriteria <- "search_site_no" + url <- appendDrURL(baseURL, + search_site_no = siteNumbers, + search_site_no_match_type = "exact" + ) + } + + multiplePcodes <- length(parameterCd) > 1 + + if (multiplePcodes) { + pCodes <- paste(parameterCd, collapse = ",") + url <- appendDrURL(url, + multiple_parameter_cds = pCodes, + param_cd_operator = "OR" + ) + } else { + url <- appendDrURL(url, + multiple_parameter_cds = parameterCd, + param_cd_operator = "AND" + ) + } + + searchCriteria <- paste(searchCriteria, "multiple_parameter_cds", sep = ",") + url <- appendDrURL(url, list_of_search_criteria = searchCriteria) + + + url <- paste(url, "group_key=NONE&sitefile_output_format=html_table&column_name=agency_cd", + "column_name=site_no&column_name=station_nm&inventory_output=0&rdb_inventory_output=file", + "TZoutput=0&pm_cd_compare=Greater%20than&radio_parm_cds=previous_parm_cds&qw_attributes=0", + "format=rdb&rdb_qw_attributes=0&date_format=YYYY-MM-DD", + "rdb_compression=value", + sep = "&" + ) + if (expanded) { + url <- appendDrURL(url, qw_sample_wide = "0") + url <- gsub("rdb_qw_attributes=0", "rdb_qw_attributes=expanded", url) + } else { + url <- appendDrURL(url, qw_sample_wide = "separated_wide") + } + + if (nzchar(startDate)) { + url <- appendDrURL(url, begin_date = startDate) + } + + if (nzchar(endDate)) { + url <- appendDrURL(url, end_date = endDate) + } + }, + rating = { + ratingType <- match.arg(ratingType, c("base", "corr", "exsa")) + url <- appendDrURL(baseURL, site_no = siteNumbers, file_type = ratingType) + }, + peak = { + url <- appendDrURL(baseURL, + site_no = siteNumbers, + range_selection = "date_range", + format = "rdb" + ) + if (nzchar(startDate)) { + url <- appendDrURL(url, begin_date = startDate) + } + if (nzchar(endDate)) { + url <- appendDrURL(url, end_date = endDate) + } + }, + measurements = { + url <- appendDrURL(baseURL, + site_no = siteNumbers, + range_selection = "date_range" + ) + if (nzchar(startDate)) { + url <- appendDrURL(url, + begin_date = startDate + ) + } + if (nzchar(endDate)) { + url <- appendDrURL(url, end_date = endDate) + } + if (expanded) { + url <- appendDrURL(url, format = "rdb_expanded") + } else { + url <- appendDrURL(url, format = "rdb") + } + }, + stat = { # for statistics service + + message("Please be aware the NWIS data service feeding this function is in BETA.\n Data formatting could be changed at any time, and is not guaranteed") - - # make sure only statTypes allowed for the statReportType are being requested - if (!grepl("(?i)daily", statReportType) && - !all(grepl("(?i)mean", statType)) && - !all(grepl("(?i)all", statType))) { - stop("Monthly and annual report types can only provide means") - } - - # make sure dates aren"t too specific for statReportType - if (grepl("(?i)monthly", statReportType) && - (length(unlist(gregexpr("-", startDate))) > 1 || - length(unlist(gregexpr("-", endDate))) > 1)) { - stop("Start and end dates for monthly statReportType can only include months and years") - } - if (grepl("(?i)annual", statReportType) && (grepl("-", startDate) || grepl("-", endDate))) { - stop("Start and end dates for annual statReportType can only include years") - } - statType <- paste(statType, collapse = ",") - parameterCd <- paste(parameterCd, collapse = ",") - url <- appendDrURL(baseURL, - sites = siteNumbers, - statType = statType, - statReportType = statReportType, - parameterCd = parameterCd - ) - if (nzchar(startDate)) { - url <- appendDrURL(url, startDT = startDate) - } - if (nzchar(endDate)) { - url <- appendDrURL(url, endDT = endDate) - } - if (!grepl("(?i)daily", statReportType)) { - url <- appendDrURL(url, missingData = "off") - } - }, - gwlevels = { - - url <- appendDrURL(baseURL, - site_no = siteNumbers, - agency_cd = "USGS", - format = "rdb" - ) - }, - { # this will be either dv, uv, groundwater - multiplePcodes <- length(parameterCd) > 1 - # Check for 5 digit parameter code: - if (multiplePcodes) { - parameterCd <- paste(parameterCd, collapse = ",") - } - - format <- match.arg(format, c("xml", "tsv", "wml1", "wml2", "rdb")) - - formatURL <- switch(format, - xml = "waterml,1.1", - rdb = "rdb,1.0", - tsv = "rdb,1.0", - wml2 = "waterml,2.0", - wml1 = "waterml,1.1" - ) - - url <- appendDrURL(baseURL, - site = siteNumbers, - format = formatURL - ) - - if (!is.na(parameterCd)) { - url <- appendDrURL(url, ParameterCd = parameterCd) - } - - if ("dv" == service) { - if (length(statCd) > 1) { - statCd <- paste(statCd, collapse = ",") - } - url <- appendDrURL(url, StatCd = statCd) - } - - if (nzchar(startDate)) { - url <- appendDrURL(url, startDT = startDate) - } else { - startorgin <- "1851-01-01" - if ("iv" == service) startorgin <- "1900-01-01" - url <- appendDrURL(url, startDT = startorgin) - } - - if (nzchar(endDate)) { - url <- appendDrURL(url, endDT = endDate) - } - } + + # make sure only statTypes allowed for the statReportType are being requested + if (!grepl("(?i)daily", statReportType) && + !all(grepl("(?i)mean", statType)) && + !all(grepl("(?i)all", statType))) { + stop("Monthly and annual report types can only provide means") + } + + # make sure dates aren"t too specific for statReportType + if (grepl("(?i)monthly", statReportType) && + (length(unlist(gregexpr("-", startDate))) > 1 || + length(unlist(gregexpr("-", endDate))) > 1)) { + stop("Start and end dates for monthly statReportType can only include months and years") + } + if (grepl("(?i)annual", statReportType) && (grepl("-", startDate) || grepl("-", endDate))) { + stop("Start and end dates for annual statReportType can only include years") + } + statType <- paste(statType, collapse = ",") + parameterCd <- paste(parameterCd, collapse = ",") + url <- appendDrURL(baseURL, + sites = siteNumbers, + statType = statType, + statReportType = statReportType, + parameterCd = parameterCd + ) + if (nzchar(startDate)) { + url <- appendDrURL(url, startDT = startDate) + } + if (nzchar(endDate)) { + url <- appendDrURL(url, endDT = endDate) + } + if (!grepl("(?i)daily", statReportType)) { + url <- appendDrURL(url, missingData = "off") + } + }, + gwlevels = { + + url <- appendDrURL(baseURL, + site_no = siteNumbers, + agency_cd = "USGS", + format = "rdb" + ) + if (nzchar(startDate)) { + url <- appendDrURL(url, begin_date = startDate) + } + if (nzchar(endDate)) { + url <- appendDrURL(url, end_date = endDate) + } + url <- paste(url, "group_key=NONE", + "date_format=YYYY-MM-DD", + "rdb_compression=value", + sep = "&") + }, + { # this will be either dv, uv, groundwater + multiplePcodes <- length(parameterCd) > 1 + # Check for 5 digit parameter code: + if (multiplePcodes) { + parameterCd <- paste(parameterCd, collapse = ",") + } + + format <- match.arg(format, c("xml", "tsv", "wml1", "wml2", "rdb")) + + formatURL <- switch(format, + xml = "waterml,1.1", + rdb = "rdb,1.0", + tsv = "rdb,1.0", + wml2 = "waterml,2.0", + wml1 = "waterml,1.1" + ) + + url <- appendDrURL(baseURL, + site = siteNumbers, + format = formatURL + ) + + if (!is.na(parameterCd)) { + url <- appendDrURL(url, ParameterCd = parameterCd) + } + + if ("dv" == service) { + if (length(statCd) > 1) { + statCd <- paste(statCd, collapse = ",") + } + url <- appendDrURL(url, StatCd = statCd) + } + + if (nzchar(startDate)) { + url <- appendDrURL(url, startDT = startDate) + } else { + startorgin <- "1851-01-01" + if ("iv" == service) startorgin <- "1900-01-01" + url <- appendDrURL(url, startDT = startorgin) + } + + if (nzchar(endDate)) { + url <- appendDrURL(url, endDT = endDate) + } + } ) - + return(url) } @@ -379,17 +387,17 @@ constructWQPURL <- function(siteNumbers, } baseURL <- paste0(baseURL, "&", parameterCd) - + if (nzchar(startDate)) { startDate <- format(as.Date(startDate), format = "%m-%d-%Y") baseURL <- paste0(baseURL, "&startDateLo=", startDate) } - + if (nzchar(endDate)) { endDate <- format(as.Date(endDate), format = "%m-%d-%Y") baseURL <- paste0(baseURL, "&startDateHi=", endDate) } - + baseURL <- paste0(baseURL, "&mimeType=csv") if(!legacy){ baseURL <- paste0(baseURL, "&dataProfile=narrow") @@ -419,16 +427,16 @@ constructWQPURL <- function(siteNumbers, constructUseURL <- function(years, stateCd, countyCd, categories) { if (is.null(stateCd)) { baseURL <- drURL("useNat", - format = "rdb", - rdb_compression = "value", - Access = pkg.env$access + format = "rdb", + rdb_compression = "value", + Access = pkg.env$access ) } else { stateCd <- stateCdLookup(input = stateCd, outputType = "postal") baseURL <- "https://waterdata.usgs.gov/" base2 <- "nwis/water_use?format=rdb&rdb_compression=value" baseURL <- paste0(baseURL, paste0(stateCd, "/"), base2) - + if (!is.null(countyCd)) { if (length(countyCd) > 1) { countyCd <- paste(countyCd, collapse = "%2C") @@ -441,6 +449,6 @@ constructUseURL <- function(years, stateCd, countyCd, categories) { years <- paste(years, collapse = "%2C") categories <- paste(categories, collapse = "%2C") retURL <- paste0(baseURL, "&wu_year=", years, "&wu_category=", categories) - + return(retURL) -} +} \ No newline at end of file diff --git a/R/readNWISdata.R b/R/readNWISdata.R index 49f59d44..a3afb5bb 100644 --- a/R/readNWISdata.R +++ b/R/readNWISdata.R @@ -118,10 +118,18 @@ #' hasDataTypeCd = "iv", service = "site" #' ) #' temp <- readNWISdata( -#' bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010", service = "site", +#' bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010", +#' service = "site", #' seriesCatalogOutput = TRUE #' ) -#' wiGWL <- readNWISdata(stateCd = "WI", service = "gwlevels") +#' +#' levels <- readNWISdata(state_cd = "WI", +#' service = "gwlevels", +#' begin_date = "2024-05-01", +#' end_date = "2024-05-30", +#' list_of_search_criteria = "state_cd") +#' +#' #' meas <- readNWISdata( #' state_cd = "WI", service = "measurements", #' format = "rdb_expanded" diff --git a/man/constructNWISURL.Rd b/man/constructNWISURL.Rd index 9839abd4..6888b3ce 100644 --- a/man/constructNWISURL.Rd +++ b/man/constructNWISURL.Rd @@ -99,10 +99,8 @@ url_daily_tsv <- constructNWISURL(site_id, pCode, startDate, endDate, "dv", url_rating <- constructNWISURL(site_id, service = "rating", ratingType = "base") url_peak <- constructNWISURL(site_id, service = "peak") url_meas <- constructNWISURL(site_id, service = "meas") -urlQW <- constructNWISURL("450456092225801", "70300", - startDate = "", endDate = "", - "qw", expanded = TRUE -) +url_gwl <- constructNWISURL(site_id, service = "gwlevels", + startDate = "2024-05-01", endDate = "2024-05-30") } \keyword{USGS} \keyword{data} diff --git a/man/readNWISdata.Rd b/man/readNWISdata.Rd index 9a16d072..5279e1ac 100644 --- a/man/readNWISdata.Rd +++ b/man/readNWISdata.Rd @@ -130,10 +130,18 @@ siteInfo <- readNWISdata( hasDataTypeCd = "iv", service = "site" ) temp <- readNWISdata( - bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010", service = "site", + bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010", + service = "site", seriesCatalogOutput = TRUE ) -wiGWL <- readNWISdata(stateCd = "WI", service = "gwlevels") + +levels <- readNWISdata(state_cd = "WI", + service = "gwlevels", + begin_date = "2024-05-01", + end_date = "2024-05-30", + list_of_search_criteria = "state_cd") + + meas <- readNWISdata( state_cd = "WI", service = "measurements", format = "rdb_expanded"