From 783aa73667230cd2746d70f7dcd696420caed0e2 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Mon, 29 Apr 2024 10:10:00 -0400 Subject: [PATCH 1/2] initial commit of loading all readable files if not specified --- src/diffpy/labpdfproc/labpdfprocapp.py | 71 +++++++++++++++----------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/src/diffpy/labpdfproc/labpdfprocapp.py b/src/diffpy/labpdfproc/labpdfprocapp.py index 93b57b2..63b0139 100644 --- a/src/diffpy/labpdfproc/labpdfprocapp.py +++ b/src/diffpy/labpdfproc/labpdfprocapp.py @@ -1,3 +1,4 @@ +import glob import sys from argparse import ArgumentParser from pathlib import Path @@ -63,41 +64,49 @@ def get_args(): def main(): args = get_args() wavelength = WAVELENGTHS[args.anode_type] - filepath = Path(args.input_file) - outfilestem = filepath.stem + "_corrected" - corrfilestem = filepath.stem + "_cve" - outfile = Path(outfilestem + ".chi") - corrfile = Path(corrfilestem + ".chi") + input_dir = Path.cwd() + if args.input_file: + filepath = Path(args.input_file) + input_dir = Path.cwd() / filepath.parent - if outfile.exists() and not args.force_overwrite: - sys.exit( - f"output file {str(outfile)} already exists. Please rerun " - f"specifying -f if you want to overwrite it" - ) - if corrfile.exists() and args.output_correction and not args.force_overwrite: - sys.exit( - f"corrections file {str(corrfile)} was requested and already " - f"exists. Please rerun specifying -f if you want to overwrite it" - ) + input_files = glob.glob(str(input_dir) + "/*", recursive=True) + filepaths = [Path(file) for file in input_files] - input_pattern = Diffraction_object(wavelength=wavelength) - xarray, yarray = loadData(args.input_file, unpack=True) - input_pattern.insert_scattering_quantity( - xarray, - yarray, - "tth", - scat_quantity="x-ray", - name=str(args.input_file), - metadata={"muD": args.mud, "anode_type": args.anode_type}, - ) + for filepath in filepaths: + outfilestem = filepath.stem + "_corrected" + corrfilestem = filepath.stem + "_cve" + outfile = Path(outfilestem + ".chi") + corrfile = Path(corrfilestem + ".chi") + + if outfile.exists() and not args.force_overwrite: + sys.exit( + f"output file {str(outfile)} already exists. Please rerun " + f"specifying -f if you want to overwrite it" + ) + if corrfile.exists() and args.output_correction and not args.force_overwrite: + sys.exit( + f"corrections file {str(corrfile)} was requested and already " + f"exists. Please rerun specifying -f if you want to overwrite it" + ) + + input_pattern = Diffraction_object(wavelength=wavelength) + xarray, yarray = loadData(filepath.name, unpack=True) + input_pattern.insert_scattering_quantity( + xarray, + yarray, + "tth", + scat_quantity="x-ray", + name=str(args.input_file), + metadata={"muD": args.mud, "anode_type": args.anode_type}, + ) - absorption_correction = compute_cve(input_pattern, args.mud, wavelength) - corrected_data = apply_corr(input_pattern, absorption_correction) - corrected_data.name = f"Absorption corrected input_data: {input_pattern.name}" - corrected_data.dump(f"{outfile}", xtype="tth") + absorption_correction = compute_cve(input_pattern, args.mud, wavelength) + corrected_data = apply_corr(input_pattern, absorption_correction) + corrected_data.name = f"Absorption corrected input_data: {input_pattern.name}" + corrected_data.dump(f"{outfile}", xtype="tth") - if args.output_correction: - absorption_correction.dump(f"{corrfile}", xtype="tth") + if args.output_correction: + absorption_correction.dump(f"{corrfile}", xtype="tth") if __name__ == "__main__": From b07789232713d44f9523605afa12fad9393357f3 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Mon, 29 Apr 2024 16:14:51 -0400 Subject: [PATCH 2/2] skip non-readable files using loadData --- src/diffpy/labpdfproc/labpdfprocapp.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/diffpy/labpdfproc/labpdfprocapp.py b/src/diffpy/labpdfproc/labpdfprocapp.py index 63b0139..2e436c7 100644 --- a/src/diffpy/labpdfproc/labpdfprocapp.py +++ b/src/diffpy/labpdfproc/labpdfprocapp.py @@ -90,7 +90,12 @@ def main(): ) input_pattern = Diffraction_object(wavelength=wavelength) - xarray, yarray = loadData(filepath.name, unpack=True) + try: + xarray, yarray = loadData(filepath, unpack=True) + except Exception as e: + print(f"Non-readable data file {filepath}: {e}") + continue + input_pattern.insert_scattering_quantity( xarray, yarray,