Skip to content

Copy target coordinates onto regridded result cube (#6844)#7124

Merged
ukmo-ccbunney merged 1 commit into
SciTools:mainfrom
gaoflow:fix/regrid-copy-target-coords
Jun 30, 2026
Merged

Copy target coordinates onto regridded result cube (#6844)#7124
ukmo-ccbunney merged 1 commit into
SciTools:mainfrom
gaoflow:fix/regrid-copy-target-coords

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Pull Request

Description

Closes #6844.

After regridding, the horizontal (X/Y) coordinates on the result cube were the same objects as the coordinates on the target grid cube, so mutating one mutated the other:

result = src_cube.regrid(grid_cube, AreaWeighted())
result.coord("longitude") is grid_cube.coord("longitude")   # True!
result.coord("longitude").points += 100
grid_cube.coord("longitude").points                         # also changed!

Cause

iris.analysis._regrid._create_cube builds the result cube and attaches the target's coordinates. Its docstring already states:

Horizontal coordinates are copied from the target cube.

…and the source coordinates it carries across are indeed copied (coord.copy()). But the target coordinates were added by reference:

for tgt_coord, dim in zip(tgt_coords, (grid_dim_x, grid_dim_y)):
    ...
    result.add_dim_coord(tgt_coord, dim)   # same object as the target's

This affects all regridders that share _create_cube. (The curvilinear caller happened to pre-copy its coords, but AreaWeighted and the rectilinear RectilinearRegridder did not — and a regridder also reuses a single target snapshot across calls, so the aliasing could leak between successive results too.)

Fix

Copy each target coordinate inside _create_cube before adding it, matching both the docstring and the existing treatment of source coordinates. This centralises the behaviour for every regridder. The pre-existing .copy() in the curvilinear caller becomes harmlessly redundant.

Verification

  • New test Test::test_result_coords_independent_of_target — asserts the result's lat/lon equal the target's but are distinct objects, and that mutating the result leaves the target unchanged. It fails on main and passes here.
  • Manually confirmed for AreaWeighted, Linear and Nearest: result coordinates carry the correct points/bounds yet are independent of the target.
  • tests/unit/analysis/regrid/ and tests/unit/analysis/area_weighted/ suites pass (81 tests; the 4 Test__derived_coord errors are pre-existing and unrelated — they require optional regridding dependencies not installed locally).
  • ruff check / ruff format clean.

@gaoflow gaoflow force-pushed the fix/regrid-copy-target-coords branch from 8e79ceb to b93b0e5 Compare June 18, 2026 21:39
@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (746f0a6) to head (fdd27dd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7124   +/-   ##
=======================================
  Coverage   90.15%   90.15%           
=======================================
  Files          91       91           
  Lines       24985    24986    +1     
  Branches     4685     4685           
=======================================
+ Hits        22526    22527    +1     
  Misses       1682     1682           
  Partials      777      777           

☔ View full report in Codecov by Harness.
📢 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.

@ukmo-ccbunney ukmo-ccbunney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix @gaoflow - code changes look good. 👍🏼

As mentioned in some of your other PRs:
There are some updates required regarding the What's New, but I see that you have already been applying those to your other PRs - top points for that! 💯

Are you happy to make the same updates here?
If not, please do say and I'll raise a PR against your branch.

_create_cube placed the target grid's horizontal coordinate objects
directly onto the result cube, so the result and the target shared the
same coordinate objects: mutating one mutated the other. The docstring
already states these coordinates are 'copied from the target cube', and
source coordinates are likewise copied, so copy the target coordinates
too. This makes all regridders that use _create_cube consistent.

Fixes SciTools#6844.
@gaoflow gaoflow force-pushed the fix/regrid-copy-target-coords branch from b93b0e5 to fdd27dd Compare June 26, 2026 11:31
@gaoflow

gaoflow commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated this PR for the new towncrier changelog layout and rebased onto current main.

Changes in fdd27dde:

  • removed the hand-written docs/src/whatsnew/latest.rst entry from this PR
  • added changelog/7124.bugfix.rst
  • kept the regrid code/test change otherwise unchanged

Verification after the rebase:

ruff check lib/iris/analysis/_regrid.py lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py
ruff format --check lib/iris/analysis/_regrid.py lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py
uv run --with pytest --with pytest-mock --with requests --with filelock --with-editable . python -m pytest lib/iris/tests/unit/analysis/area_weighted/test_AreaWeightedRegridder.py -q
uv run --with towncrier towncrier build --draft --version 0.0
git diff --check origin/main...HEAD

The targeted test file passed with 9 passed, 2 skipped; the skips are the existing external-data tests.

@ukmo-ccbunney ukmo-ccbunney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @gaoflow for taking the time to rebase you branch and add the news fragment. 🏅

Changes look good - LGTM! 🚀

@ukmo-ccbunney ukmo-ccbunney merged commit 03489e8 into SciTools:main Jun 30, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Regridding maintains a reference to target coordinates

2 participants