Skip to content

feat(waterdata): select bulk.get_urls files by state, county, site type, and HUC - #481

Draft
thodson-usgs wants to merge 5 commits into
DOI-USGS:mainfrom
thodson-usgs:feat/bulk-urls-location-filters
Draft

thodson-usgs wants to merge 5 commits into
DOI-USGS:mainfrom
thodson-usgs:feat/bulk-urls-location-filters

Conversation

@thodson-usgs

Copy link
Copy Markdown
Collaborator

Summary

Depends on #479. This branch contains #479's commits, so review only the last one, 40eb8cc.

waterdata.bulk.get_urls() now takes the named location filters of waterdata.get_monitoring_locations: state, state_code, state_name, agency_code, county_code, county_name, site_type_code, site_type, and hydrologic_unit_code. They have the same names and types as there, and state accepts a name, postal code, or FIPS code. This makes get_urls the counterpart of R dataRetrieval's search_stac, without the caller having to write CQL.

files, md = bulk.get_urls("daily", state="OH", site_type_code="ST")
  • A single value becomes name = 'v', and several values become name IN (...). The clauses are joined to the caller's filter by AND, and the filter is parenthesized.
  • Matching is exact, as in get_monitoring_locations. R's search_stac matches hydrologic_unit_code by prefix. The docstring and NEWS show filter="hydrologic_unit_code LIKE '07090002%'" for every location in a HUC8.
  • A search too long for one catalog URL raises ValueError before any request.
  • The return type is unchanged: one row per file. A richer frame is under discussion.
  • The bulk getters' signatures are unchanged.

Evidence

  • Offline: 1,785 tests pass, and coverage is 99.02%. ruff, ruff format, mypy (strict), lint-imports (8 contracts kept), xenon, and complexipy pass.
  • Live (2026-12-02): get_urls("daily", state="OH", site_type_code="ST") sends filter=state_code = '39' AND site_type_code = 'ST' and returns 550 files. The HUC-prefix filter returns 132 files for 07090002.

Merge Danger

Door: two-way. get_urls has not been released.
Blast Radius: small. One function gains keyword arguments.

🤖 Generated with Claude Code

thodson-usgs and others added 5 commits December 1, 2026 15:19
…files

USGS publishes the daily and continuous collections as Parquet files, one
set per monitoring location, written each night and indexed by the Water
Data STAC catalog. waterdata.bulk.get_daily() and get_continuous() read a
query's history from the files, and only the records changed since from the
API, and return the columns and types of waterdata.get_daily() and
get_continuous(), except the record id. waterdata.bulk.get_urls() lists the
files and downloads nothing. waterdata.get_daily() and get_continuous() do
not change.

Also in this commit:

- waterdata.catalog is the one STAC search. get_ratings() uses it, so a long
  monitoring_location_id list is split under the catalog's 2,094-character
  URL limit instead of failing with HTTP 403. The search stays scoped to
  the ratings collection (DOI-USGS#474).
- files_api_key (API_USGS_FILES_PAT) is a second key, sent with requests
  for /stac-files/ and /stac/, so file downloads have their own hourly count.
- The pyarrow extra, which the bulk getters need. [test] includes it.
- On pandas 3, an OGC text column has the same type however the result was
  paged.

Split from DOI-USGS#476, which keeps the bulk setting that routes waterdata.get_daily()
and get_continuous() to the files, and ADR 0014.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- catalog: build each search through ogc.planning._try_build, so a filter
  over the 64 KB httpx can put in one URL component raises the ValueError
  with its remedy instead of httpx.InvalidURL.
- One _as_list, in catalog, for ratings, catalog.search and _retrieval.
- catalog.__all__ lists the names the bulk modules import.
- One masked renderer for api_key and files_api_key in show_configuration.
- ogc.errors._error_body reads the request URL and its key variables once.
- _change_clauses builds the set of changing locations once and floors the
  cutoffs as one DatetimeIndex.
- Without geopandas, the file rows' geometry is looked up once per location
  (pd.factorize), as with geopandas, instead of Series.map per row.
- qualifier: only the non-null lists are converted to Python objects
  (1.4M rows, 3% non-null: 27 ms to 12 ms).
- Wording: "the bulk getters" instead of "the files route".
- Tests: the ratings scoping test duplicated DOI-USGS#474's and is removed; the
  world-readable warning test covers both keys; the no-pyarrow fixture
  leaves conftest for a monkeypatch in its one test; _response uses
  inert_response.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With files_api_key, the catalog searches and file downloads count against
a second key, but md.header reported the lowest x-ratelimit-remaining of
every request, so it could show the files key's count as the API's. It now
reports the lowest among the requests sent the change query's key. With
one key, nothing changes.

The live comparison with the API getters compares types only for columns
with a value. The API getter types a column null in every row object, and
the bulk getter keeps the files' type; method_category at USGS-01435000 has
been null since 2026-10-01, so the test failed every run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…full record

Rename the second Water Data key from files_api_key / API_USGS_FILES_PAT to
stac_api_key / STAC_USGS_PAT, the variable R dataRetrieval reads, so one
export serves both packages.

Document the bulk getters by their main use first: omit time to get a
location's whole period of record. Add the example to the README, the
configuration guide, the getter docstrings and NEWS.

In get_urls, note that a location's current and historical continuous files
can both hold a record, and show how to keep the newest last_modified in
pandas and DuckDB. Scope the DuckDB example's secret to the Water Data host.

Print a one-time pointer to a STAC key when a catalog search or file
download is sent api_key because no STAC key is set. It is shown only for the
authorized host.

bulk._merge now orders file rows by last_modified, newest first, before
dropping duplicates, so the newer of two overlapping files wins whatever
order they were read in. Before, the first file read won.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pe, and HUC

bulk.get_urls() now takes the named location filters of
get_monitoring_locations: state, state_code, state_name, agency_code,
county_code, county_name, site_type_code, site_type, and
hydrologic_unit_code. Each matches exactly. A single value becomes
name = 'v' and several become name IN (...). The state argument accepts
a name, postal code, or FIPS code and is sent as state_code.

The clauses are joined by AND with the caller's CQL2 filter, which is
parenthesized so an OR inside it stays inside it. A search too long for
one URL raises ValueError before any request. The docstring and NEWS
note that hydrologic_unit_code is not a prefix match and show a LIKE
filter for every location inside a HUC8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thodson-usgs thodson-usgs added the v1 Non-breaking: can merge for the next 1.x release label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Non-breaking: can merge for the next 1.x release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant