Skip to content

fix: GridMET basin-mean on border boxes (NaN retry loop) and missing leap-year 31 Dec - #18

Merged
galib9690 merged 4 commits into
mainfrom
fix/gridmet-large-bbox-leapday
Nov 10, 2026
Merged

galib9690 merged 4 commits into
mainfrom
fix/gridmet-large-bbox-leapday

Conversation

@galib9690

Copy link
Copy Markdown
Member

Summary

Fixes the two GridMET defects from the DEMO-1 rehearsal (finding F1). The root cause differs from the rehearsal's reading.

Root cause

  1. Large-box failure is not a server limit. Live probes of thredds.northwestknowledge.net show the server returns a valid NetCDF file (HTTP 200, ~4 KB) for the whole 0.893 x 0.632 deg Fish River box. The failure is in pygridmet 0.19.4: it treats any NaN in a downloaded file as a corrupt download, deletes it, re-downloads 15 times, then raises "GridMET did NOT process your request successfully" (that string is pygridmet's, not an NCSS error body). GridMET has no data outside CONUS, so a box that reaches Canada or the sea always holds NaN cells and can never succeed. The rehearsal's 0.15 deg box held no such cell (0 of 20 cells NaN); its 0.18 deg box held one (1 of 30). With our call_with_retry + chunk-retry layers, one failing call cost about 270 requests.
  2. Leap day. pygridmet's dates_tolist drops 31 Dec of leap years (its docstring is copied from pydaymet, whose calendar has 365 days). GridMET holds the day: 2000-12-31 returned real values (live). Every fetch was one day short per leap year (1460 of 1461 for WY2000-2003).

Fix (pygridmet not forked)

  • New aihydro_data/sources/_gridmet_ncss.py: direct THREDDS NCSS client, one request per calendar-year chunk. hyriver.py uses it through a _gridmet_client() seam; the hyriver extra still installs pygridmet (Daymet path unchanged).
  • Basin mean keeps the existing rule: cell centre inside the polygon, unweighted mean over cells with data. Recorded in the result: notes plus FetchResult.upstream["gridmet_ncss"]["basin_mean"] (method=cell_centre_in_polygon_unweighted_mean, cells_in_geometry, cells_with_data, cells_without_data, all_touched_fallback), with request URLs and response SHA-256s. A border basin is averaged over its US cells only and says so (Fish River: 313 of 315).
  • Error body / HTTP 200: any non-NetCDF response raises GridmetRequestRejected (GRIDMET_REQUEST_REJECTED, an AihydroDataError) at once with the body excerpt; never retried; aborts the whole fetch (no 40 failing yearly calls) so the router can fall through. Before giving up the box is split in two (<= 4 levels, <= 48 requests per call, mosaic checked for gaps) in case a size limit exists. No limit was observed, so this path is exercised against a synthetic server only.
  • Bounded retries: 5xx/timeout 3 attempts; NaN on some days only -> 1 re-request then disclosed; error body 0; per-call request budget 48.
  • Leap day: all calendar days requested. skips_leap_dec31 is now Daymet-only. A day the server does not return is counted in missing_ranges / days_missing_internal, flips coverage_complete, and is never filled.
  • Cache: _VARIABLE_CACHE_REVISION precipitation/tmax/tmin/pet 2 -> 3 (old entries are one day short per leap year and are refetched).

Tests

Offline suite (HOME/AIHYDRO_HOME in scratch, PYTHONPATH = worktree, imports verified from the worktree): 912 passed, 56 deselected.
New tests/test_gridmet_ncss.py (28): synthetic NCSS server returning real NetCDF bytes; error-body-with-HTTP-200 fixture (1 request, with retries on and off); size-limited server (split + mosaic equals whole box); budget and depth bounds; 5xx bounds; partial-NaN re-request; cell-centre masking vs an independent reference; unweighted-not-area-weighted; WY2000 = 366 days, WY2000-2003 = 1461 days; server lacking 31 Dec is counted and not filled; pipeline fetch() surfaces method and upstream. tests/test_gridmet_daily_validation.py moved to the new seam and leap semantics.

Live check (approved host only, throttled 1.5 s, 10 requests of the 50 allowed)

  • 7 diagnostic requests (full Fish box 1 day; 2000-12-31 small box; 0.18 and 0.15 deg boxes) established the root cause above.
  • Fish River box (0.893 x 0.632 deg), WY2000 (1999-10-01..2000-09-30), precipitation, through the new code: 2 requests, 366 of 366 days, 0 missing, 0 split, 0 rejected; basin mean over 313 of 315 cells (2 outside the grid); 5.3 s. (Note: WY2000 ends 2000-09-30, so 31 Dec 2000 is outside it.)
  • Calendar 2000 (leap 31 Dec): 1 request, 366 of 366 days, 2000-12-31 present (basin mean 18.50 mm; 2000-12-30: 0.01 mm, so not a duplicated slice).
  • These are transport checks, not scientific results. Artifacts (not committed): /private/tmp/claude-501/-Users-mgalib-Documents-AI-Hydro/c31e1c57-abc3-4322-9112-432e4aca4c98/scratchpad/gridmet/live/ (requests.jsonl, live_result.json).

Docs

CHANGELOG (Unreleased), ARCHITECTURE ("GridMET transport"), DECISIONS entry.

Risks / open

  • Server-side size limits, if any, are unverified beyond 0.89 x 0.63 deg x 366 days.
  • The NaN-vs-corruption call is now ours: permanent NaN = outside grid (disclosed); partial NaN = one re-request then disclosed.
  • MCP/aihydro-data working tree untouched (worktree created from its git dir).

🤖 Generated with Claude Code

mohdgalib and others added 4 commits November 10, 2026 11:26
… years

pygridmet treats any NaN as a corrupt download (retries 15x, then "did NOT
process your request"); cells outside CONUS are always NaN, so border/coastal
boxes such as Fish River could never succeed (~270 requests per failing call).
It also never requests 31 Dec of leap years, which the server holds.

GridMET is now fetched by a direct NCSS client (sources/_gridmet_ncss.py):
no-data cells are counted and disclosed, not retried; basin mean keeps the
cell-centre-in-polygon unweighted rule and records it; a non-NetCDF response
fails fast (GRIDMET_REQUEST_REJECTED) with a bounded split; retries bounded;
all calendar days requested, an absent day is counted and never filled.
Cache revision for precipitation/tmax/tmin/pet 2 -> 3.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The NCSS client does not use pygridmet; CI installs without it, so the
require_import in the backend failed 20+ tests. Tests now also pass with
pygridmet blocked.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t needs; add probe evidence

The NCSS client opened responses with xarray's scipy engine and clipped with
rioxarray; neither is installed by the CI extras (nor by the hyriver extra).
Now: netCDF4 engine (declared in hyriver and dev), shapely cell-centre mask,
structured HYRIVER_NOT_INSTALLED on a missing module, is_available() checks
requests/xarray/netCDF4, pygridmet removed from the hyriver extra.
Also: default 1 s throttle, stale pygridmet cache comments/log wording,
basin-mean note on cells beyond the grid, cache-bump scope in CHANGELOG, and
the live-probe artifact under docs/vision-2040/evidence/.

Verified in a clean venv with exactly CI's extras (.[dev,viz,gee,opendap,legacy-usgs]).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…allback; keep a raw WY2000 response

The mask is shapely.contains_xy (strict interior), not bit-identical to the old
GDAL-scanline rio.clip: a centre exactly on the boundary is excluded (about one
cell per boundary-aligned edge). A polygon with no cell centre now falls back to
the cells it touches (the old code returned an empty grid). Adds the raw Fish
River WY2000 NCSS response to the evidence folder so 313/315 re-derives offline.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@galib9690
galib9690 merged commit 0f89320 into main Nov 10, 2026
10 checks passed
@galib9690
galib9690 deleted the fix/gridmet-large-bbox-leapday branch November 10, 2026 06:34
galib9690 added a commit that referenced this pull request Nov 10, 2026
… evidence folders (#19)

*.nc is gitignored, so the five raw responses cited by the
gridmet-ncss-probe-2026-11-10 README were dropped from #18. Add them, and a
gitignore negation for docs/vision-2040/evidence/**/*.nc so future evidence is
not silently dropped.

Co-authored-by: Mohammad Galib <mgalib@purdue.edu>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
@galib9690 galib9690 mentioned this pull request Nov 10, 2026
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.

2 participants