Skip to content

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

Open
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/regrid-copy-target-coords
Open

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

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 1, 2026

Copy link
Copy Markdown

🚀 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.

_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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Regridding maintains a reference to target coordinates

1 participant