Updates to usgs.py routine to use new USGS API#191
Open
kammereraj wants to merge 6 commits intomasterfrom
Open
Updates to usgs.py routine to use new USGS API#191kammereraj wants to merge 6 commits intomasterfrom
kammereraj wants to merge 6 commits intomasterfrom
Conversation
Contributor
|
Thank you @kammereraj, I'll take a look at this, I hope you don't mind some delay. On the first glance there are some obvious things to fix, such as black styling updates, but also we need to update the tests so that they support getting no-geometry dfs, etc. I'll take a deeper dive later to see if I can address any specific ones. |
Contributor
|
@kammereraj I added some minor fixes for the test to work, but I cannot get the actual USGS results. I have set up my API key locally, but I get empty results for stations when I try. Can you confirm if it works on your side and you can get the full station list using: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR migrates the USGS data retrieval module from the legacy NWIS API (
dataretrieval.nwis) to the modernized Water Data API (dataretrieval.waterdata). The new API provides continued access to USGS hydrologic data as the legacy services are being phased out.Breaking Changes
dataretrievalversion: Now requires>=1.1.2(was>=1)disable_progress_barremoved fromget_usgs_data()(was unused)begin_dateandend_datecolumns are no longer available in station metadata (not provided by new API)New Features
API Key Support
The modernized Water Data API supports authentication via API key for higher rate limits.
Obtaining an API Key
Configuring the API Key
Option 1: Environment Variable (Recommended)
Set the
API_USGS_PATenvironment variable:Option 2: Pass Directly to Functions
Rate Limits
A warning is logged once per session if no API key is detected.
Changes
API Migration
dataretrieval.nwisdataretrieval.waterdatanwis.get_info()waterdata.get_monitoring_locations()nwis.get_iv()waterdata.get_continuous()"01646500""USGS-01646500"start,endtime="YYYY-MM-DD/YYYY-MM-DD"Code Improvements
get_usgs_api_key()and_set_api_key_env()functions_normalize_station_data()function with extracted helpers_get_dataset_from_station_data()now validates site_nos exist in metadataNew Helper Functions
Parameter Availability Tracking (NEW)
A new feature allows querying which variables are available at each station before attempting data retrieval. This enables significant efficiency gains by skipping API calls for unavailable data.
New Function:
get_station_parameter_availability()Enhanced
get_usgs_stations()with Parameter AvailabilityThe
get_usgs_stations()function now accepts aninclude_parameter_availabilityparameter:Parameter Code Groups
New constants define which USGS parameter codes map to each variable type:
Efficiency Impact
By checking parameter availability before data retrieval, downstream applications can avoid unnecessary API calls:
Parameter Code Configuration
Parameter codes are now defined in a static dictionary for better maintainability:
Test Updates
New Test Classes
TestAPIKeyManagement: Tests for API key retrieval from parameters and environmentTestIDConversion: Tests for site_no ↔ monitoring_location_id conversionTestRateLimitConfiguration: Tests for rate limit configuration with/without API keyTestParameterInfo: Tests for parameter code lookupUpdated Tests
test_get_usgs_station_data: Updated assertions for instantaneous data (15-min intervals)test_get_usgs_data: Added structure verification for xarray Datasettest_get_usgs_station_data_by_string_enddate: Added assertions for multiple readingstest_normalize_empty_data_df: Updated to use new_normalize_station_datasignaturetest_request_nonexistant_data: Updated to use minimal DataFrame fixtureRemoved Test Assertions
begin_date/end_datedtype checks (columns no longer available)parm_cdfrom test fixtures (not in new API response)Usage Examples
Basic Usage
With API Key
With Parameter Availability (Efficient Data Retrieval)
Dependencies
References
Checklist
dataretrievalversion requirement to>=1.1.2nwistowaterdatamoduleUSGS-prefix formatget_station_parameter_availability()include_parameter_availabilityoption toget_usgs_stations()