CF-Xarray

0.10.11 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import xarray as xr
import cf_xarray # This registers the .cf accessor

# Load a sample dataset (e.g., from xarray's tutorial data)
ds = xr.tutorial.load_dataset("air_temperature")

# Access CF-compliant coordinates
print("CF-identified coordinates:", ds.cf.coordinates)
print("Latitude variable name:", ds.cf["latitude"])

# Perform a mean operation using CF standard names
mean_temp = ds.air.cf.mean("latitude")
print("Mean temperature along latitude:\n", mean_temp)

# Add missing CF attributes (if necessary)
ds_incomplete = ds.copy(deep=True)
ds_incomplete.lat.attrs.pop('standard_name')
ds_incomplete.cf.guess_coord_axis(verbose=True)
print("Guessed coordinates for incomplete dataset:", ds_incomplete.cf.coordinates)

view raw JSON →