From 20e0255b3ea63f7ff1daec15a83dbb904c514e43 Mon Sep 17 00:00:00 2001 From: Laura A DeCicco Date: Thu, 5 Sep 2024 14:36:25 -0500 Subject: [PATCH] when we changed the code for the gwlevel state data, I didn't test how it would affect the whatNWISsites function. --- R/readNWISdata.R | 3 ++- R/whatNWISsites.R | 21 +++++++++++++++++++++ tests/testthat/tests_general.R | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/R/readNWISdata.R b/R/readNWISdata.R index 0e42f305..48f579ab 100644 --- a/R/readNWISdata.R +++ b/R/readNWISdata.R @@ -506,7 +506,8 @@ readNWISdots <- function(...) { } } - if (service %in% c("peak", "gwlevels") && "state_cd" %in% names(values)) { + if (service %in% c("peak", "gwlevels") && "stateCd" %in% names(values)) { + names(values)[names(values) == "stateCd"] <- "state_cd" values["list_of_search_criteria"] <- "state_cd" } diff --git a/R/whatNWISsites.R b/R/whatNWISsites.R index 136790b5..2c61f463 100644 --- a/R/whatNWISsites.R +++ b/R/whatNWISsites.R @@ -34,11 +34,32 @@ #' oneSite <- whatNWISsites(sites = "05114000") #' } whatNWISsites <- function(...) { + + matchReturn <- convertLists(...) valuesList <- readNWISdots(...) values <- sapply(valuesList$values, function(x) utils::URLencode(x)) values["format"] <- "mapper" + + ################# + # temporary gwlevels fixes + values <- values[!names(values) %in% c("date_format", + "TZoutput", + "rdb_inventory_output", + "list_of_search_criteria")] + + names(values)[names(values) == "state_cd"] <- "stateCd" + ################## + + if("service" %in% names(matchReturn)){ + values["hasDataTypeCd"] <- switch(valuesList$service, + "gwlevels" = "gw", + "iv" = "iv", + "dv" = "dv", + "peak" = "pk") + } + urlCall <- drURL("site", Access = pkg.env$access, arg.list = values) rawData <- getWebServiceData(urlCall, encoding = "gzip") diff --git a/tests/testthat/tests_general.R b/tests/testthat/tests_general.R index 6922e9cf..2ca5913b 100644 --- a/tests/testthat/tests_general.R +++ b/tests/testthat/tests_general.R @@ -59,6 +59,24 @@ test_that("General NWIS retrievals working", { ) expect_is(siteInfo$station_nm, "character") + gw_data <- readNWISdata( + stateCd = "AL", + service = "gwlevels", + startDate = "2024-05-01", + endDate = "2024-05-30") + + expect_true(nrow(gw_data) > 0) + expect_equal(attr(gw_data, "url"), + "https://nwis.waterdata.usgs.gov/nwis/gwlevels?state_cd=AL&begin_date=2024-05-01&end_date=2024-05-30&date_format=YYYY-MM-DD&rdb_inventory_output=file&TZoutput=0&range_selection=date_range&list_of_search_criteria=state_cd&format=rdb") + + gw_data2 <- readNWISdata( + state_cd = "AL", + service = "gwlevels", + startDate = "2024-05-01", + endDate = "2024-05-30") + + expect_equal(nrow(gw_data), nrow(gw_data2)) + # nolint start: line_length_linter url <- "https://waterservices.usgs.gov/nwis/dv/?site=09037500&format=rdb&ParameterCd=00060&StatCd=00003&startDT=1985-10-02&endDT=2012-09-06" dv <- importRDB1(url, asDateTime = FALSE) @@ -346,6 +364,11 @@ test_that("whatNWISsites working", { bboxSites <- whatNWISsites(bbox = c(-92.5, 45.4, -87, 47), parameterCd = "00060") expect_true(nrow(bboxSites) > 0) expect_true(is.numeric(bboxSites$dec_lat_va)) + + #gwlevels: + info <- whatNWISsites(stateCd = "NY", service="gwlevels") + expect_true(nrow(info) > 0) + expect_equal(attr(info, "url"), "https://waterservices.usgs.gov/nwis/site/?stateCd=NY&format=mapper&hasDataTypeCd=gw") }) context("readWQPdots")