Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

API Reference

Core Features

load_dataset

load_dataset(path, shift_time=False, comp=None, hstr=None, grid=None, vn=None, **kws)

Load a netCDF file and form a xarray.Dataset

Parameters


open_dataset

open_dataset(path, shift_time=False, comp=None, hstr=None, grid=None, vn=None, **kws)

Open a netCDF file and form a xarray.Dataset with a lazy load mode

Parameters


open_mfdataset

open_mfdataset(paths, shift_time=False, comp=None, hstr=None, grid=None, vn=None, **kws)

Open multiple netCDF files and form a xarray.Dataset in a lazy load mode

Parameters


XDataset

regrid

regrid(dlon=1, dlat=1, weight_file=None, gs='T', method='bilinear', periodic=True)

Regrid the CESM output to a normal lat/lon grid

Supported atmosphere regridding: ne16np4, ne16pg3, ne30np4, ne30pg3, ne120np4, ne120pg4 TO 1x1d / 2x2d. Supported ocean regridding: any grid similar to g16 TO 1x1d / 2x2d. For any other regridding, weight_file must be provided by the user.

For the atmosphere grid regridding, the default method is area-weighted; while for the ocean grid, the default is bilinear.

Parameters


get_plev

get_plev(ps, vn=None, lev_mode='hybrid', **kws)

Interpolate a hybrid-level field to pressure levels and return a Dataset.

This method converts a 3D atmospheric variable that is on hybrid model levels (a/k/a k-levels) into pressure levels using the provided surface pressure ps (either an xarray.DataArray or an xarray.Dataset that contains a variable named “PS”). It wraps geocat.comp.interpolation.interp_hybrid_to_pressure and returns a copy of the original Dataset with the requested variable replaced by its pressure-level version.

Parameters

Returns

Notes


zavg

zavg(depth_top, depth_bot, vn=None)

Vertically average an ocean/column field between two depths and return a Dataset.

The method selects the vertical range along the z_t coordinate from depth_top to depth_bot, applies area/volume weights provided by the dataset variable dz, computes the weighted mean over the vertical dimension, and returns a copy of the original Dataset with the specified variable replaced by its vertically averaged version.

Parameters

Returns

Notes


annualize

annualize(months=None, days_weighted=False, time2year=False)

Annualize/seasonalize a xarray.Dataset

Parameters


to_netcdf

to_netcdf(path, **kws)

Write to netCDF, dropping the non-serializable x4c grid attrs

The grid attrs (gw/lat/lon/dz) are stripped from a copy, so this Dataset keeps them and stays usable by the accessors afterwards.


XDataArray

annualize

annualize(months=None, days_weighted=False)

Annualize/seasonalize a xarray.DataArray

Parameters


regrid

regrid(*args, **kws)

Regrid this DataArray by delegating to the parent Dataset regrid.

Positional arguments are forwarded too, so da.x.regrid(1, 1) works the same as ds.x.regrid(1, 1). Keyword-only used to be the signature here, which made the documented spell form |regrid(1,1) fail on the DataArray path -- the one Timeseries.calc actually uses.

This wraps XDataset.regrid by converting the DataArray to a temporary Dataset, calling the dataset-level regrid helper, then extracting and returning the regridded DataArray. Any dataset-level lat/lon attributes added during the transformation are removed from the returned DataArray attributes for cleanliness.

Forwarded kwargs are the same as XDataset.regrid (e.g., dlon, dlat, weight_file, gs, method, periodic).


get_plev

get_plev(**kws)

See: https://geocat-comp.readthedocs.io/en/v2024.04.0/user_api/generated/geocat.comp.interpolation.interp_hybrid_to_pressure.html


to_netcdf

to_netcdf(path, **kws)

Write to netCDF, dropping the non-serializable x4c grid attrs

The grid attrs (gw/lat/lon/dz) are stripped from a copy, so this DataArray keeps them and stays usable by the accessors afterwards.


nearest2d

nearest2d(lat=None, lon=None, lat_coord='lat', lon_coord='lon', lat_dim='lat', lon_dim='lon')

Select the nearest non-NaN grid point(s) for the given lat/lon targets.

Given one or more target lat/lon pairs, this method finds the nearest valid (non-NaN across non-spatial dims) grid cell in the DataArray and returns a concatenated DataArray with a new dimension site indexing the selected points.

Parameters

Returns


nearest3d

nearest3d(lat=None, lon=None, depth=None, lat_coord='lat', lon_coord='lon', lat_dim='lat', lon_dim='lon', depth_coord='z_t', depth_dim='z_t', depth_unit='cm')

Select the nearest non-NaN 3D grid cell(s) for the given lat/lon/depth targets.

Given one or more target lat/lon/depth triples, this method finds the nearest valid (non-NaN across non-spatial dims) grid cell in the full 3D (depth, lat, lon) domain of the DataArray and returns a concatenated DataArray with a new site dimension indexing the selected points.

Distance is Euclidean in km, combining horizontal great-circle distance and absolute vertical |dz|. The depth_coord is converted from depth_unit to km, while target depth values are assumed to be in meters.

Parameters

Returns


eof

eof(n=4, weight=True)

Perform EOF analysis

Parameters


geo_mean

geo_mean(ind=None, latlon_range=(-90, 90, 0, 360), **kws)

Calculate the geospatial-weighted (latitude or area) mean over a specified region or climate index.

Parameters

Returns

Raises


plot

plot(title=None, figsize=None, ax=None, latlon_range=None, add_clabels=False, clevels=None, clabel_kwargs=None, projection='Robinson', transform='PlateCarree', central_longitude=180, proj_args=None, bad_color='dimgray', add_gridlines=False, gridline_labels=True, gridline_style='--', ssv=None, log=False, vmin=None, vmax=None, coastline_zorder=99, coastline_width=1, site_markersizes=100, df_sites=None, colname_dict=None, gs='T', ux=False, site_marker_dict=None, site_color_dict=None, count_site_num=False, lgd_kws=None, legend=True, return_im=False, mesh=False, **kws)

The plotting functionality

Parameters


CESM Postprocessing

History

Handle CESM history files for a single case.

Provides utilities to discover history file paths, list time-series variables, split (isolate) variables into separate files, and re-merge them across time ranges. Designed to work with NCO tools and MPI for parallel operations.

get_ts_vns

get_ts_vns(comp, hstr, exclude_vars=[...])

Return list of time-varying variable names for a given component and hstr by inspecting the first history file.


get_paths

get_paths(comp, hstr, timespan=None)

Return history file paths for a component/hstr optionally filtered by a timespan.

timespan may be provided in a variety of formats accepted by utils.parse_timespan.


isolate_vn

isolate_vn(vn, comp, hstr, in_path, output_dirpath, overwrite=True)

Create a new netCDF file containing only variable vn from the input history file in_path.

Uses ncks to drop other variables and writes result to output_dirpath with a standardized filename.


bigbang

bigbang(comp, hstr, output_dirpath, timespan=None, overwrite=True, nproc=1, vns=None)

Split history files into per-variable files in parallel using MPI.

Each MPI rank handles a subset of (file,variable) tasks.


get_hstr_based_on_vn

get_hstr_based_on_vn(vn)

Return the first hstr that contains variable vn.

This searches across all components and hstrs and returns the matching hstr string or None if not found.


merge_vn

merge_vn(hstr, vn, input_dirpath, output_dirpath, timespan=None, overwrite=True, compression=1)

Concatenate per-variable files across time into a single file.

Uses ncrcat with optional compression level to produce an aggregated timeseries file for vn and hstr.


bigcrunch

bigcrunch(comp, hstr, input_dirpath, output_dirpath, timespan=None, overwrite=True, nproc=1, compression=1, vns=None)

Merge per-variable files back into timeseries files in parallel.

Coordinates work across MPI ranks similar to bigbang.


gen_ts

gen_ts(output_dirpath, staging_dirpath=None, comps=['atm', 'ocn', 'lnd', 'ice', 'rof'], timespan=None, timestep=None, timestep_unit='year', dir_structure='comp/proc/tseries/hstr', overwrite=True, nproc=1, compression=1)

Generate timeseries files for selected components and timespans.

This orchestrates splitting (bigbang) and merging (bigcrunch) stages and moves results from staging to final output directories.


find_timespan_files

find_timespan_files(timespan, comps=['atm', 'ice', 'ocn', 'rof', 'lnd'])

List the history files within a timespan, without touching them

Resolves the glob in Python rather than through a shell, so no part of root_dir is ever interpreted as shell syntax.

Parameters

Returns


rm_timespan

rm_timespan(timespan, comps=['atm', 'ice', 'ocn', 'rof', 'lnd'], nworkers=None, rehearsal=True)

Delete the archived history files within a timespan

This is the one destructive operation in x4c, so it is deliberately conservative:

Parameters

Returns


CESM Diagnostics

Timeseries

CESM Timeseries case helper.

Manages discovery and loading of preprocessed CESM timeseries files produced by CESM postprocessing. Provides convenience methods to locate paths, load raw or derived diagnostics, compute spells, and create plots and seasonal means.

get_paths

get_paths(comp, hstr, vn, timespan=None)

Return list of timeseries file paths for vn under comp/hstr.

If timespan is provided it filters the returned paths to those fully covering the requested interval.


get_comp_hstr

get_comp_hstr(vn)

Find all (component, hstr) pairs where vn is present.


load

load(vn, vtype=None, comp=None, hstr=None, timespan=None, load_idx=-1, verbose=True, reload=False, **kws)

Load a variable or derived diagnostic into self.ds.

Automatically detects whether vn is a raw timeseries or a derived diagnostic and loads or computes it. Results are stored in self.ds[vn].


calc

calc(spell, comp=None, timespan=None, load_idx=-1, recalculate=False, verbose=True, **kws)

Compute a diagnostic spell and cache the result.

The spell string controls regridding, slicing, spatial/vertical averaging and other modifiers parsed by Spell. The final xarray DataArray is stored in self.diags[spell].


plot

plot(spell, t_idx=None, regrid=False, gs='T', ssv='SSH', recalculate_ssv=False, timespan=None, **kws)

Plot a computed diagnostic spell.

Detects plot type (map, ts, zm, yz) from the DataArray and dispatches to the plotting helpers in diags/visual.


quickview

quickview(timespan=None, nrow=None, ncol=None, wspace=0.3, hspace=0.5, ax_loc=None, figsize=None, stat_period=-50, roll_int=50, ylim_dict=None, spells=None, recalculate=False)

Create a multi-panel overview figure for a selection of spells.

Returns (fig, ax) where ax is a dict of axes keyed by spell keys.


get_ts

get_ts(vn, comp=None, hstr=None, timespan=None, slicing=False, regrid=False, dlat=1, dlon=1)

Open and return a Dataset for vn, without caching it in self.ds.

Applies optional slicing and regridding before returning the Dataset.

Parameters


save_means

save_means(vn, comp=None, output_dirpath=None, timespan=None, hstr=None, slicing=False, regrid=False, dlat=1, dlon=1, overwrite=False)

Save seasonal and annual mean files for vn into output_dirpath.

Writes files for ANN, DJF, MAM, JJA and SON for the given timespan and optionally regrids results.


clear_ds

clear_ds(vn=None)

Clear the existing .ds property


copy

copy()

Return a deep copy of this Timeseries instance.


Logs

Manage CESM log files for a case and extract time series variables.

This helper locates compressed component log files (e.g. ocn.log.*.gz), parses monthly reported diagnostics, and provides plotting helpers for inspected variables.

get_vars

get_vars(vn=[...])

Parse log files and extract listed variables into a DataFrame.

Parameters

Side-effects

Sets self.df, self.df_ann and self.vn with parsed results.


plot_vars

plot_vars(vn=None, annualize=True, xlim=None, ylim_dict=None, unit_dict=None, clr_dict=None, figsize=[20, 5], ncol=4, nrow=None, wspace=0.5, hspace=0.5, kws=None, title=None)

Plot one or more variables parsed from the logs.

Parameters

Returns


compare_vars

compare_vars(L_ref, vn=None, annualize=True, xlim=None, unit_dict=None, clr_dict=None, figsize=[20, 5], ncol=4, nrow=None, wspace=0.3, hspace=0.5, kws=None, title=None)

Overlay variables from a reference Logs instance for comparison.

Parameters


The Spell Mini-Language

Spell

The Spell System

A “spell” is a string that summarizes a series of data processing steps. A basic sentence should be in the form: “vn:ann_method:sa_method”, where

One may also add more operations after the vn part:

An optional "alias ~ " prefix renames the result.

Examples::

'TS:ann:gm'
'GMSST ~ SST:ann:gm'
'TEMP.isel(z_t=0):ann:gm'
'T|regrid(1,1)|plev(500):climo'
'TEMP|zavg(0,1000):ann:gm'
'SST.sel(lat=slice(-5,5)):ann'

Arguments are parsed into structured form (regrid_args, slicing_kwargs, plev_levels, ...) so callers never have to eval the string. The raw fragments remain available as slicing, regrid, plev and zavg for display. Note : is the field separator and cannot appear inside arguments.

Attributes

parse_sentence

parse_sentence()

Split the vn[:ann_method[:sa_method]] skeleton


parse_slicing

parse_slicing()

Extract a leading .isel(...) / .sel(...) on the variable


Derived-Variable Registry

F

F(func=None, name=None)

Decorator to register a diagnostic function, with optional custom key.


Registry

get_F

get_F(cls, name)

Retrieve a diagnostic function by name.


Visualization Helpers

set_style

set_style(style='journal', font_scale=1.0)

Modify the visualization style

This function is inspired by Seaborn. See a demo in the example_notebooks folder on GitHub to look at the different styles

Parameters


subplots

subplots(nrow, ncol, ax_loc, projs=None, projs_kws=None, figsize=None, wspace=None, hspace=None, annotation=False, annotation_kws=None, annotation_separate=False, annotation_skip=None)

Create a named grid of axes, with optional Cartopy projections

A thin wrapper over matplotlib.gridspec.GridSpec that returns the axes in a dict keyed by name instead of an array, so that a subplot can be referred to as ax['ts'] rather than by position. Any subset of the axes can be given a Cartopy projection, which makes mixed layouts (maps next to timeseries) straightforward.

Parameters

Returns


savefig

savefig(fig, path, verbose=True, **kws)

Save a figure to a path

Parameters


showfig

showfig(fig, close=True)

Show the figure

Parameters


closefig

closefig(fig=None)

Show the figure

Parameters


add_annotation

add_annotation(ax, fs=20, loc_x=-0.15, loc_y=1.03, start=0, style=None)

Label axes with (a), (b), ... in order

Parameters


infer_cmap

infer_cmap(da)

Guess a sensible colormap from a DataArray’s long_name

Matches keywords against da.attrs['long_name'], lowercased, and returns the colormap for the first keyword that matches -- so the order below is a precedence order, e.g. “sea ice temperature” resolves to the temperature map, not the ice one. Diverging maps are used for fields that are naturally read about a center value (temperature, pressure, precipitation, correlation) and sequential ones otherwise.

============= ========== keyword colormap ============= ========== temperature RdBu_r pressure bwr_r precipitation BrBG correlation RdBu_r r2 Reds salinity PiYG circulation RdBu_r depth GnBu height PiYG kmt BrBG ice Blues ============= ==========

Parameters

Returns


Utilities

fetch_sample_data

fetch_sample_data(case=DEFAULT_SAMPLE_CASE, url=None, sha256=None, verbose=True, force=False)

Return a local path to a tutorial sample case, downloading it if needed

The tutorial notebooks run against a reduced copy of a real CESM case. It lives in the cache directory (see cache_dir), not in the repository.

Resolution order:

  1. $X4C_SAMPLE_DIR, if set and it contains the case -- use this to point at a copy you already have, e.g. on a shared filesystem.

  2. the cache directory, if the case is already extracted there

  3. otherwise download the archive and extract it

Parameters

Returns

[(<DocstringSectionKind.examples: ‘examples’>, “>>> import x4c\n>>> case_dir = x4c.fetch_sample_data(case=‘cesm1’)”)]


cache_dir

cache_dir()

The directory x4c downloads regrid weight files into

Resolution order:

  1. $X4C_CACHE_DIR, if set

  2. $XDG_CACHE_HOME/x4c, if set

  3. ~/.cache/x4c

Deliberately not the installed package directory: that fails outright on a read-only or shared site-packages, and in an editable install it drops multi-MB binaries into the source tree.