lair.geo.BaseGrid#

class lair.geo.BaseGrid(data, crs, **kwargs)[source]#

Bases: object

Base class for working with gridded data.

This class is a wrapper around xarray DataArray and Dataset objects, with additional methods for clipping, regridding, resampling, and reprojection. All operations are performed inplace, but return the grid object for chaining.

copy()[source]#

Create a copy of the grid.

Returns:

BaseGrid – The copied grid.

Return type:

Self

property gridcell_area: DataArray#

Calculate the grid cell area in km^2.

Returns:

xr.DataArray – The grid cell area.

clip(bbox=None, extent=None, geom=None, crs=None, inplace=False, **kwargs)[source]#

Clip the data to the given bounds.

Note

The result can be slightly different between supplying a geom and a bbox/extent. Clipping with a geom seems to be exclusive of the bounds, while clipping with a bbox/extent seems to be inclusive of the bounds.

Parameters:
  • bbox (tuple[minx, miny, maxx, maxy]) – The bounding box to clip the data to.

  • extent (tuple[minx, maxx, miny, maxy]) – The extent to clip the data to.

  • geom (shapely.Polygon) – The geometry to clip the data to.

  • crs (Any) –

    The CRS of the input geometries. If not provided, the CRS of the data is used. inplace : bool, optional

    Whether to modify the data in place. Default is False (returns a new copy with the clipped data).

  • kwargs (Any) – Additional keyword arguments to pass to the rioxarray clip method.

  • inplace (bool)

Returns:

BaseGrid – The clipped grid

Return type:

Self

regrid(out_grid, method='bilinear', inplace=False)[source]#

Regrid the data to a new grid. Uses xesmf for regridding.

Note

At present, xesmf only supports regridding lat-lon grids. self.data must be on a lat-lon grid. Possibly could use xesmf.frontend.BaseRegridder to regrid to a generic grid.

Warning

xarray.Dataset.cf.add_bounds is known to have issues, including near the 180th meridian. Care should be taken when using this method, especially with global datasets.

Parameters:
  • out_grid (xr.DataArray) – The new grid to resample to. Must be a lat-lon grid.

  • method (str, optional) – The regridding method, by default ‘bilinear’.

  • inplace (bool, optional) – Whether to modify the object in-place. Default is False.

Returns:

BaseGrid – The regridded grid

Return type:

Self

resample(resolution, regrid_method='bilinear', inplace=False)[source]#

Resample the data to a new resolution.

Parameters:
  • resolution (float | tuple[x_res, y_res]) – The new resolution in degrees. If a single value is provided, the resolution is assumed to be the same in both dimensions.

  • regrid_method (str, optional) – The regridding method, by default ‘bilinear’.

  • inplace (bool, optional) – Whether to modify the object in-place. Default is False.

Returns:

BaseGrid – The resampled grid

Return type:

Self

reproject(resolution, regrid_method='bilinear', inplace=False)[source]#

Reproject the data to a lat lon rectilinear grid.

Parameters:
  • resolution (float | tuple[x_res, y_res]) – The new resolution in degrees. If a single value is provided, the resolution is assumed to be the same in both dimensions.

  • regrid_method (str, optional) – The regridding method, by default ‘bilinear’.

  • inplace (bool, optional) – Whether to modify the object in-place. Default is False.

Returns:

BaseGrid – The reprojected grid

Return type:

Self