selectors module#
This module provides a structured, object-oriented interface for selecting and subsetting data from an xsnowDataset object.
The Selector classes are designed to provide a more explicit and reusable way to define complex queries compared to using dictionaries directly with xarray’s .sel() or .isel() methods.
The base Selector class intelligently routes the selection to either .sel() (for label-based indexing) or .isel() (for integer-based indexing) based on the type of the input.
— Usage Example —
from xsnow.dataset import xsnowDataset from selectors import TimeSelector, LocationSelector
# 1. Load a dataset ds = xsnowDataset(‘path/to/your/data’)
# 2. Create selector objects # By station ID location_selector_id = LocationSelector(sel=’station_A’)
# By geographic bounding box, specifying the CRS location_selector_bbox = LocationSelector(
lat_range=(45.0, 46.0), lon_range=(10.0, 11.0), crs=’EPSG:4326’ # WGS 84
)
# 3. Apply the selectors to the dataset subset_by_id = location_selector_id.apply(ds.data) subset_by_bbox = location_selector_bbox.apply(ds.data)
print(subset_by_id) print(subset_by_bbox)
- class xsnow.selectors.Selector(dim, sel)#
Bases:
objectA base class for creating dimension-specific data selectors.
This class provides the core logic to intelligently apply a selection to an xarray.Dataset, automatically choosing between label-based (.sel) and integer-based (.isel) indexing.
- apply(ds)#
Applies the selector to a given xarray.Dataset.
By default, it routes the selection through .sel if the input looks like a label (e.g., a string, timestamp, or slice of those), and through .isel otherwise (e.g., an integer or slice of integers).
- Parameters:
ds (xr.Dataset) – The dataset to apply the selection to.
- Returns:
The subsetted dataset.
- Return type:
xr.Dataset
- class xsnow.selectors.TimeSelector(sel)#
Bases:
SelectorA selector specifically for the ‘time’ dimension.
- class xsnow.selectors.LocationSelector(sel=None, lat_range=None, lon_range=None, crs='EPSG:4326')#
Bases:
SelectorA selector for the ‘location’ dimension that can filter by station ID or by a geographic bounding box.
- apply(ds)#
Apply location selection - NOW MUCH SIMPLER!
- Return type:
Dataset
- class xsnow.selectors.SlopeSelector(sel, tolerance=None)#
Bases:
SelectorSelector for the ‘slope’ dimension.
Supports: - int / list[int] / slice / xr.DataArray (existing positional/boolean logic) - dict query like {‘inclination’: 30, ‘azimuth’: 180} to match slope
metadata coords/vars with optional tolerance.
- Parameters:
sel (int, slice, list, DataArray, or dict) – Selection criteria for slopes
tolerance (float, optional) – When using dict-style queries with numerical values, allow matches within this tolerance. Useful for floating-point comparisons. If None, uses exact equality for integers and small default tolerance (1e-6) for floats.
- apply(ds)#
Applies the selector to a given xarray.Dataset.
By default, it routes the selection through .sel if the input looks like a label (e.g., a string, timestamp, or slice of those), and through .isel otherwise (e.g., an integer or slice of integers).
- Parameters:
ds (xr.Dataset) – The dataset to apply the selection to.
- Returns:
The subsetted dataset.
- Return type:
xr.Dataset