iris icon indicating copy to clipboard operation
iris copied to clipboard

`save_mesh` and `Mesh.to_MeshCoord` fails with confusing errors when input coordinates do not have `standard_name`

Open schlunma opened this issue 1 year ago • 0 comments

🐛 Bug Report

Calling save_mesh or Mesh.to_MeshCoord fails with confusing error messages when the coordinates used to create the mesh have standard_name=None

How To Reproduce

Steps to reproduce the behaviour:

MWE
from pathlib import Path

from iris.coords import AuxCoord
from iris.experimental.ugrid import Mesh, Connectivity, save_mesh


# Coordinate metadata that doesn't create the bug
face_lat = AuxCoord([2])
face_lon = AuxCoord([2])
node_lat = AuxCoord([0, 0, 4])
node_lon = AuxCoord([0, 4, 2])
face_node_conn = [[0, 1, 2]]

# Adapt standard_names to force/not force bug
# face_lat.standard_name = 'latitude'
# face_lon.standard_name = 'longitude'
# node_lat.standard_name = 'latitude'
# node_lon.standard_name = 'longitude'

# Create mesh (always succeeds)
connectivity = Connectivity(
    indices=face_node_conn,
    cf_role='face_node_connectivity',
)
mesh = Mesh(
    topology_dimension=2,
    node_coords_and_axes=[(node_lat, 'y'), (node_lon, 'x')],
    connectivities=[connectivity],
    face_coords_and_axes=[(face_lat, 'y'), (face_lon, 'x')],
)


# Save mesh (always fails when either face or node standard_names are None)
mesh_path = Path.home() / 'mesh_new.nc'
save_mesh(mesh, mesh_path)
print("save_mesh ok")


# Save MeshCoord (always fails when either face or node standard_names are None)
mesh_coord = mesh.to_MeshCoord('face', 'y')
print("Mesh.to_MeshCoord ok")

Expected behaviour

Either a clear error message or no error.

Error messages

  • save_mesh and node_coord.standard_name=None: KeyError: 'node'
Traceback (most recent call last):
  File "mesh_bug.py", line 35, in <module>
    save_mesh(mesh, mesh_path)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/save.py", line 59, in save_mesh
    sman._add_mesh(mesh)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/fileformats/netcdf.py", line 1438, in _add_mesh
    element_dims=(mesh_dims[location],),
KeyError: 'node'
  • save_mesh and face_coord.standard_name=None: AssertionError
Traceback (most recent call last):
  File "mesh_bug.py", line 35, in <module>
    save_mesh(mesh, mesh_path)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/save.py", line 59, in save_mesh
    sman._add_mesh(mesh)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/fileformats/netcdf.py", line 1434, in _add_mesh
    coord_name = self._create_generic_cf_array_var(
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/fileformats/netcdf.py", line 2315, in _create_generic_cf_array_var
    cf_name = self._get_coord_variable_name(cube_or_mesh, element)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/fileformats/netcdf.py", line 2150, in _get_coord_variable_name
    assert isinstance(coord, Connectivity)
AssertionError
  • Mesh.to_MeshCoord and node_coord.standard_name=None: iris.exceptions.CoordinateNotFoundError: 'Expected to find exactly 1 coordinate, but found none.'
Traceback (most recent call last):
  File "mesh_bug.py", line 40, in <module>
    mesh_coord = mesh.to_MeshCoord('face', 'y')
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 1947, in to_MeshCoord
    return MeshCoord(mesh=self, location=location, axis=axis)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 2835, in __init__
    points, bounds = self._construct_access_arrays()
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 3049, in _construct_access_arrays
    points_coord = self.mesh.coord(include_faces=True, axis=axis)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 1614, in coord
    result = self._coord_manager.filter(
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 2283, in filter
    raise CoordinateNotFoundError(emsg)
iris.exceptions.CoordinateNotFoundError: 'Expected to find exactly 1 coordinate, but found none.'
  • Mesh.to_MeshCoord and face_coord.standard_name=None: iris.exceptions.CoordinateNotFoundError: 'Expected to find exactly 1 coordinate, but found none.'
Traceback (most recent call last):
  File "mesh_bug.py", line 40, in <module>
    mesh_coord = mesh.to_MeshCoord('face', 'y')
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 1947, in to_MeshCoord
    return MeshCoord(mesh=self, location=location, axis=axis)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 2835, in __init__
    points, bounds = self._construct_access_arrays()
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 3040, in _construct_access_arrays
    node_coord = self.mesh.coord(include_nodes=True, axis=axis)
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 1614, in coord
    result = self._coord_manager.filter(
  File "/work/bd0854/b309141/mambaforge/envs/esm38/lib/python3.8/site-packages/iris/experimental/ugrid/mesh.py", line 2283, in filter
    raise CoordinateNotFoundError(emsg)
iris.exceptions.CoordinateNotFoundError: 'Expected to find exactly 1 coordinate, but found none.'

Environment

OS: Red Hat Enterprise Linux 8.4 (Ootpa) iris version: 3.2.1.post0

schlunma avatar Jul 14 '22 08:07 schlunma