diff --git a/climada/hazard/trop_cyclone/trop_cyclone_windfields.py b/climada/hazard/trop_cyclone/trop_cyclone_windfields.py index f8ac09078..ac77e0f43 100644 --- a/climada/hazard/trop_cyclone/trop_cyclone_windfields.py +++ b/climada/hazard/trop_cyclone/trop_cyclone_windfields.py @@ -902,6 +902,15 @@ def _coriolis_parameter(lat: np.ndarray) -> np.ndarray: cp : np.ndarray of same shape as input Coriolis parameter. """ + if not u_coord.check_if_geo_coords(lat, 0): + raise ValueError( + "Input lat and lon coordinates do not seem to correspond" + " to geographic coordinates in degrees. This can be because" + " total extents are > 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90360 are allowed to cover cases + of objects being defined close to the 180 meridian. + + Parameters + ---------- + lat, lon : ndarrays of floats, same shape + Latitudes and longitudes of points. + + Returns + ------- + test : bool + True if lat/lon ranges seem to be in the geographic coordinates range, otherwise False. + """ + lat = np.array(lat) + lon = np.array(lon) + + # Check if latitude is within -90 to 90 and longitude is within -540 to 540 + # and extent are smaller than 180 and 360 respectively + test = ( + lat.min() >= -91 and lat.max() <= 91 and lon.min() >= -541 and lon.max() <= 541 + ) and ((lat.max() - lat.min()) <= 181 and (lon.max() - lon.min()) <= 361) + return bool(test) + + def latlon_to_geosph_vector(lat, lon, rad=False, basis=False): """Convert lat/lon coodinates to radial vectors (on geosphere) @@ -448,13 +475,24 @@ def get_gridcellarea(lat, resolution=0.5, unit="ha"): unit: string, optional unit of the output area (default: ha, alternatives: m2, km2) """ - + # first check that lat is in geographic coordinates + if not check_if_geo_coords(lat, 0): + raise ValueError( + "Input lat and lon coordinates do not seem to correspond" + " to geographic coordinates in degrees. This can be because" + " total extents are > 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90= 0 else 100) return epsg_utm_base + (math.floor((lon + 180) / 6) % 60) @@ -564,6 +611,15 @@ def dist_to_coast(coord_lat, lon=None, highres=False, signed=False): raise ValueError( f"Mismatching input coordinates size: {lat.size} != {lon.size}" ) + if not check_if_geo_coords(lat, lon): + raise ValueError( + "Input lat and lon coordinates do not seem to correspond" + " to geographic coordinates in degrees. This can be because" + " total extents are > 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90 360: + if not check_if_geo_coords(extent[2:], extent[:2]): raise ValueError( - f"longitude extent range is greater than 360: {extent[0]} to {extent[1]}" + "Input lat and lon coordinates do not seem to correspond" + " to geographic coordinates in degrees. This can be because" + " total extents are > 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90 0 and exact_assign_idx.size != coords_view.size: + # check that coords are geographic before proceeding to nearest neighbor search + if not ( + check_if_geo_coords(coords[:, 0], coords[:, 1]) + and check_if_geo_coords(coords_to_assign[:, 0], coords_to_assign[:, 1]) + ): + raise ValueError( + "Input lat and lon coordinates do not seem to correspond" + " to geographic coordinates in degrees. This can be because" + " total extents are > 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90 180 for lat or > 360 for lon, lat coordinates" + " are outside of -90