diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 0ca56c33f0..1411698cb9 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -1704,8 +1704,12 @@ def data(self): def data(self, data): # Assign the new data to the data manager. self._data_manager.data = data - # Clear the cube fill-value. - self._fill_value = None + if ma.isMaskedArray(data): + # Set the cube fill-value. + self.fill_value = data.fill_value + else: + # Clear the cube fill-value. + self.fill_value = None def has_lazy_data(self): return self._data_manager.has_lazy_data() diff --git a/lib/iris/tests/results/analysis/aggregated_by/multi_missing.cml b/lib/iris/tests/results/analysis/aggregated_by/multi_missing.cml index ebde7b1425..e3272e9ce2 100644 --- a/lib/iris/tests/results/analysis/aggregated_by/multi_missing.cml +++ b/lib/iris/tests/results/analysis/aggregated_by/multi_missing.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/aggregated_by/single_missing.cml b/lib/iris/tests/results/analysis/aggregated_by/single_missing.cml index d06328fd6b..02451d07a3 100644 --- a/lib/iris/tests/results/analysis/aggregated_by/single_missing.cml +++ b/lib/iris/tests/results/analysis/aggregated_by/single_missing.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/last_quartile_foo_3d_masked.cml b/lib/iris/tests/results/analysis/last_quartile_foo_3d_masked.cml index 9eaf7d59b7..ff8cb0f862 100644 --- a/lib/iris/tests/results/analysis/last_quartile_foo_3d_masked.cml +++ b/lib/iris/tests/results/analysis/last_quartile_foo_3d_masked.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/proportion_foo_2d_masked.cml b/lib/iris/tests/results/analysis/proportion_foo_2d_masked.cml index daf9e9cd9b..e800afcd56 100644 --- a/lib/iris/tests/results/analysis/proportion_foo_2d_masked.cml +++ b/lib/iris/tests/results/analysis/proportion_foo_2d_masked.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/regrid/linear_subset_masked_1.cml b/lib/iris/tests/results/analysis/regrid/linear_subset_masked_1.cml index cca909d638..2d89929d21 100644 --- a/lib/iris/tests/results/analysis/regrid/linear_subset_masked_1.cml +++ b/lib/iris/tests/results/analysis/regrid/linear_subset_masked_1.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/regrid/linear_subset_masked_2.cml b/lib/iris/tests/results/analysis/regrid/linear_subset_masked_2.cml index cca909d638..2d89929d21 100644 --- a/lib/iris/tests/results/analysis/regrid/linear_subset_masked_2.cml +++ b/lib/iris/tests/results/analysis/regrid/linear_subset_masked_2.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_1.cml b/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_1.cml index d2428e37dd..e73cdbcb9f 100644 --- a/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_1.cml +++ b/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_1.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_2.cml b/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_2.cml index d2428e37dd..e73cdbcb9f 100644 --- a/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_2.cml +++ b/lib/iris/tests/results/analysis/regrid/nearest_subset_masked_2.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/experimental/analysis/interpolate/LinearInterpolator/orthogonal_cube_with_factory.cml b/lib/iris/tests/results/experimental/analysis/interpolate/LinearInterpolator/orthogonal_cube_with_factory.cml index 3657ad1943..89bed81bb6 100644 --- a/lib/iris/tests/results/experimental/analysis/interpolate/LinearInterpolator/orthogonal_cube_with_factory.cml +++ b/lib/iris/tests/results/experimental/analysis/interpolate/LinearInterpolator/orthogonal_cube_with_factory.cml @@ -1,6 +1,6 @@ - + - + @@ -20,7 +20,7 @@ - + @@ -41,7 +41,7 @@ - + @@ -66,7 +66,7 @@ - + @@ -89,7 +89,7 @@ - + @@ -112,7 +112,7 @@ - + @@ -131,7 +131,7 @@ - + @@ -150,7 +150,7 @@ - + @@ -170,7 +170,7 @@ - + @@ -190,7 +190,7 @@ - + @@ -213,7 +213,7 @@ - + @@ -232,7 +232,7 @@ - + @@ -252,7 +252,7 @@ - + @@ -272,7 +272,7 @@ - + @@ -295,7 +295,7 @@ - + @@ -320,7 +320,7 @@ - + @@ -333,7 +333,7 @@ - + @@ -346,7 +346,7 @@ - + @@ -359,7 +359,7 @@ - + @@ -372,7 +372,7 @@ - + @@ -385,7 +385,7 @@ - + @@ -404,7 +404,7 @@ - + @@ -424,7 +424,7 @@ - + @@ -447,7 +447,7 @@ - + @@ -460,7 +460,7 @@ - + @@ -473,7 +473,7 @@ - + @@ -486,7 +486,7 @@ - + @@ -499,7 +499,7 @@ - + diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index cce1504b05..d86bdef720 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -1323,12 +1323,19 @@ def test_cube_setter(self): cube.fill_value = fill_value self.assertEqual(cube.fill_value, fill_value) - def test_cube_data_setter_with_none(self): + def test_cube_data_setter_with_ndarray(self): fill_value = 123 cube = Cube(np.array(0), fill_value=fill_value) self.assertEqual(cube.fill_value, fill_value) cube.data = np.array(1) - self.assertEqual(cube.fill_value, None) + self.assertIsNone(cube.fill_value) + + def test_cube_data_setter_with_masked_array(self): + fill_value = 123 + cube = Cube(np.array(0)) + self.assertIsNone(cube.fill_value) + cube.data = ma.masked_array(1, fill_value=fill_value) + self.assertEqual(cube.fill_value, fill_value) def test_fill_value_bool(self): fill_value = np.bool_(True) @@ -1676,6 +1683,21 @@ def test_lazydata___getitem__(self): self.assertEqual(subcube.dtype, dtype) self.assertArrayAllClose(subcube.fill_value, fill_value) + def test_data_getter__masked_with_fill_value(self): + fill_value = 1234 + cube = self._sample_cube(masked=True, + cube_fill_value=fill_value) + self.assertEqual(cube.fill_value, fill_value) + data = cube.data + self.assertEqual(data.fill_value, fill_value) + + def test_data_getter__masked_with_fill_value_default(self): + cube = self._sample_cube(masked=True) + self.assertIsNone(cube.fill_value) + data = cube.data + np_fill_value = ma.masked_array(0, dtype=cube.dtype).fill_value + self.assertEqual(data.fill_value, np_fill_value) + class TestSubset(tests.IrisTest): def test_scalar_coordinate(self):