From 630578991559bded5c9963ffd9671904c92a2274 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Tue, 20 Jul 2021 17:21:42 +0100 Subject: [PATCH 1/3] add failing test --- lib/iris/coords.py | 4 +++- lib/iris/tests/unit/coords/test_Coord.py | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index b236d407da..8cf9603a7a 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2440,7 +2440,9 @@ def nearest_neighbour_index(self, point): if self.has_bounds(): # make bounds ranges complete+separate, so point is in at least one increasing = self.bounds[0, 1] > self.bounds[0, 0] - bounds = bounds.copy() + # identify data type that bounds and point can safely cast to + dtype = np.result_type(bounds, np.min_scalar_type(point)) + bounds = bounds.astype(dtype) # sort the bounds cells by their centre values sort_inds = np.argsort(np.mean(bounds, axis=1)) bounds = bounds[sort_inds] diff --git a/lib/iris/tests/unit/coords/test_Coord.py b/lib/iris/tests/unit/coords/test_Coord.py index 43170b6c4e..5f707f91db 100644 --- a/lib/iris/tests/unit/coords/test_Coord.py +++ b/lib/iris/tests/unit/coords/test_Coord.py @@ -74,6 +74,11 @@ def test_scalar(self): target = [0, 0, 0, 0, 0] self._test_nearest_neighbour_index(target) + def test_bounded_float_point(self): + coord = DimCoord(1, bounds=[0, 2]) + result = coord.nearest_neighbour_index(2.5) + self.assertEqual(result, 0) + class Test_nearest_neighbour_index__descending(tests.IrisTest): def setUp(self): From ca4f1b10edf1695348ba358df1a4885535197bcb Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Tue, 20 Jul 2021 17:29:28 +0100 Subject: [PATCH 2/3] pass test --- lib/iris/coords.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 8cf9603a7a..8bcba776fd 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2441,7 +2441,7 @@ def nearest_neighbour_index(self, point): # make bounds ranges complete+separate, so point is in at least one increasing = self.bounds[0, 1] > self.bounds[0, 0] # identify data type that bounds and point can safely cast to - dtype = np.result_type(bounds, np.min_scalar_type(point)) + dtype = np.result_type(bounds, point) bounds = bounds.astype(dtype) # sort the bounds cells by their centre values sort_inds = np.argsort(np.mean(bounds, axis=1)) From a5266dc2cd032ba13c59fa506924ceb5b98d5d87 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Tue, 20 Jul 2021 17:38:14 +0100 Subject: [PATCH 3/3] add whatsnew --- docs/src/whatsnew/dev.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/whatsnew/dev.rst b/docs/src/whatsnew/dev.rst index 9ed3cb23c2..0ef6c7f65e 100644 --- a/docs/src/whatsnew/dev.rst +++ b/docs/src/whatsnew/dev.rst @@ -54,6 +54,10 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ ensured that a :class:`matplotlib.axes.Axes`'s position is preserved when Iris replaces it with a :class:`cartopy.mpl.geoaxes.GeoAxes`, fixing :issue:`1157`. (:pull:`4273`) + +#. `@rcomer`_ fixed :meth:`~iris.coords.Coord.nearest_neighbour_index` for edge + cases where the requested point is float and the coordinate has integer + bounds, reported at :issue:`2969`. (:pull:`4245`) 💣 Incompatible Changes