Skip to content

Add optional variable-aware regridding#406

Open
rbeucher wants to merge 1 commit into
mainfrom
feature/optional-variable-aware-regridding
Open

Add optional variable-aware regridding#406
rbeucher wants to merge 1 commit into
mainfrom
feature/optional-variable-aware-regridding

Conversation

@rbeucher
Copy link
Copy Markdown
Member

Summary

Adds opt-in regridding to the CMORisation pipeline via a new access_moppy.regrid module. Native-grid output remains the default; regridding is activated by adding a regrid: block to the batch config.

Changes

  • access_moppy.regrid: RegridConfig/WeightConfig parsing, variable-aware method selection, cached sparse-weight application (no ESMF at runtime), xESMF-based weight generation, built-in cmip7-1x1 target grid.
  • Method policy (auto-selected per variable): conservative for flux/extensive quantities, bilinear for smooth state fields, nearest_s2d for masks/categories, vector fields refused with a clear error.
  • Pipeline integration: regridding applied after derivation/metadata/time handling, before reorder/write; hooked into ACCESS_ESM_CMORiser, realm CMORisers, and batch templates.
  • CLI: optional moppy-regrid-weights entry point for pre-generating weight files.
  • Metadata: updates grid_label, grid, regular lat/lon + bounds, removes stale native-grid cell_measures.
  • Docs: docs/source/regridding.rst + example config block.
  • Tests: unit tests for method policy, target grid metadata, cached-weight application, vector refusal, and conservative-bounds error.

Verification

  • python3 -m compileall -q src/access_moppy tests/unit/test_regrid.py
  • git diff --check
  • Full pytest blocked in the coding environment (missing numpy/xarray/pytest); CI will run the full suite.

Known limitations / follow-up

  • Sparse weight application materialises NumPy arrays; Dask laziness is future work.
  • Only the built-in cmip7-1x1 target grid; a target-grid registry is planned.
  • Vector interpolation/rotation not yet supported.
  • Weight-file validation is minimal.

@rbeucher rbeucher requested review from charles-turner-1 and removed request for charles-turner-1 May 28, 2026 04:43
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

❌ Patch coverage is 68.42105% with 84 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.8%. Comparing base (85df727) to head (52347bb).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/access_moppy/regrid.py 68.5% 59 Missing and 22 partials ⚠️
src/access_moppy/base.py 40.0% 2 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (68.4%) is below the target coverage (90.0%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##            main    #406     +/-   ##
=======================================
- Coverage   74.9%   74.8%   -0.1%     
=======================================
  Files         28      29      +1     
  Lines       5246    5545    +299     
  Branches     963    1012     +49     
=======================================
+ Hits        3929    4146    +217     
- Misses      1092    1152     +60     
- Partials     225     247     +22     
Flag Coverage Δ
unit 74.8% <68.4%> (-0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@charles-turner-1 charles-turner-1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple minor comments and theres a fair amount of missing coverage but not spotting anything that worries me!

Comment on lines +351 to +354
method: str,
weight_path: Path,
) -> xr.Dataset:
del method # Method is encoded in the weight file; kept for future validation.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems kinda weird to me?

Comment on lines +540 to +564
def main(argv: list[str] | None = None) -> None:
parser = argparse.ArgumentParser(
description="Create ACCESS-MOPPy cached regridding weights"
)
subparsers = parser.add_subparsers(dest="command", required=True)
create = subparsers.add_parser("create", help="create xESMF/ESMF weight file")
create.add_argument(
"--source-grid",
required=True,
help="NetCDF file containing source grid coordinates/bounds",
)
create.add_argument(
"--target-grid", required=True, help="Target grid name, e.g. cmip7-1x1"
)
create.add_argument("--method", required=True, choices=sorted(REGRID_METHODS))
create.add_argument("--output", required=True, help="Output weight NetCDF file")
args = parser.parse_args(argv)
if args.command == "create":
path = create_weights(
args.source_grid, args.target_grid, args.method, args.output
)
print(f"Created regridding weights: {path}")


if __name__ == "__main__":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure this is the right place for the entry point? I had a look through some of the other files (base, driver, ocean) and it doesn't look like they have similar ones - is there a cli.py or something?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file would probably benefit from docstrings. Nothing looks alarming without it and the functions are generally pretty short but I think it'd make it more readable

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.

3 participants