From 1984f1da698fc2442b8bc652c27fc8e0b212925e Mon Sep 17 00:00:00 2001 From: Chris Fairless Date: Wed, 13 Mar 2024 14:34:18 +0100 Subject: [PATCH 1/2] Bugfix coordinates.lon_bounds for edge case returning >360 range See issue https://github.com/CLIMADA-project/climada_python/issues/858 --- climada/util/coordinates.py | 1 + climada/util/test/test_coordinates.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/climada/util/coordinates.py b/climada/util/coordinates.py index 09a22f84f..bcbc78237 100644 --- a/climada/util/coordinates.py +++ b/climada/util/coordinates.py @@ -200,6 +200,7 @@ def lon_bounds(lon, buffer=0.0): if lon_diff_max < 2: # since the largest gap is comparably small, enforce the [-180, 180] value range gap_max = lon_diff.size - 1 + lon_diff_max = lon_uniq[gap_max + 1] - lon_uniq[gap_max] if lon_diff_max <= 2 * buffer: # avoid (-1, 359) and similar equivalent outputs for bounds covering the full circle lon_min, lon_max = (-180, 180) diff --git a/climada/util/test/test_coordinates.py b/climada/util/test/test_coordinates.py index 5903c2f1c..d8be2f2be 100644 --- a/climada/util/test/test_coordinates.py +++ b/climada/util/test/test_coordinates.py @@ -265,6 +265,12 @@ def test_latlon_bounds(self): bounds = u_coord.latlon_bounds(lat, lon, buffer=1) self.assertEqual(bounds, (-180, -90, 180, 90)) + # edge with values closer to the antimeridian than buffers and global coverage + lon = np.concatenate([[-179.99], np.arange(-179.7, 179.9, 0.2), [179.99]]) + lat = np.repeat(0, lon.size) + bounds = u_coord.latlon_bounds(lat, lon, buffer = 0.1) + self.assertEqual(bounds, (-180, -0.1, 180, 0.1)) + def test_toggle_extent_bounds(self): """Test the conversion between 'extent' and 'bounds'""" self.assertEqual(u_coord.toggle_extent_bounds((0, -1, 1, 3)), (0, 1, -1, 3)) From 424fba357b8ca8d57faa1ed9c2807fead4834cae Mon Sep 17 00:00:00 2001 From: Thomas Vogt Date: Thu, 14 Mar 2024 10:41:36 +0100 Subject: [PATCH 2/2] lon_bounds: refactor --- climada/util/coordinates.py | 2 +- climada/util/test/test_coordinates.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/climada/util/coordinates.py b/climada/util/coordinates.py index bcbc78237..741b40586 100644 --- a/climada/util/coordinates.py +++ b/climada/util/coordinates.py @@ -200,7 +200,7 @@ def lon_bounds(lon, buffer=0.0): if lon_diff_max < 2: # since the largest gap is comparably small, enforce the [-180, 180] value range gap_max = lon_diff.size - 1 - lon_diff_max = lon_uniq[gap_max + 1] - lon_uniq[gap_max] + lon_diff_max = lon_diff[gap_max] if lon_diff_max <= 2 * buffer: # avoid (-1, 359) and similar equivalent outputs for bounds covering the full circle lon_min, lon_max = (-180, 180) diff --git a/climada/util/test/test_coordinates.py b/climada/util/test/test_coordinates.py index d8be2f2be..15b8b9119 100644 --- a/climada/util/test/test_coordinates.py +++ b/climada/util/test/test_coordinates.py @@ -267,8 +267,8 @@ def test_latlon_bounds(self): # edge with values closer to the antimeridian than buffers and global coverage lon = np.concatenate([[-179.99], np.arange(-179.7, 179.9, 0.2), [179.99]]) - lat = np.repeat(0, lon.size) - bounds = u_coord.latlon_bounds(lat, lon, buffer = 0.1) + lat = np.zeros_like(lon) + bounds = u_coord.latlon_bounds(lat, lon, buffer=0.1) self.assertEqual(bounds, (-180, -0.1, 180, 0.1)) def test_toggle_extent_bounds(self):