Skip to content

Commit

Permalink
Perserve dtype of source cube with area weighted regridder (#2203)
Browse files Browse the repository at this point in the history
Perserve dtype of source cube with area weighted regridder & add what's new
  • Loading branch information
djkirkham authored and pelson committed Jan 17, 2018
1 parent a25d32e commit 6b49cc4
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* A cube resulting from a regrid operation using the `iris.analysis.AreaWeighted` regridding scheme will now have the smallest floating point data type to which the source cube's data type can be safely converted using NumPy's type promotion rules.
9 changes: 7 additions & 2 deletions lib/iris/experimental/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,17 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
if y_dim is not None:
new_shape[y_dim] = grid_y_bounds.shape[0]

# Use input cube dtype or convert values to the smallest possible float
# dtype when necessary.
dtype = np.promote_types(src_data.dtype, np.float16)

# Flag to indicate whether the original data was a masked array.
src_masked = ma.isMaskedArray(src_data)
if src_masked:
new_data = ma.zeros(new_shape, fill_value=src_data.fill_value)
new_data = ma.zeros(new_shape, fill_value=src_data.fill_value,
dtype=dtype)
else:
new_data = ma.zeros(new_shape)
new_data = ma.zeros(new_shape, dtype=dtype)
# Assign to mask to explode it, allowing indexed assignment.
new_data.mask = False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def test_global_data_same_res(self):
src.coord('latitude').guess_bounds()
src.coord('longitude').guess_bounds()
res = regrid_area_weighted(src, src)
self.assertArrayShapeStats(res, (73, 96), 279.945167, 16.345842)
self.assertArrayShapeStats(res, (73, 96), 279.945160, 16.345842)

@tests.skip_data
def test_global_data_subset(self):
Expand All @@ -510,7 +510,7 @@ def test_global_data_subset(self):
dest.add_dim_coord(dest_lon, 1)

res = regrid_area_weighted(src, dest)
self.assertArrayShapeStats(res, (40, 30), 280.979336, 16.640421)
self.assertArrayShapeStats(res, (40, 30), 280.979310, 16.640421)

@tests.skip_data
def test_circular_subset(self):
Expand All @@ -530,7 +530,7 @@ def test_circular_subset(self):
dest.add_dim_coord(dest_lon, 1)

res = regrid_area_weighted(src, dest)
self.assertArrayShapeStats(res, (40, 7), 285.653967, 15.212710)
self.assertArrayShapeStats(res, (40, 7), 285.653960, 15.212710)

@tests.skip_data
def test_non_circular_subset(self):
Expand All @@ -551,7 +551,7 @@ def test_non_circular_subset(self):
dest.add_dim_coord(dest_lon, 1)

res = regrid_area_weighted(src, dest)
self.assertArrayShapeStats(res, (40, 7), 285.550800, 15.190245)
self.assertArrayShapeStats(res, (40, 7), 285.550814, 15.190245)


if __name__ == "__main__":
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 dtype="float64" standard_name="air_potential_temperature" units="K">
<cube dtype="float32" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down Expand Up @@ -70,6 +70,6 @@
</coord>
</coords>
<cellMethods/>
<data dtype="float64" shape="(5, 18)" state="loaded"/>
<data dtype="float32" shape="(5, 18)" state="loaded"/>
</cube>
</cubes>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"std": 0.049194271888948204, "min": 287.83400456072997, "max": 288.09036649547346, "shape": [5, 18], "masked": false, "mean": 288.00649837715025}
{"std": 0.04919414967298508, "min": 287.8340148925781, "max": 288.0903625488281, "shape": [5, 18], "masked": false, "mean": 288.0065002441406}
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 dtype="float64" standard_name="air_potential_temperature" units="K">
<cube dtype="float32" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down Expand Up @@ -64,6 +64,6 @@
</coord>
</coords>
<cellMethods/>
<data dtype="float64" shape="(5, 12)" state="loaded"/>
<data dtype="float32" shape="(5, 12)" state="loaded"/>
</cube>
</cubes>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"std": 0.053120598663116025, "min": 287.90810573168818, "max": 288.14679568802029, "shape": [5, 12], "masked": false, "mean": 288.02641852126851}
{"std": 0.05311942473053932, "min": 287.9081115722656, "max": 288.14678955078125, "shape": [5, 12], "masked": false, "mean": 288.02642822265625}
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 dtype="float64" standard_name="air_potential_temperature" units="K">
<cube dtype="float32" standard_name="air_potential_temperature" units="K">
<attributes>
<attribute name="source" value="Iris test case"/>
</attributes>
Expand Down Expand Up @@ -433,6 +433,6 @@
</coord>
</coords>
<cellMethods/>
<data dtype="float64" shape="(2, 5, 16, 21)" state="loaded"/>
<data dtype="float32" shape="(2, 5, 16, 21)" state="loaded"/>
</cube>
</cubes>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"std": 0.10611990310218537, "min": 287.59569757163996, "max": 288.16508077725081, "shape": [2, 5, 16, 21], "masked": false, "mean": 287.91804930402083}
{"std": 0.10611984878778458, "min": 287.595703125, "max": 288.1650695800781, "shape": [2, 5, 16, 21], "masked": false, "mean": 287.91802978515625}

0 comments on commit 6b49cc4

Please sign in to comment.