Skip to content

Commit

Permalink
Merge pull request #736 from ldecicco-USGS/main
Browse files Browse the repository at this point in the history
Empty legacy returns fixes
  • Loading branch information
ldecicco-USGS authored Oct 25, 2024
2 parents 062e50c + 9f459fa commit 51f8ffb
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
4 changes: 4 additions & 0 deletions R/importWQP.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ importWQP <- function(obs_url, tz = "UTC",
doc <- obs_url
}

last_chars <- as.character(substr(doc, nchar(doc)-1, nchar(doc)))
if(last_chars != c("\n")){
doc <- paste0(doc, "\n")
}
retval <- suppressWarnings(readr::read_delim(doc,
col_types = readr::cols(.default = "c"),
quote = ifelse(csv, '\"', ""),
Expand Down
10 changes: 5 additions & 5 deletions R/readNWISunit.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ readNWISpeak <- function(siteNumbers,
}


siteInfo <- readNWISsite(siteNumbers)
siteInfo <- suppressMessages(readNWISsite(siteNumbers))
siteInfo <- merge(
x = unique(data[, c("agency_cd", "site_no")]),
y = siteInfo,
Expand Down Expand Up @@ -284,7 +284,7 @@ readNWISrating <- function(siteNumber, type = "base", convertType = TRUE) {
attr(data, "RATING") <- Rat
}

siteInfo <- readNWISsite(siteNumber)
siteInfo <- suppressMessages(readNWISsite(siteNumber))

attr(data, "siteInfo") <- siteInfo
attr(data, "variableInfo") <- NULL
Expand Down Expand Up @@ -404,7 +404,7 @@ readNWISmeas <- function(siteNumbers,
}


siteInfo <- readNWISsite(siteNumbers)
siteInfo <- suppressMessages(readNWISsite(siteNumbers))
siteInfo <- merge(
x = unique(data[, c("agency_cd", "site_no")]),
y = siteInfo,
Expand Down Expand Up @@ -534,7 +534,7 @@ readNWISgwl <- function(siteNumbers,
data$lev_dt <- as.Date(data$lev_dt)
}
}
siteInfo <- readNWISsite(siteNumbers)
siteInfo <- suppressMessages(readNWISsite(siteNumbers))
siteInfo <- merge(
x = unique(data[, c("agency_cd", "site_no")]),
y = siteInfo,
Expand Down Expand Up @@ -641,7 +641,7 @@ readNWISstat <- function(siteNumbers, parameterCd, startDate = "", endDate = "",
convertType = convertType
)

siteInfo <- readNWISsite(siteNumbers)
siteInfo <- suppressMessages(readNWISsite(siteNumbers))

if (nrow(data) > 0) {
siteInfo <- merge(
Expand Down
6 changes: 4 additions & 2 deletions R/readWQPdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,20 @@ readWQPdata <- function(...,

create_WQP_attributes <- function(retval, ...){

siteInfo <- suppressWarnings(whatWQPsites(...))

siteInfo <- suppressWarnings(whatWQPsites(..., legacy = attr(retval, "legacy")))
attr(retval, "siteInfo") <- siteInfo

if(!attr(retval, "legacy")){
attr(retval, "headerInfo") <- wqp_check_status(attr(retval, "headerInfo")$`wqp-request-id`)
attr(retval, "queryTime") <- as.POSIXct(attr(retval, "headerInfo")[["requestStartTime"]],
format = "%Y-%m-%dT%H:%M:%OS", tz = "GMT")
format = "%Y-%m-%dT%H:%M:%OS", tz = "UTC")
} else {
attr(retval, "queryTime") <- Sys.time()
}



return(retval)
}

Expand Down
4 changes: 2 additions & 2 deletions R/readWQPqw.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ readWQPqw <- function(siteNumbers,
}

if (!all(is.na(retval)) && !ignore_attributes) {
retval <- create_WQP_attributes(retval, siteid = sites)
retval <- suppressMessages(create_WQP_attributes(retval, siteid = sites))
}

if(legacy){
wqp_message()
} else {
wqp_message_beta
wqp_message_beta()
}
attr(retval, "url") <- url

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/tests_userFriendly_fxns.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ test_that("WQP qw tests", {

df2 <- readWQPqw("USGS-05427718", parameterCd = "all")
expect_true(nrow(df2) > 0)

#Empty legacy:
df3 <- readWQPqw(siteNumbers = "USGS-385032115220501",
parameterCd = "all", legacy = TRUE)
expect_true(nrow(df3) == 0)

})

context("readNWISstat tests")
Expand Down
8 changes: 1 addition & 7 deletions vignettes/Status.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ knitr::kable(df)

## readWQPqw

The `readWQPqw()` function is generally advertised as a user-friendly function since it only works with a known list of sites, parameter codes or characterisitic names, and start/end dates.
The `readWQPqw()` function is generally advertised as a user-friendly function since it only works with a known list of sites, parameter codes or characteristic names, and start/end dates.

As of `dataRetrieval` 2.7.17, this function will use the default WQX version 2 dataProfile, specified by the `legacy = TRUE` argument. Setting `legacy = FALSE` will return the WQX 3.0 "narrow" dataProfile. Keep in mind the 2.0 profiles will eventually be retired. For any more flexibility, users will need to use the `readWQPdata()` function.

Expand All @@ -82,7 +82,6 @@ library(dataRetrieval)
rawPcode <- readWQPqw(siteNumbers = "USGS-01594440",
parameterCd = "01075",
legacy = FALSE)
attr(rawPcode, "url")
```

Compared to using the WQX 2.0 legacy results:
Expand All @@ -91,7 +90,6 @@ Compared to using the WQX 2.0 legacy results:
rawPcode_legacy <- readWQPqw(siteNumbers = "USGS-01594440",
parameterCd = "01075",
legacy = TRUE)
attr(rawPcode_legacy, "url")
```

Expand Down Expand Up @@ -119,27 +117,23 @@ data_full <- readWQPdata(siteid = "USGS-04024315",
dataProfile = "fullPhysChem",
service = "ResultWQX3")
ncol(data_full)
attr(data_full, "url")
data_basic <- readWQPdata(siteid = "USGS-04024315",
characteristicName = "pH",
dataProfile = "basicPhysChem",
service = "ResultWQX3")
ncol(data_basic)
attr(data_basic, "url")
data_narrow <- readWQPdata(siteid = "USGS-04024315",
characteristicName = "pH",
dataProfile = "narrow",
service = "ResultWQX3")
ncol(data_narrow)
attr(data_narrow, "url")
data_sites <- readWQPdata(siteid = "USGS-04024315",
characteristicName = "pH",
service = "StationWQX3")
ncol(data_sites)
attr(data_sites, "url")
```

Expand Down
7 changes: 5 additions & 2 deletions vignettes/tutorial.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,15 @@ qw_data_sp <- readWQPqw(siteNumbers = paste0("USGS-", site),

This is all great when you know your site numbers. What do you do when you don't?

There are 2 `dataRetrieval` functions that help:
There are 2 `dataRetrieval` functions that help with discover in NWIS:

* `whatNWISsites` finds sites within a specified filter (quicker)
* `whatNWISdata` summarizes the data within the specified filter (more information)

And 2 functions that help with discover in WQP:

* `readWQPsummary` summarizes the data available within the WQP by year.
* `whatWQPdata` summarizes the data available within the WQP.
* `whatWQPdata` summarizes the data available within the WQP.

There are several ways to specify the requests. The best way to discover how flexible the USGS web services are is to click on the links and see all of the filtering options:
[http://waterservices.usgs.gov/](http://waterservices.usgs.gov/)
Expand Down

0 comments on commit 51f8ffb

Please sign in to comment.