Skip to content

Commit d813ec2

Browse files
committed
Fixes #720
1 parent 8d426d2 commit d813ec2

File tree

3 files changed

+83
-87
lines changed

3 files changed

+83
-87
lines changed

R/readNWISdata.R

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,11 @@
9191
#' tz = "America/Chicago"
9292
#' )
9393
#'
94-
#' # Empty:
95-
#' multiSite <- readNWISdata(
96-
#' sites = c("04025000", "04072150"), service = "iv",
97-
#' parameterCd = "00010"
98-
#' )
99-
#' # Not empty:
10094
#' multiSite <- readNWISdata(
10195
#' sites = c("04025500", "040263491"),
10296
#' service = "iv", parameterCd = "00060"
10397
#' )
98+
#'
10499
#' bBoxEx <- readNWISdata(bBox = c(-83, 36.5, -81, 38.5), parameterCd = "00010")
105100
#'
106101
#' startDate <- as.Date("2013-10-01")
@@ -118,17 +113,19 @@
118113
#' hasDataTypeCd = "iv", service = "site"
119114
#' )
120115
#' temp <- readNWISdata(
121-
#' bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010",
122-
#' service = "site",
116+
#' bBox = c(-83, 36.5, -82.5, 36.75), parameterCd = "00010", service = "site",
123117
#' seriesCatalogOutput = TRUE
124118
#' )
125-
#'
119+
#' GWL <- readNWISdata(site_no = c("392725077582401",
120+
#' "375907091432201"),
121+
#' parameterCd = "62610",
122+
#' service = "gwlevels")
123+
#'
126124
#' levels <- readNWISdata(stateCd = "WI",
127125
#' service = "gwlevels",
128126
#' startDate = "2024-05-01",
129127
#' endDate = "2024-05-30")
130-
#'
131-
#'
128+
#'
132129
#' meas <- readNWISdata(
133130
#' state_cd = "WI", service = "measurements",
134131
#' format = "rdb_expanded"
@@ -202,15 +199,15 @@
202199
#' }
203200
readNWISdata <- function(..., asDateTime = TRUE, convertType = TRUE, tz = "UTC") {
204201
tz <- match.arg(tz, OlsonNames())
205-
202+
206203
valuesList <- readNWISdots(...)
207-
204+
208205
service <- valuesList$service
209206
if (length(service) > 1) {
210207
warning("Only one service value is allowed. Service: ", service[1], " will be used.")
211208
service <- service[1]
212209
}
213-
210+
214211
if (any(service %in% c("qw", "qwdata"))) {
215212
.Deprecated(
216213
old = "readNWISdata", package = "dataRetrieval",
@@ -221,12 +218,12 @@ for more information.
221218
https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.html"
222219
)
223220
}
224-
221+
225222
values <- sapply(valuesList$values, function(x)utils:: URLencode(x))
226-
223+
227224
baseURL <- drURL(service, arg.list = values)
228-
229-
if (service %in% c("site", "dv", "iv", "gwlevels")) {
225+
226+
if (service %in% c("site", "dv", "iv")) {
230227
baseURL <- appendDrURL(baseURL, Access = pkg.env$access)
231228
}
232229
# actually get the data
@@ -238,7 +235,7 @@ https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.h
238235
} else {
239236
retval <- importWaterML1(baseURL, tz = tz, asDateTime = asDateTime)
240237
}
241-
238+
242239
if ("dv" == service) {
243240
tzLib <- stats::setNames(
244241
c(
@@ -263,15 +260,17 @@ https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.h
263260
retval$dateTime <- as.POSIXct(retval$dateTime, tzLib[tz = retval$tz_cd[1]])
264261
}
265262
}
266-
263+
267264
if ("iv" == service || "iv_recent" == service) {
268265
if (tz == "") {
269266
retval$tz_cd <- rep("UTC", nrow(retval))
270267
} else {
271268
retval$tz_cd <- rep(tz, nrow(retval))
272269
}
270+
} else if("gwlevels" == service && "parameterCd" %in% names(values)){
271+
retval <- retval[retval$parameter_cd %in% values[["parameterCd"]], ]
273272
}
274-
273+
275274
return(retval)
276275
}
277276

@@ -293,7 +292,7 @@ https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.h
293292
#' stateCdLookup(c("West Virginia", "Wisconsin", 200, 55, "MN"))
294293
stateCdLookup <- function(input, outputType = "postal") {
295294
outputType <- match.arg(outputType, c("postal", "fullName", "tableIndex", "id"))
296-
295+
297296
retVal <- rep(NA, length(input))
298297
index <- 1
299298
for (i in input) {
@@ -304,24 +303,24 @@ stateCdLookup <- function(input, outputType = "postal") {
304303
} else {
305304
i <- which(tolower(i) == tolower(stateCd$STATE_NAME))
306305
}
307-
306+
308307
if (length(i) > 0) {
309308
output <- switch(outputType,
310-
postal = stateCd$STUSAB[i],
311-
fullName = stateCd$STATE_NAME[i],
312-
tableIndex = i,
313-
id = as.integer(stateCd$STATE[i])
309+
postal = stateCd$STUSAB[i],
310+
fullName = stateCd$STATE_NAME[i],
311+
tableIndex = i,
312+
id = as.integer(stateCd$STATE[i])
314313
)
315314
retVal[index] <- output
316315
}
317-
316+
318317
index <- index + 1
319318
}
320-
319+
321320
if (length(retVal[-1]) == 0) {
322321
paste("Could not find", input, "in the state lookup table. See `stateCd` for complete list.")
323322
}
324-
323+
325324
return(retVal)
326325
}
327326

@@ -342,31 +341,31 @@ stateCdLookup <- function(input, outputType = "postal") {
342341
#' already_correct <- countyCdLookup(county = "51001")
343342
countyCdLookup <- function(state, county, outputType = "id") {
344343
outputType <- match.arg(outputType, c("fullName", "tableIndex", "id", "fullEntry"))
345-
344+
346345
if (missing(state)) {
347346
return(county)
348347
}
349-
348+
350349
if (missing(county)) {
351350
stop("No county code provided")
352351
}
353-
352+
354353
if (length(state) > 1) {
355354
stop("Only one state allowed in countyCdLookup.")
356355
}
357-
356+
358357
# first turn state into stateCd postal name
359358
stateCd <- stateCdLookup(state, outputType = "postal")
360359
state_counties <- countyCd[countyCd$STUSAB == stateCd, ]
361-
360+
362361
if (is.numeric(county) || !is.na(suppressWarnings(as.numeric(county)))) {
363362
county_i <- which(as.numeric(county) == as.numeric(countyCd$COUNTY) & stateCd == countyCd$STUSAB)
364363
} else {
365364
county_in_state <- grep(tolower(county), tolower(state_counties$COUNTY_NAME))
366-
365+
367366
county_i <- which(countyCd$STUSAB == stateCd &
368-
countyCd$COUNTY_NAME == state_counties$COUNTY_NAME[county_in_state])
369-
367+
countyCd$COUNTY_NAME == state_counties$COUNTY_NAME[county_in_state])
368+
370369
if (length(county_i) == 0) {
371370
stop(paste(
372371
"Could not find", county, "(county), ", stateCd,
@@ -379,14 +378,14 @@ countyCdLookup <- function(state, county, outputType = "id") {
379378
))
380379
}
381380
}
382-
381+
383382
retVal <- switch(outputType,
384-
fullName = countyCd$COUNTY_NAME[county_i],
385-
tableIndex = county_i,
386-
id = countyCd$COUNTY[county_i],
387-
fullEntry = countyCd[county_i, ]
383+
fullName = countyCd$COUNTY_NAME[county_i],
384+
tableIndex = county_i,
385+
id = countyCd$COUNTY[county_i],
386+
fullEntry = countyCd[county_i, ]
388387
)
389-
388+
390389
return(retVal)
391390
}
392391

@@ -398,51 +397,51 @@ readNWISdots <- function(...) {
398397
if (length(list(...)) == 0) {
399398
stop("No arguments supplied")
400399
}
401-
400+
402401
matchReturn <- convertLists(...)
403-
402+
404403
if (anyNA(unlist(matchReturn))) {
405404
stop("NA's are not allowed in query")
406405
}
407-
406+
408407
if ("service" %in% names(matchReturn)) {
409408
service <- matchReturn$service
410409
matchReturn$service <- NULL
411410
} else {
412411
service <- "dv"
413412
}
414-
413+
415414
match.arg(service, c(
416415
"dv", "iv", "iv_recent", "gwlevels",
417416
"site", "uv", "qw", "measurements",
418417
"qwdata", "stat", "rating", "peak"
419418
))
420-
419+
421420
if (service == "uv") {
422421
service <- "iv"
423422
} else if (service == "qw") {
424423
service <- "qwdata"
425424
}
426-
425+
427426
if (length(service) > 1) {
428427
stop("Only one service call allowed.")
429428
}
430-
429+
431430
values <- sapply(matchReturn, function(x) as.character(paste0(eval(x), collapse = ",")))
432-
431+
433432
names(values)[names(values) == "startDate"] <- "startDT"
434433
names(values)[names(values) == "endDate"] <- "endDT"
435434
names(values)[names(values) == "siteNumber"] <- "sites"
436435
names(values)[names(values) == "siteNumbers"] <- "sites"
437-
436+
438437
format.default <- "waterml,1.1"
439-
438+
440439
if (service == "iv" && "startDT" %in% names(values)) {
441440
if (as.Date(values[["startDT"]]) >= Sys.Date() - 120) {
442441
service <- "iv_recent"
443442
}
444443
}
445-
444+
446445
names(values)[names(values) == "statecode"] <- "stateCd"
447446
if ("stateCd" %in% names(values)) {
448447

@@ -458,15 +457,15 @@ readNWISdots <- function(...) {
458457
stop("NWIS does not include U.S. Minor Outlying Islands")
459458
}
460459
}
461-
460+
462461
if ("parameterCd" %in% names(matchReturn)) {
463462
pcodeCheck <- (nchar(matchReturn$parameterCd) == 5) & !is.na(suppressWarnings(as.numeric(matchReturn$parameterCd)))
464463
if (!all(pcodeCheck)) {
465464
badPcode <- matchReturn$parameterCd[which(!pcodeCheck)]
466465
stop("The following pCodes appear mistyped:", paste(badPcode, collapse = ", "))
467466
}
468467
}
469-
468+
470469
names(values)[names(values) == "countycode"] <- "countyCd"
471470
if ("countyCd" %in% names(values)) {
472471
if ("stateCd" %in% names(values)) {
@@ -477,14 +476,14 @@ readNWISdots <- function(...) {
477476
values <- values[names(values) != "stateCd"]
478477
}
479478
}
480-
479+
481480
if (service %in% c("peak", "qwdata", "measurements", "gwlevels")) {
482481
format.default <- "rdb"
483-
482+
484483
names(values)[names(values) == "startDT"] <- "begin_date"
485484
names(values)[names(values) == "endDT"] <- "end_date"
486485
names(values)[names(values) == "sites"] <- "site_no"
487-
486+
488487
if ("bBox" %in% names(values)) {
489488
values["nw_longitude_va"] <- as.character(matchReturn$bBox[1])
490489
values["nw_latitude_va"] <- as.character(matchReturn$bBox[2])
@@ -493,49 +492,49 @@ readNWISdots <- function(...) {
493492
values["coordinate_format"] <- "decimal_degrees"
494493
values <- values[-which("bBox" %in% names(values))]
495494
}
496-
495+
497496
values["date_format"] <- "YYYY-MM-DD"
498497
values["rdb_inventory_output"] <- "file"
499498
values["TZoutput"] <- "0"
500-
499+
501500
if (all(c("begin_date", "end_date") %in% names(values))) {
502501
values["range_selection"] <- "date_range"
503502
}
504-
503+
505504
if (service == "qwdata" && !("qw_sample_wide" %in% names(values))) {
506505
values["qw_sample_wide"] <- "wide"
507506
}
508507
}
509-
508+
510509
if (service %in% c("peak", "gwlevels") && "state_cd" %in% names(values)) {
511510
values["list_of_search_criteria"] <- "state_cd"
512511
}
513-
514-
if (service%in% c("peak", "gwlevels") && "huc2_cd" %in% names(values)) {
512+
513+
if (service %in% c("peak", "gwlevels") && "huc2_cd" %in% names(values)) {
515514
values["list_of_search_criteria"] <- "huc2_cd"
516515
}
517-
516+
517+
if(service == "gwlevels" && "aquiferCd" %in% names(values)){
518+
values["aquiferCd"] <- "nat_aqfr_cd"
519+
}
520+
518521
if (service %in% c("peak", "gwlevels") && "bBox" %in% names(values)) {
519522
values["list_of_search_criteria"] <- "lat_long_bounding_box"
520523
}
521-
522-
if (service == "gwlevels" && "aquiferCd" %in% names(values)) {
523-
values["aquiferCd"] <- "nat_aqfr_cd"
524-
}
525524

526525
if (service %in% c("site", "gwlevels", "stat", "rating", "peak")) {
527526
format.default <- "rdb"
528527
}
529-
528+
530529
if (service == "stat") {
531530
message("Please be aware the NWIS data service feeding this function is in BETA.\n
532531
Data formatting could be changed at any time, and is not guaranteed")
533532
}
534-
533+
535534
if (!("format" %in% names(values))) {
536535
values["format"] <- format.default
537536
}
538-
537+
539538
return(list(values = values, service = service))
540539
}
541540

@@ -548,4 +547,4 @@ convertLists <- function(...) {
548547
list(...)[sapply(list(...), class) != "list"]
549548
) # get the non-list parts
550549
return(matchReturn)
551-
}
550+
}

R/readNWISunit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ readNWISgwl <- function(siteNumbers,
521521
tz = tz
522522
)
523523

524-
if(!is.na(parameterCd)){
524+
if(!all(is.na(parameterCd))){
525525
data <- data[data$parameter_cd %in% parameterCd, ]
526526
}
527527

0 commit comments

Comments
 (0)