Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted the Export script #29

Merged
merged 17 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions src/main/python/powerFactory2json/pf2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import json
import inspect
import os
from pf2jsonUtils import excluded_fields, elements4export, nested_elements4export, reserved_keywords
import pf2jsonUtils
# fixme: Delete reload after development
import importlib
importlib.reload(pf2jsonUtils)
from pf2jsonUtils import attributes4export, elements4export, nested_elements4export, reserved_keywords


#################
# Configuration #
Expand All @@ -21,31 +26,53 @@ def safe_name(unsafe_str):
return unsafe_str


def json_elements(raw_elements, invalid_field_names):
def get_attribute_dict(raw_element, attributes_to_include, append_type=False):
"""
Creates a dict which includes all members/fields noted in included_fields of a given raw PowerFactory element.
"""
element = {"uid": raw_element.GetFullName()}
for member in inspect.getmembers(raw_element):
if not (
member[0].startswith('_')
and inspect.ismethod(member[1])
and isinstance(member[1], powerfactory.Method)
and inspect.isclass(member[1])
) and member[0] in attributes_to_include:
if not isinstance(member[1], powerfactory.DataObject):
element[safe_name(member[0])] = member[1]
elif isinstance(member[1], powerfactory.DataObject) and member[0] in nested_elements4export:
element[safe_name(member[0])] = get_attribute_dicts([member[1]], attributes4export[member[0]])
if append_type:
element["pfCls"] = raw_element.GetClassName()
return element


def get_attribute_dicts(raw_elements, attributes_to_include):
"""
Creates a list with an attribute dictionary for each raw PowerFactory element
"""
elements = []
for raw_element in raw_elements:
element = {}
for i in inspect.getmembers(raw_element):
if not i[0].startswith('_'):
if not inspect.ismethod(i[1]):
if not isinstance(i[1], powerfactory.Method):
if not inspect.isclass(i[1]):
if not i[0] in invalid_field_names:
if not isinstance(i[1], powerfactory.DataObject):
element.update({safe_name(i[0]): i[1]})
elif isinstance(i[1], powerfactory.DataObject) and i[0] in nested_elements4export:
element.update({safe_name(i[0]): json_elements([i[1]], excluded_fields[i[0]])})
element = get_attribute_dict(raw_element, attributes_to_include)

# export connected elements of nodes, transformers and lines to get grid topology
if (raw_element.GetClassName() in ["ElmTerm", "ElmTr2", "ElmTr3", "ElmLne", "ElmCoup"]):
element["conElms"] = []
for con_elm in raw_element.GetConnectedElements():
element["conElms"].append(get_attribute_dict(con_elm, attributes4export["conElms"], True))

elements.append(element)
return elements


dpf = powerfactory.GetApplication()
dpf.EchoOff()
pfGrid = {} # resulting pf grid json export

# generate json strings
for element_name in elements4export:
pfGrid.update({element_name: json_elements(dpf.GetCalcRelevantObjects(elements4export[element_name]),
excluded_fields[element_name])})
pfGrid.update({element_name: get_attribute_dicts(dpf.GetCalcRelevantObjects(elements4export[element_name]),
attributes4export[element_name])})

# write
if not os.path.exists(exported_grid_dir):
Expand Down
Loading