iris
iris copied to clipboard
Can't collapse mesh
🐛 Bug Report
It's not possible to collapse a mesh cube. There's an error as it tries to collapse the mesh coord.
return newcube.collapsed(coords, aggregator, **aggkeys)
File "/home/h02/bblay/.conda/envs/autoassess_new/lib/python3.10/site-packages/iris/cube.py", line 3904, in collapsed
collapsed_cube.replace_coord(coord.collapsed(local_dims))
File "/home/h02/bblay/.conda/envs/autoassess_new/lib/python3.10/site-packages/iris/coords.py", line 2284, in collapsed
coord = self.copy(points=points, bounds=bounds)
File "/home/h02/bblay/.conda/envs/autoassess_new/lib/python3.10/site-packages/iris/experimental/ugrid/mesh.py", line 2973, in copy
raise ValueError(msg)
ValueError: Cannot change the content of a MeshCoord.
@pp-mo suggests, "I think this is probably a little bug introduced because MeshCoords are "like" AuxCoords but a bit different.", and also suggests a workaround is to remove the mesh coords, "You need to index the cube on the mesh dimension, with something other than ':' or 'slice(None)'." e.g with cube[..., 0:] or cube[tuple(slice(0, None) for _ in cube.shape)]).
Environment
- Iris Version: 3.6
Thamks for raising @bblay! For the future developer looking at this: do you have an easy way to reproduce the above stack trace? We have some sample 'mesh Cubes' somewhere in iris.tests.stock.
I've encountered the same issue, I'm trying to compare something UM and LFRic and its nice and easy to do a weighted average on a regular cube:
grid_areas = iris.analysis.cartography.area_weights(um_in_cube)
um_in_cube_mean = um_in_cube.collapsed(['longitude', 'latitude'], iris.analysis.MEAN, weights=grid_areas)
But not as easy (if not just impossible) to do in LFRic mesh world!
Yup, we need to make this easier out the box for the community to use 👍