Created
January 23, 2026 08:09
-
-
Save dutta-alankar/d6e82d796b563132ebe0e047557c5503 to your computer and use it in GitHub Desktop.
Create a set of `xmf` files at a different location that can refer to the original `hdf5` files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Sat Jan 23 09:06:48 2026 | |
| @author: alankar. | |
| Usage: time python create-duplicate-xmf.py <source_dir> <dest_dir> <last_file_number> | |
| """ | |
| import os | |
| import argparse | |
| import xml.etree.ElementTree as ET | |
| def update_xdmf_files(source_dir, dest_dir, max_index): | |
| # Ensure destination directory exists | |
| if not os.path.exists(dest_dir): | |
| os.makedirs(dest_dir) | |
| print(f"Created destination directory: {dest_dir}") | |
| for i in range(max_index + 1): | |
| filename = f"data.{i:04d}.flt.xmf" | |
| source_path = os.path.join(source_dir, filename) | |
| dest_path = os.path.join(dest_dir, filename) | |
| if not os.path.isfile(source_path): | |
| # Skip indices that don't have a corresponding file | |
| print(f"Could not find {source_path}/{filename}") | |
| continue | |
| print(f"Processing: {filename}...", end="\r") | |
| try: | |
| # Parse the XML file | |
| tree = ET.parse(source_path) | |
| root = tree.getroot() | |
| # Find all DataItem elements | |
| # They contain the strings like "data.0350.flt.h5:/path/to/data" | |
| for data_item in root.iter('DataItem'): | |
| if data_item.text: | |
| original_text = data_item.text.strip() | |
| # Prepend the source_directory to the text | |
| # We use the raw source_dir string provided in arguments | |
| new_text = f"\n {source_dir}/{original_text}\n " | |
| data_item.text = new_text | |
| # Write the modified XML to the destination | |
| # Note: We include the declaration and doctype manually to match your format | |
| with open(dest_path, 'wb') as f: | |
| f.write(b'<?xml version="1.0" ?>\n') | |
| f.write(b'<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>\n') | |
| tree.write(f, encoding='utf-8', xml_declaration=False) | |
| except Exception as e: | |
| print(f"Error processing {filename}: {e}") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Update XDMF files with new HDF5 source paths.") | |
| parser.add_argument("source_dir", help="Directory containing original .xmf and .h5 files") | |
| parser.add_argument("dest_dir", help="Directory where updated .xmf files will be saved") | |
| parser.add_argument("max_index", type=int, help="The upper limit of the file numbering (e.g., 350)") | |
| args = parser.parse_args() | |
| update_xdmf_files(args.source_dir, args.dest_dir, args.max_index) | |
| print("\nOperation complete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment