mcpField
X
xarray

xarray

github.com/steadfastasartData & databases7483 installsUpdated 7w ago
Repository

About xarray

xarray is a Python tool for scientific and geoscience data analysis, built around labeled multidimensional arrays. It supports NetCDF and Zarr workflows and Dask-based processing for climate data selection and aggregation.

Key features

  1. 01Work with labeled multidimensional arrays
  2. 02Read and write NetCDF and Zarr
  3. 03Select data by coordinates or index
  4. 04Group, resample, and calculate statistics
  5. 05Process large datasets lazily with Dask

Use cases

  1. 01Analyze climate data by time or region
  2. 02Calculate monthly climatologies and anomalies
  3. 03Process large datasets in chunks

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/steadfastasart/geoscience-skills --skill xarray

After installing, type/xarrayin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
computation.md
io_formats.md
scripts/
climate_analysis.py

Details

From the project README

xarray - Multi-Dimensional Geoscience Data

Quick Reference

import xarray as xr

# Read
ds = xr.open_dataset('data.nc')

# Access data
temp = ds['temperature']         # DataArray
values = temp.values             # numpy array
df = ds.to_dataframe()           # pandas DataFrame

# Structure info
print(ds)                        # Overview
print(ds.dims)                   # Dimensions
print(ds.data_vars)              # Variables

# Write
ds.to_netcdf('output.nc')

Key Classes

ClassPurpose
DatasetCollection of aligned DataArrays (like NetCDF file)
DataArraySingle variable with labeled dimensions
CoordinatesDimension labels (time, lat, lon)

Essential Operations

Select Data
# By coordinate value
temp_jan = ds['temperature'].sel(time='2020-01-15')
temp_region = ds['temperature'].sel(lat=slice(-30, 30), lon=slice(-60, 60))

# Nearest value
temp_point = ds['temperature'].sel(lat=35.5, lon=-120.3, method='nearest')

# By index
temp_first = ds['temperature'].isel(time=0)
Compute Statistics
temp = ds['temperature']
temp_mean_time = temp.mean(dim='time')           # Spatial map
temp_mean_space = temp.mean(dim=['lat', 'lon'])  # Time series

# Area-weighted mean
import numpy as np
weights = np.cos(np.deg2rad(ds.lat))
temp_weighted = temp.weighted(weights).mean(dim=['lat', 'lon'])
GroupBy and Resample
temp = ds['temperature']

# Temporal aggregations
monthly_mean = temp.groupby('time.month').mean()
annual_mean = temp.groupby('time.year').mean()

# Climatology and anomalies
climatology = temp.groupby('time.month').mean('time')
anomalies = temp.groupby('time.month') - climatology

# Resample time series
monthly = temp.resample(time='1M').mean()
rolling_30d = temp.rolling(time=30, center=True).mean()
Create New Dataset
import numpy as np
import pandas as pd

times = pd.date_range('2020-01-01', periods=365, freq='D')
lats = np.linspace(-90, 90, 180)
lons = np.linspace(-180, 180, 360)

da = xr.DataArray(
    data=np.random.randn(365, 180, 360),
    dims=['time', 'lat', 'lon'],
    coords={'time': times, 'lat': lats, 'lon': lons},
    attrs={'units': 'degC', 'long_name': 'Temperature'}
)

ds = xr.Dataset({'temperature': da})
ds.to_netcdf('output.nc')
Masking
temp_warm = temp.where(temp > 20)                 # Mask by condition
temp_clipped = temp.where(temp > 0, 0)            # Replace negative with 0

tropics = (ds.lat > -23.5) & (ds.lat < 23.5)
temp_tropics = temp.where(tropics, drop=True)    # Mask by coordinate
Large Datasets (Dask)
# Open with chunking (lazy loading)
ds = xr.open_dataset('large_file.nc', chunks={'time': 100})
ds = xr.open_mfdataset('data_*.nc', chunks='auto')

# Operations are lazy until .compute()
result = ds['temperature'].mean(dim='time').compute()

When to Use vs Alternatives

ToolBest ForLimitations
xarrayLabeled multi-dim arrays, NetCDF/Zarr, Dask integrationLearning curve for newcomers from numpy
irisMet Office climate workflows, UGRID mesh supportSmaller community, UK-centric conventions
CDOFast command-line climate data operationsNot Python-native, limited custom analysis
NCOQuick NetCDF file manipulation and arithmeticCommand-line only, no visualization

Use xarray when you need labeled dimension handling, seamless NetCDF/Zarr I/O, groupby/resample operations, or Dask-based parallel processing of large datasets.

Consider alternatives when you need fast one-off command-line operations on NetCDF files (use CDO/NCO), or you work within the Met Office ecosystem with UGRID meshes (use iris).

Common Workflows

Climate data analysis with temporal aggregation
  • Open NetCDF dataset with xr.open_dataset() (use chunks= if large)
  • Inspect dimensions, coordinates, and variables with print(ds)
  • Select region of interest with .sel(lat=slice(), lon=slice())
  • Compute climatology with .groupby('time.month').mean('time')
  • Calculate anomalies by subtracting climatology from data
  • Compute area-weighted spatial mean using cosine latitude weights
  • Resample to desired temporal resolution (monthly, annual)
  • Save results to NetCDF with .to_netcdf()

Common Issues

IssueSolution
Memory errorUse chunks= for lazy loading
Time decoding failsdecode_times=False then manual decode
Missing coordinatesCheck ds.coords and ds.dims
Alignment errorsCheck coordinate values match

References

Scripts

Comments

Markdown supported