xbudget.collect.collect_budgets¶
- xbudget.collect.collect_budgets(data, recipe, allow_rechunk=True, on_missing='warn')[source]¶
Materialize every budget term described by
recipeintodata.The recipe dict is parsed into a typed expression tree (
xbudget.nodes) and evaluated (xbudget.evaluate.evaluate_budgets()). It does not mutaterecipe; it only adds derived variables to the dataset. Each derived variable is named by its term path with operator infixes dropped (e.g.heat_rhs_diffusion_lateral) and carriesxbudget_path,xbudget_op, andprovenanceattributes. Query the result withxbudget.query.BudgetQuery.- Parameters:
data (xgcm.Grid or xr.Dataset) – Budget diagnostics to read from and write derived variables into, modified in place. A grid is required if the recipe uses
differenceorlateral_divergenceoperations.recipe (dict) – A recipe in xbudget format (e.g. from
load_preset_budget).allow_rechunk (bool (default: True)) – Whether to temporarily rechunk when taking differences along a dimension, e.g. to compute flux divergences on center from fluxes on outer or tendencies on center from snapshots on outer.
on_missing ({"warn", "raise", "ignore"} (default: "warn")) – How to handle a required diagnostic that a term references but that is absent from
data."warn"(default) skips the term and emits one end-of-run summaryUserWarningnaming the missing diagnostics and the now-incomplete terms;"raise"raisesMissingDiagnosticErrorso a recipe/dataset mismatch fails loudly;"ignore"skips silently. In every case the affected variables are stamped withxbudget_incomplete/xbudget_missingattributes (query them withxbudget.BudgetQuery.missing()), and a term declaredoptionalin the recipe is exempt — its absence is expected and never alarms.
- Returns:
data – The same object passed in, for convenience.
- Return type:
xgcm.Grid or xr.Dataset
Examples
Load a preset recipe, collect its budgets into a grid, and query the result:
>>> recipe = xbudget.load_preset_budget(model="MOM6") >>> xbudget.collect_budgets(grid, recipe) # adds derived vars in place >>> q = xbudget.BudgetQuery(grid, recipe) >>> q.var("heat_rhs") # the closed RHS heat tendency 'heat_rhs' >>> grid._ds["heat_rhs"].attrs["xbudget_path"] ['heat', 'rhs']