def publish(self, file_name): self.region.UUID = str(CimWriter.uuid()) self.cimobject_by_uuid_dict[self.region.UUID] = self.region self.add_location(self.centroid.x, self.centroid.y, is_center=True) total_line_length = 0 for circuit in self.circuits: station1 = circuit.members[0] station2 = circuit.members[-1] if 'station' in station1.type: connectivity_node1 = self.substation_to_cim(station1, circuit.voltage) elif 'plant' in station1.type or 'generator' in station1.type: connectivity_node1 = self.generator_to_cim(station1, circuit.voltage) else: self.root.error('Invalid circuit! - Skip circuit') circuit.print_circuit() continue if 'station' in station2.type: connectivity_node2 = self.substation_to_cim(station2, circuit.voltage) elif 'plant' in station2.type or 'generator' in station2.type: connectivity_node2 = self.generator_to_cim(station2, circuit.voltage) else: self.root.error('Invalid circuit! - Skip circuit') circuit.print_circuit() continue lines_wsg84 = [] line_length = 0 for line_wsg84 in circuit.members[1:-1]: lines_wsg84.append(line_wsg84.geom) line_length += line_wsg84.length line_wsg84 = linemerge(lines_wsg84) total_line_length += line_length self.root.debug('Map line from (%lf,%lf) to (%lf,%lf) with length %s meters', station1.geom.centroid.y, station1.geom.centroid.x, station2.geom.centroid.y, station2.geom.centroid.x, str(line_length)) self.line_to_cim(connectivity_node1, connectivity_node2, line_length, circuit.name, circuit.voltage, line_wsg84.centroid.y, line_wsg84.centroid.x) # self.root.info('The inferred net\'s length is %s meters', str(total_line_length)) self.attach_loads() cimwrite(self.cimobject_by_uuid_dict, file_name + '.xml', encoding='utf-8') cimwrite(self.cimobject_by_uuid_dict, file_name + '.rdf', encoding='utf-8') # pretty print cim file xml = parse(file_name + '.xml') pretty_xml_as_string = xml.toprettyxml(encoding='utf-8') matches = re.findall('#x[0-9a-f]{4}', pretty_xml_as_string) for match in matches: pretty_xml_as_string = pretty_xml_as_string.replace(match, unichr(int(match[2:len(match)], 16))) pretty_file = io.open(file_name + '_pretty.xml', 'w', encoding='utf8') pretty_file.write(unicode(pretty_xml_as_string)) pretty_file.close()
def testSerialise(self): """Test CIM RDF/XML serialisation. """ d = cimread(RDFXML_FILE) output = StringIO() cimwrite(d, output) output.seek(0) dd = cimread(output) output.close() self.assertEqual(len(dd), 5894)
def testSerialise(self): """Test CIM RDF/XML serialisation. """ d = cimread(RDFXML_FILE) output = StringIO.StringIO() cimwrite(d, output) output.seek(0) dd = cimread(output) output.close() self.assertEqual(len(dd), 5894)
def publish(self, file_name): self.region.UUID = str(CimWriter.uuid()) self.cimobject_by_uuid_dict[self.region.UUID] = self.region self.add_location(self.centroid.x, self.centroid.y, is_center=True) total_line_length = 0 voltages = set() cables = set() wires = set() types = set() line_length = 0 for circuit in self.circuits: station1 = circuit.members[0] station2 = circuit.members[-1] try: for line_part in circuit.members[1:-1]: tags_list = ast.literal_eval(str(line_part.tags)) line_tags = dict(zip(tags_list[::2], tags_list[1::2])) line_tags_keys = line_tags.keys() voltages.update([ CSVWriter.try_parse_int(v) for v in line_part.voltage.split(';') ]) if 'cables' in line_tags_keys: cables.update( [CSVWriter.try_parse_int(line_tags['cables'])]) if 'wires' in line_tags_keys: wires.update( CSVWriter.convert_wire_names_to_numbers( CSVWriter.sanitize_csv(line_tags['wires']))) types.update([line_part.type]) line_length += line_part.length except Exception as ex: print('Error line_to_cim_param_extraction') if 'station' in station1.type: connectivity_node1 = self.substation_to_cim( station1, circuit.voltage) elif 'plant' in station1.type or 'generator' in station1.type: connectivity_node1 = self.generator_to_cim( station1, circuit.voltage) else: self.root.error('Invalid circuit! - Skip circuit') circuit.print_circuit() continue if 'station' in station2.type: connectivity_node2 = self.substation_to_cim( station2, circuit.voltage) elif 'plant' in station2.type or 'generator' in station2.type: connectivity_node2 = self.generator_to_cim( station2, circuit.voltage) else: self.root.error('Invalid circuit! - Skip circuit') circuit.print_circuit() continue lines_wsg84 = [] line_length = 0 for line_wsg84 in circuit.members[1:-1]: lines_wsg84.append(line_wsg84.geom) line_length += line_wsg84.length line_wsg84 = linemerge(lines_wsg84) total_line_length += line_length self.root.debug( 'Map line from (%lf,%lf) to (%lf,%lf) with length %s meters', station1.geom.centroid.y, station1.geom.centroid.x, station2.geom.centroid.y, station2.geom.centroid.x, str(line_length)) self.line_to_cim(connectivity_node1, connectivity_node2, line_length, circuit.name, circuit.voltage, line_wsg84.centroid.y, line_wsg84.centroid.x, line_length, cables, voltages, wires) # self.root.info('The inferred net\'s length is %s meters', str(total_line_length)) self.attach_loads() cimwrite(self.cimobject_by_uuid_dict, file_name + '.xml', encoding='utf-8') cimwrite(self.cimobject_by_uuid_dict, file_name + '.rdf', encoding='utf-8') # pretty print cim file xml = parse(file_name + '.xml') pretty_xml_as_string = xml.toprettyxml(encoding='utf-8') matches = re.findall('#x[0-9a-f]{4}', pretty_xml_as_string) for match in matches: pretty_xml_as_string = pretty_xml_as_string.replace( match, unichr(int(match[2:len(match)], 16))) pretty_file = io.open(file_name + '_pretty.xml', 'w', encoding='utf8') pretty_file.write(unicode(pretty_xml_as_string)) pretty_file.close()
import uuid from CIM14.IEC61970.Core import ConnectivityNode from CIM14.IEC61970.Core import Terminal from CIM14.IEC61970.Wires import SynchronousMachine from CIM14.IEC61970.Wires import Breaker from CIM14.IEC61970.LoadModel import ConformLoad from CIM14.IEC61970.Wires import EnergyConsumer from PyCIM import cimwrite g1 = SynchronousMachine(name="Generator", UUID=str(uuid.uuid4())) t1 = Terminal(name="Terminal 1", UUID=str(uuid.uuid4())) t2 = Terminal(name="Terminal 2", UUID=str(uuid.uuid4())) t3 = Terminal(name="Terminal 3", UUID=str(uuid.uuid4())) t4 = Terminal(name="Terminal 4", UUID=str(uuid.uuid4())) c1 = ConnectivityNode(name='Node 1', UUID=str(uuid.uuid4())) c2 = ConnectivityNode(name='Node 2', UUID=str(uuid.uuid4())) b1 = Breaker(name="Breaker", UUID=str(uuid.uuid4())) l1 = EnergyConsumer(name="Consumer", UUID=str(uuid.uuid4())) g1.addTerminals(t1) c1.addTerminals(t1, t2) b1.addTerminals(t2, t3) c2.addTerminals(t3, t4) l1.addTerminals(t4) d = {} for elem in [g1, t1, t2, t3, t4, c1, c2, b1, l1]: d[elem.UUID] = elem cimwrite(d, "simple_model.xml")
dictionary[ter611_capacitor.UUID]= ter611_capacitor #_________________________________________________________ #_____________________ Sort Dictionary ___________________ #_________________________________________________________ #This sorting process isn't fundamental, but #makes output more human readable. import collections #package to sort the dictionary d_vals=sorted(dictionary.values()) d_keys= sorted(dictionary, key=dictionary.get) dictionary = collections.OrderedDict(zip(d_keys, d_vals)) #_________________________________________________________ #_____________________ Write CIM _________________________ #_________________________________________________________ #Final step is to write the instances in CIM text from PyCIM import cimwrite path_out = 'IEEE_13ntf_cim.xml' cimwrite(dictionary, path_out, 'us-ascii') print '\nWhat is in path:\n' from PyCIM.PrettyPrintXML import xmlpp print xmlpp(path_out)
#put the objects in the dictionary for cimwrite dictionary[ter_load.UUID]= ter_load dictionary[load.UUID]= load #_________________________________________________________ #_____________________ Sort Dictionary ___________________ #_________________________________________________________ #This sorting process isn't fundamental, but #makes output more human readable. import collections #package to sort the dictionary d_vals=sorted(dictionary.values()) d_keys= sorted(dictionary, key=dictionary.get) dictionary = collections.OrderedDict(zip(d_keys, d_vals)) #_________________________________________________________ #_____________________ Write CIM _________________________ #_________________________________________________________ #Final step is to write the instances in CIM text from PyCIM import cimwrite #module to write the CIM text path_out = 'PyCIM_OpenGridMap_basics.xml' cimwrite(dictionary, path_out) print '\nWhat is in path:\n' from PyCIM.PrettyPrintXML import xmlpp #module to print and view output print xmlpp(path_out)
def publish(self): self.region.UUID = str(CimWriter.uuid()) self.cimobject_by_uuid_dict[self.region.UUID] = self.region self.add_location(self.centroid.x, self.centroid.y, is_center=True) total_line_length = 0 for circuit in self.circuits: station1 = circuit.stations[0] if circuit.stations[0].id < circuit.stations[1].id else circuit.stations[1] station2 = circuit.stations[1] if circuit.stations[0].id < circuit.stations[1].id else circuit.stations[0] if 'station' == str(station1.type) or 'substation' == str(station1.type): connectivity_node1 = self.substation_to_cim(station1, circuit.voltage) elif 'plant' == str(station1.type) or 'generator' == str(station1.type): connectivity_node1 = self.generator_to_cim(station1, circuit.voltage) else: continue if 'station' == str(station2.type) or 'substation' == str(station2.type): connectivity_node2 = self.substation_to_cim(station2, circuit.voltage) elif 'plant' == str(station2.type) or 'generator' == str(station2.type): connectivity_node2 = self.generator_to_cim(station2, circuit.voltage) else: continue lines_wsg84 = [] line_length = 0 for line_wsg84 in circuit.powerlines: lines_wsg84.append(to_shape(line_wsg84.geom)) line_length += line_wsg84.length line_wsg84 = linemerge(lines_wsg84) total_line_length += line_length self.line_to_cim(connectivity_node1, connectivity_node2, line_length, circuit.name, circuit.voltage, line_wsg84.centroid.y, line_wsg84.centroid.x) self.attach_loads() base_dir = '../../models/{0}/'.format(self.uuid()) dir_name = join(dirname(__file__), base_dir) if not exists(dir_name): makedirs(dir_name) file_name = dir_name + '/cim' cimwrite(self.cimobject_by_uuid_dict, file_name + '.xml', encoding='utf-8') cimwrite(self.cimobject_by_uuid_dict, file_name + '.rdf', encoding='utf-8') # pretty print cim file xml = parse(file_name + '.xml') pretty_xml_as_string = xml.toprettyxml(encoding='utf-8') matches = re.findall('#x[0-9a-f]{4}', pretty_xml_as_string) for match in matches: pretty_xml_as_string = pretty_xml_as_string.replace(match, unichr(int(match[2:len(match)], 16))) pretty_file = io.open(file_name + '_pretty.xml', 'w', encoding='utf8') pretty_file.write(unicode(pretty_xml_as_string)) pretty_file.close() in_memory = BytesIO() zip_cim = ZipFile(in_memory, "a") zip_cim.write(file_name + '.xml', basename(file_name + '.xml')) zip_cim.write(file_name + '.rdf', basename(file_name + '.rdf')) zip_cim.write(file_name + '_pretty.xml', basename(file_name + '_pretty.xml')) zip_cim.close() shutil.rmtree(dir_name) in_memory.seek(0) return in_memory.read()