Skip to content

MaskRegions(2d_mask, 3d_grid) fails: xgcm.pad iterates all grid axes #24

Description

@hdrake

Symptom

On regionate 0.6.0 (topology-overhaul), passing a 2D boolean mask
(dims=('yh', 'xh')) together with a 3D xgcm.Grid (X, Y, Z axes)
crashes in xgcm.pad:

```
File .../regionate/regions.py:186, in MaskRegions.init
labels, ncomp = connected_components(grid, self.mask)
File .../regionate/boundaries.py:156, in connected_components
Ip = _pad_center(grid, ids_da).transpose(Yc, Xc).values[None]
File .../regionate/boundaries.py:104, in _pad_center
return pad(da, grid, padding_width, padding=padding, fill_value=np.nan)
File .../xgcm/padding.py:869, in pad
da_padded = _pad_basic(data, grid, padding_width, padding, fill_value)
File .../xgcm/padding.py:595, in _pad_basic
_, dim = axis._get_position_name(da)
File .../xgcm/axis.py:240, in Axis._get_position_name
raise KeyError(...)
KeyError: "None of the DataArray's dims ('yh', 'xh') were found in axis coords."
```

_pad_center iterates every axis in grid.axes and calls
xgcm.pad on each; when the Z axis has no matching dim in the 2D id
field, xgcm raises.

Repro (minimal)

Any workflow that traces a 2D isobath / depth mask on a grid that
carries a Z axis — e.g. building a full ice-shelf-aware
xgcm.Grid and then calling
MaskRegions((deptho < 1000.), grid) on the same grid. In our
downstream project (xcryocouple), this exact call pattern worked on
regionate 0.5.5.

Workaround we're using

Building a temporary 2D grid alongside the 3D one, sharing X/Y coords
and padding:

```python
grid2d = xgcm.Grid(
grid._ds,
coords={ax: grid.axes[ax].coords
for ax in grid.axes if ax in ('X', 'Y')},
padding={ax: grid.axes[ax].padding
for ax in grid.axes if ax in ('X', 'Y')},
autoparse_metadata=False,
)
regions = reg.MaskRegions(mask_2d, grid2d)
```

Suggested fix

In connected_components (and any other regionate helper that pads
with _pad_center), only iterate axes that intersect mask.dims.
Or expose an explicit axes=('X','Y') argument on MaskRegions
so a caller can restrict to the axes their mask actually spans.

Happy to open a PR if the fix direction is agreed. Reporting per a
maintainer request to surface friction points during the topology-
overhaul migration.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions