Skip to content
Open
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
4 changes: 3 additions & 1 deletion docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ This document explains the changes made to Iris for this release
🐛 Bugs Fixed
=============

#. N/A
#. :user:`gaoflow` fixed :meth:`iris.cube.Cube.rolling_window` so that it no
longer raises a ``TypeError`` when the cube has a lazy auxiliary coordinate
along the windowed dimension. (:issue:`6480`)


💣 Incompatible Changes
Expand Down
7 changes: 5 additions & 2 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -5243,12 +5243,15 @@ def rolling_window(
# window and the bounds are the first and last points in the
# window as with numeric coordinates.
new_points = np.apply_along_axis(lambda x: "|".join(x), -1, new_bounds)
new_bounds = new_bounds[:, (0, -1)]
# Index with a list rather than a tuple: a tuple is interpreted
# as a multidimensional index by Dask, so lazy coordinates would
# otherwise fail here (see #6480).
new_bounds = new_bounds[:, [0, -1]]
else:
# Take the first and last element of the rolled window (i.e.
# the bounds) and the new points are the midpoints of these
# bounds.
new_bounds = new_bounds[:, (0, -1)]
new_bounds = new_bounds[:, [0, -1]]
new_points = np.mean(new_bounds, axis=-1)

# wipe the coords points and set the bounds
Expand Down
16 changes: 16 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,22 @@ def test_lazy(self):
)
_shared_utils.assert_masked_array_equal(expected_result, res_cube.data)

def test_lazy_aux_coord(self):
# A lazy aux-coord paralleling the rolled dimension must not cause a
# failure, and should give the same result as a real one, staying lazy
# (see #6480).
self.cube.add_aux_coord(AuxCoord(da.arange(6), long_name="lazy_extra"), 0)
res_cube = self.cube.rolling_window("val", iris.analysis.MEAN, 3)
result_coord = res_cube.coord("lazy_extra")
assert result_coord.has_lazy_points()
assert result_coord.has_lazy_bounds()
expected = AuxCoord(
np.array([1.0, 2.0, 3.0, 4.0]),
bounds=np.array([[0, 2], [1, 3], [2, 4], [3, 5]]),
long_name="lazy_extra",
)
assert result_coord == expected

def test_ancillary_variables_and_cell_measures_kept(self, dataless):
if dataless:
self.cube.data = None
Expand Down
Loading