Mercurial > repos > muon-spectroscopy-computational-project > muspinsim_config
diff build_file.py @ 4:e1e338f56656 draft default tip
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_config commit 70a4d37ecdf5d586703cfc509922311e95d3205c
author | muon-spectroscopy-computational-project |
---|---|
date | Tue, 18 Jul 2023 13:26:20 +0000 |
parents | 331d0776abb4 |
children |
line wrap: on
line diff
--- a/build_file.py Fri Feb 03 15:39:07 2023 +0000 +++ b/build_file.py Tue Jul 18 13:26:20 2023 +0000 @@ -1,6 +1,7 @@ import json import re import sys +from typing import List from muspinsim import MuSpinInput @@ -395,21 +396,63 @@ return err_found +def append_template_file( + template_path: str, + mu_params: dict, + file_contents: List[str] +): + """ + Loads an input file generated using muspinsim-gen and appends its contents + to what has already been created from config. Also ensures that the spins + are appended correctly. + """ + # Check if we have already defined spins in the file + spins_line = None + spins_line_index = None + if ("spins" in mu_params): + # Find the current line definition in the file + # In the format 'spins\n e\n' + for i, line in enumerate(file_contents): + if line.startswith("spins"): + spins_line = line.split("\n")[1].strip() + spins_line_index = i + if spins_line_index is not None: + del file_contents[spins_line_index] + + # Append the template file's contents + with open(template_path, encoding="utf-8") as template_file: + for line in template_file: + # Append the spins if needed + if line.startswith("spins") and spins_line is not None: + next_line = template_file.readline().strip() + file_contents += f"spins\n {next_line} {spins_line}\n" + else: + file_contents += line + + def main(): """ Entry point """ input_json_path = sys.argv[1] - mu_params = json.load(open(input_json_path, "r", encoding="utf-8")) + mu_input_params = json.load(open(input_json_path, "r", encoding="utf-8")) + + out_file_name = mu_input_params["out_file_prefix"].strip().replace( + " ", "_") - out_file_name = mu_params["out_file_prefix"].strip().replace(" ", "_") + # Check if using a template + template_path = None + if (mu_input_params["use_structure_file_conditional"] + ["use_structure_file"]) == "true": + template_path = "muspinsim_gen_out.in" # combine all sections mu_params = { - **mu_params["spins"], - **mu_params["interaction_params"], - **mu_params["experiment_params"], - **mu_params["fitting_params"]["fitting_options"], + **mu_input_params["use_structure_file_conditional"]["spins"], + ** (mu_input_params["use_structure_file_conditional"] + ["interaction_params"]), + **mu_input_params["experiment_params"], + **mu_input_params["fitting_params"]["fitting_options"], } # get experiment parameters @@ -430,6 +473,10 @@ if parse_dict(parse_func_dict, mu_params, file_contents): sys.exit(1) + # Load and append the template if specified + if template_path is not None: + append_template_file(template_path, mu_params, file_contents) + write_file("outfile.in", file_contents) try: