Loading sample_simulation_mcvine/base/mcvine_script.py 0 → 100644 +46 −0 Original line number Diff line number Diff line from detector_utils import extract_bank_data from detector_utils import generate_detectors def generate_mcvine_script_from_xml(xml_file, output_file="twodiamonds.py"): """ Extracts bank data from an XML file, generates detectors, and creates a McVine instrument script. Parameters: xml_file (str): Path to the XML instrument definition file. output_file (str): Path for the final McVine instrument script. """ original_content = """ import mcvine, mcvine.components as mc import numpy as np from EventMonitor_4D import EventMonitor_4D as E4D from detector import Detector samplename = "Al" def instrument(): instrument = mcvine.instrument() source = mc.sources.NeutronFromStorage('source', 'beam/NOMAD_incidentBeam_widerLambda') instrument.append(source, position=(0,0,0)) sample = mc.samples.SampleAssemblyFromXml('sample', "grid_SQ/sampleassembly.xml") instrument.append(sample, position=(0,0,0.5)) """ # Extract bank data from XML bank_data = extract_bank_data(xml_file) # Generate detector snippet snippet_string = generate_detectors(bank_data) # Format and append generated detectors final_content = original_content + "\n" + snippet_string.replace("\n", "\n ") + "\n return instrument\n" # Write the final script to file with open(output_file, "w") as file: file.write(final_content) print(f"McVine instrument script generated: {output_file}") return final_content # Example usage: # generate_mcvine_script_from_xml("NOMAD_Definition.xml", "twodiamonds.py") Loading
sample_simulation_mcvine/base/mcvine_script.py 0 → 100644 +46 −0 Original line number Diff line number Diff line from detector_utils import extract_bank_data from detector_utils import generate_detectors def generate_mcvine_script_from_xml(xml_file, output_file="twodiamonds.py"): """ Extracts bank data from an XML file, generates detectors, and creates a McVine instrument script. Parameters: xml_file (str): Path to the XML instrument definition file. output_file (str): Path for the final McVine instrument script. """ original_content = """ import mcvine, mcvine.components as mc import numpy as np from EventMonitor_4D import EventMonitor_4D as E4D from detector import Detector samplename = "Al" def instrument(): instrument = mcvine.instrument() source = mc.sources.NeutronFromStorage('source', 'beam/NOMAD_incidentBeam_widerLambda') instrument.append(source, position=(0,0,0)) sample = mc.samples.SampleAssemblyFromXml('sample', "grid_SQ/sampleassembly.xml") instrument.append(sample, position=(0,0,0.5)) """ # Extract bank data from XML bank_data = extract_bank_data(xml_file) # Generate detector snippet snippet_string = generate_detectors(bank_data) # Format and append generated detectors final_content = original_content + "\n" + snippet_string.replace("\n", "\n ") + "\n return instrument\n" # Write the final script to file with open(output_file, "w") as file: file.write(final_content) print(f"McVine instrument script generated: {output_file}") return final_content # Example usage: # generate_mcvine_script_from_xml("NOMAD_Definition.xml", "twodiamonds.py")