Skip to content

Preserve masked bounds in Coord.cell (#5158)#7120

Open
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/cell-preserve-masked-bound
Open

Preserve masked bounds in Coord.cell (#5158)#7120
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/cell-preserve-masked-bound

Conversation

@gaoflow
Copy link
Copy Markdown

@gaoflow gaoflow commented Jun 1, 2026

🚀 Pull Request

Description

Closes #5158.

When a coordinate has a masked bound, :meth:iris.coords.Coord.cell revealed the value stored underneath the mask instead of reporting it as masked. This also surfaced in the cube / coordinate printout.

The cause is the use of np.array() when extracting the bound, which drops the mask:

>>> import numpy as np
>>> print(np.array(np.ma.masked_array([2], mask=[1])))      # mask lost
[2]
>>> print(np.asanyarray(np.ma.masked_array([2], mask=[1]))) # mask kept
[--]

The point path was already switched to np.asanyarray for exactly this reason (and #5158 was diagnosed by @pp-mo as pointing at Coord.cell); this PR applies the same treatment to bounds.

Reproduction (before)

import numpy.ma as ma
from iris.coords import AuxCoord

coord = AuxCoord([5.0], bounds=ma.masked_array([[3.0, 7.0]], mask=[[True, False]]))
print(coord.cell(0))
# Cell(point=5.0, bound=(3.0, 7.0))   <- 3.0 should be masked

After

print(coord.cell(0))
# Cell(point=5.0, bound=(masked, 7.0))

and the cube printout now shows height 5.0, bound=(--, 7.0) instead of (3.0, 7.0).

Implementation

np.array(x, ndmin=1) is replaced with np.atleast_1d(np.asanyarray(x)), the mask-preserving equivalent (mirroring the existing point handling on the line above). Unmasked bounds are unaffected.

Verification

  • New tests in Test_cell: test_masked_bound (fails on main, passes here) and test_masked_point (guards the already-fixed point path).
  • Full tests/unit/coords/ suite (411), test_Cell (31), and tests/unit/representation/ cube summary/printout suite (59) all pass.
  • ruff check / ruff format clean.

Coord.cell() used np.array() to extract the bound for a given index,
which silently drops the mask and reveals the raw value stored beneath
it. The point path was already changed to np.asanyarray for this reason;
apply the same treatment to bounds so a masked bound stays masked, both
in the returned Cell and in the cube/coordinate printout.

Fixes SciTools#5158.
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.

cube printout incorrectly displays a masked scalar coord

1 participant