Exemplo n.º 1
0
def generate_model_run(
    config,
    timeseries_dataframes,
    debug_comments,
    applied_overrides,
    scenario,
    subsets,
):
    """
    Returns a processed model_run configuration AttrDict and a debug
    YAML object with comments attached, ready to write to disk.

    Parameters
    ----------
    config : AttrDict
    timeseries_dataframes : dict
    debug_comments : AttrDict
    scenario : str
    """
    model_run = AttrDict()
    model_run["scenario"] = scenario
    model_run["applied_overrides"] = ";".join(applied_overrides)

    # 1) Initial checks on model configuration
    warning_messages, errors = checks.check_initial(config)
    exceptions.print_warnings_and_raise_errors(warnings=warning_messages,
                                               errors=errors)

    # 2) Fully populate techs
    # Raises ModelError if necessary
    model_run["techs"], debug_techs, errors = process_techs(config)
    debug_comments.set_key("model_run.techs", debug_techs)
    exceptions.print_warnings_and_raise_errors(errors=errors)

    # 3) Fully populate tech_groups
    model_run["tech_groups"] = process_tech_groups(config, model_run["techs"])

    # 4) Fully populate nodes
    (
        model_run["nodes"],
        debug_nodes,
        warning_messages,
        errors,
    ) = nodes.process_nodes(config, model_run["techs"])
    debug_comments.set_key("model_run.nodes", debug_nodes)
    exceptions.print_warnings_and_raise_errors(warnings=warning_messages,
                                               errors=errors)

    # 5) Fully populate timeseries data
    # Raises ModelErrors if there are problems with timeseries data at this stage
    (
        model_run["timeseries_data"],
        model_run["timeseries_vars"],
    ) = process_timeseries_data(config, model_run, timeseries_dataframes)

    # 6) Grab additional relevant bits from run and model config
    model_run["run"] = config["run"]
    model_run["model"] = config["model"]

    # model_run["sets"] = all_sets
    model_run["subsets"] = subsets
    # model_run["constraint_sets"] = constraint_sets.generate_constraint_sets(model_run)

    # 8) Final sense-checking
    final_check_comments, warning_messages, errors = checks.check_final(
        model_run)
    debug_comments.union(final_check_comments)
    exceptions.print_warnings_and_raise_errors(warnings=warning_messages,
                                               errors=errors)

    # 9) Build a debug data dict with comments and the original configs
    debug_data = AttrDict({
        "comments": debug_comments,
        "config_initial": config,
    })

    return model_run, debug_data
Exemplo n.º 2
0
def generate_model_run(config, timeseries_dataframes, debug_comments,
                       applied_overrides, scenario):
    """
    Returns a processed model_run configuration AttrDict and a debug
    YAML object with comments attached, ready to write to disk.

    Parameters
    ----------
    config : AttrDict
    timeseries_dataframes : dict
    debug_comments : AttrDict
    scenario : str
    """
    model_run = AttrDict()
    model_run["scenario"] = scenario
    model_run["applied_overrides"] = ";".join(applied_overrides)

    # 1) Initial checks on model configuration
    warning_messages, errors = checks.check_initial(config)
    exceptions.print_warnings_and_raise_errors(warnings=warning_messages,
                                               errors=errors)

    # 2) Fully populate techs
    # Raises ModelError if necessary
    model_run["techs"], debug_techs, errors = process_techs(config)
    debug_comments.set_key("model_run.techs", debug_techs)
    exceptions.print_warnings_and_raise_errors(errors=errors)

    # 3) Fully populate tech_groups
    model_run["tech_groups"] = process_tech_groups(config, model_run["techs"])

    # 4) Fully populate locations
    (
        model_run["locations"],
        debug_locs,
        warning_messages,
        errors,
    ) = locations.process_locations(config, model_run["techs"])
    debug_comments.set_key("model_run.locations", debug_locs)
    exceptions.print_warnings_and_raise_errors(warnings=warning_messages,
                                               errors=errors)

    # 5) Fully populate timeseries data
    # Raises ModelErrors if there are problems with timeseries data at this stage
    model_run["timeseries_data"], model_run[
        "timesteps"] = process_timeseries_data(config, model_run,
                                               timeseries_dataframes)

    # 6) Grab additional relevant bits from run and model config
    model_run["run"] = config["run"]
    model_run["model"] = config["model"]
    model_run["group_constraints"] = config.get("group_constraints", {})

    # 7) Initialize sets
    all_sets = sets.generate_simple_sets(model_run)
    all_sets.union(sets.generate_loc_tech_sets(model_run, all_sets))
    all_sets = AttrDict({k: list(v) for k, v in all_sets.items()})
    model_run["sets"] = all_sets
    model_run["constraint_sets"] = constraint_sets.generate_constraint_sets(
        model_run)

    # 7.5) get scaling factors if available
    if "scale" in config:
        model_run['scale'] = config['scale']

    # 8) Final sense-checking
    final_check_comments, warning_messages, errors = checks.check_final(
        model_run)
    debug_comments.union(final_check_comments)
    exceptions.print_warnings_and_raise_errors(warnings=warning_messages,
                                               errors=errors)

    # 9) Build a debug data dict with comments and the original configs
    debug_data = AttrDict({
        "comments": debug_comments,
        "config_initial": config,
    })

    return model_run, debug_data