reprocessing.py 2.44 KB
Newer Older
payno's avatar
payno committed
1
2
3
import argparse
import sys
import logging
payno's avatar
payno committed
4
5
from est.core.types import XASObject
from est.core.reprocessing import get_process_instance_frm_h5_desc
payno's avatar
payno committed
6
from typing import Union
payno's avatar
payno committed
7
8
try:
    import h5py
payno's avatar
payno committed
9

payno's avatar
payno committed
10
11
12
13
14
15
16
17
    has_h5py = True
except:
    has_h5py = False

logging.basicConfig(level=logging.DEBUG)
_logger = logging.getLogger(__name__)


payno's avatar
payno committed
18
def exec_(
payno's avatar
payno committed
19
20
21
22
23
    h5_file: str,
    entry: str = "scan1",
    spectra_path: Union[None, str] = None,
    energy_path: Union[None, str] = None,
    configuration_path: Union[None, str] = None,
payno's avatar
payno committed
24
):
payno's avatar
payno committed
25
26

    try:
payno's avatar
payno committed
27
28
29
30
31
32
33
        xas_obj = XASObject.from_file(
            h5_file=h5_file,
            entry=entry or "scan1",
            spectra_path=spectra_path or "data/absorbed_beam",
            energy_path=energy_path or "data/energy",
            configuration_path=configuration_path,
        )
payno's avatar
payno committed
34
35
36
37
38
39
        xas_obj.link_to_h5(h5_file=h5_file)
    except Exception as e:
        _logger.error(e)
        xas_obj = None

    if not xas_obj:
payno's avatar
payno committed
40
        _logger.error("Unable to create an object from the given file and path")
payno's avatar
payno committed
41
42
43

    xas_processes = xas_obj.get_process_flow()
    if len(xas_processes) == 0:
payno's avatar
payno committed
44
        _logger.warning("no xas process found in the given h5 file")
payno's avatar
payno committed
45
46
47
48
49
50
51
52
53
54
55
56
        return
    xas_obj.clean_process_flow()
    for i_process, process in xas_processes.items():
        process_ins = get_process_instance_frm_h5_desc(process)
        xas_obj = process_ins(xas_obj)

    return xas_obj


def main(argv):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
payno's avatar
payno committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        "workflow_file", help="Workflow file providing the workflow description (.h5)"
    )
    parser.add_argument(
        "--spectra_path",
        dest="spectra_path",
        default=None,
        help="path to the spectra data",
    )
    parser.add_argument(
        "--energy_path",
        dest="energy_path",
        default=None,
        help="path to the energy data",
    )
    parser.add_argument(
        "--configuration_path",
        dest="configuration_path",
        default=None,
        help="path to the process configuration",
    )
    parser.add_argument(
        "--entry",
        dest="entry to load",
        default=None,
        help="path to the process configuration",
    )
payno's avatar
payno committed
83
    options = parser.parse_args(argv[1:])
payno's avatar
payno committed
84
85
86
87
88
89
    exec_(
        h5_file=options.workflow_file,
        spectra_path=options.spectra_path,
        energy_path=options.energy_path,
        configuration_path=options.configuration_path,
    )
payno's avatar
payno committed
90
91
92
93


if __name__ == "__main__":
    main(sys.argv)