esmtools.spatial.extract_region

esmtools.spatial.extract_region(ds, xgrid, ygrid, coords, lat_dim='lat', lon_dim='lon')[source]

Extract a subset of some larger spatial data.

Parameters:
  • ds (xarray object) – Data to be subset.
  • xgrid (array_like) – Meshgrid of longitudes.
  • ygrid (array_like) – Meshgrid of latitudes.
  • coords (1-D array or list) – [x0, x1, y0, y1] pertaining to the corners of the box to extract.
  • lat_dim (optional str) – Latitude dimension name (default ‘lat’).
  • lon_dim (optional str) – Longitude dimension name (default ‘lon’)
Returns:

Data subset to domain of interest.

Return type:

subset_data (xarray object)

Examples

>>> import esmtools as et
>>> import numpy as np
>>> import xarray as xr
>>> x = np.linspace(0, 360, 37)
>>> y = np.linspace(-90, 90, 19)
>>> xx, yy = np.meshgrid(x, y)
>>> ds = xr.DataArray(np.random.rand(19, 37), dims=['lat', 'lon'])
>>> ds['latitude'] = (('lat', 'lon'), yy)
>>> ds['longitude'] = (('lat', 'lon'), xx)
>>> coords = [0, 30, -20, 20]
>>> subset = et.spatial.extract_region(ds, xx, yy, coords)