diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index a420494157..7411a517fd 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -50,7 +50,7 @@ This document explains the changes made to Iris for this release #. `@bjlittle`_ and `@lbdreyer`_ (reviewer) fixed the building of the CF Standard Names module ``iris.std_names`` for the ``setup.py`` commands ``develop`` and ``std_names``. (:issue:`4951`, :pull:`4952`) - + #. `@lbdreyer`_ and `@pp-mo`_ (reviewer) fixed the cube print out such that scalar ancillary variables are displayed in a dedicated section rather than being added to the vector ancillary variables section. Further, ancillary @@ -71,6 +71,10 @@ This document explains the changes made to Iris for this release :obj:`~iris.analysis.SUM`, :obj:`~iris.analysis.COUNT` and :obj:`~iris.analysis.PROPORTION` on real data. (:pull:`4905`) +#. `@bouweandela`_ made :meth:`iris.coords.Coord.cells` faster for time + coordinates. This also affects :meth:`iris.cube.Cube.extract`, + :meth:`iris.cube.Cube.subset`, and :meth:`iris.coords.Coord.intersect`. + (:pull:`4969`) 🔥 Deprecations =============== diff --git a/lib/iris/coords.py b/lib/iris/coords.py index d0d471a634..ba0ed8ee5d 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -1895,7 +1895,22 @@ def cells(self): ... """ - return _CellIterator(self) + if self.ndim != 1: + raise iris.exceptions.CoordinateMultiDimError(self) + + points = self.points + bounds = self.bounds + if self.units.is_time_reference(): + points = self.units.num2date(points) + if self.has_bounds(): + bounds = self.units.num2date(bounds) + + if self.has_bounds(): + for point, bound in zip(points, bounds): + yield Cell(point, bound) + else: + for point in points: + yield Cell(point) def _sanity_check_bounds(self): if self.ndim == 1: @@ -3137,22 +3152,6 @@ def xml_element(self, doc): return cellMethod_xml_element -# See Coord.cells() for the description/context. -class _CellIterator(Iterator): - def __init__(self, coord): - self._coord = coord - if coord.ndim != 1: - raise iris.exceptions.CoordinateMultiDimError(coord) - self._indices = iter(range(coord.shape[0])) - - def __next__(self): - # NB. When self._indices runs out it will raise StopIteration for us. - i = next(self._indices) - return self._coord.cell(i) - - next = __next__ - - # See ExplicitCoord._group() for the description/context. class _GroupIterator(Iterator): def __init__(self, points):