"""
Grid utilities
"""
from xoa.cf import get_cf_specs
[docs]def geosel(ds, lon, lat, pad=0):
"""Geographic selection"""
# Cf specs
cfs = get_cf_specs()
# Input grid coordinates
glon = cfs.coords.get(ds, 'lon')
glat = cfs.coords.get(ds, 'lat')
# Selection specs
lon_min = float(min(lon)) - pad
lon_max = float(max(lon)) + pad
lat_min = float(min(lat)) - pad
lat_max = float(max(lat)) + pad
# Rectangular selection
if glon.ndim == 1:
return ds.sel({glon.name: slice(lon_min, lon_max),
glat.name: slice(lat_min, lat_max)})
# Curvilinear selection
return ds.where((glon >= lon_min) & (glon <= lon_max) &
(glat >= lat_min) & (glat <= lat_max), drop=True)
[docs]class GeoSelector(object):
"""Geographic selector"""
[docs] def __init__(self, lon, lat, pad=0):
self.lon = lon
self.lat = lat
self.pad = pad
[docs] def select(self, ds):
"""Apply slection with :func:`geosel`"""
return geosel(ds, self.lon, self.lat, self.pad)
__call__ = select