Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion lib/iris/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def callback(cube, field, filename):
from iris._deprecation import IrisDeprecation, warn_deprecated
import iris.fileformats
import iris.io
from iris._lazy_data import co_realise_cubes


try:
Expand All @@ -127,7 +128,7 @@ def callback(cube, field, filename):
__all__ = ['load', 'load_cube', 'load_cubes', 'load_raw',
'save', 'Constraint', 'AttributeConstraint', 'sample_data_path',
'site_configuration', 'Future', 'FUTURE',
'IrisDeprecation']
'IrisDeprecation', 'co_realise_cubes']


Constraint = iris._constraints.Constraint
Expand Down
17 changes: 16 additions & 1 deletion lib/iris/_lazy_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2017, Met Office
# (C) British Crown Copyright 2017 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -158,3 +158,18 @@ def multidim_lazy_stack(stack):
result = da.stack([multidim_lazy_stack(subarray)
for subarray in stack])
return result


def co_realise_cubes(cubes):
"""
Fetch real data for multiple cubes at one time.

This fetches lazy content, equivalent to accessing each cube.data.
However, lazy calculations and data fetches can be shared between the
calculations, improving performance.

"""
results = da.compute(list(cube.core_data() for cube in cubes))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In as_concrete_data() there's an instance check for a MaskedConstant. This is needed because the dtype of a MaskedConstant is always float, so we create a scalar masked array with the correct dtype instead. You probably want to do the same here. In fact, it might be worth extending as_concrete_data() to work with sequences, and calling it here, to prevent duplication of code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good spot ! 💐
And a good suggestion. Will look into it...

for cube, result in zip(cubes, results):
cube.data = result
return cubes