|
| 1 | +from pathlib import Path |
| 2 | +import toml |
| 3 | +import click |
| 4 | +import os |
| 5 | + |
| 6 | +from PyDSS.simulation_input_models import MappedControllers |
| 7 | +from PyDSS.pydss_project import PyDssScenario |
| 8 | +from PyDSS.common import CONTROLLER_TYPES, ControllerType |
| 9 | + |
| 10 | +def build_scenario(project_path:str, scenario_name:str, controller_mapping:str): |
| 11 | + project_path = Path(project_path) |
| 12 | + controller_mapping_path = Path(controller_mapping) |
| 13 | + assert project_path.exists(), "Provided project path does not exist" |
| 14 | + assert (project_path / "Scenarios").exists(), "provided project is not a valid PyDSS project" |
| 15 | + assert controller_mapping_path.exists(), "rovided controller_mapping file does not exist" |
| 16 | + assert controller_mapping_path.suffix.lower() == ".toml", "controller_mapping should be a TOML file" |
| 17 | + |
| 18 | + controller_map = toml.load(controller_mapping_path) |
| 19 | + mapped_controllers = MappedControllers(**controller_map) |
| 20 | + acceptable_controller_types = CONTROLLER_TYPES |
| 21 | + controllers = {} |
| 22 | + for controller in mapped_controllers.mapping: |
| 23 | + settings_path_obj = Path(controller.controller_file) |
| 24 | + assert controller.controller_type in ControllerType, \ |
| 25 | + f"{controller.controller_type} is not a valid contoller. Options are {acceptable_controller_types}" |
| 26 | + assert settings_path_obj.exists(), \ |
| 27 | + f"file for controller type {controller.controller_type} does not exist" |
| 28 | + controller_data = toml.load(settings_path_obj) |
| 29 | + if controller_data: |
| 30 | + if controller.controller_type in controllers: |
| 31 | + msg= f"Multiple keys files for the same controller type {controller.controller_type}." \ |
| 32 | + "Each controller type can only be attached to a single file." |
| 33 | + raise ValueError(msg) |
| 34 | + controllers[controller.controller_type] = toml.load(settings_path_obj) |
| 35 | + scenario_dir = project_path / "Scenarios" / scenario_name |
| 36 | + scenario_obj = PyDssScenario( |
| 37 | + [scenario_name], list(controllers.keys()), export_modes=None, visualization_types=None |
| 38 | + ) |
| 39 | + scenario_obj.serialize(str(scenario_dir)) |
| 40 | + |
| 41 | +@click.argument("project-path", type=click.Path(exists=True)) |
| 42 | +@click.option( |
| 43 | + "-s", "--scenario_name", |
| 44 | + required=True, |
| 45 | + help="name of the new scenario", |
| 46 | +) |
| 47 | +@click.option( |
| 48 | + "-c", "--controller-mapping", |
| 49 | + required=True, |
| 50 | + default=None, |
| 51 | + type=click.Path(exists=True), |
| 52 | + help="JSON file that maps controller type to controller definition files", |
| 53 | +) |
| 54 | +@click.command() |
| 55 | +def add_scenario(project_path:str, scenario_name:str, controller_mapping:str): |
| 56 | + """Add a new scenario to an existing project""" |
| 57 | + build_scenario(project_path, scenario_name, controller_mapping) |
| 58 | + |
0 commit comments