Copy target coordinates onto regridded result cube (#6844)#7124
Conversation
8e79ceb to
b93b0e5
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
ukmo-ccbunney
left a comment
There was a problem hiding this comment.
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.
b93b0e5 to
fdd27dd
Compare
|
Updated this PR for the new towncrier changelog layout and rebased onto current Changes in
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...HEADThe targeted test file passed with |
ukmo-ccbunney
left a comment
There was a problem hiding this comment.
Thank you @gaoflow for taking the time to rebase you branch and add the news fragment. 🏅
Changes look good - LGTM! 🚀
🚀 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:
Cause
iris.analysis._regrid._create_cubebuilds the result cube and attaches the target's coordinates. Its docstring already states:…and the source coordinates it carries across are indeed copied (
coord.copy()). But the target coordinates were added by reference:This affects all regridders that share
_create_cube. (The curvilinear caller happened to pre-copy its coords, butAreaWeightedand the rectilinearRectilinearRegridderdid 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_cubebefore 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
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 onmainand passes here.AreaWeighted,LinearandNearest: result coordinates carry the correct points/bounds yet are independent of the target.tests/unit/analysis/regrid/andtests/unit/analysis/area_weighted/suites pass (81 tests; the 4Test__derived_coorderrors are pre-existing and unrelated — they require optional regridding dependencies not installed locally).ruff check/ruff formatclean.