DOI-USGS / dataRetrieval

This R package is designed to obtain USGS or EPA water quality sample data, streamflow data, and metadata directly from web services.
https://doi-usgs.github.io/dataRetrieval/
Other
256 stars 85 forks source link

Error using multiple states for stateCd parameter in readNWISdata #648

Closed jmmallard closed 1 year ago

jmmallard commented 1 year ago

Description When multiple state codes are used as an input to readNWISdata, the following error is generated: Error in if (values["stateCd"] == "UM") { : missing value where TRUE/FALSE needed

To Reproduce

library(dataRetrieval)

discharge_records <- whatNWISdata(stateCd = c("OR","WA"))

Expected behavior Expected this to provide output for all gauges in both Oregon and Washington

Session Info

sessionInfo()

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dataRetrieval_2.7.11

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         rstudioapi_0.13    knitr_1.30         xml2_1.3.2         magrittr_2.0.1     hms_0.5.3          units_0.6-7        tidyselect_1.1.0   R6_2.5.0          
[10] rlang_0.4.10       httr_1.4.2         dplyr_1.0.2        tools_4.0.3        grid_4.0.3         xfun_0.19          KernSmooth_2.23-17 e1071_1.7-4        DBI_1.1.0         
[19] ellipsis_0.3.1     htmltools_0.5.0    class_7.3-17       digest_0.6.27      tibble_3.0.4       lifecycle_0.2.0    crayon_1.3.4       sf_0.9-7           readr_1.4.0       
[28] purrr_0.3.4        vctrs_0.3.5        curl_4.3           glue_1.4.2         pillar_1.4.7       compiler_4.0.3     generics_0.1.0     classInt_0.4-3     lubridate_1.7.9.2 
[37] jsonlite_1.7.1     pkgconfig_2.0.3   

Additional context Also reproduced with different states, and by using different columns from the stateCd table.

ldecicco-USGS commented 1 year ago

NWIS only allows one state to be queried at a time. This is done as a way to limit the web service queries a bit.

This issue is reminding me I need to add a check to make sure only 1 state is being queried and give the user a more user-friendly error message if that's not true.

Luckily for you, we just added a new article to the documentation that describes how to do a large data pull via a loop of states. It's written for the Water Quality Portal, but you should be able to adjust it to NWIS. Let me know if you have any questions:

https://rconnect.usgs.gov/dataRetrieval/articles/wqp_large_pull_script.html

jmmallard commented 1 year ago

Lucky indeed! Much appreciated