A simple, universal Python wrapper for CDO (Climate Data Operators) with seamless xarray integration
Project description
Python CDO Wrapper
A simple, universal Python wrapper for CDO (Climate Data Operators) with seamless xarray integration. Perfect for Jupyter notebooks and climate data analysis workflows.
Features
- 🚀 Simple API: Single function to handle all CDO operations
- 📊 Auto-detection: Automatically detects text vs. data commands
- 🔄 xarray Integration: Returns xarray.Dataset for data operations
- 🧹 Clean Output: Automatic temp file management
- 🐛 Debug Mode: Easy troubleshooting with detailed output
- 📝 Type Hints: Full typing support for IDE autocompletion
- ⚡ Zero Config: Works out of the box
Installation
pip install python-cdo-wrapper
Prerequisites
CDO must be installed on your system:
# macOS (Homebrew)
brew install cdo
# Ubuntu/Debian
sudo apt install cdo
# Conda (recommended for HPC)
conda install -c conda-forge cdo
Quick Start
from python_cdo_wrapper import cdo
# Text commands return strings
info = cdo("sinfo data.nc")
print(info)
# Data commands return xarray.Dataset
ds, log = cdo("yearmean data.nc")
print(ds)
# Chain operators
ds, log = cdo("-yearmean -selname,temperature input.nc")
Usage Examples
Getting File Information
from python_cdo_wrapper import cdo
# File structure info
info = cdo("sinfo data.nc")
print(info)
# Grid description
grid = cdo("griddes data.nc")
print(grid)
# Time information
times = cdo("showtimestamp data.nc")
print(times)
# Variable names
vars = cdo("showname data.nc")
print(vars)
Data Processing
from python_cdo_wrapper import cdo
# Calculate yearly mean
ds, log = cdo("yearmean input.nc")
# Select specific variable
ds, log = cdo("-selname,temperature input.nc")
# Select time range
ds, log = cdo("-seldate,2020-01-01,2020-12-31 input.nc")
# Regrid to different resolution
ds, log = cdo("-remapbil,r360x180 input.nc")
# Calculate field mean
ds, log = cdo("fldmean input.nc")
# Chain multiple operators
ds, log = cdo("-yearmean -selname,temp -sellonlatbox,-10,30,35,70 input.nc")
Saving Output
from python_cdo_wrapper import cdo
# Save to specific file (file persists)
ds, log = cdo("yearmean input.nc", output_file="output.nc")
# Without loading into xarray (useful for large files)
_, log = cdo("yearmean input.nc", output_file="output.nc", return_xr=False)
Debugging
from python_cdo_wrapper import cdo
# Enable debug mode for detailed output
ds, log = cdo("yearmean input.nc", debug=True)
# Output:
# CDO Command: cdo yearmean input.nc /tmp/xxx.nc
# Return code: 0
# Stdout: ...
# Stderr: ...
Error Handling
from python_cdo_wrapper import cdo, CDOError
try:
ds, log = cdo("invalid_command data.nc")
except CDOError as e:
print(f"CDO failed with code {e.returncode}")
print(f"Command: {e.command}")
print(f"Error: {e.stderr}")
except FileNotFoundError as e:
print(f"File or CDO not found: {e}")
Utility Functions
from python_cdo_wrapper.core import get_cdo_version, list_operators
# Get CDO version
version = get_cdo_version()
print(version)
# List available operators
ops = list_operators()
print(ops)
Text vs Data Commands
The wrapper automatically detects command types:
Text Commands (return str)
These operators print information and don't produce NetCDF output:
| Category | Operators |
|---|---|
| File Info | sinfo, info, ninfo, tinfo, vlist |
| Grid | griddes, showgrid, showprojection |
| Variables | showname, showvar, showcode, showunit |
| Time | showdate, showtimestamp, showyear, showmonth |
| Counts | ntsteps, nvars, nlevels, ngrids |
Data Commands (return tuple[xr.Dataset, str])
All other operators that produce NetCDF output:
| Category | Example Operators |
|---|---|
| Statistics | yearmean, monmean, daymean, fldmean |
| Selection | selname, seldate, sellevel, sellonlatbox |
| Remapping | remapbil, remapcon, remapnn |
| Arithmetic | add, sub, mul, div, expr |
| Comparison | eq, ne, gt, lt |
API Reference
cdo(cmd, *, output_file=None, return_xr=True, debug=False, check_files=True)
Execute a CDO command and return results as Python objects.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
cmd |
str |
required | CDO command (without leading "cdo") |
output_file |
str | Path | None |
None |
Output file path (temp file if None) |
return_xr |
bool |
True |
Return xarray.Dataset for data commands |
debug |
bool |
False |
Print detailed execution info |
check_files |
bool |
True |
Validate input files exist |
Returns:
- Text commands:
str - Data commands:
tuple[xr.Dataset, str]ortuple[None, str]
Raises:
CDOError: CDO command failedFileNotFoundError: CDO not installed or input file missing
Configuration
Environment Variables
The wrapper uses the system CDO installation. You can configure CDO behavior with standard environment variables:
# Set CDO temp directory
export CDO_TMPDIR=/path/to/tmp
# Set number of OpenMP threads
export OMP_NUM_THREADS=4
Comparison with Other Libraries
| Feature | python-cdo-wrapper | python-cdo | cdo-bindings |
|---|---|---|---|
| Simple API | ✅ Single function | ❌ Object-oriented | ❌ Complex |
| Auto text detection | ✅ | ❌ | ❌ |
| xarray integration | ✅ Native | ⚠️ Manual | ⚠️ Manual |
| Temp file cleanup | ✅ Automatic | ⚠️ Manual | ⚠️ Manual |
| Type hints | ✅ Full | ❌ | ❌ |
| Dependencies | Minimal | Heavy | Heavy |
Development
Setup
# Clone the repository
git clone https://github.com/NarenKarthikBM/python-cdo-wrapper.git
cd python-cdo-wrapper
# Install with dev dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=python_cdo_wrapper
# Run only unit tests (no CDO required)
pytest -m "not integration"
# Run integration tests (requires CDO)
pytest -m integration
Code Quality
# Format code
ruff format .
# Lint code
ruff check .
# Type check
mypy python_cdo_wrapper
Building
# Build package
hatch build
# Check package
twine check dist/*
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- CDO (Climate Data Operators) by MPI-M
- xarray for N-dimensional labeled arrays
- Climate research community for feedback and testing
Citation
If you use this package in your research, please consider citing:
@software{python_cdo_wrapper,
title = {Python CDO Wrapper},
author = {Your Name},
year = {2024},
url = {https://github.com/NarenKarthikBM/python-cdo-wrapper},
}
Changelog
See CHANGELOG.md for a list of changes.
Made with ❤️ for the climate science community
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_cdo_wrapper-0.1.0.tar.gz.
File metadata
- Download URL: python_cdo_wrapper-0.1.0.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5426b557ebbca19e916f0a2d2827978d8aad93bfd82cdaa3903c0249239c61c
|
|
| MD5 |
f7bdbca57a8c02eb0bd6c1b933ec11fb
|
|
| BLAKE2b-256 |
ac461d3f2370cf68eae83d9317368cea5d035bce071b9352616a074fe1322bfa
|
Provenance
The following attestation bundles were made for python_cdo_wrapper-0.1.0.tar.gz:
Publisher:
publish.yml on NarenKarthikBM/python-cdo-wrapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_cdo_wrapper-0.1.0.tar.gz -
Subject digest:
a5426b557ebbca19e916f0a2d2827978d8aad93bfd82cdaa3903c0249239c61c - Sigstore transparency entry: 743547782
- Sigstore integration time:
-
Permalink:
NarenKarthikBM/python-cdo-wrapper@179ec295683a6dcac0c5e187a48e737b937e5b06 -
Branch / Tag:
refs/tags/V0.1.0 - Owner: https://github.com/NarenKarthikBM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@179ec295683a6dcac0c5e187a48e737b937e5b06 -
Trigger Event:
release
-
Statement type:
File details
Details for the file python_cdo_wrapper-0.1.0-py3-none-any.whl.
File metadata
- Download URL: python_cdo_wrapper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0469efaeaa62f0efc9498a5bce79a6b240bc20401b396c087dfefcfe8277f154
|
|
| MD5 |
128f24997eda54bd0920e7be0fa75019
|
|
| BLAKE2b-256 |
5a4c94b2fc6c30bfaa86bf13a091a76d13a3d5da2144a0a26edd4af6b619e7c3
|
Provenance
The following attestation bundles were made for python_cdo_wrapper-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on NarenKarthikBM/python-cdo-wrapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_cdo_wrapper-0.1.0-py3-none-any.whl -
Subject digest:
0469efaeaa62f0efc9498a5bce79a6b240bc20401b396c087dfefcfe8277f154 - Sigstore transparency entry: 743547800
- Sigstore integration time:
-
Permalink:
NarenKarthikBM/python-cdo-wrapper@179ec295683a6dcac0c5e187a48e737b937e5b06 -
Branch / Tag:
refs/tags/V0.1.0 - Owner: https://github.com/NarenKarthikBM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@179ec295683a6dcac0c5e187a48e737b937e5b06 -
Trigger Event:
release
-
Statement type: