From 48f1f4ef0882e0b1a353430ee8aeb264a69d8759 Mon Sep 17 00:00:00 2001 From: stephenworsley <49274989+stephenworsley@users.noreply.github.com> Date: Mon, 29 Jul 2024 15:20:57 +0100 Subject: [PATCH] Load performance improvement (ignoring UGRID) (#6088) * proof of concept load performance improvement * remove print line * adjust for merge * adjust to tolerant load behaviour, make methods private * fix tests * simplify changes * Update lib/iris/fileformats/cf.py Co-authored-by: Bill Little * address review comment * add whatsnew --------- Co-authored-by: Bill Little --- docs/src/whatsnew/latest.rst | 4 ++++ lib/iris/fileformats/cf.py | 24 +++++++++++++++++++++++ lib/iris/fileformats/netcdf/ugrid_load.py | 12 +++++++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index bbef181057..8b7fbf5864 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -98,6 +98,10 @@ This document explains the changes made to Iris for this release subclasses of a common generic :class:`~iris.mesh.components.Mesh` class. (:issue:`6057`, :pull:`6061`, :pull:`6077`) +#. `@pp-mo`_ and `@stephenworsley`_ Turned on UGRID loading by default, effectively removing + the need for and deprecating the :func:`~iris.ugrid.experimental.PARSE_UGRID_ON_LOAD` + context manager. (:pull:`6054`, :pull:`6088`) + 🚀 Performance Enhancements =========================== diff --git a/lib/iris/fileformats/cf.py b/lib/iris/fileformats/cf.py index 556642003a..024bcb6f1d 100644 --- a/lib/iris/fileformats/cf.py +++ b/lib/iris/fileformats/cf.py @@ -1331,6 +1331,11 @@ def __init__(self, file_source, warn=False, monotonic=False): self._check_monotonic = monotonic + self._with_ugrid = True + if not self._has_meshes(): + self._trim_ugrid_variable_types() + self._with_ugrid = False + self._translate() self._build_cf_groups() self._reset() @@ -1348,6 +1353,25 @@ def __exit__(self, exc_type, exc_value, traceback): # When used as a context-manager, **always** close the file on exit. self._close() + def _has_meshes(self): + result = False + for variable in self._dataset.variables.values(): + if hasattr(variable, "mesh") or hasattr(variable, "node_coordinates"): + result = True + break + return result + + def _trim_ugrid_variable_types(self): + self._variable_types = ( + CFAncillaryDataVariable, + CFAuxiliaryCoordinateVariable, + CFBoundaryVariable, + CFClimatologyVariable, + CFGridMappingVariable, + CFLabelVariable, + CFMeasureVariable, + ) + @property def filename(self): """The file that the CFReader is reading.""" diff --git a/lib/iris/fileformats/netcdf/ugrid_load.py b/lib/iris/fileformats/netcdf/ugrid_load.py index 210e112629..0a70567f16 100644 --- a/lib/iris/fileformats/netcdf/ugrid_load.py +++ b/lib/iris/fileformats/netcdf/ugrid_load.py @@ -56,11 +56,13 @@ def _meshes_from_cf(cf_reader): # Mesh instances are shared between file phenomena. # TODO: more sophisticated Mesh sharing between files. # TODO: access external Mesh cache? - mesh_vars = cf_reader.cf_group.meshes - meshes = { - name: _build_mesh(cf_reader, var, cf_reader.filename) - for name, var in mesh_vars.items() - } + meshes = {} + if cf_reader._with_ugrid: + mesh_vars = cf_reader.cf_group.meshes + meshes = { + name: _build_mesh(cf_reader, var, cf_reader.filename) + for name, var in mesh_vars.items() + } return meshes