From a00b74e381ca09e1b1d319ca6b4088471d6986c6 Mon Sep 17 00:00:00 2001 From: SebastianDD <43751982+SebastianDD@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:58:36 +0200 Subject: [PATCH] fix: export without extra meta data (#275) Fixes #274 --- examples/powerfactory_control.ipynb | 17 ++++--- .../versions/pf2022/exporter/exporter.py | 46 +++++++++---------- .../versions/pf2024/exporter/exporter.py | 46 +++++++++---------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/examples/powerfactory_control.ipynb b/examples/powerfactory_control.ipynb index 8af3894..da474b3 100644 --- a/examples/powerfactory_control.ipynb +++ b/examples/powerfactory_control.ipynb @@ -45,6 +45,7 @@ "metadata": {}, "outputs": [], "source": [ + "from powerfactory_tools.utils.io import FileType\n", "from powerfactory_tools.versions.pf2024 import PowerFactoryInterface\n", "from powerfactory_tools.versions.pf2024.constants import NAME_SEPARATOR\n", "from powerfactory_tools.versions.pf2024.interface import ValidPythonVersion\n", @@ -53,7 +54,6 @@ "from powerfactory_tools.versions.pf2024.types import PFClassId\n", "from powerfactory_tools.versions.pf2024.types import ResultExportMode\n", "from powerfactory_tools.versions.pf2024.utils.io import ExportHandler\n", - "from powerfactory_tools.utils.io import FileType\n", "\n", "# PowerFactory configuration\n", "PF_SERVICE_PACK = 2 # mandatory\n", @@ -100,11 +100,16 @@ " lines = pfi.lines(calc_relevant=True) # noqa: F841\n", " loads = pfi.loads(calc_relevant=True)\n", " generators = pfi.generators(calc_relevant=True) # noqa: F841\n", - " # As within this study case only one grid is active, the following statement leads to the same result:\n", - " generators_grid = pfi.generators(grid_name=grid_name) # noqa: F841\n", "\n", - " # Expert mode: Get elements by user defined filter, e.g. get again all generators\n", - " generators2 = pfi.grid_elements( # noqa: F841\n", + " ## Let's dive a bit deeper into the PowerFactoryInterface\n", + " # Get all calculation relevant generators from the active study case, but exclude generators that are out of service\n", + " generators2 = pfi.generators(calc_relevant=True, include_out_of_service=False) # noqa: F841\n", + " # Get all calculation relevant generators from the active study case, but only the ones from the grid with the specified name\n", + " # As within this study case only one grid is active, the following statement leads to the same result as in variable generators:\n", + " generators3 = pfi.generators(grid_name=grid_name) # noqa: F841\n", + "\n", + " # In general, one can use grid_elements() function to get elements by user defined filter, e.g. get again all generators\n", + " generators4 = pfi.grid_elements( # noqa: F841\n", " class_name=PFClassId.GENERATOR.value, # class name, same as passing the raw string: \"ElmGenstat\"\n", " name=\"*\", # name doesn't matter\n", " grid_name=grid_name, # which grid is to be used to search for generators\n", @@ -123,7 +128,7 @@ " logging.info(\"3_Bus : Run load flow and export nodal voltages ...\")\n", " pfi.run_ldf(ac=True, symmetrical=True)\n", "\n", - " ## uncomment for demonstration purposes: show the PF application window in non-interavtive mode\n", + " ## Do uncomment for demonstration purposes: show the PF application window in non-interavtive mode\n", " # do not close the window by your own by clicking on the red cross, but process via the user input\n", " # pfi.app.Show()\n", " # time.sleep(5) # wait for 5 seconds\n", diff --git a/src/powerfactory_tools/versions/pf2022/exporter/exporter.py b/src/powerfactory_tools/versions/pf2022/exporter/exporter.py index d2f03c8..0dcf200 100644 --- a/src/powerfactory_tools/versions/pf2022/exporter/exporter.py +++ b/src/powerfactory_tools/versions/pf2022/exporter/exporter.py @@ -544,8 +544,7 @@ def create_external_grid( bus=ext_grid.bus1, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(ext_grid, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(ext_grid, self.element_specific_attrs, grid_name=grid_name) return ExternalGrid( name=name, @@ -592,8 +591,7 @@ def create_node( phases = self.get_terminal_phases(TerminalPhaseConnectionType(terminal.phtech)) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(terminal, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(terminal, self.element_specific_attrs, grid_name=grid_name) return Node(name=name, u_n=u_n, phases=phases, description=description, optional_data=extra_meta_data) @@ -704,8 +702,8 @@ def create_line( # noqa: PLR0915 bus=line.bus2, grid_name=grid_name, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(line, self.element_specific_attrs, grid_name=grid_name) + + extra_meta_data = self.get_extra_element_attrs(line, self.element_specific_attrs, grid_name=grid_name) return Branch( name=name, @@ -810,8 +808,7 @@ def create_coupler( phases_1 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t1.phtech)) phases_2 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t2.phtech)) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(coupler, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(coupler, self.element_specific_attrs, grid_name=grid_name) return Branch( name=name, @@ -894,8 +891,7 @@ def create_fuse( phases_1 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t1.phtech)) phases_2 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t2.phtech)) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(fuse, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(fuse, self.element_specific_attrs, grid_name=grid_name) return Branch( name=name, @@ -1126,12 +1122,11 @@ def create_transformer_2w( neutral_connected=neutral_connected_l, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs( - transformer_2w, - self.element_specific_attrs, - grid_name=grid_name, - ) + extra_meta_data = self.get_extra_element_attrs( + transformer_2w, + self.element_specific_attrs, + grid_name=grid_name, + ) return Transformer( node_1=t_high_name, @@ -1781,8 +1776,7 @@ def create_consumer( load_name=l_name, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(load, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(load, self.element_specific_attrs, grid_name=grid_name) return Load( name=l_name, @@ -2090,12 +2084,11 @@ def create_producer( ) load_type = LoadType.STORAGE if system_type in STORAGE_SYSTEM_TYPES else LoadType.PRODUCER - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs( - generator, - self.element_specific_attrs, - grid_name=grid_name, - ) + extra_meta_data = self.get_extra_element_attrs( + generator, + self.element_specific_attrs, + grid_name=grid_name, + ) return Load( name=gen_name, @@ -4132,7 +4125,7 @@ def get_transformer2w_3ph_phases( # may adapt in future def get_extra_element_attrs( self, element: PFTypes.DataObject, - element_specific_attrs: dict[PFClassId, Sequence[str | dict]], # dict[PFClassId, set[str]] + element_specific_attrs: dict[PFClassId, Sequence[str | dict]] | None, # dict[PFClassId, set[str]] /, *, grid_name: str | None = None, @@ -4149,6 +4142,9 @@ def get_extra_element_attrs( Returns: Sequence[AttributeData] | None: list of AttributeData or None if no attributes have been defined for this element type """ + if element_specific_attrs is None: + return None + for elm_type, attributes in element_specific_attrs.items(): if self.pfi.is_of_type(element, elm_type): attribute_data = [ diff --git a/src/powerfactory_tools/versions/pf2024/exporter/exporter.py b/src/powerfactory_tools/versions/pf2024/exporter/exporter.py index e1775f8..b5b784a 100644 --- a/src/powerfactory_tools/versions/pf2024/exporter/exporter.py +++ b/src/powerfactory_tools/versions/pf2024/exporter/exporter.py @@ -543,8 +543,7 @@ def create_external_grid( bus=ext_grid.bus1, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(ext_grid, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(ext_grid, self.element_specific_attrs, grid_name=grid_name) return ExternalGrid( name=name, @@ -591,8 +590,7 @@ def create_node( phases = self.get_terminal_phases(TerminalPhaseConnectionType(terminal.phtech)) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(terminal, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(terminal, self.element_specific_attrs, grid_name=grid_name) return Node(name=name, u_n=u_n, phases=phases, description=description, optional_data=extra_meta_data) @@ -703,8 +701,8 @@ def create_line( # noqa: PLR0915 bus=line.bus2, grid_name=grid_name, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(line, self.element_specific_attrs, grid_name=grid_name) + + extra_meta_data = self.get_extra_element_attrs(line, self.element_specific_attrs, grid_name=grid_name) return Branch( name=name, @@ -809,8 +807,7 @@ def create_coupler( phases_1 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t1.phtech)) phases_2 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t2.phtech)) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(coupler, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(coupler, self.element_specific_attrs, grid_name=grid_name) return Branch( name=name, @@ -893,8 +890,7 @@ def create_fuse( phases_1 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t1.phtech)) phases_2 = self.get_terminal_phases(phase_connection_type=TerminalPhaseConnectionType(t2.phtech)) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(fuse, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(fuse, self.element_specific_attrs, grid_name=grid_name) return Branch( name=name, @@ -1125,12 +1121,11 @@ def create_transformer_2w( neutral_connected=neutral_connected_l, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs( - transformer_2w, - self.element_specific_attrs, - grid_name=grid_name, - ) + extra_meta_data = self.get_extra_element_attrs( + transformer_2w, + self.element_specific_attrs, + grid_name=grid_name, + ) return Transformer( node_1=t_high_name, @@ -1780,8 +1775,7 @@ def create_consumer( load_name=l_name, ) - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs(load, self.element_specific_attrs, grid_name=grid_name) + extra_meta_data = self.get_extra_element_attrs(load, self.element_specific_attrs, grid_name=grid_name) return Load( name=l_name, @@ -2089,12 +2083,11 @@ def create_producer( ) load_type = LoadType.STORAGE if system_type in STORAGE_SYSTEM_TYPES else LoadType.PRODUCER - if self.element_specific_attrs is not None: - extra_meta_data = self.get_extra_element_attrs( - generator, - self.element_specific_attrs, - grid_name=grid_name, - ) + extra_meta_data = self.get_extra_element_attrs( + generator, + self.element_specific_attrs, + grid_name=grid_name, + ) return Load( name=gen_name, @@ -4131,7 +4124,7 @@ def get_transformer2w_3ph_phases( # may adapt in future def get_extra_element_attrs( self, element: PFTypes.DataObject, - element_specific_attrs: dict[PFClassId, Sequence[str | dict]], # dict[PFClassId, set[str]] + element_specific_attrs: dict[PFClassId, Sequence[str | dict]] | None, # dict[PFClassId, set[str]] /, *, grid_name: str | None = None, @@ -4148,6 +4141,9 @@ def get_extra_element_attrs( Returns: Sequence[AttributeData] | None: list of AttributeData or None if no attributes have been defined for this element type """ + if element_specific_attrs is None: + return None + for elm_type, attributes in element_specific_attrs.items(): if self.pfi.is_of_type(element, elm_type): attribute_data = [