Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up operations that use the Coord.cells method for time coordinates #4969

Merged
merged 4 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
===============
Expand Down
33 changes: 16 additions & 17 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down