openairclim.gui.config_io

Configuration loading, validation, and saving logic.

Split into two validation stages so the sidebar can build an editable form as soon as a config is structurally sound, without forcing every referenced file to already exist:

  • parse_and_check_structure() — parse TOML, apply aliases, check required keys/types, fill in defaults. A failure here means the config can’t be safely edited (keys may be missing), so the caller should not build a form from it.

  • check_full_config() — run the core’s own full config check (core.read_config.check_config): structure, aircraft/contrail setup, and that every referenced inventory/response file actually exists. This function is run explicitly when a config file is loaded or when the user clicks the “validate” button.

openairclim.gui.config_io.blank_config()[source]

Return a configuration skeleton satisfying the required fields of core.config_model.Config, with everything the model can default itself (responses.*, temperature.*, metrics, parametric.*, inventories.base/rel_to_base, …) filled in by validate_config.

Only holds fields Config has no default for.

Returns:

Blank configuration dictionary with stringified paths.

Return type:

dict

openairclim.gui.config_io.check_full_config(working_dir, config)[source]

Run the core’s own full configuration check on a local config file.

Reuses core.read_config.check_config as-is. Operates on a deep copy, since check_config mutates/returns its input (migrates deprecated keys and merges in defaults in place). The live config being edited in the GUI shouldn’t change as a side effect of validating it.

Parameters:
  • working_dir (str) – Project working directory — paths inside the config are resolved relative to this, so the check runs with the cwd temporarily switched there.

  • config (dict) – Structurally-seeded configuration dictionary (e.g. state.edited_config).

Raises:

Exception – Whatever check_config raises for an invalid config (pydantic.ValidationError, ValueError, KeyError, FileNotFoundError, …).

openairclim.gui.config_io.check_required_fields(edited_config)[source]

Run every card’s required-field check against a config dict.

Lets other modules (the sidebar’s Validate button, run_full_validation below) run the exact same checks shown as ⚠️ icons on the Config tab’s card titles, without needing the cards to actually be rendered.

Parameters:

edited_config (dict) – Working configuration dict.

Returns:

(card_title, status_icon) tuples for cards with a

non-None status, in card order. Empty if nothing’s missing.

Return type:

list

openairclim.gui.config_io.default_repository_dir() Path[source]

OpenAirClim’s shared repository data cache directory. Used as the implicit default for the background and responses section directories when left blank. This is also what core.read_config._resolve_repository_dirs does.

Returns:

The resolved shared cache directory (see

openairclim.repository.get_cache_dir).

Return type:

Path

openairclim.gui.config_io.list_nc_data_vars(filepath)[source]

List data variable names in a NetCDF file.

Used to discover the available scenario names inside a background concentration file (e.g. “SSP2-4.5”), which are stored as data variables rather than declared anywhere in the config.

Parameters:

filepath (Path or str) – Path to the NetCDF file.

Returns:

Sorted list of data variable names. Empty list if the

file doesn’t exist or can’t be opened.

Return type:

list

openairclim.gui.config_io.list_nc_files(directory_path)[source]

List NetCDF filenames in a directory.

Parameters:

directory_path (Path) – Directory to scan.

Returns:

Sorted list of .nc filenames found, or an empty list

if the directory does not exist.

Return type:

list

openairclim.gui.config_io.parse_and_check_structure(working_dir, config_path)[source]

Load a TOML config file and validate its structure (keys/types).

Does not check that referenced files exist. This is done by check_files_exist().

Parameters:
  • working_dir (str) – Project working directory.

  • config_path (str) – Path to the config file, absolute or relative to working_dir.

Returns:

(config dict or None, list of error message strings).

Return type:

tuple

openairclim.gui.config_io.parse_toml_text(text)[source]

Parse and structurally validate a TOML config given as text.

Mirrors parse_and_check_structure(), for config content that hasn’t been saved to a file yet — e.g. hand-edited on the GUI’s “Config (Expert)” text tab.

Parameters:

text (str) – TOML config content.

Returns:

(config dict or None, list of error message strings).

Return type:

tuple

openairclim.gui.config_io.prepare_for_save(config, working_dir)[source]

Return a copy of config with directory paths made relative.

During editing, directory fields are kept as absolute paths so that they remain correct regardless of when the working directory happens to be set. This converts them back to working-dir-relative form, once, right before writing a portable TOML file.

Parameters:
  • config (dict) – Configuration dictionary (absolute dir paths).

  • working_dir (str) – Project working directory.

Returns:

Deep copy of config with directory fields made relative

to working_dir where possible.

Return type:

dict

openairclim.gui.config_io.resolve_dir(working_dir, dir_str)[source]

Resolve a (possibly relative) directory string against working_dir.

Parameters:
  • working_dir (str) – Project working directory.

  • dir_str (str) – Directory path, absolute or relative.

Returns:

Resolved absolute path, with any “.” / “..” segments

collapsed — doesn’t require the path to exist.

Return type:

Path

openairclim.gui.config_io.run_config(working_dir, config_path)[source]

Run OpenAirClim using a saved config file.

All paths inside a saved config (inventory dir, response dir, etc.) are relative to working_dir — exactly like check_files_exist, this temporarily changes into working_dir so they resolve correctly, then restores the original directory regardless of outcome.

Parameters:
  • working_dir (str) – Project working directory.

  • config_path (str) – Path to a saved config TOML file.

openairclim.gui.config_io.run_full_validation(state)[source]

Run the full validation pipeline against state.edited_config.

Parameters:

state (AppState) – Shared application state.

Returns:

(bool valid, str markdown status message).

Return type:

tuple

openairclim.gui.config_io.to_relative(working_dir, absolute_path)[source]

Convert an absolute path to one relative to working_dir.

Uses “..” segments where necessary, so that directories outside working_dir still resolve correctly on another machine or OS, as long as their position relative to working_dir is preserved. Falls back to the absolute path unchanged only when no relative path can be computed at all (e.g. paths on different drives on Windows).

Parameters:
  • working_dir (str) – Project working directory.

  • absolute_path (str) – Absolute path to convert.

Returns:

Relative path (forward-slash separated) if possible,

otherwise the absolute path unchanged.

Return type:

str

openairclim.gui.config_io.to_toml_string(config)[source]

Format a configuration dictionary as TOML text.

Each top-level key becomes a [section] header; nested dicts within a section are flattened to dotted keys (e.g. CO2.file = “…”), matching OpenAirClim’s existing config style.

Parameters:

config (dict) – Configuration dictionary to format.

Returns:

TOML-formatted text.

Return type:

str

openairclim.gui.config_io.write_toml(config, filepath)[source]

Write a configuration dictionary to a TOML file.

Parameters:
  • config (dict) – Configuration dictionary to write.

  • filepath (str or Path) – Destination file path.