Coverage for /Users/Newville/Codes/xraylarch/larch/io/athena_to_hdf5.py: 0%
36 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
1#!/usr/bin/env python
2"""
3Convert an Athena Project file to HDF5
4"""
6import os
7import h5py
8from silx.io.dictdump import dicttoh5
9from larch.io.athena_project import AthenaProject
10from larch.utils.logging import getLogger
12_logger = getLogger("athena_to_hdf5", level="INFO")
15def athena_to_hdf5(
16 filename,
17 fileout=None,
18 overwrite=False,
19 match=None,
20 do_preedge=True,
21 do_bkg=True,
22 do_fft=True,
23 use_hashkey=False,
24):
25 """Read Athena project file (.prj) and write to HDF5 (.h5)
27 Arguments:
28 filename (string): name of Athena Project file
29 fileout (None or string): name of the output file [None -> filename_root.h5]
30 overwrite (boolean): force overwrite if fileout exists [False]
31 match (string): pattern to use to limit imported groups (see Note 1)
32 do_preedge (bool): whether to do pre-edge subtraction [True]
33 do_bkg (bool): whether to do XAFS background subtraction [True]
34 do_fft (bool): whether to do XAFS Fast Fourier transform [True]
35 use_hashkey (bool): whether to use Athena's hash key as the
36 group name instead of the Athena label [False]
38 Returns:
39 None, writes HDF5 file.
41 Notes:
42 1. There is currently a bug in h5py, track_order is ignored for the root group:
43 https://github.com/h5py/h5py/issues/1471
45 """
46 aprj = AthenaProject()
47 aprj.read(
48 filename,
49 match=match,
50 do_preedge=do_preedge,
51 do_bkg=do_bkg,
52 do_fft=do_fft,
53 use_hashkey=use_hashkey,
54 )
55 adict = aprj.as_dict()
56 if fileout is None:
57 froot = filename.split(".")[0]
58 fileout = f"{froot}.h5"
59 if os.path.isfile(fileout) and os.access(fileout, os.R_OK):
60 _logger.info(f"{fileout} exists")
61 _fileExists = True
62 if overwrite is False:
63 _logger.info(f"overwrite is {overwrite} -> nothing to do!")
64 return
65 else:
66 _fileExists = False
67 if overwrite and _fileExists:
68 os.remove(fileout)
69 h5out = h5py.File(fileout, mode="a", track_order=True)
70 create_ds_args = {"track_order": True}
71 dicttoh5(adict, h5out, create_dataset_args=create_ds_args)
72 h5out.close()
73 _logger.info(f"Athena project converted to {fileout}")
76if __name__ == "__main__":
77 # some tests while devel
78 _curdir = os.path.dirname(os.path.realpath(__file__))
79 _exdir = os.path.join(os.path.dirname(os.path.dirname(_curdir)), "examples", "pca")
80 fnroot = "cyanobacteria"
81 atpfile = os.path.join(_exdir, f"{fnroot}.prj")
82 if 0:
83 aprj = AthenaProject()
84 aprj.read(atpfile, do_bkg=False) # there is currently a bug in do_bkg!
85 adict = aprj.as_dict()
86 if 0:
87 athena_to_hdf5(atpfile, fileout=f"{fnroot}.h5", overwrite=True)
88 pass