Skip to content

Can't collapse over a mesh dimension #4672

@pp-mo

Description

@pp-mo

A cube cannot currently be collapsed over a MeshCoord (or mesh dim),
since the collapse operation expects a bounds shape of (N, 2) and it can't handle (N, 4).

Example:

>>> import iris.tests.stock.mesh as ism
>>> cube = ism.sample_mesh_cube()
>>> cube.collapsed('latitude', iris.analysis.MEAN)
/home/h05/itpp/git/iris/iris_main/lib/iris/cube.py:3696: UserWarning: Collapsing spatial coordinate 'latitude' without weighting
  warnings.warn(msg.format(coord.name()))
/home/h05/itpp/git/iris/iris_main/lib/iris/coords.py:2220: UserWarning: Collapsing a non-contiguous coordinate. Metadata may not be fully descriptive for 'i_mesh_face'.
  warnings.warn(msg.format(self.name()))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/h05/itpp/git/iris/iris_main/lib/iris/cube.py", line 3744, in collapsed
    collapsed_cube.replace_coord(coord.collapsed(local_dims))
  File "/home/h05/itpp/git/iris/iris_main/lib/iris/coords.py", line 2215, in collapsed
    elif not self.is_contiguous():
  File "/home/h05/itpp/git/iris/iris_main/lib/iris/coords.py", line 2018, in is_contiguous
    contiguous, _ = self._discontiguity_in_bounds(rtol=rtol, atol=atol)
  File "/home/h05/itpp/git/iris/iris_main/lib/iris/coords.py", line 1944, in _discontiguity_in_bounds
    self._sanity_check_bounds()
  File "/home/h05/itpp/git/iris/iris_main/lib/iris/coords.py", line 1900, in _sanity_check_bounds
    raise ValueError(
ValueError: Invalid operation for 'latitude', with 4 bound(s). Contiguous bounds are only defined for 1D coordinates with 2 bounds.
>>> 

So, the problem isn't really specific to the peculiarities of MeshCoords, it is about the shape of the bounds :
(N. 2) is expected and nothing else works.
However, it would make perfect sense to discard the bounds here and proceed (with a warning).

In the case of a MeshCoord, we can see that this does produce a perfectly sensible result.

>>> cube = cube[..., 0:]
>>> print(cube)
mesh_phenom / (unknown)             (level: 2; i_mesh_face: 3)
    Dimension coordinates:
        level                             x               -
        i_mesh_face                       -               x
    Auxiliary coordinates:
        latitude                          -               x
        longitude                         -               x
        mesh_face_aux                     -               x
>>> cube.collapsed('latitude', iris.analysis.MEAN)
   . . .
ValueError: Invalid operation for 'latitude', with 4 bound(s). Contiguous bounds are only defined for 1D coordinates with 2 bounds.
>>> 
>>> cube.coord('latitude').bounds = None
>>> cube.coord('longitude').bounds = None
>>> cube_1d = cube.collapsed('latitude', iris.analysis.MEAN)
>>> print(cube_1d)
mesh_phenom / (unknown)             (level: 2)
    Dimension coordinates:
        level                             x
    Scalar coordinates:
        i_mesh_face                 1, bound=(0, 2)
        latitude                    3201, bound=(3200, 3202)
        longitude                   3101 degrees_east, bound=(3100, 3102) degrees_east
        mesh_face_aux               0.0, bound=(0.0, 0.0)
    Cell methods:
        mean                        latitude
>>> 


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions