def export(project_path, fmt="csv", compress=False, output_dir=None, verbose=False): """Export data from a PyDSS project.""" if not os.path.exists(project_path): sys.exit(1) filename = "pydss_export.log" console_level = logging.INFO file_level = logging.INFO if verbose: console_level = logging.DEBUG file_level = logging.DEBUG setup_logging( "PyDSS", filename=filename, console_level=console_level, file_level=file_level, ) logger.info("CLI: [%s]", get_cli_string()) results = PyDssResults(project_path) for scenario in results.scenarios: scenario.export_data(output_dir, fmt=fmt, compress=compress)
def add_post_process(project_path, scenario_name, script, config_file): """Add post-process script to PyDSS scenario.""" setup_logging("PyDSS", console_level=logging.INFO) project = PyDssProject.load_project(project_path) scenario = project.get_scenario(scenario_name) pp_info = {"script": script, "config_file": config_file} scenario.add_post_process(pp_info) project.serialize()
def extract_element_files(project_path, output_dir=None, verbose=False): """Extract the element info files from an archived PyDSS project.""" if not os.path.exists(project_path): print(f"project-path={project_path} does not exist") sys.exit(1) filename = "pydss_extract.log" console_level = logging.INFO file_level = logging.INFO if verbose: console_level = logging.DEBUG file_level = logging.DEBUG setup_logging( "PyDSS", filename=filename, console_level=console_level, file_level=file_level, ) logger.info("CLI: [%s]", get_cli_string()) project = PyDssProject.load_project(project_path) fs_intf = project.fs_interface results = PyDssResults(project_path) for scenario in results.scenarios: for filename in scenario.list_element_info_files(): text = fs_intf.read_file(filename) if output_dir is None: path = os.path.join(project_path, filename) else: path = os.path.join(output_dir, filename) os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w") as f_out: f_out.write(text) print(f"Extracted {filename} to {path}")
def create_project(path=None, project=None, scenarios=None, simulation_file=None, simulation_config=None, controller_types=None, export_modes=None, options=None, visualization_types=None, opendss_project_folder=None, master_dss_file=None, force=False): """Create PyDSS project.""" setup_logging("PyDSS", console_level=logging.INFO) if controller_types is not None: controller_types = [ControllerType(x) for x in controller_types.split(",")] if export_modes is not None: export_modes = [ExportMode(x) for x in export_modes.split(",")] if visualization_types is not None: visualization_types = [VisualizationType(x) for x in visualization_types.split(",")] if options is not None: options = ast.literal_eval(options) if not isinstance(options, dict): logger.error(f"options must be of type dict; received {type(options)}") sys.exit(1) scenarios = [ PyDssScenario( name=x.strip(), controller_types=controller_types, export_modes=export_modes, visualization_types=visualization_types, ) for x in scenarios.split(",") ] PyDssProject.create_project( path, project, scenarios, simulation_config, options=options, simulation_file=simulation_file, master_dss_file=master_dss_file, opendss_project_folder = opendss_project_folder, force=force, )
def extract(project_path, file_path, output_dir=None, verbose=False): """Extract a file from an archived PyDSS project.""" if not os.path.exists(project_path): print(f"project-path={project_path} does not exist") sys.exit(1) filename = "pydss_extract.log" console_level = logging.INFO file_level = logging.INFO if verbose: console_level = logging.DEBUG file_level = logging.DEBUG setup_logging( "PyDSS", filename=filename, console_level=console_level, file_level=file_level, ) logger.info("CLI: [%s]", get_cli_string()) project = PyDssProject.load_project(project_path) data = project.fs_interface.read_file(file_path) if output_dir is None: path = os.path.join(project_path, file_path) else: path = os.path.join(output_dir, file_path) os.makedirs(os.path.dirname(path), exist_ok=True) ext = os.path.splitext(file_path)[1] mode = "wb" if ext in (".h5", ".feather") else "w" with open(path, mode) as f_out: f_out.write(data) print(f"Extracted {file_path} to {path}")
def run(project_path, options=None, tar_project=False, zip_project=False, verbose=False, simulations_file=None, dry_run=False): """Run a PyDSS simulation.""" project_path = Path(project_path) settings = PyDssProject.load_simulation_settings(project_path, simulations_file) if verbose: # Override the config file. settings.logging.logging_level = logging.DEBUG filename = None console_level = logging.INFO file_level = logging.INFO if not settings.logging.enable_console: console_level = logging.ERROR if verbose: console_level = logging.DEBUG file_level = logging.DEBUG if settings.logging.enable_file: logs_path = project_path / "Logs" if not logs_path.exists(): logger.error("Logs path %s does not exist", logs_path) sys.exit(1) filename = logs_path / "pydss.log" setup_logging( "PyDSS", filename=filename, console_level=console_level, file_level=file_level, ) logger.info("CLI: [%s]", get_cli_string()) if options is not None: options = ast.literal_eval(options) if not isinstance(options, dict): logger.error("options are invalid: %s", options) sys.exit(1) project = PyDssProject.load_project(project_path, options=options, simulation_file=simulations_file) project.run(tar_project=tar_project, zip_project=zip_project, dry_run=dry_run) if dry_run: maxlen = max([len(k) for k in project.estimated_space.keys()]) if len("ScenarioName") > maxlen: maxlen = len("ScenarioName") template = "{:<{width}} {}\n".format("ScenarioName", "EstimatedSpace", width=maxlen) total_size = 0 for k, v in project.estimated_space.items(): total_size += v vstr = make_human_readable_size(v) template += "{:<{width}} : {}\n".format(k, vstr, width=maxlen) template = template.strip() logger.info(template) logger.info("-" * 30) logger.info(f"TotalSpace: {make_human_readable_size(total_size)}") logger.info("=" * 30) logger.info( "Note: compression may reduce the size by ~90% depending on the data." )
def run(project_path, options=None, tar_project=False, zip_project=False, verbose=False, simulations_file=None, dry_run=False): """Run a PyDSS simulation.""" if not os.path.exists(project_path): print(f"project-path={project_path} does not exist") sys.exit(1) config = PyDssProject.load_simulation_config(project_path, simulations_file) if verbose: # Override the config file. config["Logging"]["Logging Level"] = logging.DEBUG filename = None console_level = logging.INFO file_level = logging.INFO if not config["Logging"]["Display on screen"]: console_level = logging.ERROR if verbose: console_level = logging.DEBUG file_level = logging.DEBUG if config["Logging"]["Log to external file"]: logs_path = os.path.join(project_path, "Logs") filename = os.path.join( logs_path, os.path.basename(project_path) + ".log", ) if not os.path.exists(logs_path): print("Logs path does not exist. 'run' is not supported on a tarred project.") sys.exit(1) setup_logging( "PyDSS", filename=filename, console_level=console_level, file_level=file_level, ) logger.info("CLI: [%s]", get_cli_string()) if options is not None: options = ast.literal_eval(options) if not isinstance(options, dict): print(f"options must be of type dict; received {type(options)}") sys.exit(1) project = PyDssProject.load_project(project_path, options=options, simulation_file=simulations_file) project.run(tar_project=tar_project, zip_project=zip_project, dry_run=dry_run) if dry_run: print("="*30) maxlen = max([len(k) for k in project.estimated_space.keys()]) if len("ScenarioName") > maxlen: maxlen = len("ScenarioName") template = "{:<{width}} {}\n".format("ScenarioName", "EstimatedSpace", width=maxlen) total_size = 0 for k, v in project.estimated_space.items(): total_size += v vstr = make_human_readable_size(v) template += "{:<{width}} : {}\n".format(k, vstr, width=maxlen) template = template.strip() print(template) print("-"*30) print(f"TotalSpace: {make_human_readable_size(total_size)}") print("="*30) print("Note: compression may reduce the size by ~90% depending on the data.")
def run(self, logging_configured=True, tar_project=False, zip_project=False, dry_run=False): """Run all scenarios in the project.""" if isinstance(self._fs_intf, PyDssArchiveFileInterfaceBase): raise InvalidConfiguration("cannot run from an archived project") if tar_project and zip_project: raise InvalidParameter( "tar_project and zip_project cannot both be True") if self._settings.project.dss_file == "": raise InvalidConfiguration( "a valid opendss file needs to be passed") inst = instance() if not logging_configured: if self._settings.logging.enable_console: console_level = logging.INFO else: console_level = logging.ERROR if self._settings.logging.enable_file: filename = os.path.join(self._project_dir, "Logs", "pydss.log") else: filename = None file_level = logging.INFO setup_logging( "PyDSS", filename=filename, console_level=console_level, file_level=file_level, ) if dry_run: store_filename = os.path.join(tempfile.gettempdir(), STORE_FILENAME) else: store_filename = os.path.join(self._project_dir, STORE_FILENAME) self._dump_simulation_settings() driver = None if self._settings.exports.export_data_in_memory: driver = "core" if os.path.exists(store_filename): os.remove(store_filename) try: # This ensures that all datasets are flushed and closed after each # scenario. If there is an unexpected crash in a later scenario then # the file will still be valid for completed scenarios. for scenario in self._scenarios: with h5py.File(store_filename, mode="a", driver=driver) as hdf_store: self._hdf_store = hdf_store self._hdf_store.attrs["version"] = DATA_FORMAT_VERSION self._settings.project.active_scenario = scenario.name inst.run(self._settings, self, scenario, dry_run=dry_run) self._estimated_space[ scenario.name] = inst.get_estimated_space() export_tables = self._settings.exports.export_data_tables generate_reports = bool(self._settings.reports) if not dry_run and (export_tables or generate_reports): # Hack. Have to import here. Need to re-organize to fix. from PyDSS.pydss_results import PyDssResults results = PyDssResults(self._project_dir) if export_tables: for scenario in results.scenarios: scenario.export_data() if generate_reports: results.generate_reports() except Exception: logger.exception("Simulation failed") raise finally: logging.shutdown() if tar_project: self._tar_project_files() elif zip_project: self._zip_project_files() if dry_run and os.path.exists(store_filename): os.remove(store_filename)