File Writers¶
cimgraph.utils has two writers that save the graph of a GraphModel to a file:
write_xml(network, filename)writes CIM RDF/XML.write_json_ld(network, filename)writes JSON-LD in the CGMES draft style.
Both write classes in alphabetical order and objects sorted by URI, so the same model gives the same file body on every run. They can be read back with XMLFile and JSONLDFile (see XML File Parser and JSON-LD File Parser).
Load a Model¶
Load the IEEE 13 bus test feeder from CIM XML:
import os
os.environ['CIMG_CIM_PROFILE'] = 'cimhub_2023'
import cimprofiles.cim17.cimhub_2023 as cim
from cimgraph.databases import XMLFile
from cimgraph.models import FeederModel
feeder = cim.Feeder(mRID='49AD8E07-3BF9-A4E2-CB8F-C3722F837B62')
network = FeederModel(connection=XMLFile(filename='../../tests/test_models/ieee13.xml'),
container=feeder, distributed=False)
The examples write to a temporary directory and print the start of each file:
import json
import tempfile
from pathlib import Path
out_dir = Path(tempfile.mkdtemp())
def print_head(path, lines=12):
print(''.join(path.read_text().splitlines(keepends=True)[:lines]))
CIM XML¶
write_xml writes an md:FullModel and cim:ModelAuthority header, then every object. Values are plain numbers in base SI units (the line length below is in meters).
from cimgraph.utils import write_xml
xml_file = out_dir / 'ieee13.xml'
write_xml(network, str(xml_file))
print_head(xml_file, lines=19)
<?xml version="1.0" encoding="utf-8"?> <rdf:RDF xmlns:cim="http://iec.ch/TC57/CIM100#" xmlns:md="http://iec.ch/TC57/61970-552/ModelDescription/1#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <md:FullModel rdf:about="urn:uuid:179bcc33-ef9b-4b82-abb5-4e130ac595ec"> <md:Model.created>2026-11-21T00:29:47Z</md:Model.created> <md:Model.modelingAuthoritySet>https://github.com/PNNL-CIM-Tools/CIM-Graph</md:Model.modelingAuthoritySet> </md:FullModel> <cim:ModelAuthority rdf:about="urn:uuid:bdf23f0c-0c08-525b-846a-e05b60f8af02"> <cim:IdentifiedObject.name>https://github.com/PNNL-CIM-Tools/CIM-Graph</cim:IdentifiedObject.name> </cim:ModelAuthority> <cim:ACLineSegment rdf:about="urn:uuid:0BBD0EA3-F665-465B-86FD-FC8B8466AD53"> <cim:Identity.identifier>0BBD0EA3-F665-465B-86FD-FC8B8466AD53</cim:Identity.identifier> <cim:ACLineSegment.PerLengthImpedance rdf:resource="urn:uuid:12C526C5-6645-4C0C-83CD-3030C440DA17"/> <cim:Conductor.length>91.44</cim:Conductor.length> <cim:ConductingEquipment.BaseVoltage rdf:resource="urn:uuid:2A158E0C-CD01-4A50-AEBA-59D761FCF15D"/> <cim:Equipment.EquipmentContainer rdf:resource="urn:uuid:49AD8E07-3BF9-A4E2-CB8F-C3722F837B62"/> <cim:PowerSystemResource.Location rdf:resource="urn:uuid:89C21411-D107-454A-93B0-0905893DD27D"/> <cim:IdentifiedObject.mRID>0BBD0EA3-F665-465B-86FD-FC8B8466AD53</cim:IdentifiedObject.mRID> <cim:IdentifiedObject.name>645646</cim:IdentifiedObject.name> </cim:ACLineSegment>
Options:
write_identifier=Falseleaves outcim:Identity.identifier.write_model_header=Falseleaves out the header.header={...}(withcontexts=[...]for extra prefixes) writes thedcat:Datasetheader described below instead ofmd:FullModel.
JSON-LD¶
write_json_ld writes the ENTSO-E metadata layout: the top-level map is a dcat:Dataset header and @graph holds the objects.
@contextis the profile's published context URL.- References are
"urn:uuid:..."strings. - Numbers are strings in the units of a units convention, named by the header's
dct:conformsTo. The default forcimhub_2023issi.
from cimgraph.utils import write_json_ld
jsonld_file = out_dir / 'ieee13.jsonld'
write_json_ld(network, str(jsonld_file))
document = json.loads(jsonld_file.read_text())
objects = document.pop('@graph')
print(json.dumps(document, indent=2))
print(json.dumps(objects[0], indent=2))
{
"@context": "https://pnnl-cim-tools.github.io/CIM-Profiles/cimhub_2023/0.2.0/context.jsonld",
"@id": "urn:uuid:31b69c37-9a67-423f-ae2b-2f56ae272344",
"@type": [
"dcat:Dataset",
"http://www.w3.org/2004/03/trix/rdfg-1/Graph"
],
"dct:identifier": "31b69c37-9a67-423f-ae2b-2f56ae272344",
"dct:issued": "2026-11-21T00:29:47Z",
"md:Model.modelingAuthoritySet": "https://github.com/PNNL-CIM-Tools/CIM-Graph",
"dct:conformsTo": [
"https://pnnl-cim-tools.github.io/CIM-Profiles/cimhub_2023/0.2.0/units-si.jsonld"
],
"dcat:distribution": {
"@type": "dcat:Distribution",
"dct:conformsTo": [
"https://pnnl-cim-tools.github.io/CIM-Profiles/cimhub_2023/0.2.0/units-si.jsonld"
]
}
}
{
"@id": "urn:uuid:0BBD0EA3-F665-465B-86FD-FC8B8466AD53",
"@type": "cim:ACLineSegment",
"cim:ACLineSegment.PerLengthImpedance": "urn:uuid:12C526C5-6645-4C0C-83CD-3030C440DA17",
"cim:Conductor.length": "91.44",
"cim:ConductingEquipment.BaseVoltage": "urn:uuid:2A158E0C-CD01-4A50-AEBA-59D761FCF15D",
"cim:Equipment.EquipmentContainer": "urn:uuid:49AD8E07-3BF9-A4E2-CB8F-C3722F837B62",
"cim:PowerSystemResource.Location": "urn:uuid:89C21411-D107-454A-93B0-0905893DD27D",
"cim:IdentifiedObject.mRID": "0BBD0EA3-F665-465B-86FD-FC8B8466AD53",
"cim:IdentifiedObject.name": "645646"
}
Header and Units Convention¶
By default, @id, dct:identifier and dct:issued are a new UUID and the export time. With header=, set them yourself, so identical exports are identical files, and add properties such as dcat:isVersionOf (the model series IRI). A header prefix that the contexts do not define raises a ValueError; pass contexts=[{'prefix': 'namespace'}] to define it.
convention='distribution' writes kW, kVAr and kVA, line constants per km and wire sizes in mm:
header = {
'@id': 'urn:uuid:5a0c3a4e-0000-5000-8000-000000000001',
'dct:identifier': '5a0c3a4e-0000-5000-8000-000000000001',
'dct:issued': '2026-11-01T00:00:00Z',
'dcat:isVersionOf': 'https://example.org/models/ieee13',
}
kw_file = out_dir / 'ieee13_kw.jsonld'
write_json_ld(network, str(kw_file), convention='distribution', header=header)
document = json.loads(kw_file.read_text())
objects = document.pop('@graph')
print(json.dumps(document, indent=2))
{
"@context": "https://pnnl-cim-tools.github.io/CIM-Profiles/cimhub_2023/0.2.0/context.jsonld",
"@id": "urn:uuid:5a0c3a4e-0000-5000-8000-000000000001",
"@type": [
"dcat:Dataset",
"http://www.w3.org/2004/03/trix/rdfg-1/Graph"
],
"dct:identifier": "5a0c3a4e-0000-5000-8000-000000000001",
"dct:issued": "2026-11-01T00:00:00Z",
"md:Model.modelingAuthoritySet": "https://github.com/PNNL-CIM-Tools/CIM-Graph",
"dct:conformsTo": [
"https://pnnl-cim-tools.github.io/CIM-Profiles/cimhub_2023/0.2.0/units-distribution.jsonld"
],
"dcat:distribution": {
"@type": "dcat:Distribution",
"dct:conformsTo": [
"https://pnnl-cim-tools.github.io/CIM-Profiles/cimhub_2023/0.2.0/units-distribution.jsonld"
]
},
"dcat:isVersionOf": "https://example.org/models/ieee13"
}
The load at bus 671 is written in kW and kVAr:
load = network.get_object('E26D83A0-D29D-41EF-9528-02C882FFCC0D')
written = next(node for node in objects if node['@id'] == f'urn:uuid:{load.uri()}')
print('written:', written['cim:EnergyConsumer.p'], 'kW,', written['cim:EnergyConsumer.q'], 'kVAr')
print('in memory:', load.p.to('kW'), 'kW,', load.q.to('kVAr'), 'kVAr')
written: 1155 kW, 660 kVAr in memory: 1155.0 kW, 660.0 kVAr
<style>
['pre { line-height: 125%; }\ntd.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }\nspan.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }\ntd.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }\nspan.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }\n.highlight-ipynb .hll { background-color: var(--jp-cell-editor-active-background) }\n.highlight-ipynb { background: var(--jp-cell-editor-background); color: var(--jp-mirror-editor-variable-color) }\n.highlight-ipynb .c { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment */\n.highlight-ipynb .err { color: var(--jp-mirror-editor-error-color) } /* Error */\n.highlight-ipynb .k { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword */\n.highlight-ipynb .o { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator */\n.highlight-ipynb .p { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation */\n.highlight-ipynb .ch { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Hashbang */\n.highlight-ipynb .cm { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Multiline */\n.highlight-ipynb .cp { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Preproc */\n.highlight-ipynb .cpf { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.PreprocFile */\n.highlight-ipynb .c1 { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Single */\n.highlight-ipynb .cs { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Special */\n.highlight-ipynb .kc { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Constant */\n.highlight-ipynb .kd { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Declaration */\n.highlight-ipynb .kn { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Namespace */\n.highlight-ipynb .kp { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Pseudo */\n.highlight-ipynb .kr { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Reserved */\n.highlight-ipynb .kt { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Type */\n.highlight-ipynb .m { color: var(--jp-mirror-editor-number-color) } /* Literal.Number */\n.highlight-ipynb .s { color: var(--jp-mirror-editor-string-color) } /* Literal.String */\n.highlight-ipynb .ow { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator.Word */\n.highlight-ipynb .pm { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation.Marker */\n.highlight-ipynb .w { color: var(--jp-mirror-editor-variable-color) } /* Text.Whitespace */\n.highlight-ipynb .mb { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Bin */\n.highlight-ipynb .mf { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Float */\n.highlight-ipynb .mh { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Hex */\n.highlight-ipynb .mi { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer */\n.highlight-ipynb .mo { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Oct */\n.highlight-ipynb .sa { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Affix */\n.highlight-ipynb .sb { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Backtick */\n.highlight-ipynb .sc { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Char */\n.highlight-ipynb .dl { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Delimiter */\n.highlight-ipynb .sd { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Doc */\n.highlight-ipynb .s2 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Double */\n.highlight-ipynb .se { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Escape */\n.highlight-ipynb .sh { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Heredoc */\n.highlight-ipynb .si { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Interpol */\n.highlight-ipynb .sx { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Other */\n.highlight-ipynb .sr { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Regex */\n.highlight-ipynb .s1 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Single */\n.highlight-ipynb .ss { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Symbol */\n.highlight-ipynb .il { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer.Long */']
</style>