From 6b49cc425f23e677303153eaeb79cc6bb2c54baf Mon Sep 17 00:00:00 2001 From: Daniel Kirkham Date: Wed, 17 Jan 2018 12:07:51 +0000 Subject: [PATCH] Perserve dtype of source cube with area weighted regridder (#2203) Perserve dtype of source cube with area weighted regridder & add what's new --- ...echange_2018-Jan-12_area_weighted_regridder_dtype.txt | 1 + lib/iris/experimental/regrid.py | 9 +++++++-- ...test_regrid_area_weighted_rectilinear_src_and_grid.py | 8 ++++---- .../const_lat_cross_section.cml | 4 ++-- .../const_lat_cross_section.data.0.json | 2 +- .../const_lon_cross_section.cml | 4 ++-- .../const_lon_cross_section.data.0.json | 2 +- .../hybridheight.cml | 4 ++-- .../hybridheight.data.0.json | 2 +- 9 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 docs/iris/src/whatsnew/contributions_2.0/incompatiblechange_2018-Jan-12_area_weighted_regridder_dtype.txt diff --git a/docs/iris/src/whatsnew/contributions_2.0/incompatiblechange_2018-Jan-12_area_weighted_regridder_dtype.txt b/docs/iris/src/whatsnew/contributions_2.0/incompatiblechange_2018-Jan-12_area_weighted_regridder_dtype.txt new file mode 100644 index 0000000000..c3bcd7f55b --- /dev/null +++ b/docs/iris/src/whatsnew/contributions_2.0/incompatiblechange_2018-Jan-12_area_weighted_regridder_dtype.txt @@ -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. \ No newline at end of file diff --git a/lib/iris/experimental/regrid.py b/lib/iris/experimental/regrid.py index 053456c02b..8127c66732 100644 --- a/lib/iris/experimental/regrid.py +++ b/lib/iris/experimental/regrid.py @@ -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 diff --git a/lib/iris/tests/experimental/regrid/test_regrid_area_weighted_rectilinear_src_and_grid.py b/lib/iris/tests/experimental/regrid/test_regrid_area_weighted_rectilinear_src_and_grid.py index 65dea3c5b8..2d171be5c3 100644 --- a/lib/iris/tests/experimental/regrid/test_regrid_area_weighted_rectilinear_src_and_grid.py +++ b/lib/iris/tests/experimental/regrid/test_regrid_area_weighted_rectilinear_src_and_grid.py @@ -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): @@ -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): @@ -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): @@ -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__": diff --git a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.cml b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.cml index 28425ef7d9..585657b642 100644 --- a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.cml +++ b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.cml @@ -1,6 +1,6 @@ - + @@ -70,6 +70,6 @@ - + diff --git a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.data.0.json b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.data.0.json index c99b0ef25d..0c2b277ef0 100644 --- a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.data.0.json +++ b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.data.0.json @@ -1 +1 @@ -{"std": 0.049194271888948204, "min": 287.83400456072997, "max": 288.09036649547346, "shape": [5, 18], "masked": false, "mean": 288.00649837715025} \ No newline at end of file +{"std": 0.04919414967298508, "min": 287.8340148925781, "max": 288.0903625488281, "shape": [5, 18], "masked": false, "mean": 288.0065002441406} \ No newline at end of file diff --git a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.cml b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.cml index f52aa165f5..4e928851fe 100644 --- a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.cml +++ b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.cml @@ -1,6 +1,6 @@ - + @@ -64,6 +64,6 @@ - + diff --git a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.data.0.json b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.data.0.json index 29d6aa3488..02772d9262 100644 --- a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.data.0.json +++ b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.data.0.json @@ -1 +1 @@ -{"std": 0.053120598663116025, "min": 287.90810573168818, "max": 288.14679568802029, "shape": [5, 12], "masked": false, "mean": 288.02641852126851} \ No newline at end of file +{"std": 0.05311942473053932, "min": 287.9081115722656, "max": 288.14678955078125, "shape": [5, 12], "masked": false, "mean": 288.02642822265625} \ No newline at end of file diff --git a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.cml b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.cml index 4b1894d492..05cc3cab8c 100644 --- a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.cml +++ b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.cml @@ -1,6 +1,6 @@ - + @@ -433,6 +433,6 @@ - + diff --git a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.data.0.json b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.data.0.json index 732977b903..bdc0cad306 100644 --- a/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.data.0.json +++ b/lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.data.0.json @@ -1 +1 @@ -{"std": 0.10611990310218537, "min": 287.59569757163996, "max": 288.16508077725081, "shape": [2, 5, 16, 21], "masked": false, "mean": 287.91804930402083} \ No newline at end of file +{"std": 0.10611984878778458, "min": 287.595703125, "max": 288.1650695800781, "shape": [2, 5, 16, 21], "masked": false, "mean": 287.91802978515625} \ No newline at end of file