Skip to content

Commit 12fe6c8

Browse files
Merge pull request DOI-USGS#866 from ldecicco-USGS/develop
Add wildcard CQL
2 parents df7edad + 7cdbe0c commit 12fe6c8

25 files changed

Lines changed: 285 additions & 173 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ vignettes/*.R
1717

1818

1919
/.quarto/
20+
21+
**/*.quarto_ipynb

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: dataRetrieval
22
Type: Package
33
Title: Retrieval Functions for USGS and EPA Hydrology and Water Quality Data
4-
Version: 2.7.22.9001
4+
Version: 2.7.23
55
Authors@R: c(
66
person("Laura", "DeCicco", role = c("aut","cre"),
77
email = "ldecicco@usgs.gov",

NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
dataRetrieval 2.7.22.9000
1+
dataRetrieval 2.7.23
22
===================
33
* Added data.table to Imports
44
* Added read_waterdata_stats_por and read_waterdata_stats_daterange to access USGS daily data statistics.
@@ -16,6 +16,9 @@ the request attribute
1616
* Added read_waterdata_field_meta, read_waterdata_combine_meta,
1717
and read_waterdata_channel
1818
* Removed readNWISgwl and readNWISmeas as services have been turned off
19+
* Updated CQL2 templates to allow HUC queries specifically to use a wildcard
20+
to get multiple inclusive HUCs.
21+
* Add deprecation message to readNGWMN functions. read_ngwmn will be coming soon.
1922

2023

2124
dataRetrieval 2.7.22

R/citations.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#'
1414
#' @examples
1515
#' \donttest{
16-
#' nwisData <- readNWISdv("04085427", "00060", "2012-01-01", "2012-06-30")
16+
#' nwisData <- read_waterdata_daily(monitoring_location_id = "USGS-04085427",
17+
#' parameter_code = "00060",
18+
#' time = c("2012-01-01", "2012-06-30"))
1719
#' nwis_citation <- create_NWIS_bib(nwisData)
1820
#' nwis_citation
1921
#'
@@ -24,16 +26,14 @@ create_NWIS_bib <- function(x){
2426

2527
textVersion <- paste0("U.S. Geological Survey, ",
2628
format(attr(x, "queryTime"), "%Y"),
27-
", National Water Information System data available on the World Wide Web (USGS Water Data for the Nation), accessed ",
29+
", USGS Water Data for the Nation: U.S. Geological Survey National Water Information System database, accessed ",
2830
format(attr(x, "queryTime"), "%b %d, %Y"),
29-
", at ",
30-
attr(x, "url"),
31-
", https://dx.doi.org/10.5066/F7P55KJN")
31+
", at https://dx.doi.org/10.5066/F7P55KJN")
3232

3333
ref <- utils::bibentry(
3434
bibtype = "Manual",
3535
textVersion = textVersion,
36-
title = "National Water Information System data available on the World Wide Web (USGS Water Data for the Nation)",
36+
title = "USGS Water Data for the Nation: U.S. Geological Survey National Water Information System database",
3737
author = utils::person("U.S. Geological Survey"),
3838
doi = "10.5066/F7P55KJN",
3939
note = paste("Accessed", format(attr(x, "queryTime"), "%b %d, %Y")),

R/construct_api_requests.R

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ construct_api_requests <- function(service,
5454
"computation_identifier")
5555

5656
if(service %in% c("monitoring-locations", "parameter-codes",
57-
"time-series-metadata")){
57+
"time-series-metadata",
58+
"field-measurements-metadata",
59+
"combined-metadata")){
5860
comma_params <- c(comma_params, "id")
5961
}
6062

@@ -138,14 +140,19 @@ construct_api_requests <- function(service,
138140
baseURL <- baseURL |>
139141
httr2::req_headers(`Content-Type` = "application/query-cql-json")
140142

141-
post_params <- list(
142-
"params" = unname(post_params)
143-
)
144-
145-
template_path_post <- system.file("templates/post.CQL2", package = "dataRetrieval")
146-
template_post <- readChar(template_path_post, file.info(template_path_post)$size)
143+
if(length(post_params) > 1){
144+
post_params <- list(
145+
"params" = unname(post_params)
146+
)
147+
148+
template_path_post <- system.file("templates/post.CQL2", package = "dataRetrieval")
149+
template_post <- readChar(template_path_post, file.info(template_path_post)$size)
150+
151+
x <- whisker::whisker.render(template_post, post_params)
152+
} else {
153+
x <- post_params[[1]]
154+
}
147155

148-
x <- whisker::whisker.render(template_post, post_params)
149156
baseURL <- httr2::req_body_raw(baseURL, x)
150157

151158
} else {
@@ -442,14 +449,39 @@ explode_post <- function(ls){
442449
#' dataRetrieval:::cql2_param(parameter)
443450
#'
444451
cql2_param <- function(parameter){
445-
template_path <- system.file("templates/param.CQL2", package = "dataRetrieval")
446-
template <- readChar(template_path, file.info(template_path)$size)
447-
448-
parameters <- paste0(unlist(parameter), collapse = '", "')
449-
parameters <- paste0('"', parameters, '"')
450-
parameter_list <- list("property" = names(parameter),
451-
"parameter" = parameters)
452-
return(whisker::whisker.render(template, parameter_list))
452+
453+
# Wildcards:
454+
if(names(parameter) %in% c("hydrologic_unit_code")){
455+
template_path <- system.file("templates/param.CQL2.like", package = "dataRetrieval")
456+
template <- readChar(template_path, file.info(template_path)$size)
457+
458+
params <- c()
459+
for(i in parameter[[1]]){
460+
parameter_list <- list("property" = names(parameter),
461+
"parameter" = i)
462+
params <- c(params,
463+
whisker::whisker.render(template, parameter_list))
464+
}
465+
template_path_or <- system.file("templates/post.CQL2.or", package = "dataRetrieval")
466+
template_or <- readChar(template_path_or, file.info(template_path_or)$size)
467+
468+
post_params <- list(
469+
"params" = paste0(params, collapse = ", ")
470+
)
471+
cql_text <- whisker::whisker.render(template_or, post_params)
472+
} else { # INs
473+
parameters <- paste0(unlist(parameter), collapse = '", "')
474+
parameters <- paste0('"', parameters, '"')
475+
parameter_list <- list("property" = names(parameter),
476+
"parameter" = parameters)
477+
478+
template_path <- system.file("templates/param.CQL2", package = "dataRetrieval")
479+
template <- readChar(template_path, file.info(template_path)$size)
480+
481+
cql_text <- whisker::whisker.render(template, parameter_list)
482+
}
483+
484+
return(cql_text)
453485
}
454486

455487

R/get_ogc_data.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ get_ogc_data <- function(args,
5353
# Mostly drop the id column except ts-meta, monitoring location:
5454
if(!service %in% c("monitoring-locations",
5555
"time-series-metadata",
56+
"field-measurements-metadata",
57+
"combined-metadata",
5658
"parameter-codes")){
5759
return_list <- return_list[, names(return_list)[names(return_list)!= output_id]]
5860
}
@@ -88,9 +90,9 @@ move_id_col <- function(df, output_id){
8890
"time_series_id")]
8991
}
9092

91-
if("field_visit_id" %in% names(df)){
92-
df <- df[, c(names(df)[names(df)!= "field_visit_id"],
93-
"field_visit_id")]
93+
if("field_series_id" %in% names(df)){
94+
df <- df[, c(names(df)[names(df)!= "field_series_id"],
95+
"field_series_id")]
9496
}
9597

9698
return(df)

R/importWQP.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ importWQP <- function(obs_url, tz = "UTC",
6262
}
6363

6464
last_chars <- as.character(substr(doc, nchar(doc)-1, nchar(doc)))
65-
66-
if(last_chars != c("\n")){
67-
doc <- paste0(doc, "\n")
68-
}
69-
retval <- suppressWarnings(readr::read_delim(doc,
70-
col_types = readr::cols(.default = "c"),
71-
quote = ifelse(csv, '\"', ""),
72-
delim = ifelse(csv, ",", "\t")))
65+
66+
retval <- data.table::fread(text = doc, data.table = FALSE,
67+
sep = ifelse(csv, ",", "\t"),
68+
fill = TRUE,
69+
quote = ifelse(csv, '\"', ""))
70+
7371

7472
attr(retval, 'spec') <- NULL
7573

R/readNGWMNdata.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
#' }
3939
#'
4040
readNGWMNdata <- function(service, ..., asDateTime = TRUE, tz = "UTC") {
41-
message("DISCLAIMER: NGWMN retrieval functions are still in flux,
42-
and no future behavior or output is guaranteed")
41+
42+
.Deprecated("",
43+
msg = "read_ngwmn_data coming soon. Check back at
44+
https://doi-usgs.github.io/dataRetrieval/articles/Status.html
45+
for more information")
4346

4447
dots <- convertLists(...)
4548

R/read_waterdata_combined_meta.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@
121121
#' monitoring_location_id = c("USGS-07069000",
122122
#' "USGS-07064000",
123123
#' "USGS-07068000"),
124-
#' #end = c("2024-07-01T00:00:00Z", NA),
124+
#' end = "P1M",
125125
#' parameter_code = "00060")
126126
#'
127-
#'
127+
#' hucs <- read_waterdata_combined_meta(
128+
#' hydrologic_unit_code = c("11010008", "11010009"),
129+
#' site_type = c("Stream", "Spring")
130+
#' )
128131
#' }
129132
read_waterdata_combined_meta <- function(monitoring_location_id = NA_character_,
130133
parameter_code = NA_character_,

R/read_waterdata_field_measurements.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#' @param field_visit_id `r get_ogc_params("field-measurements")$field_visit_id`
2323
#' @param vertical_datum `r get_ogc_params("field-measurements")$vertical_datum`
2424
#' @param measuring_agency `r get_ogc_params("field-measurements")$measuring_agency`
25+
#' @param control_condition `r get_ogc_params("field-measurements")$control_condition`
26+
#' What and where the control of flow is for the gage pool.
27+
#' @param measurement_rated `r get_ogc_params("field-measurements")$measurement_rated`
28+
#' Rated measurement based on the hydrologic/hydraulic conditions in which the measurement was made
29+
#' (excellent (2 percent), good (5 percent), fair (8 percent), or poor (more than 8 percent). percent)
2530
#' @param properties A vector of requested columns to be returned from the query.
2631
#' Available options are:
2732
#' `r dataRetrieval:::get_properties_for_docs("field-measurements", "field_measurement_id")`.
@@ -100,6 +105,8 @@ read_waterdata_field_measurements <- function(monitoring_location_id = NA_charac
100105
observing_procedure = NA_character_,
101106
vertical_datum = NA_character_,
102107
measuring_agency = NA_character_,
108+
control_condition = NA_character_,
109+
measurement_rated = NA_character_,
103110
skipGeometry = NA,
104111
time = NA_character_,
105112
bbox = NA,

0 commit comments

Comments
 (0)