Skip to content

Commit bca6e3a

Browse files
committed
Add restriction for haversine with non-degree units
1 parent ef250c4 commit bca6e3a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

climada/util/coordinates.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,8 @@ def _nearest_neighbor_approx(
13481348
# first check that unit is in degree
13491349
if unit != "degree":
13501350
raise ValueError(
1351-
"Only degree unit is supported for nearest neighbor approximation"
1351+
"Only degree unit is supported for nearest neighbor approximation."
1352+
"Please use euclidean distance for non-degree units."
13521353
)
13531354
# Compute only for the unique coordinates. Copy the results for the
13541355
# not unique coordinates
@@ -1413,6 +1414,12 @@ def _nearest_neighbor_haversine(centroids, coordinates, unit, threshold):
14131414
np.array
14141415
with as many rows as coordinates containing the centroids indexes
14151416
"""
1417+
# first check that unit is in degree
1418+
if unit != "degree":
1419+
raise ValueError(
1420+
"Only degree unit is supported for nearest neighbor approximation."
1421+
"Please use euclidean distance for non-degree units."
1422+
)
14161423
# Construct tree from centroids
14171424
tree = BallTree(centroids, metric="haversine")
14181425
# Select unique exposures coordinates
@@ -1433,8 +1440,7 @@ def _nearest_neighbor_haversine(centroids, coordinates, unit, threshold):
14331440

14341441
# Raise a warning if the minimum distance is greater than the
14351442
# threshold and set an unvalid index -1
1436-
if unit == "degree":
1437-
dist = dist * EARTH_RADIUS_KM
1443+
dist = dist * EARTH_RADIUS_KM
14381444
num_warn = np.sum(dist > threshold)
14391445
if num_warn:
14401446
LOGGER.warning(
@@ -1549,7 +1555,7 @@ def _nearest_neighbor_antimeridian(centroids, coordinates, threshold, assigned):
15491555
if np.any(cent_strip_bool):
15501556
cent_strip = centroids[cent_strip_bool]
15511557
strip_assigned = _nearest_neighbor_haversine(
1552-
cent_strip, coord_strip, "degree", threshold
1558+
np.deg2rad(cent_strip), np.deg2rad(coord_strip), "degree", threshold
15531559
)
15541560
new_coords = cent_strip_bool.nonzero()[0][strip_assigned]
15551561
new_coords[strip_assigned == -1] = -1

0 commit comments

Comments
 (0)