TNO Intern

Commit c1108e0b authored by Hen Brett's avatar Hen Brett 🐔
Browse files

Enabling the parsing of TG scenarios from an xml

parent b4214402
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -2,16 +2,22 @@ from pathlib import Path
import xml.etree.ElementTree as ET
from pythermogis.thermogis_classes.jvm_start import start_jvm
from jpype import JClass
import os

def instantiate_utc_properties_from_xml(xml_file: str | Path) -> JClass:
    """Provided the path to an xml scenario file, parse this file for the utc properties; beware, the xml parses does some validation checks, checking that files exist.
    Even if these parameters are not needed by the pyThermoGIS module, if these validation checks fail, the xml parser will fail."""
    start_jvm()
    xml_parser = JClass("thermogis.properties.parsers.UTCXmlParser")()
    tree = ET.parse(xml_file)
    root = tree.getroot()
    root = ET.parse(xml_file).getroot() # Parsing the xml file to a string

    # change the parameters input_data_directory, results_directory, and temperature_voxet_file to null
    variables = [root.find(variable) for variable in ["input_data_directory", "results_directory", "temperature_voxet_file"]]
    for var in variables:
        var.text = ""

    # parse to string and pass to the utc xml parser
    xmlstr = ET.tostring(root, encoding='utf8', method='xml')
    return xml_parser.parse(xmlstr)
    return JClass("thermogis.properties.parsers.UTCXmlParser")().parse(xmlstr)

def instantiate_utc_properties_builder() -> JClass:
    """
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@
	<tolerance_of_utc_increase___bar_>0.0</tolerance_of_utc_increase___bar_>
	<temperature_from_input_grids>0.0</temperature_from_input_grids>
	<use_3d_temperature_voxet_model>1.0</use_3d_temperature_voxet_model>
	<temperature_voxet_file>/data/InputTemperatureModel/restart.vo</temperature_voxet_file>
	<temperature_voxet_file>C:/restart.vo</temperature_voxet_file>
	<temp_gradient__surface_temp__below__also_used_>31.0</temp_gradient__surface_temp__below__also_used_>
	<surface_temperature>10.0</surface_temperature>
	<kh_cutoff__speed_up_calculation_>1.0</kh_cutoff__speed_up_calculation_>
+22 −6
Original line number Diff line number Diff line
from os import path
from pathlib import Path
from unittest import TestCase
from unittest.case import skip
import xarray as xr

from pythermogis import calculate_doublet_performance
from pythermogis.thermogis_classes.utc_properties import instantiate_utc_properties_from_xml


class UTCBuilder(TestCase):
    scenarios_file_path = Path(path.dirname(__file__), "resources") / "test_input" / "scenarios"

    @skip
    def test_parse_scenario_xmls(self):
        # This doesn't work as the xmls contain paths; these paths are not needed by this module; yet the xml parser runs a check on whether the paths exist.
        # This is not as simple as I first thought to implement.
        # This test is being left here as an example from which to build upon in the future.
        instantiate_utc_properties_from_xml(self.scenarios_file_path / "doublet_techno-econ_basecase.xml")
 No newline at end of file
        # The ga4a scenarios no longer work as they are missing necessary input parameters; they were generated by an older version of TG
        scenarios = ["doublet_techno-econ_basecase.xml", "doublet_techno-econ_HP.xml", "doublet_techno-econ_STIM.xml", "doublet_techno-econ_STIM_HP.xml"]
        utc_properties = [instantiate_utc_properties_from_xml(self.scenarios_file_path / scenario) for scenario in scenarios]

        # If test reaches here, then parsing of xml's worked, check that the scenarios actually run on a set of test data:
        [self.run_scenario(utc_property) for utc_property in utc_properties]

    def run_scenario(self, utc_properties):
        # This tests that the python API runs on a simple set of input with single values
        input_data = xr.Dataset({
            "thickness_mean": ((), 300),
            "thickness_sd": ((), 50),
            "ntg": ((), 0.5),
            "porosity": ((), 0.5),
            "depth": ((), 5000),
            "ln_permeability_mean": ((), 5),
            "ln_permeability_sd": ((), 0.5),
        })

        calculate_doublet_performance(input_data, utc_properties=utc_properties, p_values=[10, 50, 90])