Skip to content

Commit

Permalink
Merge pull request #2524 from bjlittle/dask-assign-fill-value-thru-cu…
Browse files Browse the repository at this point in the history
…be-data

Populate cube.fill_value thru cube.data setter.
  • Loading branch information
lbdreyer authored May 4, 2017
2 parents 2332870 + 2c7d0b5 commit fe99864
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 41 deletions.
8 changes: 6 additions & 2 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float64" dtype="float64" long_name="temperature" units="kelvin">
<cube core-dtype="float64" dtype="float64" fill_value="999999.0" long_name="temperature" units="kelvin">
<coords>
<coord datadims="[0]">
<auxCoord id="ecc4ad5a" long_name="height" points="[1, 1, 2, 2, 3, 4, 4, 1, 5]" shape="(9,)" units="Unit('m')" value_type="int32"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float64" dtype="float64" long_name="temperature" units="kelvin">
<cube core-dtype="float64" dtype="float64" fill_value="999999.0" long_name="temperature" units="kelvin">
<coords>
<coord datadims="[0]">
<auxCoord id="ecc4ad5a" long_name="height" points="[1, 2, 3, 4, 5, 6, 7, 8]" shape="(8,)" units="Unit('m')" value_type="int32"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float64" dtype="float64" long_name="thingness" units="1">
<cube core-dtype="float64" dtype="float64" fill_value="999999.0" long_name="thingness" units="1">
<coords>
<coord datadims="[0]">
<dimCoord id="4a0cb9d8" points="[90, 0, -90]" shape="(3,)" standard_name="latitude" units="Unit('degrees')" value_type="int64"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float64" dtype="float64" long_name="thingness" units="1">
<cube core-dtype="float64" dtype="float64" fill_value="999999.0" long_name="thingness" units="1">
<coords>
<coord>
<dimCoord bounds="[[0, 15]]" id="434cbbd8" long_name="bar" points="[7.5]" shape="(1,)" units="Unit('1')" value_type="float64"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float32" dtype="float32" standard_name="air_potential_temperature" units="K">
<cube core-dtype="float32" dtype="float32" fill_value="1e+20" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float32" dtype="float32" standard_name="air_potential_temperature" units="K">
<cube core-dtype="float32" dtype="float32" fill_value="1e+20" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float32" dtype="float32" standard_name="air_potential_temperature" units="K">
<cube core-dtype="float32" dtype="float32" fill_value="1e+20" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float32" dtype="float32" standard_name="air_potential_temperature" units="K">
<cube core-dtype="float32" dtype="float32" fill_value="1e+20" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube core-dtype="float64" dtype="float64" standard_name="air_temperature" units="K">
<cube core-dtype="float64" dtype="float64" fill_value="999999.0" standard_name="air_temperature" units="K">
<coords>
<coord datadims="[1, 2]">
<auxCoord id="9041e969" points="[[-660.0, -610.0, -560.0, -510.0, -460.0, -410.0],
Expand Down
56 changes: 28 additions & 28 deletions lib/iris/tests/results/netcdf/netcdf_cell_methods.cml

Large diffs are not rendered by default.

26 changes: 24 additions & 2 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fe99864

Please sign in to comment.