Coverage for /Users/Newville/Codes/xraylarch/larch/xafs/xafsutils.py: 55%
42 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"""
2Utility functions used for xafs analysis
3"""
4import numpy as np
5from larch import Group, Journal
6from larch.utils import gformat
7import scipy.constants as consts
8KTOE = 1.e20*consts.hbar**2 / (2*consts.m_e * consts.e) # 3.8099819442818976
9ETOK = 1.0/KTOE
10TINY_ENERGY = 0.005 # in eV:
12FT_WINDOWS = ('Kaiser-Bessel', 'Hanning', 'Parzen', 'Welch', 'Gaussian', 'Sine')
13FT_WINDOWS_SHORT = tuple([a[:3].lower() for a in FT_WINDOWS])
15NUMLEN = 10
16def gfmt(x):
17 return gformat(x, length=NUMLEN)
19def etok(energy):
20 """convert photo-electron energy to wavenumber"""
21 if energy < 0: return 0
22 return np.sqrt(energy*ETOK)
24def ktoe(k):
25 """convert photo-electron wavenumber to energy"""
26 return k*k*KTOE
28def guess_energy_units(e):
29 """guesses the energy units of the input array of energies
30 returns one of
31 'eV' energy looks to be in eV
32 'keV' energy looks to be in keV
33 'deg' energy looks to be in degrees
34 'steps' energy looks to be in angular steps
36 The default is 'eV'.
37 keV : max(e) < 120, smallest step < 0.005, e increasing
38 deg : max(e) < 90, smallest step < 0.005, e decreasing
39 steps : max(e) > 200,000
41 Note that there is a potential for ambiguity between data
42 measured in 'deg' and data measured in 'keV' with e decreasing!
43 """
44 try:
45 ework = e.flatten()
46 ediff = np.diff(ework)
47 emax = max(ework)
48 units = 'eV'
49 if emax > 200000:
50 units = 'steps'
51 if emax < 120.0 and (abs(ediff).min() < 0.005):
52 units = 'keV'
53 if emax < 90.0 and (ediff.mean() < 0.0):
54 units = 'deg'
55 except ValueError:
56 units = 'unknown'
57 return units
59def set_xafsGroup(group, _larch=None):
60 """set _sys.xafsGroup to the supplied group (if not None)
62 return _sys.xafsGroup.
64 if needed, a new, empty _sys.xafsGroup may be created.
65 """
66 if group is None:
67 if _larch is None:
68 group = Group()
69 else:
70 group = getattr(_larch.symtable._sys, 'xafsGroup', Group())
71 if not hasattr(group, 'journal'):
72 group.journal = Journal()
74 if _larch is not None:
75 _larch.symtable._sys.xafsGroup = group
76 return group