dataset

xarray extensions for SMB and firn model data

Source code

General Attributes and Methods

class FirnCorr.io.dataset.DataTree(dtree)[source]

Accessor for extending an xarray.DataTree for SMB and firn data

assign_coords(x: ndarray, y: ndarray, crs: str | int | dict = 4326, **kwargs)[source]

Assign new coordinates to the DataTree

Parameters:
x: np.ndarray

Updated x-coordinates

y: np.ndarray

Updated y-coordinates

crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of coordinates

kwargs: dict

Keyword arguments for xarray.Dataset.assign_coords

Returns:
dtree: xarray.DataTree

DataTree with updated coordinates

coords_as(x: ndarray, y: ndarray, crs: str | int | dict = 4326, **kwargs)[source]

Transform coordinates into DataArrays in the DataTree coordinate reference system

Parameters:
x: np.ndarray

Input x-coordinates

y: np.ndarray

Input y-coordinates

crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of input coordinates

Returns:
X: xarray.DataArray

Transformed x-coordinates

Y: xarray.DataArray

Transformed y-coordinates

crop(*args, **kwargs)[source]

Crop DataTree to input bounding box

inpaint(**kwargs)[source]

Inpaint over missing data in DataTree

interp(x: ndarray, y: ndarray, **kwargs)[source]

Interpolate DataTree to new coordinates

Parameters:
x: np.ndarray

Interpolation x-coordinates

y: np.ndarray

Interpolation y-coordinates

transform_as(x: ndarray, y: ndarray, crs: str | int | dict = 4326, **kwargs)[source]

Transform coordinates to/from the DataTree coordinate reference system

Parameters:
x: np.ndarray

Input x-coordinates

y: np.ndarray

Input y-coordinates

crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of input coordinates

direction: str, default ‘FORWARD’

Direction of transformation

  • 'FORWARD': from input crs to model crs

  • 'INVERSE': from model crs to input crs

Returns:
X: np.ndarray

Transformed x-coordinates

Y: np.ndarray

Transformed y-coordinates

property crs

Coordinate reference system of the DataTree

class FirnCorr.io.dataset.Dataset(ds)[source]

Accessor for extending an xarray.Dataset for SMB and firn data

assign_coords(x: ndarray, y: ndarray, crs: str | int | dict = 4326, **kwargs)[source]

Assign new coordinates to the Dataset

Parameters:
x: np.ndarray

Updated x-coordinates

y: np.ndarray

Updated y-coordinates

crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of coordinates

kwargs: dict

Keyword arguments for xarray.Dataset.assign_coords

Returns:
ds: xarray.Dataset

Dataset with updated coordinates

cell_area()[source]

Calculate the area of each grid cell in the Dataset

Returns:
area: xarray.DataArray

Area of each grid cell in the dataset

coords_as(x: ndarray, y: ndarray, crs: str | int | dict = 4326, **kwargs)[source]

Transform coordinates into DataArrays in the Dataset coordinate reference system

Parameters:
x: np.ndarray

Input x-coordinates

y: np.ndarray

Input y-coordinates

crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of input coordinates

Returns:
X: xarray.DataArray

Transformed x-coordinates

Y: xarray.DataArray

Transformed y-coordinates

crop(bounds: list | tuple, buffer: int | float = 0)[source]

Crop Dataset to input bounding box

Parameters:
bounds: list, tuple

Bounding box [min_x, max_x, min_y, max_y]

buffer: int or float, default 0

Buffer to add to bounds for cropping

cumsum(**kwargs)[source]

Calculate cumulative sum of Dataset along time dimension

Returns:
ds: xarray.Dataset

Cumulative sum of the Dataset

extrap_like(other: Dataset, **kwargs)[source]

Extrapolate missing values in Dataset using nearest-neighbors

Parameters:
other: xarray.Dataset

Dataset with missing values to be extrapolated

kwargs: dict

Keyword arguments for FirnCorr.interpolate._nearest_neighbors()

Returns:
other: xarray.Dataset

Dataset with extrapolated values

gaussian_filter(sigma: float | list[float] = 1.5, **kwargs)[source]

Apply Gaussian smoothing to the Dataset

Parameters:
sigma: float or list, default 1.5

Standard deviation for Gaussian kernel in x and y directions

kwargs: dict

Keyword arguments for scipy.ndimage.gaussian_filter

Returns:
ds: xarray.Dataset

Smoothed Dataset

get(name: str)[source]

Get variable in Dataset using a case-insensitive search

Parameters:
name: str

Name of variable to find in dataset

Returns:
var: xarray.DataArray or None

Variable from dataset if found, otherwise None

grid_interp(x: ndarray, y: ndarray, method='linear', **kwargs)[source]

Interpolate a regular or rectilinear Dataset to new coordinates

Parameters:
x: np.ndarray

Interpolation x-coordinates

y: np.ndarray

Interpolation y-coordinates

method: str, default ‘linear’

Interpolation method

Returns:
other: xarray.Dataset

Interpolated Dataset

inpaint(**kwargs)[source]

Inpaint over missing data in Dataset

Parameters:
kwargs: dict

Keyword arguments for FirnCorr.interpolate.inpaint()

Returns:
ds: xarray.Dataset

Interpolated Dataset

interp(x: ndarray, y: ndarray, **kwargs)[source]

Interpolate Dataset to new coordinates

Parameters:
x: np.ndarray

Interpolation x-coordinates

y: np.ndarray

Interpolation y-coordinates

extrapolate: bool, default False

Flag to extrapolate values using nearest-neighbors

cutoff: int or float, default np.inf

Maximum distance for extrapolation

kwargs: dict

Additional keyword arguments for interpolation functions

Returns:
other: xarray.Dataset

Interpolated Dataset

pad(n: int = 1, chunks=None)[source]

Pad Dataset by repeating edge values in the x-direction

Parameters:
n: int, default 1

Number of padding values to add on each side

Returns:
ds: xarray.Dataset

Padded Dataset

to_anomaly(reference: str | None = None, climatology: list | None = None)[source]

Convert Dataset to anomalies relative to a reference period

Parameters:
reference: str or None

Method for referencing anomalies

  • 'first': remove first time step

  • 'mean': remove mean over a time range

climatology: list, default None

Time range for calculating mean reference

to_geographic(crs: str | int | dict = 4326)[source]

Get latitude and longitude coordinates for the Dataset

Parameters:
crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system for geographic coordinates

Returns:
lon: xarray.DataArray

Longitude coordinates for the dataset

lat: xarray.DataArray

Latitude coordinates for the dataset

transform_as(x: ndarray, y: ndarray, crs: str | int | dict = 4326, **kwargs)[source]

Transform coordinates to/from the Dataset coordinate reference system

Parameters:
x: np.ndarray

Input x-coordinates

y: np.ndarray

Input y-coordinates

crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of input coordinates

direction: str, default ‘FORWARD’

Direction of transformation

  • 'FORWARD': from input crs to model crs

  • 'INVERSE': from model crs to input crs

Returns:
X: np.ndarray

Transformed x-coordinates

Y: np.ndarray

Transformed y-coordinates

to_units(units: str, value: float = 1.0)[source]

Convert Dataset to specified units

Parameters:
units: str

Output units

value: float, default 1.0

Scaling factor to apply

to_base_units()[source]

Convert Dataset to base units

to_default_units()[source]

Convert Dataset to default units

property crs

Coordinate reference system of the Dataset

property is_global: bool

Determine if Dataset covers a global domain

property area_of_use: str | None

Area of use from the Dataset CRS

property axis_units: str

Units of the coordinate axes from the Dataset CRS

class FirnCorr.io.dataset.DataArray(da)[source]

Accessor for extending an xarray.DataArray for SMB and firn data

to_units(units: str, value: float = 1.0)[source]

Convert DataArray to specified units

Parameters:
units: str

Output units

value: float, default 1.0

Scaling factor to apply

to_base_units(value=1.0)[source]

Convert DataArray to base units

Parameters:
value: float, default 1.0

Scaling factor to apply

to_default_units(value=1.0)[source]

Convert DataArray to default units

Parameters:
value: float, default 1.0

Scaling factor to apply

property units

Units of the DataArray

property quantity

Pint Quantity of the DataArray

property group

Variable group of the DataArray

FirnCorr.io.dataset.combine_attrs(attrs_list: list[dict], context: str | None, **kwargs) dict[source]

Combine attributes from multiple datasets into a single dictionary merging conflicting values into a list

Parameters:
attrs_list: list of dict

List of attribute dictionaries from multiple datasets

context: str

Context for the attributes being combined

skip_keys: list of str, default [“units”]

List of attribute keys to skip from comparison

Returns:
result: dict

Combined attributes dictionary

FirnCorr.io.dataset.equivalent_attrs(a: Any, b: Any) bool[source]

Check if two attribute values are equivalent (ignoring case for strings)

Adapted from xarray.structure.merge.equivalent_attrs

Parameters:
a: Any

First attribute value

b: Any

Second attribute value

FirnCorr.io.dataset.get_variable(ds: Dataset, name: str) DataArray[source]

Get variable from a Dataset using a case-insensitive search

Parameters:
ds: xarray.Dataset

Dataset to search for variable

name: str

Name the variable to find

Returns:
var: xarray.DataArray

Variable matching the input name

FirnCorr.io.dataset.register_datatree_subaccessor(name)[source]

Register a custom subaccessor on DataTree objects

Parameters:
name: str

Name of the subaccessor

FirnCorr.io.dataset.register_dataset_subaccessor(name)[source]

Register a custom subaccessor on Dataset objects

Parameters:
name: str

Name of the subaccessor

FirnCorr.io.dataset.register_dataarray_subaccessor(name)[source]

Register a custom subaccessor on DataArray objects

Parameters:
name: str

Name of the subaccessor

FirnCorr.io.dataset._transform(i1: ndarray, i2: ndarray, source_crs: str | int | dict = 4326, target_crs: str | int | dict = None, **kwargs)[source]

Transform coordinates to/from the dataset coordinate reference system

Parameters:
i1: np.ndarray

Input x-coordinates

i2: np.ndarray

Input y-coordinates

source_crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of input coordinates

target_crs: str, int, or dict, default None

Coordinate reference system of output coordinates

direction: str, default ‘FORWARD’

Direction of transformation

  • 'FORWARD': from input crs to model crs

  • 'INVERSE': from model crs to input crs

Returns:
o1: np.ndarray

Transformed x-coordinates

o2: np.ndarray

Transformed y-coordinates

FirnCorr.io.dataset._coords(x: ndarray, y: ndarray, source_crs: str | int | dict = 4326, target_crs: str | int | dict = None, **kwargs)[source]

Transform coordinates into DataArrays in a new coordinate reference system

Parameters:
x: np.ndarray

Input x-coordinates

y: np.ndarray

Input y-coordinates

source_crs: str, int, or dict, default 4326 (WGS84 Latitude/Longitude)

Coordinate reference system of input coordinates

target_crs: str, int, or dict, default None

Coordinate reference system of output coordinates

type: str or None, default None

Coordinate data type

If not provided: must specify time parameter to auto-detect

  • None: determined from input variable dimensions

  • 'drift': drift buoys or satellite/airborne altimetry

  • 'grid': spatial grids or images

  • 'time series': time series at a single point

time: np.ndarray or None, default None

Time variable for determining coordinate data type

Returns:
X: xarray.DataArray

Transformed x-coordinates

Y: xarray.DataArray

Transformed y-coordinates