91
91
# ' tz = "America/Chicago"
92
92
# ' )
93
93
# '
94
- # ' # Empty:
95
- # ' multiSite <- readNWISdata(
96
- # ' sites = c("04025000", "04072150"), service = "iv",
97
- # ' parameterCd = "00010"
98
- # ' )
99
- # ' # Not empty:
100
94
# ' multiSite <- readNWISdata(
101
95
# ' sites = c("04025500", "040263491"),
102
96
# ' service = "iv", parameterCd = "00060"
103
97
# ' )
98
+ # '
104
99
# ' bBoxEx <- readNWISdata(bBox = c(-83, 36.5, -81, 38.5), parameterCd = "00010")
105
100
# '
106
101
# ' startDate <- as.Date("2013-10-01")
118
113
# ' hasDataTypeCd = "iv", service = "site"
119
114
# ' )
120
115
# ' 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",
123
117
# ' seriesCatalogOutput = TRUE
124
118
# ' )
125
- # '
119
+ # ' GWL <- readNWISdata(site_no = c("392725077582401",
120
+ # ' "375907091432201"),
121
+ # ' parameterCd = "62610",
122
+ # ' service = "gwlevels")
123
+ # '
126
124
# ' levels <- readNWISdata(stateCd = "WI",
127
125
# ' service = "gwlevels",
128
126
# ' startDate = "2024-05-01",
129
127
# ' endDate = "2024-05-30")
130
- # '
131
- # '
128
+ # '
132
129
# ' meas <- readNWISdata(
133
130
# ' state_cd = "WI", service = "measurements",
134
131
# ' format = "rdb_expanded"
202
199
# ' }
203
200
readNWISdata <- function (... , asDateTime = TRUE , convertType = TRUE , tz = " UTC" ) {
204
201
tz <- match.arg(tz , OlsonNames())
205
-
202
+
206
203
valuesList <- readNWISdots(... )
207
-
204
+
208
205
service <- valuesList $ service
209
206
if (length(service ) > 1 ) {
210
207
warning(" Only one service value is allowed. Service: " , service [1 ], " will be used." )
211
208
service <- service [1 ]
212
209
}
213
-
210
+
214
211
if (any(service %in% c(" qw" , " qwdata" ))) {
215
212
.Deprecated(
216
213
old = " readNWISdata" , package = " dataRetrieval" ,
@@ -221,12 +218,12 @@ for more information.
221
218
https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.html"
222
219
)
223
220
}
224
-
221
+
225
222
values <- sapply(valuesList $ values , function (x )utils :: URLencode(x ))
226
-
223
+
227
224
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" )) {
230
227
baseURL <- appendDrURL(baseURL , Access = pkg.env $ access )
231
228
}
232
229
# actually get the data
@@ -238,7 +235,7 @@ https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.h
238
235
} else {
239
236
retval <- importWaterML1(baseURL , tz = tz , asDateTime = asDateTime )
240
237
}
241
-
238
+
242
239
if (" dv" == service ) {
243
240
tzLib <- stats :: setNames(
244
241
c(
@@ -263,15 +260,17 @@ https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.h
263
260
retval $ dateTime <- as.POSIXct(retval $ dateTime , tzLib [tz = retval $ tz_cd [1 ]])
264
261
}
265
262
}
266
-
263
+
267
264
if (" iv" == service || " iv_recent" == service ) {
268
265
if (tz == " " ) {
269
266
retval $ tz_cd <- rep(" UTC" , nrow(retval ))
270
267
} else {
271
268
retval $ tz_cd <- rep(tz , nrow(retval ))
272
269
}
270
+ } else if (" gwlevels" == service && " parameterCd" %in% names(values )){
271
+ retval <- retval [retval $ parameter_cd %in% values [[" parameterCd" ]], ]
273
272
}
274
-
273
+
275
274
return (retval )
276
275
}
277
276
@@ -293,7 +292,7 @@ https://cran.r-project.org/web/packages/dataRetrieval/vignettes/qwdata_changes.h
293
292
# ' stateCdLookup(c("West Virginia", "Wisconsin", 200, 55, "MN"))
294
293
stateCdLookup <- function (input , outputType = " postal" ) {
295
294
outputType <- match.arg(outputType , c(" postal" , " fullName" , " tableIndex" , " id" ))
296
-
295
+
297
296
retVal <- rep(NA , length(input ))
298
297
index <- 1
299
298
for (i in input ) {
@@ -304,24 +303,24 @@ stateCdLookup <- function(input, outputType = "postal") {
304
303
} else {
305
304
i <- which(tolower(i ) == tolower(stateCd $ STATE_NAME ))
306
305
}
307
-
306
+
308
307
if (length(i ) > 0 ) {
309
308
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 ])
314
313
)
315
314
retVal [index ] <- output
316
315
}
317
-
316
+
318
317
index <- index + 1
319
318
}
320
-
319
+
321
320
if (length(retVal [- 1 ]) == 0 ) {
322
321
paste(" Could not find" , input , " in the state lookup table. See `stateCd` for complete list." )
323
322
}
324
-
323
+
325
324
return (retVal )
326
325
}
327
326
@@ -342,31 +341,31 @@ stateCdLookup <- function(input, outputType = "postal") {
342
341
# ' already_correct <- countyCdLookup(county = "51001")
343
342
countyCdLookup <- function (state , county , outputType = " id" ) {
344
343
outputType <- match.arg(outputType , c(" fullName" , " tableIndex" , " id" , " fullEntry" ))
345
-
344
+
346
345
if (missing(state )) {
347
346
return (county )
348
347
}
349
-
348
+
350
349
if (missing(county )) {
351
350
stop(" No county code provided" )
352
351
}
353
-
352
+
354
353
if (length(state ) > 1 ) {
355
354
stop(" Only one state allowed in countyCdLookup." )
356
355
}
357
-
356
+
358
357
# first turn state into stateCd postal name
359
358
stateCd <- stateCdLookup(state , outputType = " postal" )
360
359
state_counties <- countyCd [countyCd $ STUSAB == stateCd , ]
361
-
360
+
362
361
if (is.numeric(county ) || ! is.na(suppressWarnings(as.numeric(county )))) {
363
362
county_i <- which(as.numeric(county ) == as.numeric(countyCd $ COUNTY ) & stateCd == countyCd $ STUSAB )
364
363
} else {
365
364
county_in_state <- grep(tolower(county ), tolower(state_counties $ COUNTY_NAME ))
366
-
365
+
367
366
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
+
370
369
if (length(county_i ) == 0 ) {
371
370
stop(paste(
372
371
" Could not find" , county , " (county), " , stateCd ,
@@ -379,14 +378,14 @@ countyCdLookup <- function(state, county, outputType = "id") {
379
378
))
380
379
}
381
380
}
382
-
381
+
383
382
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 , ]
388
387
)
389
-
388
+
390
389
return (retVal )
391
390
}
392
391
@@ -398,51 +397,51 @@ readNWISdots <- function(...) {
398
397
if (length(list (... )) == 0 ) {
399
398
stop(" No arguments supplied" )
400
399
}
401
-
400
+
402
401
matchReturn <- convertLists(... )
403
-
402
+
404
403
if (anyNA(unlist(matchReturn ))) {
405
404
stop(" NA's are not allowed in query" )
406
405
}
407
-
406
+
408
407
if (" service" %in% names(matchReturn )) {
409
408
service <- matchReturn $ service
410
409
matchReturn $ service <- NULL
411
410
} else {
412
411
service <- " dv"
413
412
}
414
-
413
+
415
414
match.arg(service , c(
416
415
" dv" , " iv" , " iv_recent" , " gwlevels" ,
417
416
" site" , " uv" , " qw" , " measurements" ,
418
417
" qwdata" , " stat" , " rating" , " peak"
419
418
))
420
-
419
+
421
420
if (service == " uv" ) {
422
421
service <- " iv"
423
422
} else if (service == " qw" ) {
424
423
service <- " qwdata"
425
424
}
426
-
425
+
427
426
if (length(service ) > 1 ) {
428
427
stop(" Only one service call allowed." )
429
428
}
430
-
429
+
431
430
values <- sapply(matchReturn , function (x ) as.character(paste0(eval(x ), collapse = " ," )))
432
-
431
+
433
432
names(values )[names(values ) == " startDate" ] <- " startDT"
434
433
names(values )[names(values ) == " endDate" ] <- " endDT"
435
434
names(values )[names(values ) == " siteNumber" ] <- " sites"
436
435
names(values )[names(values ) == " siteNumbers" ] <- " sites"
437
-
436
+
438
437
format.default <- " waterml,1.1"
439
-
438
+
440
439
if (service == " iv" && " startDT" %in% names(values )) {
441
440
if (as.Date(values [[" startDT" ]]) > = Sys.Date() - 120 ) {
442
441
service <- " iv_recent"
443
442
}
444
443
}
445
-
444
+
446
445
names(values )[names(values ) == " statecode" ] <- " stateCd"
447
446
if (" stateCd" %in% names(values )) {
448
447
@@ -458,15 +457,15 @@ readNWISdots <- function(...) {
458
457
stop(" NWIS does not include U.S. Minor Outlying Islands" )
459
458
}
460
459
}
461
-
460
+
462
461
if (" parameterCd" %in% names(matchReturn )) {
463
462
pcodeCheck <- (nchar(matchReturn $ parameterCd ) == 5 ) & ! is.na(suppressWarnings(as.numeric(matchReturn $ parameterCd )))
464
463
if (! all(pcodeCheck )) {
465
464
badPcode <- matchReturn $ parameterCd [which(! pcodeCheck )]
466
465
stop(" The following pCodes appear mistyped:" , paste(badPcode , collapse = " , " ))
467
466
}
468
467
}
469
-
468
+
470
469
names(values )[names(values ) == " countycode" ] <- " countyCd"
471
470
if (" countyCd" %in% names(values )) {
472
471
if (" stateCd" %in% names(values )) {
@@ -477,14 +476,14 @@ readNWISdots <- function(...) {
477
476
values <- values [names(values ) != " stateCd" ]
478
477
}
479
478
}
480
-
479
+
481
480
if (service %in% c(" peak" , " qwdata" , " measurements" , " gwlevels" )) {
482
481
format.default <- " rdb"
483
-
482
+
484
483
names(values )[names(values ) == " startDT" ] <- " begin_date"
485
484
names(values )[names(values ) == " endDT" ] <- " end_date"
486
485
names(values )[names(values ) == " sites" ] <- " site_no"
487
-
486
+
488
487
if (" bBox" %in% names(values )) {
489
488
values [" nw_longitude_va" ] <- as.character(matchReturn $ bBox [1 ])
490
489
values [" nw_latitude_va" ] <- as.character(matchReturn $ bBox [2 ])
@@ -493,49 +492,49 @@ readNWISdots <- function(...) {
493
492
values [" coordinate_format" ] <- " decimal_degrees"
494
493
values <- values [- which(" bBox" %in% names(values ))]
495
494
}
496
-
495
+
497
496
values [" date_format" ] <- " YYYY-MM-DD"
498
497
values [" rdb_inventory_output" ] <- " file"
499
498
values [" TZoutput" ] <- " 0"
500
-
499
+
501
500
if (all(c(" begin_date" , " end_date" ) %in% names(values ))) {
502
501
values [" range_selection" ] <- " date_range"
503
502
}
504
-
503
+
505
504
if (service == " qwdata" && ! (" qw_sample_wide" %in% names(values ))) {
506
505
values [" qw_sample_wide" ] <- " wide"
507
506
}
508
507
}
509
-
508
+
510
509
if (service %in% c(" peak" , " gwlevels" ) && " state_cd" %in% names(values )) {
511
510
values [" list_of_search_criteria" ] <- " state_cd"
512
511
}
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 )) {
515
514
values [" list_of_search_criteria" ] <- " huc2_cd"
516
515
}
517
-
516
+
517
+ if (service == " gwlevels" && " aquiferCd" %in% names(values )){
518
+ values [" aquiferCd" ] <- " nat_aqfr_cd"
519
+ }
520
+
518
521
if (service %in% c(" peak" , " gwlevels" ) && " bBox" %in% names(values )) {
519
522
values [" list_of_search_criteria" ] <- " lat_long_bounding_box"
520
523
}
521
-
522
- if (service == " gwlevels" && " aquiferCd" %in% names(values )) {
523
- values [" aquiferCd" ] <- " nat_aqfr_cd"
524
- }
525
524
526
525
if (service %in% c(" site" , " gwlevels" , " stat" , " rating" , " peak" )) {
527
526
format.default <- " rdb"
528
527
}
529
-
528
+
530
529
if (service == " stat" ) {
531
530
message(" Please be aware the NWIS data service feeding this function is in BETA.\n
532
531
Data formatting could be changed at any time, and is not guaranteed" )
533
532
}
534
-
533
+
535
534
if (! (" format" %in% names(values ))) {
536
535
values [" format" ] <- format.default
537
536
}
538
-
537
+
539
538
return (list (values = values , service = service ))
540
539
}
541
540
@@ -548,4 +547,4 @@ convertLists <- function(...) {
548
547
list (... )[sapply(list (... ), class ) != " list" ]
549
548
) # get the non-list parts
550
549
return (matchReturn )
551
- }
550
+ }
0 commit comments