Skip to content

Commit

Permalink
fix: export without extra meta data (#275)
Browse files Browse the repository at this point in the history
Fixes #274
  • Loading branch information
SebastianDD authored Sep 9, 2024
1 parent dcbcf64 commit a00b74e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 56 deletions.
17 changes: 11 additions & 6 deletions examples/powerfactory_control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
46 changes: 21 additions & 25 deletions src/powerfactory_tools/versions/pf2022/exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 = [
Expand Down
46 changes: 21 additions & 25 deletions src/powerfactory_tools/versions/pf2024/exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 = [
Expand Down

0 comments on commit a00b74e

Please sign in to comment.