Copy target coordinates onto regridded result cube (#6844)#7124
Open
gaoflow wants to merge 1 commit into
Open
Conversation
_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 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.