From f74dddb98b7e8ea7a121ab30e03c90e26f1ea2f2 Mon Sep 17 00:00:00 2001 From: David Kleiven Date: Mon, 26 Aug 2024 14:33:34 +0200 Subject: [PATCH] fix: switch to eic code for center nodes and distinguish enstsoe namespaces enstoe: cim/schemaextension entsoeSecretariat: secretariat/profilextension --- cimsparql/data_models.py | 2 +- cimsparql/model.py | 7 ++++++- cimsparql/sparql/bus.sparql | 6 +++--- cimsparql/sparql/connectivity_nodes.sparql | 18 ++++++++++-------- .../add_eic_bidding_area_code.sparql | 4 ++-- .../sparql/transformer_center_nodes.sparql | 5 +++-- pkg_data/namespaces.json | 3 ++- tests/test_graphdb.py | 1 + tests/test_xml_adaptor.py | 5 +++-- 9 files changed, 31 insertions(+), 20 deletions(-) diff --git a/cimsparql/data_models.py b/cimsparql/data_models.py index dddb7e89..8fcb65c6 100644 --- a/cimsparql/data_models.py +++ b/cimsparql/data_models.py @@ -64,7 +64,7 @@ class BusDataSchema(JsonSchemaOut): un: Series[float] = pa.Field() substation_mrid: Series[str] = pa.Field() bidzone: Series[str] = pa.Field(nullable=True) - sv_voltage: Series[float] = pa.Field(nullable=True) + sv_voltage: Series[float] = pa.Field() island: Series[str] = pa.Field() is_swing_bus: Series[bool] = pa.Field() diff --git a/cimsparql/model.py b/cimsparql/model.py index 5a0ac376..272a78d0 100644 --- a/cimsparql/model.py +++ b/cimsparql/model.py @@ -248,7 +248,7 @@ def bus_data(self, region: str | None = None) -> BusDataFrame: """ dfs = [ self.get_table_and_convert(self.bus_data_query(region), index="node"), - self.get_table_and_convert(self.transformer_center_nodes_query(region), index="node"), + self.transformer_center_nodes(region), ] df = pd.concat(dfs) return BusDataFrame(df) @@ -257,6 +257,11 @@ def loads_query(self, region: str | None = None) -> str: substitutes = {"region": region or ".*"} return self.template_to_query(templates.LOADS_QUERY, substitutes) + @time_it + def transformer_center_nodes(self, region: str | None = None) -> BusDataFrame: + df = self.get_table_and_convert(self.transformer_center_nodes_query(region), index="node") + return BusDataFrame(df) + @time_it def loads(self, region: str | None = None) -> LoadsDataFrame: """Query load data. diff --git a/cimsparql/sparql/bus.sparql b/cimsparql/sparql/bus.sparql index 0a4dcde2..fa89d4ee 100644 --- a/cimsparql/sparql/bus.sparql +++ b/cimsparql/sparql/bus.sparql @@ -1,7 +1,7 @@ # Name: Bus PREFIX cim:<${cim}> PREFIX SN:<${SN}> -PREFIX entsoe:<${entsoe}> +PREFIX entsoeSecretariat:<${entsoeSecretariat}> select (?mrid as ?node) ?busname (?substation_name as ?substation) ?un ?substation_mrid ?bidzone ?sv_voltage ?island ?is_swing_bus where { # Extract propertoes from the TP/SV/SSH profile for each topological node @@ -28,7 +28,7 @@ where { # Extract the bidzone of each substation if it exists optional { - ?_substation SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoe:IdentifiedObject.energyIdentCodeEIC ?direct_bidzone + ?_substation SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC ?direct_bidzone } } . optional { @@ -40,7 +40,7 @@ where { ?_eq_subj2 ?eq_repo2 . service ?eq_repo2 { # Collect bidzones from substations connected to the current substation via a ConductingEquipment - ?con_node ^cim:Terminal.ConnectivityNode/cim:Terminal.ConductingEquipment/^cim:Terminal.ConductingEquipment/cim:Terminal.ConnectivityNode/cim:ConnectivityNode.ConnectivityNodeContainer/cim:VoltageLevel.Substation/SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoe:IdentifiedObject.energyIdentCodeEIC ?connected_bidzone . + ?con_node ^cim:Terminal.ConnectivityNode/cim:Terminal.ConductingEquipment/^cim:Terminal.ConductingEquipment/cim:Terminal.ConnectivityNode/cim:ConnectivityNode.ConnectivityNodeContainer/cim:VoltageLevel.Substation/SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC ?connected_bidzone . }} group by ?top_node} } bind(coalesce(?direct_bidzone, ?nearby_bidzone) as ?bidzone) diff --git a/cimsparql/sparql/connectivity_nodes.sparql b/cimsparql/sparql/connectivity_nodes.sparql index a4c52346..38023141 100644 --- a/cimsparql/sparql/connectivity_nodes.sparql +++ b/cimsparql/sparql/connectivity_nodes.sparql @@ -2,7 +2,7 @@ PREFIX cim:<${cim}> PREFIX SN:<${SN}> -PREFIX entsoe:<${entsoe}> +PREFIX entsoeSecretariat:<${entsoeSecretariat}> select ?mrid (?container_mrid as ?container) ?container_name ?un ?bidzone ?container_type where { @@ -19,19 +19,21 @@ where { cim:IdentifiedObject.name ?container_name; cim:Substation.Region/cim:SubGeographicalRegion.Region/cim:IdentifiedObject.name ?area . # Extract the bidzone of each substation if it exists + optional { + ?_substation SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC ?direct_bidzone + } . { - ?_substation SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoe:IdentifiedObject.energyIdentCodeEIC ?bidzone - } union { - filter not exists {?_substation SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoe:IdentifiedObject.energyIdentCodeEIC ?non_existent_bz} + filter(!bound(?bidzone)) # When an EIC code does not exist, look for an EIC code in neighbouring nodes { - select (max(?related_bidzone) as ?bidzone) where { + select (max(?related_bidzone) as ?nearby_bidzone) where { # Collect bidzones from substations connected to the current substation via a ConductingEquipment - ?con_node ^cim:Terminal.ConnectivityNode/cim:Terminal.ConductingEquipment/^cim:Terminal.ConductingEquipment/cim:Terminal.ConnectivityNode/cim:ConnectivityNode.ConnectivityNodeContainer/cim:VoltageLevel.Substation/SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoe:IdentifiedObject.energyIdentCodeEIC ?related_bidzone . + ?con_node ^cim:Terminal.ConnectivityNode/cim:Terminal.ConductingEquipment/^cim:Terminal.ConductingEquipment/cim:Terminal.ConnectivityNode/cim:ConnectivityNode.ConnectivityNodeContainer/cim:VoltageLevel.Substation/SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC ?related_bidzone . } } - } - bind("Substation" as ?container_type) + } + bind("Substation" as ?container_type) + bind(coalesce(?direct_bidzone, ?nearby_bidzone) as ?bidzone) } union { ?con_node_container cim:Line.Region/cim:IdentifiedObject.name ?area ; cim:IdentifiedObject.mRID ?container_mrid; diff --git a/cimsparql/sparql/test_configuration_modifications/add_eic_bidding_area_code.sparql b/cimsparql/sparql/test_configuration_modifications/add_eic_bidding_area_code.sparql index 6b913f17..e7b46c92 100644 --- a/cimsparql/sparql/test_configuration_modifications/add_eic_bidding_area_code.sparql +++ b/cimsparql/sparql/test_configuration_modifications/add_eic_bidding_area_code.sparql @@ -1,11 +1,11 @@ prefix cim:<${cim}> prefix SN:<${SN}> -prefix entsoe:<${entsoe}> +prefix entsoeSecretariat:<${entsoeSecretariat}> insert { graph { ?substation SN:Substation.MarketDeliveryPoint _:b0 . _:b0 SN:MarketDeliveryPoint.BiddingArea _:b1 . - _:b1 entsoe:IdentifiedObject.energyIdentCodeEIC "10Y1001A1001A48H" + _:b1 entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC "10Y1001A1001A48H" }} where { ?substation a cim:Substation diff --git a/cimsparql/sparql/transformer_center_nodes.sparql b/cimsparql/sparql/transformer_center_nodes.sparql index 2a844819..a4389373 100644 --- a/cimsparql/sparql/transformer_center_nodes.sparql +++ b/cimsparql/sparql/transformer_center_nodes.sparql @@ -1,7 +1,8 @@ # Name: Transformer center nodes PREFIX cim:<${cim}> PREFIX SN:<${SN}> -select ?node ?busname (?container_name as ?substation) ?un (?container_mrid as ?substation_mrid) ?bidzone ?island (False as ?is_swing_bus) ?connectivity_node +PREFIX entsoeSecretariat:<${entsoeSecretariat}> +select ?node ?busname (?container_name as ?substation) ?un (?container_mrid as ?substation_mrid) ?bidzone ?island (False as ?is_swing_bus) ?connectivity_node (?un as ?sv_voltage) where { optional{ @@ -23,7 +24,7 @@ where ?con_node cim:IdentifiedObject.mRID ?connectivity_node . ?container cim:IdentifiedObject.mRID ?container_mrid; cim:IdentifiedObject.name ?container_name. - optional {?container SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/SN:BiddingArea.marketCode ?bidzone} + ?container SN:Substation.MarketDeliveryPoint/SN:MarketDeliveryPoint.BiddingArea/entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC ?bidzone optional {?p_transformer SN:Equipment.networkAnalysisEnable ?_network_analysis} } filter(?network_analysis) diff --git a/pkg_data/namespaces.json b/pkg_data/namespaces.json index b4415110..1fd58f07 100644 --- a/pkg_data/namespaces.json +++ b/pkg_data/namespaces.json @@ -4,7 +4,8 @@ "cim": "http://iec.ch/TC57/2013/CIM-schema-cim16#", "cims": "http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#", "dc": "http://purl.org/dc/elements/1.1/", - "entsoe": "http://entsoe.eu/Secretariat/ProfileExtension/1#", + "entsoeSecretariat": "http://entsoe.eu/Secretariat/ProfileExtension/1#", + "entsoe": "http://entsoe.eu/CIM/SchemaExtension/3/1#", "foaf": "http://xmlns.com/foaf/0.1/", "md": "http://iec.ch/TC57/61970-552/ModelDescription/1#", "owl": "http://www.w3.org/2002/07/owl#", diff --git a/tests/test_graphdb.py b/tests/test_graphdb.py index 9272a84f..d18de38e 100644 --- a/tests/test_graphdb.py +++ b/tests/test_graphdb.py @@ -79,6 +79,7 @@ async def collect_data(model: Model) -> dict[str, pd.DataFrame]: model.transformer_windings, model.transformers, model.transformers_connected_to_converter, + model.transformer_center_nodes, model.wind_generating_units, ) diff --git a/tests/test_xml_adaptor.py b/tests/test_xml_adaptor.py index f4b21a69..d3deb3f5 100644 --- a/tests/test_xml_adaptor.py +++ b/tests/test_xml_adaptor.py @@ -54,6 +54,7 @@ def test_add_sv_injection(xml_adaptor: XmlModelAdaptor) -> None: def test_add_eic_code(xml_adaptor: XmlModelAdaptor) -> None: query = "select * where {?s a cim:Substation}" num_substations = len(xml_adaptor.graph.query(query)) + assert num_substations > 0 xml_adaptor.add_eic_code() - query = "select * where {?s entsoe:IdentifiedObject.energyIdentCodeEIC ?eic}" - assert len(xml_adaptor.graph.query(query)) == num_substations + query = "select * where {?s entsoeSecretariat:IdentifiedObject.energyIdentCodeEIC ?eic}" + assert len(xml_adaptor.graph.query(query, initNs=xml_adaptor.namespaces())) == num_substations