You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When applying a constraint on a MeshCoord and a callback which checks the loaded cube has a mesh, the callback is not applied on the returned cube.
How To Reproduce
Steps to reproduce the behaviour:
Define a custom callback function which raises an exception if a cube does not have a mesh.
Attempt to load UGrid data with a constraint on latitude (a MeshCoord) and pass the custom callback.
The cube is loaded, without a mesh. The MeshCoords have been converted to AuxCoords as expected, but no exception was raised.
Expected behaviour
I expected an exception to have been raised by the custom callback function, since the returned cube has no mesh.
Reproducible example
importirisfromiris.experimental.ugridimportPARSE_UGRID_ON_LOADimportiris.exceptionsfromirisimportsample_data_pathdefugrid_load(uris, constraints=None):
"""Load a UGrid file. Will not load if the file contains regular data. Parameters ---------- uris : Any Location of file to load. Must be a NetCDF file. constraints: Any | None One or more iris constraints. Each constraint can be either a string, or an instance of :class:`iris.Constraint`. If the constraint is a string it will be used to match against cube.name(). Returns ------- iris.cube.CubeList An iris :class:`iris.cube.CubeList` containing the loaded data. Raises ------ iris.exceptions.InvalidCubeError If UGrid data cannot be loaded from the file. """withPARSE_UGRID_ON_LOAD.context():
ugrid_cubelist=iris.load(uris=uris, constraints=constraints, callback=mesh_check)
returnugrid_cubelistdefmesh_check(cube, field, filename):
"""Check if the loaded data is UGrid or not. A function to be passed to callback in iris.load. Parameters ---------- cube : iris.cube.Cube Loaded :class:`iris.cube.CubeList` to be checked field : Any Only present to satisfy the function signature used by callback. filename : Any Original file from which the cube data was loaded Raises ------ iris.exceptions.InvalidCubeError If the loaded cube does not contain UGrid data """ifcube.meshisNone:
raiseiris.exceptions.InvalidCubeError(
f"Attempting to load from file '{filename}' did not return UGrid data."
)
if__name__=="__main__":
latitude_constraint=iris.Constraint(latitude=lambdacell: cell>0)
# Try with non-UGrid netcdf first to confirm it worksnon_ugrid_path=sample_data_path("atlantic_profiles.nc")
try:
ugrid_load(non_ugrid_path, latitude_constraint)
exceptiris.exceptions.InvalidCubeErrorasexc:
print(exc)
# Now try constraining on a MeshCoord on load of a file containing UGrid data# Expected to raise an exception since the returned cube has no mesh due to the constraint# having converted the MeshCoords to AuxCoordsmesh_C4_path=sample_data_path("mesh_C4_synthetic_float.nc")
ugrid_cubelist=ugrid_load(mesh_C4_path, latitude_constraint)
iflen(ugrid_cubelist) !=0:
print(f"Data successfully loaded from '{mesh_C4_path}' with constraint '{latitude_constraint}'.")
print(ugrid_cubelist[0])
print(f"Cube's mesh is: {ugrid_cubelist[0].mesh}")
Output:
Attempting to load from file '.../iris_sample_data/sample_data/atlantic_profiles.nc' did not return UGrid data.
Data successfully loaded from '.../iris_sample_data/sample_data/mesh_C4_synthetic_float.nc' with constraint 'Constraint(coord_values={'latitude': <function <lambda> at 0x7fd980bc0310>})'.
synthetic / (1) (-- : 32)
Auxiliary coordinates:
latitude x
longitude x
Attributes:
NCO 'netCDF Operators version 4.7.5 (Homepage = http://nco.sf.net, Code = h ...'
history 'Mon Apr 12 01:44:41 2021: ncap2 -s synthetic=float(synthetic) mesh_C4_synthetic.nc ...'
nco_openmp_thread_number 1
Cube's mesh is: None
Environment
OS & Version: RHEL 7.9
Iris Version: 3.7.1
The text was updated successfully, but these errors were encountered:
馃悰 Bug Report
When applying a constraint on a
MeshCoord
and a callback which checks the loaded cube has a mesh, the callback is not applied on the returned cube.How To Reproduce
Steps to reproduce the behaviour:
MeshCoord
) and pass the custom callback.MeshCoord
s have been converted toAuxCoord
s as expected, but no exception was raised.Expected behaviour
I expected an exception to have been raised by the custom callback function, since the returned cube has no mesh.
Reproducible example
Output:
Environment
The text was updated successfully, but these errors were encountered: