Skip to content
Closed
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 lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,8 @@ def __getitem__(self, keys):
# We can turn a masked array into a normal array if it's full.
if isinstance(data, ma.core.MaskedArray):
if ma.count_masked(data) == 0:
msg = 'Casting full masked array to NumPy array.'
warnings.warn(msg)
data = data.filled()

# Make the new cube slice
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 @@ -23,6 +23,7 @@
import iris.tests as tests

import itertools
import warnings

import biggus
import mock
Expand Down Expand Up @@ -65,6 +66,21 @@ def test_matrix(self):
self.assertArrayEqual(cube.data, data)


class Test___getitem__(tests.IrisTest):
def test_full_masked_array_warning(self):
# Check that a warning is raised when a cube's data that is a
# full masked array is cast to a numpy array.
# This happens when such a cube is indexed.
data = np.ma.array(np.arange(12).reshape(3, 4))
cube = Cube(data)
self.assertEqual(type(cube.data), np.ma.MaskedArray)
with warnings.catch_warnings():
# Cause all warnings to raise Exceptions.
warnings.simplefilter('error')
with self.assertRaisesRegexp(UserWarning, 'Casting full masked'):
cube[:2]


class Test_extract(tests.IrisTest):
def test_scalar_cube_exists(self):
# Ensure that extract is able to extract a scalar cube.
Expand Down