pycoupler.LpjmlConfig#

class pycoupler.LpjmlConfig(sub_config)[source]#

Bases: SubConfig

LPJmL config class for easy access, conversion to a dictionary, or writing as a JSON file. Provides methods to get/set outputs, restarts, and sockets for model coupling.

Parameters:

sub_config (SubConfig) – SubConfig instance (ideally built from an LPJmL config dictionary) used to build up a nested LpjmlConfig class with corresponding fields.

get_output_avail(id_only=True, to_dict=False)[source]#

Get available output (outputvar) names or objects.

Parameters:
  • id_only (bool, default True) – If True, only output IDs are returned. If False, the whole outputvar objects are returned.

  • to_dict (bool, default False) – If True, a dictionary is returned. If False, a list with the config objects of the outputvar.

Returns:

List of output IDs (if id_only=True and to_dict=False), dict of output objects (if to_dict=True), or list of outputvar objects (if id_only=False and to_dict=False).

Return type:

list or dict

get_output(id_only=True, to_dict=False, fmt=None)[source]#

Get defined output IDs or objects.

Parameters:
  • id_only (bool, default True) – If True, only output IDs are returned. If False, the whole output objects are returned.

  • to_dict (bool, default False) – If True, a dictionary is returned. If False, a list with the config objects of the output.

  • fmt (str, optional) – If specified, only outputs with the defined file format are returned.

Returns:

List of output IDs (if id_only=True and to_dict=False), dict of output objects (if to_dict=True), or list of output objects (if id_only=False and to_dict=False).

Return type:

list or dict

set_spinup(sim_path, sim_name='spinup')[source]#

Set configuration required for spinup model runs.

Parameters:
  • sim_path (str) – Path where simulation data is written to.

  • sim_name (str, default “spinup”) – Name of the simulation.

set_transient(sim_path, start_year, end_year, sim_name='transient', dependency=None, temporal_resolution='annual', write_output=[], write_file_format='cdf', append_output=True)[source]#

Set configuration required for historic model runs.

Parameters:
  • sim_path (str) – Path where simulation data is written to.

  • start_year (int) – Start year of simulation.

  • end_year (int) – End year of simulation.

  • sim_name (str, default “transient”) – Name of the simulation.

  • dependency (str, optional) – Name of simulation to depend on (e.g., spinup run).

  • temporal_resolution (str or dict, default “annual”) – Temporal resolution for outputs. Can be a dict of temporal resolutions corresponding to outputs or a str to set the same resolution for all outputs. Choose between “annual”, “monthly”, “daily”.

  • write_output (list, default []) – Output IDs to be written by LPJmL. Check available outputs with get_output_avail().

  • write_file_format (str, default “cdf”) – File format of output files. Choose between “raw”, “clm”, and “cdf”.

  • append_output (bool, default True) – If True, defined output entries are appended. If False, existing outputs are overwritten.

set_coupled(sim_path, start_year, end_year, coupled_input, coupled_output, sim_name='coupled', dependency=None, coupled_year=None, temporal_resolution='annual', write_output=[], write_file_format='cdf', append_output=True, model_name='copan:CORE')[source]#

Set configuration required for coupled model runs.

Parameters:
  • sim_path (str) – Path where simulation data is written to.

  • start_year (int) – Start year of simulation.

  • end_year (int) – End year of simulation.

  • coupled_input (list) – List of inputs to be used as socket for coupling. Provide dictionary/json key as identifier.

  • coupled_output (list) – List of outputs to be used as socket for coupling. Provide output ID as identifier.

  • sim_name (str, default “coupled”) – Name of the simulation.

  • dependency (str, optional) – Name of simulation to depend on (e.g., transient run).

  • coupled_year (int, optional) – Start year of coupled simulation. If None, uses start_year.

  • temporal_resolution (str or dict, default “annual”) – Temporal resolution for outputs. Can be a dict of temporal resolutions corresponding to outputs or a str to set the same resolution for all outputs. Choose between “annual”, “monthly”, “daily”.

  • write_output (list, default []) – Output IDs to be written by LPJmL. Check available outputs with get_output_avail().

  • write_file_format (str, default “cdf”) – File format of output files. Choose between “raw”, “clm”, and “cdf”.

  • append_output (bool, default True) – If True, defined output entries are appended. If False, existing outputs are overwritten.

  • model_name (str, default “copan:CORE”) – Name of the coupled model.

get_input_sockets(id_only=False)[source]#

Get defined socket inputs as dict

Parameters:

id_only (bool, default False) – If True, only input IDs are returned. If False, all input information is returned.

get_output_sockets(id_only=False)[source]#

Get defined socket outputs as dict

Parameters:

id_only (bool, default False) – If True, only output IDs are returned. If False, all output information is returned.

add_config(file_name)[source]#

Add config file of coupled model to LPJmL config

Parameters:

file_name (str) – Path to coupled config file

regrid(sim_path, model_path=None, country_code='BEL', overwrite=False)[source]#

Regrid LPJmL configuration file to a new country.

Parameters:
  • sim_path (str) – Directory to check whether required subfolders exist. If not, create corresponding folders (input, output, restart).

  • model_path (str, optional) – Path to LPJmL_internal (lpjml repository). If None, uses self.model_path.

  • country_code (str, default “BEL”) – Country code of country to regrid to.

  • overwrite (bool, default False) – Whether to overwrite existing country-specific input files.

Raises:

OSError – If sim_path or model_path do not exist, or if required grid files are missing.

convert_cdf_to_raw(output_id=None)[source]#

Convert netcdf files to raw (binary) files. :param output_id: list with ids of outputs to convert from netcdf to

raw

to_dict()#

Convert class object to dictionary.

Returns:

Dictionary representation of the class object.

Return type:

dict

to_json(file_name=None)#

Write the configuration to a JSON file.

Parameters:

file_name (str, optional) – File name (including relative/absolute path) to write JSON to. If None, a default name is used.

Returns:

File name of the written JSON file.

Return type:

str