grib2io.utils.spatial
1import numpy as np 2 3 4def snap_to_nearest_cell_center(grid_lats, grid_lons, point_lat, point_lon): 5 """ 6 Snap the given point lat/lon to the nearest grid cell center. 7 8 Parameters 9 ---------- 10 grid_lats 11 The latitude values for each cell in the grid. 12 grid_lons 13 The longitude values for each cell in the grid. 14 point_lat 15 Latitude of point to snap. 16 point_lon 17 Longitude of point to snap. 18 19 Returns 20 ------- 21 lat 22 Latitude snapped to nearest cell center. 23 lon 24 Longitude snapped to nearest cell center. 25 """ 26 snap_lat = np.abs(grid_lats - point_lat) 27 snap_lon = np.abs(grid_lons - point_lon) 28 max_idx = np.maximum(snap_lat, snap_lon) 29 j, i = np.where(max_idx == np.min(max_idx)) 30 return np.array(grid_lats[j, i])[0], np.array(grid_lons[j, i])[0] 31 32 33def verify_lat_lon_bounds(lats, lons): 34 """ 35 Verify that the given latitude and longitude bounds are valid. 36 37 Parameters 38 ---------- 39 lats 40 Latitude bounds. 41 lons 42 Longitude bounds. 43 44 Returns 45 ------- 46 None 47 """ 48 if lats is not None: 49 if len(lats) != 2: 50 raise ValueError( 51 f"The `lats` keyword should supply a two-item tuple or list of (southern lat, northern lat) boundaries instead of '{lats}'." 52 ) 53 if lats[0] < -90 or lats[1] > 90: 54 raise ValueError( 55 f"The southern latitude boundary '{lats[0]}' and northern latitude boundary '{lats[1]}' must be between -90 and 90 degrees." 56 ) 57 if lats[0] > lats[1]: 58 raise ValueError(f"The southern latitude boundary '{lats[0]}' must be less than the northern latitude boundary '{lats[1]}'") 59 if lons is not None: 60 if len(lons) != 2: 61 raise ValueError( 62 f"The `lons` keyword should supply a two-item tuple or list of (western lon, eastern lon) boundaries instead of '{lons}'." 63 ) 64 if lons[0] > lons[1]: 65 raise ValueError(f"The western longitude boundary '{lons[0]}' must be less than the eastern longitude boundary '{lons[1]}'")
def
snap_to_nearest_cell_center(grid_lats, grid_lons, point_lat, point_lon):
5def snap_to_nearest_cell_center(grid_lats, grid_lons, point_lat, point_lon): 6 """ 7 Snap the given point lat/lon to the nearest grid cell center. 8 9 Parameters 10 ---------- 11 grid_lats 12 The latitude values for each cell in the grid. 13 grid_lons 14 The longitude values for each cell in the grid. 15 point_lat 16 Latitude of point to snap. 17 point_lon 18 Longitude of point to snap. 19 20 Returns 21 ------- 22 lat 23 Latitude snapped to nearest cell center. 24 lon 25 Longitude snapped to nearest cell center. 26 """ 27 snap_lat = np.abs(grid_lats - point_lat) 28 snap_lon = np.abs(grid_lons - point_lon) 29 max_idx = np.maximum(snap_lat, snap_lon) 30 j, i = np.where(max_idx == np.min(max_idx)) 31 return np.array(grid_lats[j, i])[0], np.array(grid_lons[j, i])[0]
Snap the given point lat/lon to the nearest grid cell center.
Parameters
- grid_lats: The latitude values for each cell in the grid.
- grid_lons: The longitude values for each cell in the grid.
- point_lat: Latitude of point to snap.
- point_lon: Longitude of point to snap.
Returns
- lat: Latitude snapped to nearest cell center.
- lon: Longitude snapped to nearest cell center.
def
verify_lat_lon_bounds(lats, lons):
34def verify_lat_lon_bounds(lats, lons): 35 """ 36 Verify that the given latitude and longitude bounds are valid. 37 38 Parameters 39 ---------- 40 lats 41 Latitude bounds. 42 lons 43 Longitude bounds. 44 45 Returns 46 ------- 47 None 48 """ 49 if lats is not None: 50 if len(lats) != 2: 51 raise ValueError( 52 f"The `lats` keyword should supply a two-item tuple or list of (southern lat, northern lat) boundaries instead of '{lats}'." 53 ) 54 if lats[0] < -90 or lats[1] > 90: 55 raise ValueError( 56 f"The southern latitude boundary '{lats[0]}' and northern latitude boundary '{lats[1]}' must be between -90 and 90 degrees." 57 ) 58 if lats[0] > lats[1]: 59 raise ValueError(f"The southern latitude boundary '{lats[0]}' must be less than the northern latitude boundary '{lats[1]}'") 60 if lons is not None: 61 if len(lons) != 2: 62 raise ValueError( 63 f"The `lons` keyword should supply a two-item tuple or list of (western lon, eastern lon) boundaries instead of '{lons}'." 64 ) 65 if lons[0] > lons[1]: 66 raise ValueError(f"The western longitude boundary '{lons[0]}' must be less than the eastern longitude boundary '{lons[1]}'")
Verify that the given latitude and longitude bounds are valid.
Parameters
- lats: Latitude bounds.
- lons: Longitude bounds.
Returns
- None