Clear existing reduced frames#

In this example script we will explain how to clear any existing reduced frame of an existing scan.

It can be the case if you have some scans with false dark and flat

It should be inserted at the beginning of the script (just after loading would be the best)

from tomwer.core.scan.nxtomoscan import NXtomoScan
import os
import h5py

scan = in_data
if scan:
    scan.clear_caches()

    darks_file = os.path.join(
        scan.path,
        f"{scan.get_dataset_basename()}_darks.hdf5",
    )
    flats_file = os.path.join(
        scan.path,
        f"{scan.get_dataset_basename()}_flats.hdf5",
    )

    if isinstance(scan, NXtomoScan):
        scan_entry = scan.entry
    else:
        scan_entry = "entry"

    for reduced_frame_file in (darks_file, flats_file):
        with h5py.File(reduced_frame_file, mode="a") as h5f:
            if scan_entry in h5f:
                del[h5f[scan_entry]]
            # if the file doesn't contain the group anymore we can remove it
            delete_file = len(h5f.keys()) == 0

        if delete_file:
            os.remove(reduced_frame_file)

out_data = scan