Source code for coseda.export

import os
import datetime
import subprocess

[docs] def hkl2mtz(input_hkl, output_dir, cellfile_path): """ Convert a .hkl file to .mtz using get_hkl. All outputs (MTZ, logs) will be written into a new export_<timestamp> folder under output_dir. Parameters: input_hkl (str): Path to the input CrystFEL .hkl file (absolute or relative). output_dir (str): Directory under which to create export_<timestamp> folder. cellfile_path (str): Path to the cell file (.cell or .inp) for get_hkl (absolute or relative). """ # Create a timestamped export directory timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") export_dir = os.path.join(output_dir, f"export_{timestamp}") os.makedirs(export_dir, exist_ok=True) # Derive output MTZ filename from input_hkl base_name = os.path.splitext(os.path.basename(input_hkl))[0] output_mtz = f"{base_name}.mtz" # Construct full paths input_hkl_path = input_hkl if os.path.isabs(input_hkl) else os.path.join(output_dir, input_hkl) output_mtz_path = os.path.join(export_dir, output_mtz) cellfile_full = cellfile_path if os.path.isabs(cellfile_path) else os.path.join(output_dir, cellfile_path) # Ensure the input HKL exists if not os.path.exists(input_hkl_path): raise FileNotFoundError(f"Input HKL file not found: {input_hkl_path}") # Prepare log files stdout_log = os.path.join(export_dir, "hkl2mtz_stdout.log") stderr_log = os.path.join(export_dir, "hkl2mtz_stderr.log") # Build the get_hkl command hkl2mtz_cmd = [ 'get_hkl', '-i', input_hkl_path, '-o', output_mtz_path, '-p', cellfile_full, '--output-format=mtz' ] # Run the command with open(stdout_log, "w") as stdout_f, open(stderr_log, "w") as stderr_f: subprocess.run(hkl2mtz_cmd, stdout=stdout_f, stderr=stderr_f, check=True) return export_dir, output_mtz_path, stdout_log, stderr_log