Skip to content

Commit f3300b0

Browse files
author
Chahan Kropf
committed
Separate check geo coordinates into two methods
1 parent a22a047 commit f3300b0

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

climada/util/coordinates.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
are not considered."""
8787

8888

89-
def check_if_geo_coords(lat, lon, raise_error=True):
90-
"""Check if latitude and longitude arrays are likely in geographic coordinates,
89+
def is_geo_coords(lat, lon):
90+
"""Test if latitude and longitude arrays are likely in geographic coordinates,
9191
testing if min/max values are within -90 to 90 for latitude and -540 to 540
9292
for longitude. Lon coordinates of <-360 or >360 are allowed to cover cases
9393
of objects being defined close to the 180 meridian for which longitude
@@ -98,25 +98,42 @@ def check_if_geo_coords(lat, lon, raise_error=True):
9898
lat, lon : ndarrays of floats, same shape
9999
Latitudes and longitudes of points.
100100
101-
Raises
102-
------
103-
ValueError : if lat or lon out of likely geographical coordinate bounds
104-
105101
Returns
106102
-------
107103
test : bool
108104
True if lat/lon ranges seem to be in the geographic coordinates range, otherwise False.
109105
"""
110-
lat = np.array(lat)
106+
lat = np.asarray(lat)
111107
lon = np.array(lon)
112108

113109
# Check if latitude is within -90 to 90 and longitude is within -540 to 540
114110
# and extent are smaller than 180 and 360 respectively
115-
like_geo = (
111+
return (
116112
lat.min() >= -91 and lat.max() <= 91 and lon.min() >= -541 and lon.max() <= 541
117113
) and ((lat.max() - lat.min()) <= 181 and (lon.max() - lon.min()) <= 361)
118114

119-
if not like_geo and raise_error:
115+
116+
def check_if_geo_coords(lat, lon):
117+
"""Check if latitude and longitude arrays are likely in geographic coordinates,
118+
testing if min/max values are within -90 to 90 for latitude and -540 to 540
119+
for longitude. Lon coordinates of <-360 or >360 are allowed to cover cases
120+
of objects being defined close to the 180 meridian for which longitude
121+
intervals such as [179, 538] or [-538, -179] might be used.
122+
123+
Parameters
124+
----------
125+
lat, lon : ndarrays of floats, same shape
126+
Latitudes and longitudes of points.
127+
128+
Raises
129+
------
130+
ValueError : if lat or lon out of likely geographical coordinate bounds
131+
132+
See Also
133+
--------
134+
climada.util.coordinates.is_geo_coords: test if coordinates likely geographic
135+
"""
136+
if not is_geo_coords(lat, lon):
120137
raise ValueError(
121138
"Input lat and lon coordinates do not seem to correspond"
122139
" to geographic coordinates in degrees. This can be because"
@@ -125,7 +142,6 @@ def check_if_geo_coords(lat, lon, raise_error=True):
125142
" If you use degree values outside of these ranges,"
126143
" please shift the coordinates to the valid ranges."
127144
)
128-
return like_geo
129145

130146

131147
def get_crs_unit(crs):

0 commit comments

Comments
 (0)