TNO Intern

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

my first commit

parent 71a6276a
Loading
Loading
Loading
Loading
+112 −0
Original line number Diff line number Diff line
import copy
from pathlib import Path

from src.datamodels.configuration import Config
import docker
import xmltodict

from src.utils.docker_util import docker_run


def edit_wp5_configuration_file(config: Config, xml_file: Path):
    """Take the configuration xml files from the App settings directory, edit them to reflect the path of the container, and overwrite the original files"""
    #   Editing the configuration files for the java code is ugly:
    #   -there are 12 variables we need to change just to say we only calculate P10, P30, P50, P90
    #   -Likewise we need to change 5 variables to set the aquifers we want to calculate.
    #   -A string "1" or "0" denotes true or false.
    stacked_aquifer_combinations = {
        "TR_STACKED": "TR_STACKED;RBMDL, RBMDU, RBMH, RBMVL, RBMVU, RBSHN, RNROF, RNSOB",
        "SLDN_STACKED": "SLDN_STACKED;SLDNA, SLDND",
        "KN_STACKED": "KN_STACKED;KNNSY, KNNSR, KNNSL, KNNSG, KNNSF_KNNSP, KNNSB, KNGLG_KNGLS",
        "RO_STACKED": "RO_STACKED;ROSL_ROSLU, ROSLL",
        "DC_STACKED": "DC_STACKED;DCH, DCD",
        "N_STACKED": "N_STACKED;NMVESO, NMVEVO, NMRUST, NMRUBE, NLDOBR, NLDOOO, NLLARE, NLLAOR"
    }

    # Read xml file as a python dictionary
    with open(xml_file, "r") as file:
        xml_content = file.read()
    wp5_config_dictionary = xmltodict.parse(xml_content)
    project_dict = wp5_config_dictionary["project"]
    is_ates = project_dict["@ATES"] == "true"

    # General parameters
    project_dict["copy_aquifer_files"] = "0"
    project_dict["calculate_stacked_aquifers"] = "0"
    project_dict["input_data_directory"] = "/data/InputData"
    project_dict["results_directory"] = "/data/Results"
    project_dict["max_number_of_processors_for_calculations"] = "10"
    project_dict["validate_input_grids"] = "0"
    project_dict["validate_output_grids"] = "0"
    project_dict["output_maps_for_petrel"] = "0"
    project_dict["output_grid_file_type___zmap___asc___nc__beta__"] = config.grid_extension
    project_dict["remove_padding_from_input_grids"] = "0"
    project_dict["xy_grid_size_factor_for_thickness_grid__integer_"] = "1"
    project_dict["xy_grid_size_factor_for_mask_grid"] = "1"

    # grid names: change to using the user inputted suffix
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_1"] = f"Permeability;no;_perm{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_2"] = f"PermeabilityLNSD;no;_ln_perm_sd{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_3"] = f"Porosity;no;_poro{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_4"] = f"Thickness;no;_thick{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_5"] = f"ThicknessSD;no;_thick_sd{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_6"] = f"Depth;no;_top{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_7"] = f"NetToGross;no;_ntg{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_8"] = f"Temperature;yes;__temperature{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_9"] = f"HCAccum;yes;_hc_accum{config.grid_extension}"
    project_dict["property_grids__in_input_data_directory__stacked_grids_will_be_created_"]["row_10"] = "BoundaryShapefile;yes;__BoundaryShapefile.shp"

    # p-values to process
    #   UTC
    project_dict["pvalues_to_calculate"] = "50"

    # Aquifers to Process
    aquifers_and_stacked_aquifers_string, stacked_aquifers_string, aquifers_string = generate_aquifer_strings(config.WP5.aquifers, config.WP5.stacked_aquifers)
    project_dict["aquifers_to_calculate"] = aquifers_and_stacked_aquifers_string
    project_dict["aquifer_list__comma_separated__excluding_stacked_aquifers_"] = aquifers_string
    project_dict["aquifers_for_potential_calculation"] = aquifers_and_stacked_aquifers_string
    project_dict["stacked_aquifers"] = {}
    for i in range(len(config.WP5.stacked_aquifers)):
        project_dict["stacked_aquifers"][f"row_{i}"] = stacked_aquifer_combinations[config.WP5.stacked_aquifers[i]]
    project_dict["aquifers_to_use_in_overview_calculation"] = stacked_aquifers_string

    # Temperature voxet and mask file
    project_dict["temperature_voxet_file"] = "/data/InputTemperatureModel/restart.vo"
    project_dict["mask_file__for_output_geometry_"] = "/data/InputData/mask.nc"


    # Overwrite the original xml file
    xml_output = xmltodict.unparse(wp5_config_dictionary, pretty=True)
    with open(xml_file, "w") as file:
        file.write(xml_output)


def generate_aquifer_strings(aquifers, aquifers_stacked):
    aquifers_string = ""
    for s in aquifers:
        aquifers_string += s + ", "

    aquifers_stacked_string = ""
    aquifers_and_stacked_aquifers_string = copy.deepcopy(aquifers_string)
    for s in aquifers_stacked:
        aquifers_stacked_string += s + ", "
        aquifers_and_stacked_aquifers_string += s + ", "

    return aquifers_and_stacked_aquifers_string, aquifers_stacked_string, aquifers_string


def run_wp5_docker_image(config: Config):
    """using docker create the python environment and run the code"""
    print("copy & edit configuration xml files")
    app_settings_path = config.model_path / "xml"
    wp5_scenario_xmls = list(app_settings_path.glob("*.xml"))
    [edit_wp5_configuration_file(config, xml_file) for xml_file in wp5_scenario_xmls]

    print(f"run docker image on {len(wp5_scenario_xmls)} scenarios")
    for xml_file in wp5_scenario_xmls:
        run_scenario(config, xml_file)


def run_scenario(config: Config, xml_file: Path):
    client = docker.from_env()
    docker_run(client, config.WP5.docker_image_tag, command=[f"/data/xml/{xml_file.name}"], volumes=[f'{str(config.model_path)}:/data'])
 No newline at end of file
+17099 −0

File added.

Preview size limit exceeded, changes collapsed.

+837 −0

File added.

Preview size limit exceeded, changes collapsed.

+583 −0

File added.

Preview size limit exceeded, changes collapsed.

+583 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading