|
| 1 | +""" |
| 2 | +construct the calliope config yaml file from the settings.yml file and the technology definition excel file. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import division |
| 6 | +from __future__ import print_function |
| 7 | +import cea.config |
| 8 | +import cea.inputlocator |
| 9 | +import cea.plugin |
| 10 | +import os |
| 11 | +import yaml |
| 12 | +from cea_energy_hub_optimizer.tech_from_excel import * |
| 13 | + |
| 14 | + |
| 15 | +__author__ = "Yiqiao Wang" |
| 16 | +__copyright__ = "Copyright 2024, Yiqiao Wang" |
| 17 | +__credits__ = ["Yiqiao Wang"] |
| 18 | +__license__ = "MIT" |
| 19 | +__version__ = "1.0.0" |
| 20 | +__maintainer__ = "Yiqiao Wang" |
| 21 | + |
| 22 | +__status__ = "Production" |
| 23 | + |
| 24 | + |
| 25 | +class CalliopeConfigConstructor(cea.plugin.CeaPlugin): |
| 26 | + """ |
| 27 | + Define the plugin class - unless you want to customize the behavior, you only really need to declare the class. The |
| 28 | + rest of the information will be picked up from ``default.config``, ``schemas.yml`` and ``scripts.yml`` by default. |
| 29 | + """ |
| 30 | + |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +def main(config: cea.config.Configuration) -> None: |
| 35 | + # read yaml file from /data/settings.yml and create a nested dictionary |
| 36 | + # my_config = MyConfig(config) |
| 37 | + with open( |
| 38 | + os.path.join(os.path.dirname(__file__), "data", "settings.yml"), "r" |
| 39 | + ) as file: |
| 40 | + calliope_config: dict = yaml.safe_load(file) |
| 41 | + if config.calliope_config_constructor.technology_excel_file == "": |
| 42 | + tech_excel_path = os.path.join( |
| 43 | + os.path.dirname(__file__), "data", "example_techDefinition.xlsx" |
| 44 | + ) |
| 45 | + else: |
| 46 | + # check if the path belongs to a .xlsx file |
| 47 | + if config.calliope_config_constructor.technology_excel_file[-5:] != ".xlsx": |
| 48 | + raise ValueError( |
| 49 | + "The technology definition file must be a .xlsx file. Please provide a valid .xlsx file." |
| 50 | + ) |
| 51 | + tech_excel_path = config.calliope_config_constructor.technology_excel_file |
| 52 | + techs_subdict = read_tech_definition(filepath=tech_excel_path) |
| 53 | + calliope_config["techs"].update(techs_subdict) |
| 54 | + |
| 55 | + store_path = config.calliope_config_constructor.yaml_storage_path |
| 56 | + if store_path == "": |
| 57 | + store_path = os.path.join(os.path.dirname(__file__), "data") |
| 58 | + if not os.path.isdir(store_path): |
| 59 | + raise ValueError("Please provide a valid folder path.") |
| 60 | + yaml_file_name = config.calliope_config_constructor.yaml_file_name |
| 61 | + if yaml_file_name == "": |
| 62 | + yaml_file_name = "energy_hub_config" |
| 63 | + yaml_file_name += ".yml" |
| 64 | + yaml_file_path = os.path.join(store_path, yaml_file_name) |
| 65 | + with open(yaml_file_path, "w") as file: |
| 66 | + yaml.dump(calliope_config, file) |
| 67 | + |
| 68 | + print(f"Yaml file is saved at {yaml_file_path}.") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main(cea.config.Configuration()) |
0 commit comments