Skip to content

Commit 6b49cc4

Browse files
djkirkhampelson
authored andcommitted
Perserve dtype of source cube with area weighted regridder (#2203)
Perserve dtype of source cube with area weighted regridder & add what's new
1 parent a25d32e commit 6b49cc4

9 files changed

Lines changed: 21 additions & 15 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

lib/iris/experimental/regrid.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,17 @@ def _regrid_area_weighted_array(src_data, x_dim, y_dim,
473473
if y_dim is not None:
474474
new_shape[y_dim] = grid_y_bounds.shape[0]
475475

476+
# Use input cube dtype or convert values to the smallest possible float
477+
# dtype when necessary.
478+
dtype = np.promote_types(src_data.dtype, np.float16)
479+
476480
# Flag to indicate whether the original data was a masked array.
477481
src_masked = ma.isMaskedArray(src_data)
478482
if src_masked:
479-
new_data = ma.zeros(new_shape, fill_value=src_data.fill_value)
483+
new_data = ma.zeros(new_shape, fill_value=src_data.fill_value,
484+
dtype=dtype)
480485
else:
481-
new_data = ma.zeros(new_shape)
486+
new_data = ma.zeros(new_shape, dtype=dtype)
482487
# Assign to mask to explode it, allowing indexed assignment.
483488
new_data.mask = False
484489

lib/iris/tests/experimental/regrid/test_regrid_area_weighted_rectilinear_src_and_grid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def test_global_data_same_res(self):
490490
src.coord('latitude').guess_bounds()
491491
src.coord('longitude').guess_bounds()
492492
res = regrid_area_weighted(src, src)
493-
self.assertArrayShapeStats(res, (73, 96), 279.945167, 16.345842)
493+
self.assertArrayShapeStats(res, (73, 96), 279.945160, 16.345842)
494494

495495
@tests.skip_data
496496
def test_global_data_subset(self):
@@ -510,7 +510,7 @@ def test_global_data_subset(self):
510510
dest.add_dim_coord(dest_lon, 1)
511511

512512
res = regrid_area_weighted(src, dest)
513-
self.assertArrayShapeStats(res, (40, 30), 280.979336, 16.640421)
513+
self.assertArrayShapeStats(res, (40, 30), 280.979310, 16.640421)
514514

515515
@tests.skip_data
516516
def test_circular_subset(self):
@@ -530,7 +530,7 @@ def test_circular_subset(self):
530530
dest.add_dim_coord(dest_lon, 1)
531531

532532
res = regrid_area_weighted(src, dest)
533-
self.assertArrayShapeStats(res, (40, 7), 285.653967, 15.212710)
533+
self.assertArrayShapeStats(res, (40, 7), 285.653960, 15.212710)
534534

535535
@tests.skip_data
536536
def test_non_circular_subset(self):
@@ -551,7 +551,7 @@ def test_non_circular_subset(self):
551551
dest.add_dim_coord(dest_lon, 1)
552552

553553
res = regrid_area_weighted(src, dest)
554-
self.assertArrayShapeStats(res, (40, 7), 285.550800, 15.190245)
554+
self.assertArrayShapeStats(res, (40, 7), 285.550814, 15.190245)
555555

556556

557557
if __name__ == "__main__":

lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lat_cross_section.cml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" ?>
22
<cubes xmlns="urn:x-iris:cubeml-0.2">
3-
<cube dtype="float64" standard_name="air_potential_temperature" units="K">
3+
<cube dtype="float32" standard_name="air_potential_temperature" units="K">
44
<attributes>
55
<attribute name="source" value="Iris test case"/>
66
</attributes>
@@ -70,6 +70,6 @@
7070
</coord>
7171
</coords>
7272
<cellMethods/>
73-
<data dtype="float64" shape="(5, 18)" state="loaded"/>
73+
<data dtype="float32" shape="(5, 18)" state="loaded"/>
7474
</cube>
7575
</cubes>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"std": 0.049194271888948204, "min": 287.83400456072997, "max": 288.09036649547346, "shape": [5, 18], "masked": false, "mean": 288.00649837715025}
1+
{"std": 0.04919414967298508, "min": 287.8340148925781, "max": 288.0903625488281, "shape": [5, 18], "masked": false, "mean": 288.0065002441406}

lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/const_lon_cross_section.cml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" ?>
22
<cubes xmlns="urn:x-iris:cubeml-0.2">
3-
<cube dtype="float64" standard_name="air_potential_temperature" units="K">
3+
<cube dtype="float32" standard_name="air_potential_temperature" units="K">
44
<attributes>
55
<attribute name="source" value="Iris test case"/>
66
</attributes>
@@ -64,6 +64,6 @@
6464
</coord>
6565
</coords>
6666
<cellMethods/>
67-
<data dtype="float64" shape="(5, 12)" state="loaded"/>
67+
<data dtype="float32" shape="(5, 12)" state="loaded"/>
6868
</cube>
6969
</cubes>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"std": 0.053120598663116025, "min": 287.90810573168818, "max": 288.14679568802029, "shape": [5, 12], "masked": false, "mean": 288.02641852126851}
1+
{"std": 0.05311942473053932, "min": 287.9081115722656, "max": 288.14678955078125, "shape": [5, 12], "masked": false, "mean": 288.02642822265625}

lib/iris/tests/results/experimental/regrid/regrid_area_weighted_rectilinear_src_and_grid/hybridheight.cml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" ?>
22
<cubes xmlns="urn:x-iris:cubeml-0.2">
3-
<cube dtype="float64" standard_name="air_potential_temperature" units="K">
3+
<cube dtype="float32" standard_name="air_potential_temperature" units="K">
44
<attributes>
55
<attribute name="source" value="Iris test case"/>
66
</attributes>
@@ -433,6 +433,6 @@
433433
</coord>
434434
</coords>
435435
<cellMethods/>
436-
<data dtype="float64" shape="(2, 5, 16, 21)" state="loaded"/>
436+
<data dtype="float32" shape="(2, 5, 16, 21)" state="loaded"/>
437437
</cube>
438438
</cubes>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"std": 0.10611990310218537, "min": 287.59569757163996, "max": 288.16508077725081, "shape": [2, 5, 16, 21], "masked": false, "mean": 287.91804930402083}
1+
{"std": 0.10611984878778458, "min": 287.595703125, "max": 288.1650695800781, "shape": [2, 5, 16, 21], "masked": false, "mean": 287.91802978515625}

0 commit comments

Comments
 (0)