processing¶
processing
¶
Core processing: zonal statistics and temporal aggregation via gdptools.
Wrap gdptools processing classes (UserTiffData, NHGFStacTiffData,
NHGFStacData, ClimRCatData) with ZonalGen, WeightGen, and
AggGen to compute area-weighted statistics of raster data over polygon
features. Support both continuous variables (mean, median, etc.) and
categorical variables (class fraction extraction).
This module provides two processor classes:
- :class:
ZonalProcessor-- static raster-on-polygon zonal statistics usingZonalGenwith the exactextract engine. Supports local GeoTIFF, STAC COG, and NHGF STAC static strategies. - :class:
TemporalProcessor-- time-varying aggregation using theWeightGen+AggGenpipeline. Supports NHGF STAC temporal and ClimateR-Catalog (OPeNDAP) strategies.
The factory function :func:get_processor selects the appropriate processor
based on fabric geometry type.
Notes
gdptools ZonalGen internally reprojects target polygons into the source
CRS, so no CRS alignment is needed in hydro-param. The exactextract engine
produces fractional coverage weights for partial cell overlap.
See Also
design.md : Section 11.5 (processing architecture).
hydro_param.data_access : Data loading functions that produce the GeoTIFFs
consumed by :class:ZonalProcessor.
hydro_param.pipeline : Orchestrator that calls these processors per batch.
Processor
¶
Bases: Protocol
Protocol defining the interface for spatial processing strategies.
All processor implementations must provide a process method that
computes zonal statistics for a raster variable over polygon features.
This protocol enables the pipeline to work with different processing
backends (currently only :class:ZonalProcessor for polygon targets;
a future grid processor for raster-on-raster via xesmf is planned).
See Also
ZonalProcessor : Concrete implementation using gdptools ZonalGen. get_processor : Factory function that selects the appropriate processor.
ZonalProcessor
¶
Compute area-weighted zonal statistics via gdptools ZonalGen.
Wrap the gdptools UserTiffData + ZonalGen pipeline to compute
statistics (mean, median, majority, class fractions) of raster data
over polygon features. This is the core processing class for the
stac_cog, local_tiff, and nhgf_stac (static) strategies.
The class is stateless -- each call to :meth:process or
:meth:process_nhgf_stac creates fresh gdptools objects. This design
allows safe reuse across batches and variables without accumulated state.
Notes
The default exactextract engine computes fractional coverage weights
for partial cell overlap, producing accurate area-weighted statistics
even when polygon boundaries do not align with raster cell edges.
For categorical variables (e.g., NLCD land cover classes), set
categorical=True to get per-class area fractions instead of
continuous statistics.
See Also
TemporalProcessor : For time-varying datasets (SNODAS, gridMET). hydro_param.data_access.fetch_stac_cog : Produces the GeoTIFFs consumed here.
process
¶
process(
fabric: GeoDataFrame,
tiff_path: Path,
variable_name: str,
id_field: str,
*,
engine: ZonalEngine = "exactextract",
statistics: list[str] | None = None,
categorical: bool = False,
source_crs: str | None = None,
x_coord: str = "x",
y_coord: str = "y",
) -> pd.DataFrame
Compute zonal statistics for a raster variable over polygon features.
Read a GeoTIFF, construct a gdptools UserTiffData + ZonalGen
pipeline, and compute area-weighted statistics for each polygon in
the target fabric. This is the workhorse method called by the
pipeline's _process_batch for stac_cog and local_tiff
strategies.
| PARAMETER | DESCRIPTION |
|---|---|
fabric
|
Target polygon features (a batch subset of the full fabric).
Must contain at least the
TYPE:
|
tiff_path
|
Path to the GeoTIFF for this variable. Typically a
batch-clipped raster saved by :func:
TYPE:
|
variable_name
|
Name of the variable being processed (e.g.,
TYPE:
|
id_field
|
Column name for feature IDs in the fabric (e.g.,
TYPE:
|
engine
|
gdptools zonal engine. One of
TYPE:
|
statistics
|
Which statistics to compute and return (e.g.,
TYPE:
|
categorical
|
If True, compute per-class area fractions instead of continuous statistics. Used for land cover variables (e.g., NLCD classes).
TYPE:
|
source_crs
|
Source dataset CRS as a string (e.g.,
TYPE:
|
x_coord
|
Name of the x coordinate in the source raster. Maps to
gdptools
TYPE:
|
y_coord
|
Name of the y coordinate in the source raster. Maps to
gdptools
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with one row per polygon feature. For continuous
variables, columns are the requested statistics (e.g.,
|
Notes
gdptools ZonalGen handles CRS alignment internally by
reprojecting the target polygons into the source raster CRS.
No manual CRS transformation is needed in hydro-param.
Source code in src/hydro_param/processing.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
process_nhgf_stac
¶
process_nhgf_stac(
fabric: GeoDataFrame,
collection_id: str,
variable_name: str,
id_field: str,
*,
year: int | None = None,
engine: ZonalEngine = "exactextract",
statistics: list[str] | None = None,
categorical: bool = False,
band: int = 1,
) -> pd.DataFrame
Compute zonal statistics from an NHGF STAC collection.
Use gdptools NHGFStacTiffData to read COGs directly from the
NHGF STAC catalog hosted on OSN (Open Storage Network), bypassing
any intermediate GeoTIFF download. This is the processing path for
the nhgf_stac (static) strategy, used primarily for NLCD
Annual land cover data (6 collections: nlcd-LndCov,
nlcd-FctImp, etc.).
Unlike :meth:process, this method does not require a local
GeoTIFF -- gdptools handles the remote COG access, subsetting,
and zonal computation in a single pipeline.
| PARAMETER | DESCRIPTION |
|---|---|
fabric
|
Target polygon features. Must contain at least the
TYPE:
|
collection_id
|
NHGF STAC collection identifier (e.g.,
TYPE:
|
variable_name
|
Variable / layer name within the collection (passed to
gdptools as
TYPE:
|
id_field
|
Column name for feature IDs in the fabric (e.g.,
TYPE:
|
year
|
Select a specific STAC item by year (e.g.,
TYPE:
|
engine
|
gdptools zonal engine. Default
TYPE:
|
statistics
|
Which statistics to compute. Defaults to
TYPE:
|
categorical
|
If True, compute per-class area fractions instead of continuous statistics. Required for NLCD land cover classes.
TYPE:
|
band
|
Raster band to read from COG files. Default is 1 (single-band).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with one row per polygon feature, indexed by
|
See Also
process : Zonal stats from local GeoTIFF files. TemporalProcessor.process_nhgf_stac : Temporal aggregation from NHGF STAC Zarr collections.
Source code in src/hydro_param/processing.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
TemporalProcessor
¶
Compute temporal aggregation via gdptools WeightGen + AggGen.
Handle time-varying datasets (e.g., SNODAS, CONUS404-BA, gridMET) by
computing spatial intersection weights and then applying those weights
to aggregate gridded time series onto polygon features. Output is an
xr.Dataset with (time, features) dimensions.
This class supports two data access backends:
- :meth:
process_nhgf_stac-- NHGF STAC Zarr collections viaNHGFStacData(for SNODAS, CONUS404-BA) - :meth:
process_climr_cat-- ClimateR-Catalog OPeNDAP datasets viaClimRCatData(for gridMET, Daymet)
The class is stateless -- each method call creates fresh gdptools objects.
Notes
The processing pipeline is: data source -> WeightGen (compute
area-weighted intersection matrix) -> AggGen (apply weights to
aggregate time series). Weight generation uses EPSG:6931 (LAEA) by
default for accurate area computation.
The pipeline supports calendar-year splitting for multi-year datasets
at the orchestrator level (see :mod:hydro_param.pipeline).
See Also
ZonalProcessor : For static raster-on-polygon zonal statistics. hydro_param.data_access.load_climr_catalog : Load the ClimateR catalog.
process_nhgf_stac
¶
process_nhgf_stac(
fabric: GeoDataFrame,
collection_id: str,
variable_names: list[str],
id_field: str,
time_period: list[str],
*,
stat_method: str = "mean",
weight_gen_crs: int = 6931,
) -> xr.Dataset
Compute temporal aggregation from an NHGF STAC Zarr collection.
Fetch gridded time series from the NHGF STAC catalog (Zarr format),
compute polygon intersection weights, and aggregate to polygon
features. Used for the nhgf_stac (temporal) strategy with
datasets like SNODAS (snow water equivalent) and CONUS404-BA
(basin-averaged atmospheric forcing).
| PARAMETER | DESCRIPTION |
|---|---|
fabric
|
Target polygon features. Must contain at least the
TYPE:
|
collection_id
|
NHGF STAC collection identifier (e.g.,
TYPE:
|
variable_names
|
Variables to process from the collection (e.g.,
TYPE:
|
id_field
|
Column name for feature IDs in the fabric (e.g.,
TYPE:
|
time_period
|
Two-element list
TYPE:
|
stat_method
|
Aggregation statistic applied per time step. One of
TYPE:
|
weight_gen_crs
|
EPSG code for the CRS used during weight generation. Default is 6931 (LAEA -- Lambert Azimuthal Equal Area), which provides accurate area-weighted intersections.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dataset
|
Dataset with dimensions |
See Also
process_climr_cat : Temporal aggregation from ClimateR-Catalog. ZonalProcessor.process_nhgf_stac : Static zonal stats from NHGF STAC.
Source code in src/hydro_param/processing.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
process_climr_cat
¶
process_climr_cat(
fabric: GeoDataFrame,
catalog_id: str,
variable_names: list[str],
id_field: str,
time_period: list[str],
*,
stat_method: str = "mean",
weight_gen_crs: int = 6931,
) -> xr.Dataset
Compute temporal aggregation from a ClimateR-Catalog OPeNDAP dataset.
Fetch gridded time series via OPeNDAP from datasets indexed in the
ClimateR-Catalog (e.g., gridMET, Daymet), compute polygon
intersection weights, and aggregate to polygon features. Used for
the climr_cat processing strategy.
This is the preferred method for gridMET access because the gridMET copy on the USGS GDP STAC is not kept up to date, whereas OPeNDAP serves the canonical source.
| PARAMETER | DESCRIPTION |
|---|---|
fabric
|
Target polygon features. Must contain at least the
TYPE:
|
catalog_id
|
ClimateR catalog identifier (e.g.,
TYPE:
|
variable_names
|
Variables to process (e.g.,
TYPE:
|
id_field
|
Column name for feature IDs in the fabric (e.g.,
TYPE:
|
time_period
|
Two-element list
TYPE:
|
stat_method
|
Aggregation statistic applied per time step (e.g.,
TYPE:
|
weight_gen_crs
|
EPSG code for weight generation CRS. Default is 6931 (LAEA) for accurate area-weighted intersections.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dataset
|
Dataset with dimensions |
See Also
process_nhgf_stac : Temporal aggregation from NHGF STAC Zarr. hydro_param.data_access.build_climr_cat_dict : Build catalog dicts. hydro_param.data_access.load_climr_catalog : Load the catalog.
Source code in src/hydro_param/processing.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
get_processor
¶
Select the appropriate processor for a fabric geometry type.
Route polygon fabrics to :class:ZonalProcessor. This is the factory
function for the processing pathway bifurcation described in design.md
section 5.3. Currently only polygon targets (Polygon, MultiPolygon) are
supported; grid target support via xesmf/rioxarray is planned.
| PARAMETER | DESCRIPTION |
|---|---|
fabric
|
Target fabric GeoDataFrame. Must be non-empty and contain only polygon geometry types.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Processor
|
A :class: |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the fabric is empty or contains unsupported geometry types (e.g., Point, LineString). The error message lists the unsupported types found. |
Notes
Grid target support (raster-on-raster operations via xesmf) is a
planned feature. When implemented, this function will also return
a GridProcessor for grid-type fabrics.
See Also
ZonalProcessor : The processor returned for polygon fabrics. TemporalProcessor : Temporal aggregation (not selected by this factory; used directly by the pipeline for temporal strategies).