CF-Xarray is a lightweight Python library that extends Xarray objects with an accessor (`.cf`) to interpret and utilize Climate and Forecast (CF) metadata conventions. It simplifies data analysis workflows by allowing users to refer to geophysical quantities by their standard CF names (e.g., 'latitude' instead of 'lat'), making code more generic across diverse CF-compliant datasets. The library is actively maintained as part of the `xarray-contrib` organization, with frequent releases.
pip install cf-xarrayVerified import paths — ran on the pinned version, not inferred.
Demonstrates loading an xarray dataset, accessing CF-compliant coordinates and axes via the `.cf` accessor, and performing an operation (mean) using CF standard names. It also shows how to use `guess_coord_axis()` for datasets with incomplete metadata.
Review xarray's documentation for attribute handling. If explicit attribute dropping or custom merging is needed, use `keep_attrs=False` or implement custom logic.
Inspect recognized attributes using `ds.cf.coordinates` or `ds.cf.axes`. Use methods like `ds.cf.guess_coord_axis()` or `ds.cf.add_canonical_attributes()` to infer or add missing metadata. Custom criteria can also be provided for identification.
Be aware of the limitations for non-rectilinear grids. For critical applications, prefer datasets with pre-calculated, explicitly provided bounds or grid metrics from the data producer.
Ensure your environment uses Python 3.11 or later and the latest compatible version of xarray.
Add `import cf_xarray` at the beginning of your script or before any `.cf` accessor calls.
Inspect `ds.cf.coordinates` or `ds.cf.axes` to see which CF attributes are recognized. You might need to manually add appropriate attributes to your xarray object (e.g., `da.attrs['standard_name'] = 'latitude'`) or use `ds.cf.guess_coord_axis()` to infer them.
Install xarray: `pip install xarray cf-xarray`.