Skip to content

Commit

Permalink
Move all iris.mesh.save into iris.fileformats.netcdf.save
Browse files Browse the repository at this point in the history
Fix experimental.ugrid import of save_mesh
  • Loading branch information
pp-mo committed Jul 24, 2024
1 parent cb063e4 commit e4e2310
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 57 deletions.
2 changes: 1 addition & 1 deletion lib/iris/experimental/ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import threading

from .._deprecation import warn_deprecated
from ..mesh import save_mesh
from ..mesh.load import load_mesh, load_meshes
from ..mesh.mesh import Connectivity as _Connectivity
from ..mesh.mesh import Mesh as _Mesh
from ..mesh.mesh import MeshCoord as _MeshCoord
from ..mesh.save import save_mesh
from ..mesh.utils import recombine_submeshes


Expand Down
39 changes: 39 additions & 0 deletions lib/iris/fileformats/netcdf/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2796,3 +2796,42 @@ def is_valid_packspec(p):
result = sman.delayed_completion()

return result


def save_mesh(mesh, filename, netcdf_format="NETCDF4"):
"""Save mesh(es) to a netCDF file.
Parameters
----------
mesh : :class:`iris.mesh.MeshXY` or iterable
Mesh(es) to save.
filename : str
Name of the netCDF file to create.
netcdf_format : str, default="NETCDF4"
Underlying netCDF file format, one of 'NETCDF4', 'NETCDF4_CLASSIC',
'NETCDF3_CLASSIC' or 'NETCDF3_64BIT'. Default is 'NETCDF4' format.
"""
# TODO: integrate with standard saving API when no longer 'experimental'.

if isinstance(mesh, typing.Iterable):
meshes = mesh
else:
meshes = [mesh]

# Initialise Manager for saving
with Saver(filename, netcdf_format) as sman:
# Iterate through the list.
for mesh in meshes:
# Get suitable dimension names.
mesh_dimensions, _ = sman._get_dim_names(mesh)

# Create dimensions.
sman._create_cf_dimensions(cube=None, dimension_names=mesh_dimensions)

# Create the mesh components.
sman._add_mesh(mesh)

# Add a conventions attribute.
# TODO: add 'UGRID' to conventions, when this is agreed with CF ?
sman.update_global_attributes(Conventions=CF_CONVENTIONS_VERSION)
3 changes: 2 additions & 1 deletion lib/iris/mesh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
Based on CF UGRID Conventions (v1.0), https://ugrid-conventions.github.io/ugrid-conventions/.
"""

from iris.fileformats.netcdf.saver import save_mesh

from ..config import get_logger
from .load import load_mesh, load_meshes
from .mesh import Connectivity, MeshCoord, MeshXY
from .save import save_mesh
from .utils import recombine_submeshes

__all__ = [
Expand Down
53 changes: 0 additions & 53 deletions lib/iris/mesh/save.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from iris.coords import AuxCoord
from iris.cube import Cube, CubeList
from iris.fileformats.netcdf import _thread_safe_nc
from iris.mesh.mesh import Connectivity, MeshXY
from iris.mesh.save import save_mesh
from iris.mesh import Connectivity, MeshXY, save_mesh
from iris.tests.stock import realistic_4d

XY_LOCS = ("x", "y")
Expand Down

0 comments on commit e4e2310

Please sign in to comment.