Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/source/examples/labpdfprocapp-example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ For example,
labpdfproc mud zro2_mo.xy 2.5 -w 0.71303
labpdfproc mud zro2_mo.xy 2.5 -w Mo

This will then save the corrected file in the same directory as the input file with the name ``zro2_mo_corrected.chi``.
This will then save the corrected file in the same directory as the input file with the name ``zro2_mo-mud-corrected.chi``.

To save the correction file, specify the ``-c`` or ``--output-correction`` flag,

.. code-block:: bash

labpdfproc mud zro2_mo.xy 2.5 -w 0.71303 -c

This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``.
This will then save the correction file in the same directory as the input file with the name ``zro2_mo-cve.chi``.

``labpdfproc zscan`` Command
----------------------------
Expand Down Expand Up @@ -125,7 +125,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag,

labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -w 0.71303 -c

This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0_cve.chi``.
This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0-cve.chi``.

``labpdfproc sample`` Command
-----------------------------
Expand Down Expand Up @@ -165,7 +165,7 @@ To save the correction file, specify the ``-c`` or ``--output-correction`` flag,

labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 -w 0.71303 -c

This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``.
This will then save the correction file in the same directory as the input file with the name ``zro2_mo-cve.chi``.

Additional CLI options
----------------------
Expand Down
25 changes: 25 additions & 0 deletions news/check-saved-files.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**Added:**

* <news item>

**Changed:**

* Changed output file behavior to raise an error early if the saved file exists.
* Changed saved file name from ``_corrected.chi`` to ``-mud-corrected.chi``.
* Changed saved file name from ``_cve.chi`` to ``-cve.chi``.

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
14 changes: 5 additions & 9 deletions src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,14 @@ def _add_credit_args(parser, use_gui=False):


def _save_corrected(corrected, input_path, args):
outfile = args.output_directory / (input_path.stem + "_corrected.chi")
if outfile.exists() and not args.force:
print(f"WARNING: {outfile} exists. Use --force to overwrite.")
return
outfile = args.output_directory / (f"{input_path.stem}-mud-corrected.chi")
corrected.metadata = corrected.metadata or {}
corrected.dump(str(outfile), xtype=args.xtype)
print(f"Saved corrected data to {outfile}")


def _save_correction(correction, input_path, args):
corrfile = args.output_directory / (input_path.stem + "_cve.chi")
if corrfile.exists() and not args.force:
print(f"WARNING: {corrfile} exists. Use --force to overwrite.")
return
corrfile = args.output_directory / (f"{input_path.stem}-cve.chi")
correction.metadata = correction.metadata or {}
correction.dump(str(corrfile), xtype=args.xtype)
print(f"Saved correction data to {corrfile}")
Expand Down Expand Up @@ -312,7 +306,9 @@ def get_args_gui():

def get_args_cli(override=None):
parser = create_parser(use_gui=False)
return parser.parse_args(override)
argv = override if override is not None else sys.argv[1:]
argv = [arg for arg in argv if arg != "--ignore-gooey"]
return parser.parse_args(argv)


def main():
Expand Down
27 changes: 27 additions & 0 deletions src/diffpy/labpdfproc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,27 @@ def load_package_info(args):
return args


def _check_saved_file_exists(args):
"""Check if the output files already exist based on the input paths
and output directory."""
existing_files = []
for path in args.input_paths:
outfile = args.output_directory / (f"{path.stem}-mud-corrected.chi")
if outfile.exists() and not args.force:
existing_files.append(outfile)
if args.output_correction:
corrfile = args.output_directory / (f"{path.stem}-cve.chi")
if corrfile.exists() and not args.force:
existing_files.append(corrfile)
if existing_files:
existing_files_str = "\n".join(str(f) for f in existing_files)
raise FileExistsError(
"The following output files already exist:"
f"\n{existing_files_str}\n"
"Use --force to overwrite them."
)


def preprocessing_args(args):
"""Perform preprocessing on the provided args. The process includes
loading package and user information, setting input, output,
Expand All @@ -542,6 +563,11 @@ def preprocessing_args(args):
-------
args : argparse.Namespace
The updated argparse Namespace with arguments preprocessed.

Raises
------
FileExistsError
If the output files already exist and --force is not used.
"""
args = load_wavelength_from_config_file(args)
args = set_mud(args)
Expand All @@ -552,6 +578,7 @@ def preprocessing_args(args):
args = load_user_metadata(args)
args = load_user_info(args)
args = load_package_info(args)
_check_saved_file_exists(args)
return args


Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def user_filesystem(tmp_path):
home_dir.mkdir(parents=True, exist_ok=True)
test_dir = base_dir / "test_dir"
test_dir.mkdir(parents=True, exist_ok=True)

output_dir = base_dir / "output_dir"
output_dir.mkdir(parents=True, exist_ok=True)
chi_data = (
"dataformat = twotheta\n mode = "
"xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n"
Expand Down Expand Up @@ -59,6 +60,10 @@ def user_filesystem(tmp_path):
f.write("good_data.xy \n")
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")

with open(output_dir / "good_data-mud-corrected.chi", "w") as f:
f.write(chi_data)
with open(output_dir / "good_data-cve.chi", "w") as f:
f.write(chi_data)
home_config_data = {
"wavelength": 0.3,
"owner_name": "home_username",
Expand Down
35 changes: 35 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,38 @@ def test_load_metadata(mocker, user_filesystem, inputs, expected):
**expected,
}
assert actual_metadata == expected_metadata


def test_preprocess_args_bad(user_filesystem, monkeypatch):
# Case: user tries to run absorption correction, but the output
# filenames already for *-mud-corrected.chi and *-cve.chi exists.
# expected: preprocess_args catches this early and raises an Error
cwd = Path(user_filesystem)
home_dir = cwd / "home_dir"
# set cwd so program can find diffpyconfig.json
monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir)
input_data_file = str(user_filesystem / "good_data.chi")
existing_corrected_file = str(
user_filesystem / "output_dir" / "good_data-mud-corrected.chi"
)
existing_corrected_cve_file = str(
user_filesystem / "output_dir" / "good_data-cve.chi"
)
cli_inputs = [
"mud",
input_data_file,
"2.5",
"-o",
str(user_filesystem / "output_dir"),
"-c", # -c flag saves the cve file
]
args = get_args_cli(cli_inputs)
args = set_input_lists(args)
msg = (
"The following output files already exist:"
f"\n{existing_corrected_file}\n"
f"{existing_corrected_cve_file}\n"
"Use --force to overwrite them."
)
with pytest.raises(FileExistsError, match=re.escape(msg)):
preprocessing_args(args)
Loading