|
4 | 4 |  |
5 | 5 |  |
6 | 6 |
|
7 | | -:warning: USGS data availability and format have changed on Water Quality Portal (WQP). Since March 2024, data obtained from WQP legacy profiles will not include new USGS data or recent updates to existing data. All USGS data (up to and beyond March 2024) are available using the new WQP beta services. You can access the beta services by setting `legacy=False` in the functions in the `wqp` module. |
| 7 | +## Latest Announcements |
8 | 8 |
|
9 | | -To view the status of changes in data availability and code functionality, visit: https://doi-usgs.github.io/dataRetrieval/articles/Status.html |
| 9 | +:mega: **11/24/2025:** `dataretrieval` now features the new `waterdata` module, |
| 10 | +which provides access to USGS's modernized [Water Data |
| 11 | +APIs](https://api.waterdata.usgs.gov/). The Water Data API endpoints include |
| 12 | +daily values, instantaneous values, field measurements, time series metadata, |
| 13 | +and discrete water quality data from the Samples database. This new module will |
| 14 | +eventually replace the `nwis` module, which provides access to the legacy [NWIS |
| 15 | +Water Services](https://waterservices.usgs.gov/). |
10 | 16 |
|
11 | | -:mega: **09/03/2024:** The groundwater levels service has switched endpoints, and `dataretrieval` was updated accordingly in [`v1.0.10`](https://github.com/DOI-USGS/dataretrieval-python/releases/tag/v1.0.10). Older versions using the discontinued endpoint will return 503 errors for `nwis.get_gwlevels` or the `service='gwlevels'` argument. Visit [Water Data For the Nation](https://waterdata.usgs.gov/blog/wdfn-waterservices-2024/) for more information. |
| 17 | +**Important:** Users of the Water Data APIs are strongly encouraged to obtain an |
| 18 | +API key for higher rate limits and greater access to USGS data. [Register for |
| 19 | +an API key](https://api.waterdata.usgs.gov/signup/) and set it as an |
| 20 | +environment variable: |
| 21 | + |
| 22 | +```python |
| 23 | +import os |
| 24 | +os.environ["API_USGS_PAT"] = "your_api_key_here" |
| 25 | +``` |
| 26 | + |
| 27 | +Check out the [NEWS](NEWS.md) file for all updates and announcements. |
12 | 28 |
|
13 | 29 | ## What is dataretrieval? |
14 | | -`dataretrieval` was created to simplify the process of loading hydrologic data into the Python environment. |
15 | | -Like the original R version [`dataRetrieval`](https://github.com/DOI-USGS/dataRetrieval), |
16 | | -it is designed to retrieve the major data types of U.S. Geological Survey (USGS) hydrology |
17 | | -data that are available on the Web, as well as data from the Water |
18 | | -Quality Portal (WQP), which currently houses water quality data from the |
19 | | -Environmental Protection Agency (EPA), U.S. Department of Agriculture |
20 | | -(USDA), and USGS. Direct USGS data is obtained from a service called the |
21 | | -National Water Information System (NWIS). |
22 | 30 |
|
23 | | -Note that the python version is not a direct port of the original: it attempts to reproduce the functionality of the R package, |
24 | | -though its organization and interface often differ. |
| 31 | +`dataretrieval` simplifies the process of loading hydrologic data into Python. |
| 32 | +Like the original R version |
| 33 | +[`dataRetrieval`](https://github.com/DOI-USGS/dataRetrieval), it retrieves major |
| 34 | +U.S. Geological Survey (USGS) hydrology data types available on the Web, as well |
| 35 | +as data from the Water Quality Portal (WQP) and Network Linked Data Index |
| 36 | +(NLDI). |
25 | 37 |
|
26 | | -If there's a hydrologic or environmental data portal that you'd like dataretrieval to |
27 | | -work with, raise it as an [issue](https://github.com/USGS-python/dataretrieval/issues). |
| 38 | +## Installation |
28 | 39 |
|
29 | | -Here's an example using `dataretrieval` to retrieve data from the National Water Information System (NWIS). |
| 40 | +Install dataretrieval using pip: |
30 | 41 |
|
31 | | -```python |
32 | | -# first import the functions for downloading data from NWIS |
33 | | -import dataretrieval.nwis as nwis |
| 42 | +```bash |
| 43 | +pip install dataretrieval |
| 44 | +``` |
| 45 | + |
| 46 | +Or using conda: |
| 47 | + |
| 48 | +```bash |
| 49 | +conda install -c conda-forge dataretrieval |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage Examples |
| 53 | + |
| 54 | +### Water Data API (Recommended - Modern USGS Data) |
34 | 55 |
|
35 | | -# specify the USGS site code for which we want data. |
36 | | -site = '03339000' |
| 56 | +The `waterdata` module provides access to modern USGS Water Data APIs. |
37 | 57 |
|
38 | | -# get instantaneous values (iv) |
39 | | -df = nwis.get_record(sites=site, service='iv', start='2017-12-31', end='2018-01-01') |
| 58 | +The example below retrieves daily streamflow data for a specific monitoring |
| 59 | +location for water year 2025, where a "/" between two dates in the "time" |
| 60 | +input argument indicates a desired date range: |
40 | 61 |
|
41 | | -# get basic info about the site |
42 | | -df2 = nwis.get_record(sites=site, service='site') |
| 62 | +```python |
| 63 | +import dataretrieval.waterdata as waterdata |
| 64 | + |
| 65 | +# Get daily streamflow data (returns DataFrame and metadata) |
| 66 | +df, metadata = waterdata.get_daily( |
| 67 | + monitoring_location_id='USGS-01646500', |
| 68 | + parameter_code='00060', # Discharge |
| 69 | + time='2024-10-01/2025-09-30' |
| 70 | +) |
| 71 | + |
| 72 | +print(f"Retrieved {len(df)} records") |
| 73 | +print(f"Site: {df['monitoring_location_id'].iloc[0]}") |
| 74 | +print(f"Mean discharge: {df['value'].mean():.2f} {df['unit_of_measure'].iloc[0]}") |
43 | 75 | ``` |
44 | | -Services available from NWIS include: |
45 | | -- instantaneous values (iv) |
46 | | -- daily values (dv) |
47 | | -- statistics (stat) |
48 | | -- site info (site) |
49 | | -- discharge peaks (peaks) |
50 | | -- discharge measurements (measurements) |
51 | | - |
52 | | -Water quality data are available from: |
53 | | -- [Samples](https://waterdata.usgs.gov/download-samples/#dataProfile=site) - Discrete USGS water quality data only |
54 | | -- [Water Quality Portal](https://www.waterqualitydata.us/) - Discrete water quality data from USGS and EPA. Older data are available in the legacy WQX version 2 format; all data are available in the beta WQX3.0 format. |
55 | | - |
56 | | -To access the full functionality available from NWIS web services, nwis.get record appends any additional kwargs into the REST request. For example, this function call: |
| 76 | +Fetch daily discharge data for multiple sites from a start date to present |
| 77 | +using the following code: |
| 78 | + |
57 | 79 | ```python |
58 | | -nwis.get_record(sites='03339000', service='dv', start='2017-12-31', parameterCd='00060') |
| 80 | +df, metadata = waterdata.get_daily( |
| 81 | + monitoring_location_id=["USGS-13018750","USGS-13013650"], |
| 82 | + parameter_code='00060', |
| 83 | + time='2024-10-01/..' |
| 84 | +) |
| 85 | + |
| 86 | +print(f"Retrieved {len(df)} records") |
59 | 87 | ``` |
60 | | -...will download daily data with the parameter code 00060 (discharge). |
| 88 | +The following example downloads location information for all monitoring |
| 89 | +locations that are categorized as stream sites in the state of Maryland: |
61 | 90 |
|
62 | | -## Accessing the "Internal" NWIS |
63 | | -If you're connected to the USGS network, dataretrieval call pull from the internal (non-public) NWIS interface. |
64 | | -Most dataretrieval functions pass kwargs directly to NWIS's REST API, which provides simple access to internal data; simply specify "access='3'". |
65 | | -For example |
66 | 91 | ```python |
67 | | -nwis.get_record(sites='05404147',service='iv', start='2021-01-01', end='2021-3-01', access='3') |
| 92 | +# Get monitoring location information |
| 93 | +locations, metadata = waterdata.get_monitoring_locations( |
| 94 | + state_name='Maryland', |
| 95 | + site_type_code='ST' # Stream sites |
| 96 | +) |
| 97 | + |
| 98 | +print(f"Found {len(locations)} stream monitoring locations in Maryland") |
68 | 99 | ``` |
| 100 | +Visit the |
| 101 | +[API Reference](https://doi-usgs.github.io/dataretrieval-python/reference/waterdata.html) |
| 102 | +for more information and examples on available services and input parameters. |
69 | 103 |
|
70 | | -More services and documentation to come! |
| 104 | +**NEW:** This new module implements |
| 105 | +[logging](https://docs.python.org/3/howto/logging.html#logging-basic-tutorial) |
| 106 | +in which users can view the URL requests sent to the USGS Water Data APIs |
| 107 | +and the number of requests they have remaining each hour. These messages can |
| 108 | +be helpful for troubleshooting and support. To enable logging in your python |
| 109 | +console or notebook: |
71 | 110 |
|
72 | | -## Quick start |
| 111 | +```python |
| 112 | +import logging |
| 113 | +logging.basicConfig(level=logging.INFO) |
| 114 | +``` |
| 115 | +To log messages to a file, you can specify a filename in the |
| 116 | +`basicConfig` call: |
73 | 117 |
|
74 | | -dataretrieval can be installed using pip: |
75 | | - |
76 | | - $ python3 -m pip install -U dataretrieval |
| 118 | +```python |
| 119 | +logging.basicConfig(filename='waterdata.log', level=logging.INFO) |
| 120 | +``` |
77 | 121 |
|
78 | | -or conda: |
| 122 | +### NWIS Legacy Services (Deprecated but still functional) |
79 | 123 |
|
80 | | - $ conda install -c conda-forge dataretrieval |
| 124 | +The `nwis` module accesses legacy NWIS Water Services: |
81 | 125 |
|
82 | | -More examples of use are include in [`demos`](https://github.com/USGS-python/dataretrieval/tree/main/demos). |
| 126 | +```python |
| 127 | +import dataretrieval.nwis as nwis |
83 | 128 |
|
84 | | -## Issue tracker |
| 129 | +# Get site information |
| 130 | +info, metadata = nwis.get_info(sites='01646500') |
| 131 | + |
| 132 | +print(f"Site name: {info['station_nm'].iloc[0]}") |
| 133 | + |
| 134 | +# Get daily values |
| 135 | +dv, metadata = nwis.get_dv( |
| 136 | + sites='01646500', |
| 137 | + start='2024-10-01', |
| 138 | + end='2024-10-02', |
| 139 | + parameterCd='00060', |
| 140 | +) |
| 141 | + |
| 142 | +print(f"Retrieved {len(dv)} daily values") |
| 143 | +``` |
85 | 144 |
|
86 | | -Please report any bugs and enhancement ideas using the dataretrieval issue |
87 | | -tracker: |
| 145 | +### Water Quality Portal (WQP) |
88 | 146 |
|
89 | | - https://github.com/USGS-python/dataretrieval/issues |
| 147 | +Access water quality data from multiple agencies: |
90 | 148 |
|
91 | | -Feel free to also ask questions on the tracker. |
| 149 | +```python |
| 150 | +import dataretrieval.wqp as wqp |
92 | 151 |
|
| 152 | +# Find water quality monitoring sites |
| 153 | +sites = wqp.what_sites( |
| 154 | + statecode='US:55', # Wisconsin |
| 155 | + siteType='Stream' |
| 156 | +) |
93 | 157 |
|
94 | | -## Contributing |
| 158 | +print(f"Found {len(sites)} stream monitoring sites in Wisconsin") |
| 159 | + |
| 160 | +# Get water quality results |
| 161 | +results = wqp.get_results( |
| 162 | + siteid='USGS-05427718', |
| 163 | + characteristicName='Temperature, water' |
| 164 | +) |
95 | 165 |
|
96 | | -Any help in testing, development, documentation and other tasks is welcome. |
97 | | -For more details, see the file [CONTRIBUTING.md](CONTRIBUTING.md). |
| 166 | +print(f"Retrieved {len(results)} temperature measurements") |
| 167 | +``` |
98 | 168 |
|
| 169 | +### Network Linked Data Index (NLDI) |
99 | 170 |
|
100 | | -## Need help? |
| 171 | +Discover and navigate hydrologic networks: |
101 | 172 |
|
102 | | -The Water Mission Area of the USGS supports the development and maintenance of `dataretrieval`. Any questions can be directed to the Computational Tools team at |
103 | | -comptools@usgs.gov. |
| 173 | +```python |
| 174 | +import dataretrieval.nldi as nldi |
104 | 175 |
|
105 | | -Resources are available primarily for maintenance and responding to user questions. |
106 | | -Priorities on the development of new features are determined by the `dataretrieval` development team. |
| 176 | +# Get watershed basin for a stream reach |
| 177 | +basin = nldi.get_basin( |
| 178 | + feature_source='comid', |
| 179 | + feature_id='13293474' # NHD reach identifier |
| 180 | +) |
107 | 181 |
|
| 182 | +print(f"Basin contains {len(basin)} feature(s)") |
| 183 | + |
| 184 | +# Find upstream flowlines |
| 185 | +flowlines = nldi.get_flowlines( |
| 186 | + feature_source='comid', |
| 187 | + feature_id='13293474', |
| 188 | + navigation_mode='UT', # Upstream tributaries |
| 189 | + distance=50 # km |
| 190 | +) |
| 191 | + |
| 192 | +print(f"Found {len(flowlines)} upstream tributaries within 50km") |
| 193 | +``` |
| 194 | + |
| 195 | +## Available Data Services |
| 196 | + |
| 197 | +### Modern USGS Water Data APIs (Recommended) |
| 198 | +- **Daily values**: Daily statistical summaries (mean, min, max) |
| 199 | +- **Field measurements**: Discrete measurements from field visits |
| 200 | +- **Monitoring locations**: Site information and metadata |
| 201 | +- **Time series metadata**: Information about available data parameters |
| 202 | +- **Latest daily values**: Most recent daily statistical summary data |
| 203 | +- **Latest instantaneous values**: Most recent high-frequency continuous data |
| 204 | +- **Samples data**: Discrete USGS water quality data |
| 205 | +- **Instantaneous values** (*COMING SOON*): High-frequency continuous data |
| 206 | + |
| 207 | +### Legacy NWIS Services (Deprecated) |
| 208 | +- **Daily values (dv)**: Legacy daily statistical data |
| 209 | +- **Instantaneous values (iv)**: Legacy continuous data |
| 210 | +- **Site info (site)**: Basic site information |
| 211 | +- **Statistics (stat)**: Statistical summaries |
| 212 | +- **Discharge peaks (peaks)**: Annual peak discharge events |
| 213 | +- **Discharge measurements (measurements)**: Direct flow measurements |
| 214 | + |
| 215 | +### Water Quality Portal |
| 216 | +- **Results**: Water quality analytical results from USGS, EPA, and other agencies |
| 217 | +- **Sites**: Monitoring location information |
| 218 | +- **Organizations**: Data provider information |
| 219 | +- **Projects**: Sampling project details |
| 220 | + |
| 221 | +### Network Linked Data Index (NLDI) |
| 222 | +- **Basin delineation**: Watershed boundaries for any point |
| 223 | +- **Flow navigation**: Upstream/downstream network traversal |
| 224 | +- **Feature discovery**: Find monitoring sites, dams, and other features |
| 225 | +- **Hydrologic connectivity**: Link data across the stream network |
| 226 | + |
| 227 | +## More Examples |
| 228 | + |
| 229 | +Explore additional examples in the |
| 230 | +[`demos`](https://github.com/USGS-python/dataretrieval/tree/main/demos) |
| 231 | +directory, including Jupyter notebooks demonstrating advanced usage patterns. |
| 232 | + |
| 233 | +## Getting Help |
| 234 | + |
| 235 | +- **Issue tracker**: Report bugs and request features at https://github.com/USGS-python/dataretrieval/issues |
| 236 | +- **Documentation**: Full API documentation available in the source code docstrings |
| 237 | + |
| 238 | +## Contributing |
| 239 | + |
| 240 | +Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for |
| 241 | +development guidelines. |
108 | 242 |
|
109 | 243 | ## Acknowledgments |
110 | | -This material is partially based upon work supported by the National Science Foundation (NSF) under award 1931297. |
111 | | -Any opinions, findings, conclusions, or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of the NSF. |
| 244 | + |
| 245 | +This material is partially based upon work supported by the National Science |
| 246 | +Foundation (NSF) under award 1931297. Any opinions, findings, conclusions, or |
| 247 | +recommendations expressed in this material are those of the authors and do not |
| 248 | +necessarily reflect the views of the NSF. |
112 | 249 |
|
113 | 250 | ## Disclaimer |
114 | 251 |
|
115 | | -This software is preliminary or provisional and is subject to revision. |
116 | | -It is being provided to meet the need for timely best science. |
117 | | -The software has not received final approval by the U.S. Geological Survey (USGS). |
118 | | -No warranty, expressed or implied, is made by the USGS or the U.S. Government as to the functionality of the software and related material nor shall the fact of release constitute any such warranty. |
119 | | -The software is provided on the condition that neither the USGS nor the U.S. Government shall be held liable for any damages resulting from the authorized or unauthorized use of the software. |
| 252 | +This software is preliminary or provisional and is subject to revision. It is |
| 253 | +being provided to meet the need for timely best science. The software has not |
| 254 | +received final approval by the U.S. Geological Survey (USGS). No warranty, |
| 255 | +expressed or implied, is made by the USGS or the U.S. Government as to the |
| 256 | +functionality of the software and related material nor shall the fact of release |
| 257 | +constitute any such warranty. The software is provided on the condition that |
| 258 | +neither the USGS nor the U.S. Government shall be held liable for any damages |
| 259 | +resulting from the authorized or unauthorized use of the software. |
120 | 260 |
|
121 | 261 | ## Citation |
122 | 262 |
|
123 | | -Hodson, T.O., Hariharan, J.A., Black, S., and Horsburgh, J.S., 2023, dataretrieval (Python): a Python package for discovering |
124 | | -and retrieving water data available from U.S. federal hydrologic web services: |
125 | | -U.S. Geological Survey software release, |
126 | | -https://doi.org/10.5066/P94I5TX3. |
| 263 | +Hodson, T.O., Hariharan, J.A., Black, S., and Horsburgh, J.S., 2023, |
| 264 | +dataretrieval (Python): a Python package for discovering and retrieving water |
| 265 | +data available from U.S. federal hydrologic web services: U.S. Geological Survey |
| 266 | +software release, https://doi.org/10.5066/P94I5TX3. |
0 commit comments