comparison 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
comparison
equal deleted inserted replaced
3:331d0776abb4 4:e1e338f56656
1 import json 1 import json
2 import re 2 import re
3 import sys 3 import sys
4 from typing import List
4 5
5 from muspinsim import MuSpinInput 6 from muspinsim import MuSpinInput
6 7
7 8
8 def write_file(file_name, content): 9 def write_file(file_name, content):
393 ) 394 )
394 err_found = True 395 err_found = True
395 return err_found 396 return err_found
396 397
397 398
399 def append_template_file(
400 template_path: str,
401 mu_params: dict,
402 file_contents: List[str]
403 ):
404 """
405 Loads an input file generated using muspinsim-gen and appends its contents
406 to what has already been created from config. Also ensures that the spins
407 are appended correctly.
408 """
409 # Check if we have already defined spins in the file
410 spins_line = None
411 spins_line_index = None
412 if ("spins" in mu_params):
413 # Find the current line definition in the file
414 # In the format 'spins\n e\n'
415 for i, line in enumerate(file_contents):
416 if line.startswith("spins"):
417 spins_line = line.split("\n")[1].strip()
418 spins_line_index = i
419 if spins_line_index is not None:
420 del file_contents[spins_line_index]
421
422 # Append the template file's contents
423 with open(template_path, encoding="utf-8") as template_file:
424 for line in template_file:
425 # Append the spins if needed
426 if line.startswith("spins") and spins_line is not None:
427 next_line = template_file.readline().strip()
428 file_contents += f"spins\n {next_line} {spins_line}\n"
429 else:
430 file_contents += line
431
432
398 def main(): 433 def main():
399 """ 434 """
400 Entry point 435 Entry point
401 """ 436 """
402 input_json_path = sys.argv[1] 437 input_json_path = sys.argv[1]
403 mu_params = json.load(open(input_json_path, "r", encoding="utf-8")) 438 mu_input_params = json.load(open(input_json_path, "r", encoding="utf-8"))
404 439
405 out_file_name = mu_params["out_file_prefix"].strip().replace(" ", "_") 440 out_file_name = mu_input_params["out_file_prefix"].strip().replace(
441 " ", "_")
442
443 # Check if using a template
444 template_path = None
445 if (mu_input_params["use_structure_file_conditional"]
446 ["use_structure_file"]) == "true":
447 template_path = "muspinsim_gen_out.in"
406 448
407 # combine all sections 449 # combine all sections
408 mu_params = { 450 mu_params = {
409 **mu_params["spins"], 451 **mu_input_params["use_structure_file_conditional"]["spins"],
410 **mu_params["interaction_params"], 452 ** (mu_input_params["use_structure_file_conditional"]
411 **mu_params["experiment_params"], 453 ["interaction_params"]),
412 **mu_params["fitting_params"]["fitting_options"], 454 **mu_input_params["experiment_params"],
455 **mu_input_params["fitting_params"]["fitting_options"],
413 } 456 }
414 457
415 # get experiment parameters 458 # get experiment parameters
416 experiment = mu_params["experiment"] 459 experiment = mu_params["experiment"]
417 mu_params = {**mu_params, **experiment} 460 mu_params = {**mu_params, **experiment}
427 build_block("name", [out_file_name.strip().replace(" ", "_")]) 470 build_block("name", [out_file_name.strip().replace(" ", "_")])
428 ] 471 ]
429 472
430 if parse_dict(parse_func_dict, mu_params, file_contents): 473 if parse_dict(parse_func_dict, mu_params, file_contents):
431 sys.exit(1) 474 sys.exit(1)
475
476 # Load and append the template if specified
477 if template_path is not None:
478 append_template_file(template_path, mu_params, file_contents)
432 479
433 write_file("outfile.in", file_contents) 480 write_file("outfile.in", file_contents)
434 481
435 try: 482 try:
436 MuSpinInput(open("outfile.in", encoding="utf-8")) 483 MuSpinInput(open("outfile.in", encoding="utf-8"))