Skip to content

Commit

Permalink
Make --overwrite switch work
Browse files Browse the repository at this point in the history
  • Loading branch information
moralejo authored Feb 4, 2025
1 parent e99e7cc commit bbe8796
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lstchain/tools/lstchain_dl1_to_dl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,16 @@ def start(self):
models_dict[models_key] = joblib.load(models_path)

for input_dl1file in self.input_files:
output_filepath = apply_to_file(input_dl1file, models_dict, self.output_dir, config, self.path_models)
output_filepath = apply_to_file(input_dl1file, models_dict, self.output_dir, config,
self.path_models, self.overwrite)
p = Provenance()
p.add_input_file(input_dl1file, role='dl1 input file')
p.add_output_file(output_filepath, role='dl2 output file')
p.add_input_file(self.path_models, role='trained model directory')
write_provenance(output_filepath, 'dl1_to_dl2')


def apply_to_file(filename, models_dict, output_dir, config, models_path):
def apply_to_file(filename, models_dict, output_dir, config, models_path, overwrite=False):
"""
Applies models to the data in the specified file and writes the output to a new file in the output directory.
Expand All @@ -155,6 +156,11 @@ def apply_to_file(filename, models_dict, output_dir, config, models_path):
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True, parents=True)
dl2_output_file = output_dir.joinpath(dl2_filename(filename.name))

# Remove previous file if overwrite option is used:
if overwrite:
dl2_output_file.unlink(missing_ok=True)

Check warning on line 162 in lstchain/tools/lstchain_dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/tools/lstchain_dl1_to_dl2.py#L162

Added line #L162 was not covered by tests

if dl2_output_file.exists():
raise IOError(str(dl2_output_file) + ' exists, exiting.')

Check warning on line 165 in lstchain/tools/lstchain_dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/tools/lstchain_dl1_to_dl2.py#L165

Added line #L165 was not covered by tests

Expand Down

0 comments on commit bbe8796

Please sign in to comment.