reprocessing.py 2.49 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
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
try:
    import h5py
    has_h5py = True
except:
    has_h5py = False

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


def exec_(h5_file, entry='scan1', spectra_path=None, energy_path=None,
          configuration_path=None):

    try:
        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)
        xas_obj.link_to_h5(h5_file=h5_file)
    except Exception as e:
        _logger.error(e)
        xas_obj = None

    if not xas_obj:
        _logger.error('Unable to create an object from the given file and path')

    xas_processes = xas_obj.get_process_flow()
    if len(xas_processes) == 0:
        _logger.warning('no xas process found in the given h5 file')
        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 getinputinfo():
    return "xas reprocessing workflow_file.h5"


def getInputFrmHdf5(file_path):
    raise NotImplementedError()


def main(argv):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        '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")
    options = parser.parse_args(argv[1:])
    exec_(h5_file=options.workflow_file, spectra_path=options.spectra_path,
          energy_path=options.energy_path,
          configuration_path=options.configuration_path)


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