Subsetting, Regions & Masks¶
Carve a dataset down to a region of interest — by bounding box, time window, named region, or boolean predicate — and add land / ocean / country masks. Named regions come from a small registry that also reads GeoJSON polygons.
Subsetting¶
SubsetBBox
¶
SubsetBBox(lon_bnds: tuple[float, float], lat_bnds: tuple[float, float], lon: str = 'lon', lat: str = 'lat')
Bases: Operator
Subset a dataset to a lon/lat bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lon_bnds
|
tuple[float, float]
|
|
required |
lat_bnds
|
tuple[float, float]
|
|
required |
lon
|
str
|
Name of the longitude coordinate. |
'lon'
|
lat
|
str
|
Name of the latitude coordinate. |
'lat'
|
Returns:
| Type | Description |
|---|---|
|
The dataset restricted to the bounding box. |
Source code in packages/xrtoolz-ops/src/xrops/geom/operators.py
SubsetTime
¶
Bases: Operator
Subset a dataset to an inclusive [time_min, time_max] window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_min
|
str
|
Start of the time window (ISO string). |
required |
time_max
|
str
|
End of the time window (ISO string). |
required |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
|
The dataset restricted to the time window. |
Source code in packages/xrtoolz-ops/src/xrops/geom/operators.py
SubsetToRegion
¶
SubsetToRegion(region: str | RegionSpec | Regions | dict[str, Any], *, lon: str = 'lon', lat: str = 'lat', validate: bool = True)
Bases: Operator
Subset a dataset to a named or custom geographic region.
Masks to the region and trims to its bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
str | RegionSpec | Regions | dict[str, Any]
|
Region specification — a registry name, a
:class: |
required |
lon
|
str
|
Name of the longitude coordinate. |
'lon'
|
lat
|
str
|
Name of the latitude coordinate. |
'lat'
|
validate
|
bool
|
Whether to validate coordinates before masking. |
True
|
Returns:
| Type | Description |
|---|---|
|
The dataset restricted to the region. |
Source code in packages/xrtoolz-ops/src/xrops/geom/operators.py
SelectVariables
¶
Bases: Operator
Select a subset of data variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables
|
str | Sequence[str]
|
A variable name or sequence of names to keep. |
required |
Returns:
| Type | Description |
|---|---|
|
The dataset containing only the selected variables. |
Source code in packages/xrtoolz-ops/src/xrops/geom/operators.py
Masks¶
AddLandMask
¶
Bases: Operator
Add a boolean land mask derived from regionmask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the mask variable to add. |
'land_mask'
|
Returns:
| Type | Description |
|---|---|
|
The input dataset with a boolean land-mask variable. |
Source code in packages/xrtoolz-ops/src/xrops/mask/operators.py
AddOceanMask
¶
Bases: Operator
Add a boolean ocean/basin mask derived from regionmask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ocean
|
str
|
Ocean/basin selector (e.g. |
'global'
|
name
|
str
|
Name of the mask variable to add. |
'ocean_mask'
|
Returns:
| Type | Description |
|---|---|
|
The input dataset with a boolean ocean-mask variable. |
Source code in packages/xrtoolz-ops/src/xrops/mask/operators.py
AddCountryMask
¶
Bases: Operator
Add a boolean country mask derived from regionmask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country
|
str
|
Country name/selector to mask. |
required |
name
|
str
|
Name of the mask variable to add. |
'country_mask'
|
Returns:
| Type | Description |
|---|---|
|
The input dataset with a boolean country-mask variable. |
Source code in packages/xrtoolz-ops/src/xrops/mask/operators.py
ApplyMask
¶
Bases: Operator
Apply a boolean mask to a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
A boolean DataArray, or the name of a mask variable already in the dataset. |
required | |
drop
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
|
The masked dataset. |
Source code in packages/xrtoolz-ops/src/xrops/mask/operators.py
Functional primitives (Layer 0)¶
These pure functions back the operators above; each takes xr.DataArray/xr.Dataset and a dim: argument.
subset_bbox
¶
subset_bbox(ds: Dataset, lon_bnds: tuple[float, float], lat_bnds: tuple[float, float], lon: str = 'lon', lat: str = 'lat') -> Dataset
Keep points whose (lon, lat) fall inside a bounding box.
Works with both 1-D rectilinear grids (where sel with slices would
also work) and 2-D lon/lat coordinate arrays, because it masks via
where(..., drop=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset. |
required |
lon_bnds
|
tuple[float, float]
|
|
required |
lat_bnds
|
tuple[float, float]
|
|
required |
lon
|
str
|
Name of the longitude coordinate. |
'lon'
|
lat
|
str
|
Name of the latitude coordinate. |
'lat'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset restricted to the bounding box. |
Source code in packages/xrtoolz-products/src/xrproducts/types/_src/subset.py
subset_time
¶
subset_time(ds: Dataset, time_min: str | datetime64 | Timestamp, time_max: str | datetime64 | Timestamp, time: str = 'time') -> Dataset
Restrict a dataset to the time range [time_min, time_max].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset. |
required |
time_min
|
str | datetime64 | Timestamp
|
Lower bound, any type xarray accepts in a |
required |
time_max
|
str | datetime64 | Timestamp
|
Upper bound, same types as |
required |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset sliced along the time dimension. |
Source code in packages/xrtoolz-products/src/xrproducts/types/_src/subset.py
subset_to_region
¶
subset_to_region(ds: Dataset, region: str | RegionSpec | Regions, *, lon: str = 'lon', lat: str = 'lat', validate: bool = True) -> Dataset
Subset a Dataset to a named, custom, or polygon region.
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/subset.py
subset_where
¶
subset_where(ds: Dataset, variable: str, min_val: float, max_val: float, drop: bool = True) -> Dataset
Keep points where variable is in [min_val, max_val].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset. |
required |
variable
|
str
|
Name of the variable used to build the mask. |
required |
min_val
|
float
|
Lower bound (inclusive). |
required |
max_val
|
float
|
Upper bound (inclusive). |
required |
drop
|
bool
|
Whether to drop cells outside the range (default |
True
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset with cells outside the range masked or dropped. |
Source code in packages/xrtoolz-products/src/xrproducts/types/_src/subset.py
select_variables
¶
Return a dataset with only the requested variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset. |
required |
variables
|
str | Sequence[str]
|
A single variable name or a sequence of names. |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset restricted to |
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/subset.py
add_land_mask
¶
Attach a 1/0 land mask (Natural Earth 110m) as a coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset with |
required |
name
|
str
|
Name of the mask coordinate to add. |
'land_mask'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset with a new |
Dataset
|
0 over ocean. |
Source code in packages/xrtoolz-ops/src/xrops/mask/_src/masks.py
add_ocean_mask
¶
Attach a 1/0 ocean mask as a coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset with |
required |
ocean
|
str
|
Ocean basin name (e.g. |
'global'
|
name
|
str
|
Name of the mask coordinate to add. |
'ocean_mask'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset with a new |
Dataset
|
requested ocean region(s) and 0 elsewhere. |
Source code in packages/xrtoolz-ops/src/xrops/mask/_src/masks.py
add_country_mask
¶
Attach a 1/0 country mask as a coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset with |
required |
country
|
str
|
Country name or 3-letter abbreviation as recognised by Natural Earth 110m country regions. |
required |
name
|
str
|
Name of the mask coordinate to add. |
'country_mask'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset with a new |
Dataset
|
|
Source code in packages/xrtoolz-ops/src/xrops/mask/_src/masks.py
apply_mask
¶
Apply a boolean mask to every variable in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input dataset. |
required |
mask
|
DataArray | str
|
Either a DataArray to mask by, or the name of a
coordinate / variable in |
required |
drop
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Masked dataset. |
Source code in packages/xrtoolz-ops/src/xrops/mask/_src/masks.py
Region registry¶
REGIONS is the built-in named-region table; RegionSpec is the frozen
bounding-box descriptor. The helpers below resolve a region from a name, a
dict, or a GeoJSON polygon.
REGIONS
module-attribute
¶
REGIONS: dict[str, RegionSpec] = {'global': custom_region(id='global', display_name='Global', lat_min=-90.0, lat_max=90.0, lon_min=-180.0, lon_max=180.0, projection='Robinson'), 'north_atlantic': custom_region(id='north_atlantic', display_name='North Atlantic', lat_min=10.0, lat_max=65.0, lon_min=-80.0, lon_max=0.0), 'gulf_stream': custom_region(id='gulf_stream', display_name='Gulf Stream', lat_min=30.0, lat_max=45.0, lon_min=-80.0, lon_max=-50.0), 'kuroshio': custom_region(id='kuroshio', display_name='Kuroshio', lat_min=25.0, lat_max=45.0, lon_min=130.0, lon_max=180.0), 'mediterranean': custom_region(id='mediterranean', display_name='Mediterranean', lat_min=30.0, lat_max=46.0, lon_min=-6.0, lon_max=36.0), 'ibi': custom_region(id='ibi', display_name='Iberia-Biscay-Ireland (IBI)', lat_min=26.17, lat_max=56.08, lon_min=-19.08, lon_max=5.08)}
RegionSpec
dataclass
¶
Named region pairing regionmask geometry with viz metadata.
bbox_region
¶
bbox_region(*, id: str, name: str, lat_min: float, lat_max: float, lon_min: float, lon_max: float) -> Regions
Construct a regionmask.Regions from a rectangular bounding box.
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/regions.py
custom_region
¶
custom_region(*, id: str, display_name: str, lat_min: float, lat_max: float, lon_min: float, lon_max: float, projection: str | None = 'PlateCarree') -> RegionSpec
Build a named RegionSpec from a rectangular bounding box.
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/regions.py
resolve_region
¶
resolve_region(region: str | RegionSpec) -> RegionSpec
Resolve a registered region id or pass through a RegionSpec.
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/regions.py
region_from_dict
¶
region_from_dict(data: dict[str, Any]) -> RegionSpec
Deserialize a RegionSpec from native or bbox-only JSON data.
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/regions.py
region_to_dict
¶
region_to_dict(region: RegionSpec | Regions) -> dict[str, Any]
Serialize a RegionSpec or raw Regions to a JSON-safe dict.
Source code in packages/xrtoolz-ops/src/xrops/geom/_src/regions.py
load_region_file
¶
load_region_file(path: str | Path) -> RegionSpec
polygon_from_geojson
¶
Construct Regions from a GeoJSON Feature, Polygon, or MultiPolygon.