Skip to content

Commit

Permalink
Avoid large chunks in climate_statistics preprocessor function with…
Browse files Browse the repository at this point in the history
… `period='full'` (#2404)
  • Loading branch information
bouweandela authored May 6, 2024
1 parent 801ffba commit c14213d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion esmvalcore/preprocessor/_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,12 @@ def get_time_weights(cube: Cube) -> np.ndarray | da.core.Array:

# Extract 1D time weights (= lengths of time intervals)
time_weights = time.lazy_bounds()[:, 1] - time.lazy_bounds()[:, 0]
if not cube.has_lazy_data():
if cube.has_lazy_data():
# Align the weight chunks with the data chunks to avoid excessively
# large chunks as a result of broadcasting.
time_chunks = cube.lazy_data().chunks[coord_dims[0]]
time_weights = time_weights.rechunk(time_chunks)
else:
time_weights = time_weights.compute()
return time_weights

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/preprocessor/_time/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,10 +1777,11 @@ def test_get_time_weights():
def test_get_time_weights_lazy():
"""Test ``get_time_weights`` for complex cube with lazy data."""
cube = _make_cube()
cube.data = cube.lazy_data()
cube.data = cube.lazy_data().rechunk((1, 1, 1, 3))
weights = get_time_weights(cube)
assert isinstance(weights, da.Array)
assert weights.shape == (2, )
assert weights.chunks == ((1, 1), )
np.testing.assert_allclose(weights, [15.0, 30.0])


Expand Down

0 comments on commit c14213d

Please sign in to comment.