Loading sample_simulation_mcvine/post_processing/modify_nomad_xml.pydeleted 100644 → 0 +0 −75 Original line number Diff line number Diff line import xml.etree.ElementTree as ET def modify_nomad_xml(input_file, output_file): """ Modifies the NOMAD instrument definition XML file by: - Removing existing idlists - Assigning new idlists to banks - Flattening group components Parameters: input_file (str): Path to the input NOMAD XML file. output_file (str): Path to save the modified XML file. """ ET.register_namespace('', "http://www.mantidproject.org/IDF/1.0") tree = ET.parse(input_file) root = tree.getroot() # Remove existing idlists for idlist in root.findall("{http://www.mantidproject.org/IDF/1.0}idlist"): root.remove(idlist) # Remove idlist attributes from certain components for component in root.findall("{http://www.mantidproject.org/IDF/1.0}component"): if component.attrib.get("type") in ["moderator", "sample-position", "monitors"]: component.attrib.pop("idlist", None) # Assign new idlists to banks bank_counter = 0 current_id = 0 pixels_in_pack = {"pack": 8 * 128, "packhalf": 8 * 128, "packhalfshort": 16 * 64} for bank in root.findall(".//{http://www.mantidproject.org/IDF/1.0}component"): if "pack" in bank.attrib.get("type", "").lower(): pack_type = bank.attrib.get("type") pixel_count = pixels_in_pack[pack_type] bank_name = f"bank{bank_counter + 1}" bank_idlist_name = f"{bank_name}_ids" bank_idlist = ET.SubElement(root, "idlist", {"idname": bank_idlist_name}) ET.SubElement(bank_idlist, "id", {"start": str(current_id), "end": str(current_id + pixel_count - 1)}) bank.attrib["idlist"] = bank_idlist_name current_id += pixel_count bank_counter += 1 # Save intermediate XML file tree.write("/tmp/Modified_NOMAD_Definition.xml") # Reload the modified XML file tree = ET.parse("/tmp/Modified_NOMAD_Definition.xml") root = tree.getroot() namespace = "{http://www.mantidproject.org/IDF/1.0}" # Remove all Group components for component in root.findall(f"{namespace}component"): if "Group" in component.attrib.get("type", ""): root.remove(component) # Flatten nested 'pack' components for type_element in root.findall(f"{namespace}type"): if "Group" in type_element.attrib.get("name", ""): for nested_component in type_element.findall(f"{namespace}component"): if "pack" in nested_component.attrib.get("type", "").lower(): pack_type = nested_component.attrib.get("type") nested_component.set("type", pack_type) root.append(nested_component) root.remove(type_element) # Save the final modified XML file tree.write(output_file) print(f"Modified NOMAD XML saved to: {output_file}") # Example usage: # modify_nomad_xml("NOMAD_Definition.xml", "/tmp/Modified_NOMAD_Definition_no_groups.xml") Loading
sample_simulation_mcvine/post_processing/modify_nomad_xml.pydeleted 100644 → 0 +0 −75 Original line number Diff line number Diff line import xml.etree.ElementTree as ET def modify_nomad_xml(input_file, output_file): """ Modifies the NOMAD instrument definition XML file by: - Removing existing idlists - Assigning new idlists to banks - Flattening group components Parameters: input_file (str): Path to the input NOMAD XML file. output_file (str): Path to save the modified XML file. """ ET.register_namespace('', "http://www.mantidproject.org/IDF/1.0") tree = ET.parse(input_file) root = tree.getroot() # Remove existing idlists for idlist in root.findall("{http://www.mantidproject.org/IDF/1.0}idlist"): root.remove(idlist) # Remove idlist attributes from certain components for component in root.findall("{http://www.mantidproject.org/IDF/1.0}component"): if component.attrib.get("type") in ["moderator", "sample-position", "monitors"]: component.attrib.pop("idlist", None) # Assign new idlists to banks bank_counter = 0 current_id = 0 pixels_in_pack = {"pack": 8 * 128, "packhalf": 8 * 128, "packhalfshort": 16 * 64} for bank in root.findall(".//{http://www.mantidproject.org/IDF/1.0}component"): if "pack" in bank.attrib.get("type", "").lower(): pack_type = bank.attrib.get("type") pixel_count = pixels_in_pack[pack_type] bank_name = f"bank{bank_counter + 1}" bank_idlist_name = f"{bank_name}_ids" bank_idlist = ET.SubElement(root, "idlist", {"idname": bank_idlist_name}) ET.SubElement(bank_idlist, "id", {"start": str(current_id), "end": str(current_id + pixel_count - 1)}) bank.attrib["idlist"] = bank_idlist_name current_id += pixel_count bank_counter += 1 # Save intermediate XML file tree.write("/tmp/Modified_NOMAD_Definition.xml") # Reload the modified XML file tree = ET.parse("/tmp/Modified_NOMAD_Definition.xml") root = tree.getroot() namespace = "{http://www.mantidproject.org/IDF/1.0}" # Remove all Group components for component in root.findall(f"{namespace}component"): if "Group" in component.attrib.get("type", ""): root.remove(component) # Flatten nested 'pack' components for type_element in root.findall(f"{namespace}type"): if "Group" in type_element.attrib.get("name", ""): for nested_component in type_element.findall(f"{namespace}component"): if "pack" in nested_component.attrib.get("type", "").lower(): pack_type = nested_component.attrib.get("type") nested_component.set("type", pack_type) root.append(nested_component) root.remove(type_element) # Save the final modified XML file tree.write(output_file) print(f"Modified NOMAD XML saved to: {output_file}") # Example usage: # modify_nomad_xml("NOMAD_Definition.xml", "/tmp/Modified_NOMAD_Definition_no_groups.xml")