diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 88d82f99f7..d2f2d3bb49 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -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 diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index e7433bcf24..9e5752c74f 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -23,6 +23,7 @@ import iris.tests as tests import itertools +import warnings import biggus import mock @@ -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.