Skip to content

feat(waterdata): list the STAC-indexed Parquet files - #475

Closed
thodson-usgs wants to merge 4 commits into
DOI-USGS:mainfrom
thodson-usgs:feat/stac-parquet-files
Closed

thodson-usgs wants to merge 4 commits into
DOI-USGS:mainfrom
thodson-usgs:feat/stac-parquet-files

Conversation

@thodson-usgs

@thodson-usgs thodson-usgs commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

What

waterdata.get_parquet_files(collection, ...) lists the Parquet files that USGS now publishes for the daily and continuous collections (announcement). It searches the STAC catalog and returns (DataFrame, BaseMetadata) with one row per file: monitoring_location_id, asset (data, current, historical), url, start_datetime, end_datetime. It downloads nothing.

files, md = waterdata.get_parquet_files(
    "daily",
    filter="state_code = '39' AND site_type_code = 'ST'",
    time=["2004-01-01", None],
)
# 340 files, 1.7 s, one catalog request

The URLs go to any Parquet reader. Docstring examples show DuckDB (read_parquet over a list, which fetches only the row groups the WHERE clause needs) and Dask (dd.from_map(pd.read_parquet, urls, ...)).

Includes the fix from #474 (merge that first; this branch then rebases cleanly). get_ratings() searched /search without collections=. Since daily and continuous were published, get_ratings(monitoring_location_id="USGS-01104475", file_type=["exsa", "corr"], download_and_parse=False) returned 5 items: 3 ratings plus the location's daily and continuous items. The STAC page walk moved to waterdata/catalog.py, so both getters share it, and it always names its collection.

Findings that shaped the design

  • The files are rate limited. Anonymous downloads returned 429 with retry-after: 1789 after a few dozen files. With X-Api-Key, the responses carry x-ratelimit-limit: 10000, and each range request consumes one unit. A DuckDB read of 20 files used 13 units. The docstring tells callers to send the key.
  • The bucket can't be listed (?list-type=2 returns 403), so the catalog is the only inventory. The prod bucket is not in the gs-chs-wma-dev account, which is the only account my SSO reaches.
  • The files are rewritten daily (Last-Modified 06:00 UTC, new ETags).
  • Continuous: 192 of the first 200 items have only a current asset, so historical is not guaranteed.
  • Readers, tested live with a key:
    • DuckDB works: 20 files filtered in 0.9 s, against 13.9 s to GET each file whole and filter in pyarrow.
    • dd.read_parquet over HTTPS fails with dask 2026.8 and fsspec 2026.9 (ClientSession._request() got an unexpected keyword argument 'default_cache'). That is an upstream bug, so the example uses dd.from_map instead.
    • Polars scan_parquet did not send the header through storage_options and hit 429.

Virtual dataset: assessment, not implemented

The question was whether something like VirtualiZarr could hold these URLs and present them as one dataset.

  • This already exists for Parquet, so a new format would be a poor design. Parquet carries its own schema and row-group statistics in each file footer, so a list of URLs is already a valid dataset for every engine that reads Parquet: DuckDB read_parquet([...]), pyarrow.dataset.dataset([...]), Polars, Dask. VirtualiZarr exists because NetCDF and HDF5 chunk byte offsets need an external index before Zarr readers can use them. Parquet doesn't have that problem.
  • Table formats would be the persistent version. Iceberg, Delta, and DuckLake keep a catalog of data-file paths plus statistics. I checked DuckLake: ducklake_add_data_files registered 10 of the remote https:// URLs as one table without copying them, and queries pruned correctly.
  • A persistent catalog is the wrong fit here. These files are rewritten in place every day. A persisted manifest records file sizes and statistics that are stale by the next morning, and a table format assumes immutable data files. USGS publishing an Iceberg or DuckLake catalog beside the files would fix that, and could be suggested to WDFN. For this package, the STAC catalog is the manifest, rebuilt on each call, so the getter returns the URL list and leaves the reading to the engine.

Not in this PR

  • A getter that downloads and reshapes the files to match get_daily and get_continuous (Phase 2). It would fan out one chunk per file over transport.fanout.FanOut. It needs pyarrow as an optional extra, so it should wait for a decision on that and for WDFN to confirm that the URL layout is stable.
  • Overlap with the unmerged feat/waterdata-stac-api branch (raw waterdata.stac namespace): this PR doesn't depend on it. If both land, catalog.search_items is the shared core.

Checks

ruff, ruff format, mypy (strict), lint-imports (7 kept), xenon, complexipy all pass. The full suite has 1672 passing tests, and coverage is 99.17% (the ratchet is 98.9). The new modules are fully covered.

Fix: chunk searches under the catalog edge's URL limit (122a4cc)

The catalog's /search edge answers HTTP 403 (an HTML "Forbidden" page) to any GET URL longer than 2,094 characters (bisected 2026-11-22: 2,094 is served, 2,095 is refused). get_parquet_files and get_ratings sent every monitoring_location_id in one IN (...) filter, so about 60 ids like OH015-383638082103300, or about 90 USGS- ids, failed with a 403 that named no remedy.

  • catalog.py: search_items is now a thin wrapper over _search_params and _run_search, which sends all requests through one run_paginated call. New search_items_chunked runs one search per filter as concurrent chunks and deduplicates items by (collection, id). New chunk_in_filters splits the ids so each URL-encoded IN clause fits EDGE_URL_LIMIT less headroom, sized per id and capped at one page.
  • Both getters use it and keep their frame shape, sorting, metadata and collections=ratings scoping. A filter, time or bbox that alone exceeds the limit raises ValueError before any request.
  • Tests: many long ids, short list, repeated ids, user filter kept per chunk, over-long filter, ratings chunking and over-long ratings bbox (all offline). Gates (ruff, mypy, lint-imports, xenon, complexipy, coverage) pass.

Live check against the real service:

  • get_parquet_files('daily', filter="state_code = '39'") returned 989 Ohio files. Passing the first 600 ids returned 600 rows, identical (ids, assets, URLs) to the state query restricted to those ids.
  • get_ratings(monitoring_location_id=<200 USGS ids>, download_and_parse=False) returned 200 features covering all 200 ids.

USGS now publishes the daily and continuous collections as Parquet files,
one set per monitoring location, indexed by the Water Data STAC catalog
(https://waterdata.usgs.gov/blog/api-faster-access/). The file host cannot
be listed, so the catalog is the only way to learn which files exist.

Add waterdata.get_parquet_files(collection, ...). It searches the catalog
by monitoring location, time, bbox, or a CQL2 filter over
monitoring-location fields, and returns (DataFrame, BaseMetadata) with one
row per file: monitoring_location_id, asset (data / current / historical),
url, start_datetime, end_datetime. It downloads nothing. The URLs go to a
Parquet reader; the docstring shows DuckDB and Dask.

Move the STAC item search out of ratings into waterdata.catalog so both
getters share one page walk, and scope it to one collection. /search spans
every collection, so get_ratings(download_and_parse=False) with a list of
file types also returned each location's daily and continuous items once
those collections were published.
…er URL limit

The Water Data STAC /search edge answers HTTP 403 (an HTML "Forbidden"
page) to any GET URL longer than 2,094 characters, measured by bisection
on 2026-11-22. get_parquet_files and get_ratings put the caller's whole
monitoring_location_id list into one IN (...) filter in one search, so
about 60 ids like OH015-383638082103300 or about 90 USGS ids failed with
a 403 that named no remedy.

catalog.search_items is split into _search_params and _run_search, which
sends every request through one run_paginated call. search_items_chunked
runs one search per filter as concurrent chunks and deduplicates items by
(collection, id). chunk_in_filters splits the ids into chunks whose
URL-encoded IN clause fits EDGE_URL_LIMIT less headroom, sized per id and
capped at one page. Both getters use it and keep their frame shape,
sorting, metadata and collection scoping. A filter, time or bbox that
alone exceeds the limit raises ValueError before any request.
pd.to_datetime infers one format from the first value; continuous items
mix 2005-10-01T04:00:00Z and 2018-12-04T22:26:36.420930Z, so a state-wide
continuous listing raised ValueError. Parse as ISO 8601.
thodson-usgs added a commit to thodson-usgs/dataretrieval-python that referenced this pull request Nov 22, 2026
bulk="auto" sends a query the bulk path cannot honor to the API, and
otherwise estimates the rows the API would page through (locations x
series x values per day x days) without a request. It reads the files
when that exceeds 10,000 rows plus a per-location download cost (250
rows' worth for a daily or current file, 10,000 for a historical one).

The constants come from a benchmark against the live services that drew
fresh monitoring locations for every run, so no run hit a warm cache: 31
query shapes, two runs each, both paths. The rule picks the faster path
in 29; the two misses cost 0.1 s and 0.4 s. A holdout of 7 shapes over
other states: 7 of 7. bulk=False stays the default.

Also: a live test that reads three files' footers with range requests
and pins the layout a range-request reader would rely on (time-sorted,
50,000-row groups, UTC last_modified); and the catalog-time parsing fix
from DOI-USGS#475 (fractional seconds), until this branch is rebased on it.
thodson-usgs added a commit to thodson-usgs/dataretrieval-python that referenced this pull request Nov 22, 2026
- prefer_bulk reuses _check_args for eligibility instead of repeating it.
- One _ROW_MODEL table and two named per-file constants replace three
  collection-keyed dicts and a string-keyed one.
- _needs_historical replaces the continuous/current-window check that
  get_bulk and prefer_bulk each spelled out.
- use_bulk resolves bulk='auto' in one place; each getter's dispatch is
  one condition.
- Tests: the router cases are one table (max_rows included), and imports
  are at module level. The layout test reads each footer with one 64 KB
  suffix range, adding a second only for a footer that outgrows it.
- Mark the URL-length splitter as a stopgap for DOI-USGS#475's chunk_in_filters.
thodson-usgs added a commit to thodson-usgs/dataretrieval-python that referenced this pull request Nov 23, 2026
… bulk setting

USGS now publishes the daily and continuous collections as Parquet files,
one set per monitoring location, rewritten every night and indexed by the
Water Data STAC catalog (https://waterdata.usgs.gov/blog/api-faster-access/).
This commit combines DOI-USGS#475 and DOI-USGS#476.

waterdata.get_parquet_files(collection, ...) lists the files. It searches the
catalog by monitoring location, time, bbox, or a CQL2 filter over
monitoring-location fields, and returns (DataFrame, BaseMetadata) with one
row per file. It downloads nothing.

A Water Data setting, bulk ("off", "auto", "always"), makes get_daily and
get_continuous read a location's history from those files and only the
records changed since from the API. "off" is the default and changes no
call. "auto" reads the files for a query large enough to finish sooner, and
"always" for every query they can answer. ADR 0014 records the design, the
merge rule, the routing rule and its benchmark, and the alternatives.

Structure:

- waterdata.catalog.search is the one catalog search. It owns the edge's
  2,094-character URL limit: it splits a long monitoring_location_id list
  across concurrent searches that fit, and raises before any request when
  the rest of the query cannot fit. get_ratings, get_parquet_files, and the
  bulk path pass identifiers and expressions and never size a URL.
- The getters call one entry point, waterdata._bulk.get_data, which reads
  the setting and answers from the files or the API. A call's arguments are
  parsed once into a query, and what differs between daily and continuous
  is one table.
- The file downloads run on FanOut, which combines their rows. The change
  query and the merge are its finalize hook, so exc.call.resume() returns
  the whole result.
- transport.fanout.inert_response is the body-less response a download
  hands the executor. It moved from ratings, so the bulk path does not
  import from another collection family.

Result contract: daily_id and continuous_id are random version-4 UUIDs that
the files do not hold. With "auto" or "always", a query the files can
answer returns no such column, whether the files or the API answered it,
so the columns do not depend on the route. A query only the API accepts,
and every call under "off", is returned as before. A record deleted from
the API since the files were written is still returned until the next
night's files.

Bug fixes in get_ratings: the search is limited to the ratings collection
(it returned daily and continuous items for a list of file types), and a
long monitoring_location_id list no longer fails with HTTP 403.

pyarrow is a new optional extra, dataretrieval[bulk]. Without it, a query
the setting sends to the files pages the API and warns.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@thodson-usgs

Copy link
Copy Markdown
Collaborator Author

Combined into #476, which now holds this work and the bulk setting as one commit. The branch feat/stac-parquet-files is left in place.

thodson-usgs added a commit to thodson-usgs/dataretrieval-python that referenced this pull request Nov 23, 2026
… bulk setting

USGS now publishes the daily and continuous collections as Parquet files,
one set per monitoring location, rewritten every night and indexed by the
Water Data STAC catalog (https://waterdata.usgs.gov/blog/api-faster-access/).
This commit combines DOI-USGS#475 and DOI-USGS#476.

waterdata.get_parquet_files(collection, ...) lists the files. It searches the
catalog by monitoring location, time, bbox, or a CQL2 filter over
monitoring-location fields, and returns (DataFrame, BaseMetadata) with one
row per file. It downloads nothing.

A Water Data setting, bulk ("off", "auto", "always"), makes get_daily and
get_continuous read a location's history from those files and only the
records changed since from the API. "off" is the default and changes no
call. "auto" reads the files for a query large enough to finish sooner, and
"always" for every query they can answer. ADR 0014 records the design, the
merge rule, the routing rule and its benchmark, and the alternatives.

Structure:

- waterdata.catalog.search is the one catalog search. It owns the edge's
  2,094-character URL limit: it splits a long monitoring_location_id list
  across concurrent searches that fit, and raises before any request when
  the rest of the query cannot fit. get_ratings, get_parquet_files, and the
  bulk path pass identifiers and expressions and never size a URL.
- The getters call one entry point, waterdata._bulk.get_data, which reads
  the setting and answers from the files or the API. A call's arguments are
  parsed once into a query, and what differs between daily and continuous
  is one table.
- The file downloads run on FanOut, which combines their rows. The change
  query and the merge are its finalize hook, so exc.call.resume() returns
  the whole result.
- transport.fanout.inert_response is the body-less response a download
  hands the executor. It moved from ratings, so the bulk path does not
  import from another collection family.

Result contract: daily_id and continuous_id are random version-4 UUIDs that
the files do not hold. With "auto" or "always", a query the files can
answer returns no such column, whether the files or the API answered it,
so the columns do not depend on the route. A query only the API accepts,
and every call under "off", is returned as before. A record deleted from
the API since the files were written is still returned until the next
night's files.

Bug fixes in get_ratings: the search is limited to the ratings collection
(it returned daily and continuous items for a list of file types), and a
long monitoring_location_id list no longer fails with HTTP 403.

pyarrow is a new optional extra, dataretrieval[bulk]. Without it, a query
the setting sends to the files pages the API and warns.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant