Skip to content

Fix to_iris conversion issues #2111

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

Merged
merged 5 commits into from
May 14, 2018
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian>`_.
- ``plot.line()`` learned new kwargs: ``xincrease``, ``yincrease`` that change the direction of the respective axes.
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fixed ``to_iris`` to maintain lazy dask array after conversion (:issue:`2046`).
By `Alex Hilson <https://github.com/AlexHilson>`_ and `Stephan Hoyer <https://github.com/shoyer>`_.

.. _whats-new.0.10.3:

Expand Down
10 changes: 2 additions & 8 deletions xarray/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .coding.times import CFDatetimeCoder, CFTimedeltaCoder
from .conventions import decode_cf
from .core import duck_array_ops
from .core.dataarray import DataArray
from .core.dtypes import get_fill_value
from .core.pycompat import OrderedDict, range
Expand Down Expand Up @@ -94,7 +95,6 @@ def to_iris(dataarray):
# Iris not a hard dependency
import iris
from iris.fileformats.netcdf import parse_cell_methods
from xarray.core.pycompat import dask_array_type

dim_coords = []
aux_coords = []
Expand All @@ -121,13 +121,7 @@ def to_iris(dataarray):
args['cell_methods'] = \
parse_cell_methods(dataarray.attrs['cell_methods'])

# Create the right type of masked array (should be easier after #1769)
if isinstance(dataarray.data, dask_array_type):
from dask.array import ma as dask_ma
masked_data = dask_ma.masked_invalid(dataarray)
else:
masked_data = np.ma.masked_invalid(dataarray)

masked_data = duck_array_ops.masked_invalid(dataarray.data)
cube = iris.cube.Cube(masked_data, **args)

return cube
Expand Down
4 changes: 4 additions & 0 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def isnull(data):
einsum = _dask_or_eager_func('einsum', array_args=slice(1, None),
requires_dask='0.17.3')

masked_invalid = _dask_or_eager_func(
'masked_invalid', eager_module=np.ma,
dask_module=getattr(dask_array, 'ma', None))


def asarray(data):
return data if isinstance(data, dask_array_type) else np.asarray(data)
Expand Down
9 changes: 4 additions & 5 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3025,12 +3025,11 @@ def test_to_and_from_iris_dask(self):
roundtripped = DataArray.from_iris(actual)
assert_identical(original, roundtripped)

# If the Iris version supports it then we should get a dask array back
# If the Iris version supports it then we should have a dask array
# at each stage of the conversion
if hasattr(actual, 'core_data'):
pass
# TODO This currently fails due to the decoding loading
# the data (#1372)
# self.assertEqual(type(original.data), type(roundtripped.data))
self.assertEqual(type(original.data), type(actual.core_data()))
self.assertEqual(type(original.data), type(roundtripped.data))

actual.remove_coord('time')
auto_time_dimension = DataArray.from_iris(actual)
Expand Down