processing.select.get_shapefile_mask#

get_shapefile_mask(ds: Dataset, shapefile_path: Path)[source]#

Generates a mask from a shapefile to apply to an xarray Dataset.

This function reads a shapefile using Geopandas, converts it to the WGS84 coordinate reference system (CRS), and creates a mask that can be applied to the input xarray Dataset. The mask identifies the grid cells that fall within the shapefile’s region.

Parameters:
  • ds (xr.Dataset) – Input xarray Dataset containing longitude and latitude coordinates.

  • shapefile_path (Path) – Path to the shapefile to be used for masking.

Returns:

mask_shp – A boolean mask array where grid cells within the shapefile region are marked as True, and those outside are marked as False.

Return type:

xr.Dataset

Notes

  • The shapefile is converted to the WGS84 CRS (EPSG:4326) before creating the mask.

  • The function uses the regionmask library to generate the mask.

Examples

>>> import xarray as xr
>>> from pathlib import Path
>>> ds = xr.open_dataset('path_to_your_dataset.nc')
>>> shapefile = Path('path_to_your_shapefile.shp')
>>> mask = get_shapefile_mask(ds, shapefile)