Commit 39fe79c4 authored by Philipp Reuber's avatar Philipp Reuber
Browse files

rename readCIGREMV to importCIGREMV, change references to this example, nicer output for cimimport

parent d6fddb09
Pipeline #218330 passed with stages
in 22 seconds
......@@ -25,7 +25,7 @@ Output:
res: A map containing the CIMpy objects with the UUID as map key
```
[Example for CIM Import](https://git.rwth-aachen.de/acs/core/cim/cimgen/blob/master/examples/quickstart/readCIGREMV.py)
[Example for CIM Import](https://git.rwth-aachen.de/acs/public/cim/cimpy/blob/master/examples/quickstart/importCIGREMV.py)
## CIM Export
Function for serialization of CIMpy objects to XML files.
......@@ -42,4 +42,4 @@ Output:
One XML file for each package in the CGMES version. The package name is added to the file name like [file_name]_[package].xml
```
[Example for CIM Export](https://git.rwth-aachen.de/acs/core/cim/cimgen/blob/master/examples/quickstart/exportCIGREMV.py)
\ No newline at end of file
[Example for CIM Export](https://git.rwth-aachen.de/acs/public/cim/cimpy/blob/master/examples/quickstart/exportCIGREMV.py)
\ No newline at end of file
......@@ -34,6 +34,9 @@ def cim_import(xml_files, cgmes_version, start_dict=None):
# map used to group errors
logger_errors_grouped = {}
# map used to group infos
logger_info_grouped = {}
# A map of uuids to CIM objects to be returned.
res = start_dict if start_dict is not None else {}
......@@ -44,16 +47,29 @@ def cim_import(xml_files, cgmes_version, start_dict=None):
# CIM element tag base (e.g. {http://iec.ch/TC57/2012/CIM-schema-cim16#} )
base = "{" + namespaces["cim"] + "}"
res, logger_errors_grouped = _instantiate_classes(res, xml_files, cgmes_version_path,
namespace_rdf, base, logger_errors_grouped)
res, logger_errors_grouped = _set_attributes(res, xml_files, namespace_rdf, base, logger_errors_grouped)
res, logger_errors_grouped, logger_info_grouped = _instantiate_classes(res, xml_files, cgmes_version_path,
namespace_rdf, base, logger_errors_grouped,
logger_info_grouped)
res, logger_errors_grouped = _set_attributes(res, xml_files, namespace_rdf, base,
logger_errors_grouped)
if logger_errors_grouped:
for error, count in logger_errors_grouped.items():
logging_message = '{} : {} times'.format(error, count)
logger.warning(logging_message)
logger.info('Created totally {} CIM objects in {}s\n\n'.format(len(res), time() - t0))
if logger_info_grouped:
for info, count in logger_info_grouped.items():
logging_message = '{} : {} times'.format(info, count)
logger.info(logging_message)
# print info which classes and how many were instantiated
print(logging_message)
elapsed_time = time() - t0
logger.info('Created totally {} CIM objects in {}s\n\n'.format(len(res), elapsed_time))
# print info of how many classes in total were instantiated to terminal
print('Created totally {} CIM objects in {}s'.format(len(res), elapsed_time))
return res, namespaces
......@@ -63,7 +79,8 @@ def cim_import(xml_files, cgmes_version, start_dict=None):
# are set in the _set_attributes function because some attributes might be stored in one package and the class in
# another. Since after this function all classes are instantiated, there should be no problem in setting the attributes.
# Also the information from which package file a class was read is stored in the serializationProfile dictionary.
def _instantiate_classes(res, xml_files, cgmes_version_path, namespace_rdf, base, logger_errors_grouped):
def _instantiate_classes(res, xml_files, cgmes_version_path, namespace_rdf, base,
logger_errors_grouped, logger_info_grouped):
# length of element tag base
m = len(base)
# first step: create the dict res{uuid}=instance_of_the_cim_class
......@@ -111,6 +128,12 @@ def _instantiate_classes(res, xml_files, cgmes_version_path, namespace_rdf, base
# Instantiate the class and map it to the uuid.
# res[uuid] = klass(UUID=uuid)
res[uuid] = klass()
info_msg = 'CIM object {} created'.format(module_name.split('.')[-1])
try:
logger_info_grouped[info_msg] += 1
except KeyError:
logger_info_grouped[info_msg] = 1
# check if the class has the attribute mRID and set the mRID to the read in UUID. If the class
# does not has this attribute, the UUID is only stored in the res dictionary.
if hasattr(res[uuid], 'mRID'):
......@@ -137,7 +160,7 @@ def _instantiate_classes(res, xml_files, cgmes_version_path, namespace_rdf, base
# Clear children of the root element to minimise memory usage.
root.clear()
return res, logger_errors_grouped
return res, logger_errors_grouped, logger_info_grouped
# This function sets all attributes after the classes are instantiated by _instanciate_classes. Cyclic attributes like
......
......@@ -3,7 +3,7 @@ import os
import cimpy
logging.basicConfig(filename='readCIGREMV.log', level=logging.INFO, filemode='w')
logging.basicConfig(filename='importCIGREMV.log', level=logging.INFO, filemode='w')
print(os.getcwd())
xml_files = [r"..\sampledata\CIGRE_MV\CIGRE_MV_Rudion_With_LoadFLow_Results\Rootnet_FULL_NE_24J13h_EQ.xml",
......@@ -14,12 +14,10 @@ xml_files_abs = []
for file in xml_files:
xml_files_abs.append(os.path.abspath(file))
# res = cimpy.cimread(xml_files)
res, _ = cimpy.cim_import(xml_files_abs, "cgmes_v2_4_15")
print(len(res))
print("\n\n\n")
results = ["ACLineSegment", "PowerTransformer", "TopologicalNode", "Switch", "EnergyConsumer"]
print("\n\n")
results = ["ACLineSegment", "PowerTransformer", "EnergyConsumer"]
for key, value in res.items():
if value.__class__.__name__ in results:
print(value.__str__())
......@@ -45,7 +45,7 @@ Optional Arguments:
Output:
:res: A map containing the CIMpy objects with the UUID as map key
`Example for CIM Import <https://git.rwth-aachen.de/acs/public/cim/cimpy/blob/master/examples/quickstart/readCIGREMV.py>`_
`Example for CIM Import <https://git.rwth-aachen.de/acs/public/cim/cimpy/blob/master/examples/quickstart/importCIGREMV.py>`_
CIM Export
""""""""""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment