xbudget.presets.save_yaml

xbudget.presets.save_yaml(recipe, filepath)[source]

Write a recipe to a yaml file, after checking that it is valid.

Validating here means a malformed recipe cannot reach disk: you get a xbudget.BudgetParseError naming the offending path while you are authoring it, rather than a confusing failure the next time it is loaded.

Key order is preserved rather than sorted, because it is meaningful: the operands of a sum/product are reported in recipe order by xbudget.BudgetQuery.get_vars().

Parameters:
  • recipe (dict) – A recipe in xbudget format.

  • filepath (str) – Path to write to.

Raises:

xbudget.BudgetParseError – If the recipe does not match the schema. Nothing is written.

Examples

>>> recipe = {"heat": {"rhs": {"sum": {"forcing": {"var": "surface_heat_flux"}}}}}
>>> xbudget.save_yaml(recipe, "my_model.yaml")
>>> xbudget.load_yaml("my_model.yaml") == recipe
True

See also

load_yaml, xbudget.parse_budgets