xgcm
General Circulation Model Postprocessing with xarray
xgcm is a python packge for working with the datasets produced by numerical General Circulation Models (GCMs) and similar gridded datasets that are amenable to finite volume analysis. In these datasets, different variables are located at different positions with respect to a volume or area element (e.g. cell center, cell face, etc.) xgcm solves the problem of how to interpolate and difference these variables from one position to another.
https://github.com/xgcm/xgcm
Official Documents
xgcm: General Circulation Model Postprocessing with xarray — xgcm 0.2.0+44.gcac66af documentation
https://xgcm.readthedocs.io/en/latest/
MITgcm Example
https://xgcm.readthedocs.io/en/latest/example_mitgcm.html
Tutorials
PyVideo.org · Xgcm: Analyzing General Circulation Models in Python | SciPy 2019 | Ryan Abernathey
https://pyvideo.org/scipy-2019/xgcm-analyzing-general-circulation-models-in-python-scipy-2019-ryan-abernathey.html
Examples
ECCOv4
Vector calculus in ECCO: The Transport, divergence, vorticity and the Barotropic Vorticity Budget
https://ecco-v4-python-tutorial.readthedocs.io/VectorCalculus_ECCO_barotropicVorticity.html
ECCOv4 budgets
https://ecco-v4-python-tutorial.readthedocs.io/ecco_budgets.html
code:python
ds = xr.Dataset({"u":("y_c","x_g",np.ma.masked_array(u,u<-100.)),
"v":("y_g","x_c",np.ma.masked_array(v,v<-100.))},
coords={'x_c': ('x_c',, lonv, {'axis': 'X'}),
'x_g': ('x_g',, lonu,
{'axis': 'X', 'c_grid_axis_shift': -0.5}),
'y_c': ('y_c',, latu, {'axis': 'Y'}),
'y_g': ('y_g',, latv,
{'axis': 'Y', 'c_grid_axis_shift': -0.5})
},
)
grid=xgcm.Grid(ds,periodic=False)
print(grid)
Metric-aware diagnostics on curvilinear grids (xgcm integration)
For C-grid ocean models (e.g., MOM, NEMO, MITgcm), using raw differences can produce physically inconsistent gradients. Pair xarray with grid-aware operators.
code:python
import xgcm
grid = xgcm.Grid(ds, metrics={
("X",): "dx_t",
("Y",): "dy_t",
("X", "Y"): "area_t"
})
# Relative vorticity ζ = dv/dx - du/dy
zeta = grid.derivative(ds"v", "X") - grid.derivative(ds"u", "Y")
Why it’s new in practice: more workflows now keep metric tensors as first-class variables, enabling consistent energetics and vorticity budgets without regridding.
Combine xarray with xgcm to run grid-aware (e.g., C-grid) flux-form diagnostics safely
By delegating staggered-grid bookkeeping and metric terms (dx, dy, dz, areas) to xgcm.Grid, you can compute vorticity/divergence/transports more robustly and with fewer silent sign/offset bugs.
code:python
import xarray as xr
from xgcm import Grid
ds = ds.chunk({"time": 30, "y": 800, "x": 800})
grid = Grid(
ds,
coords={
"X": {"center": "x", "left": "x_u"},
"Y": {"center": "y", "left": "y_v"},
"Z": {"center": "z", "left": "z_w"},
},
metrics={
("X",): "dx_t",
("Y",): "dy_t",
("Z",): "dz_t",
("X", "Y"): "area_t",
},
periodic=False,
)
# Example: horizontal divergence (conceptual; adapt names/coords to your dataset)
div = grid.diff(ds"u", "X") / ds"dx_t" + grid.diff(ds"v", "Y") / ds"dy_t"
See also
xmitgcm: python package for reading MITgcm binary MDS files into xarray data structures.