Skip to content

Commit

Permalink
Merge pull request #614 from djhoese/bugfix-np210-compat
Browse files Browse the repository at this point in the history
Fix geocentric_resolution compatibility with numpy 2.1.0
  • Loading branch information
mraspaud authored Aug 24, 2024
2 parents 6ba78d2 + 0a47fd3 commit af0d4cc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
- [ ] Closes #xxxx <!-- remove if there is no corresponding issue, which should only be the case for minor changes -->
- [ ] Tests added <!-- for all bug fixes or enhancements -->
- [ ] Tests passed <!-- for all non-documentation changes -->
- [ ] Passes ``git diff origin/main **/*py | flake8 --diff`` <!-- remove if you did not edit any Python files -->
- [ ] Fully documented <!-- remove if this change should not be visible to users, e.g., if it is an internal clean-up, or if this is part of a larger project that will be documented later -->
13 changes: 11 additions & 2 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,15 @@ def geocentric_resolution(self, ellps='WGS84', radius=None):
edge averages.
"""
def _safe_bin_edges(arr):
try:
return np.histogram_bin_edges(arr, bins=10)[:2]
except ValueError:
# numpy 2.1.0+ produces a ValueError if it can't fill
# all bins due to a small data range
# we just arbitrarily use the first 2 elements as all elements
# should be within floating point precision for our use case
return arr[:2]
from pyproj.transformer import Transformer
rows, cols = self.shape
mid_row = rows // 2
Expand Down Expand Up @@ -2726,9 +2735,9 @@ def geocentric_resolution(self, ellps='WGS84', radius=None):
# Very useful near edge of disk geostationary areas.
hor_res = vert_res = 0
if hor_dist.size:
hor_res = np.mean(np.histogram_bin_edges(hor_dist)[:2])
hor_res = np.mean(_safe_bin_edges(hor_dist))
if vert_dist.size:
vert_res = np.mean(np.histogram_bin_edges(vert_dist)[:2])
vert_res = np.mean(_safe_bin_edges(vert_dist))
# Use the maximum distance between the two midlines instead of
# binning both of them together. If we binned them together then
# we are highly dependent on the shape of the area (more rows in
Expand Down
15 changes: 15 additions & 0 deletions pyresample/test/test_geometry/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,21 @@ def test_area_def_geocentric_resolution(self, create_test_area):
geo_res = area_def.geocentric_resolution()
np.testing.assert_allclose(298.647232, geo_res, atol=1e-1)

def test_area_def_geocentric_resolution_close_dist(self, create_test_area):
"""Test geocentric resolution when distance range isn't big enough for histogram bins.
The method currently uses `np.histogram_bin_edges`. Starting in numpy
2.1.0, if the number of bins requested (10 in this default case) can't
be created because the range of the data is too small, it will raise an
exception. This test makes sure that geocentric_resolution doesn't
error out when this case is encountered.
"""
# this area is known to produce horizontal distances of ~999.9999989758
# and trigger the error in numpy 2.1.0
ar = create_test_area(4087, 5, 5, (-2500.0, -2500.0, 2500.0, 2500.0))
np.testing.assert_allclose(ar.geocentric_resolution(), 999.999999, atol=1e-2)

def test_area_def_geocentric_resolution_latlong(self, create_test_area):
"""Test the AreaDefinition.geocentric_resolution method on a latlong projection."""
area_extent = (-110.0, 45.0, -95.0, 55.0)
Expand Down

0 comments on commit af0d4cc

Please sign in to comment.