Skip to content

Commit 551f332

Browse files
committed
updated python module to match new C structure, with outer_array
1 parent bd6c971 commit 551f332

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

python/lib/xdi.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,47 @@
66
import ctypes
77
import ctypes.util
88

9-
__version__ = '1.0.0'
9+
__version__ = '1.1.0'
1010

1111
try:
12-
from numpy import array, pi, exp, log, sin, arcsin
12+
from numpy import array, exp, log, sin, arcsin
1313
HAS_NUMPY = True
14-
RAD2DEG = 180.0/pi
15-
# from NIST.GOV CODATA:
16-
# Planck constant over 2 pi times c: 197.3269718 (0.0000044) MeV fm
17-
PLANCK_hc = 1973.269718 * 2 * pi # hc in eV * Ang = 12398.4193
1814
except ImportError:
1915
HAS_NUMPY = False
2016

17+
PI = 3.14159265358979323846
18+
RAD2DEG = 180.0/PI
19+
20+
# from NIST.GOV CODATA:
21+
# Planck constant over 2 pi times c: 197.3269718 (0.0000044) MeV fm
22+
PLANCK_HC = 1973.269718 * 2 * PI # hc in eV * Ang = 12398.4193
23+
2124
class XDIFileStruct(ctypes.Structure):
2225
"emulate XDI File"
2326
_fields_ = [('nmetadata', ctypes.c_long),
2427
('narrays', ctypes.c_long),
2528
('npts', ctypes.c_long),
2629
('narray_labels', ctypes.c_long),
30+
('nouter', ctypes.c_long),
31+
('error_lineno', ctypes.c_long),
2732
('dspacing', ctypes.c_double),
28-
('xdi_libversion', ctypes.c_char_p),
33+
('xdi_libversion',ctypes.c_char_p),
2934
('xdi_version', ctypes.c_char_p),
3035
('extra_version', ctypes.c_char_p),
3136
('filename', ctypes.c_char_p),
3237
('element', ctypes.c_char_p),
3338
('edge', ctypes.c_char_p),
3439
('comments', ctypes.c_char_p),
40+
('error_line', ctypes.c_char_p),
3541
('array_labels', ctypes.c_void_p),
42+
('outer_label', ctypes.c_char_p),
3643
('array_units', ctypes.c_void_p),
3744
('meta_families', ctypes.c_void_p),
3845
('meta_keywords', ctypes.c_void_p),
3946
('meta_values', ctypes.c_void_p),
40-
('array', ctypes.c_void_p)]
47+
('array', ctypes.c_void_p),
48+
('outer_array', ctypes.c_void_p),
49+
('outer_breakpts', ctypes.c_void_p)]
4150

4251
def add_dot2path():
4352
"""add this folder to begninng of PATH environmental variable"""
@@ -88,9 +97,11 @@ class XDIFile(object):
8897
def __init__(self, filename=None):
8998
self.filename = filename
9099
self.xdi_pyversion = __version__
100+
self.xdilib = get_xdilib()
91101
self.comments = []
92102
self.rawdata = []
93103
self.attrs = {}
104+
self.status = None
94105
if self.filename:
95106
self.read(self.filename)
96107

@@ -103,12 +114,11 @@ def read(self, filename=None):
103114
"""
104115
if filename is None and self.filename is not None:
105116
filename = self.filename
106-
XDILIB = get_xdilib()
107117

108118
pxdi = ctypes.pointer(XDIFileStruct())
109-
out = XDILIB.XDI_readfile(filename, pxdi)
110-
if out != 0:
111-
msg = XDILIB.XDI_errorstring(out)
119+
self.status = out = self.xdilib.XDI_readfile(filename, pxdi)
120+
if out < 0:
121+
msg = self.xdilib.XDI_errorstring(out)
112122
msg = 'Error reading XDIFile %s\n%s' % (filename, msg)
113123
raise XDIFileException(msg)
114124

@@ -119,7 +129,7 @@ def read(self, filename=None):
119129
pchar = ctypes.c_char_p
120130
self.array_labels = (self.narrays*pchar).from_address(xdi.array_labels)[:]
121131
self.array_units = (self.narrays*pchar).from_address(xdi.array_units)[:]
122-
132+
123133
mfams = (self.nmetadata*pchar).from_address(xdi.meta_families)[:]
124134
mkeys = (self.nmetadata*pchar).from_address(xdi.meta_keywords)[:]
125135
mvals = (self.nmetadata*pchar).from_address(xdi.meta_values)[:]
@@ -133,7 +143,18 @@ def read(self, filename=None):
133143

134144
parrays = (xdi.narrays*ctypes.c_void_p).from_address(xdi.array)[:]
135145
rawdata = [(xdi.npts*ctypes.c_double).from_address(p)[:] for p in parrays]
136-
146+
147+
nout = xdi.nouter
148+
outer, breaks = [], []
149+
if nout > 1:
150+
outer = (nout*ctypes.c_double).from_address(xdi.outer_array)[:]
151+
breaks = (nout*ctypes.c_long).from_address(xdi.outer_breakpts)[:]
152+
for attr in ('outer_array', 'outer_breakpts', 'nouter'):
153+
delattr(self, attr)
154+
self.outer_array = array(outer)
155+
self.outer_breakpts = array(breaks)
156+
157+
137158
if HAS_NUMPY:
138159
rawdata = array(rawdata)
139160
rawdata.shape = (self.narrays, self.npts)
@@ -169,13 +190,13 @@ def _assign_arrays(self):
169190
xunits = units
170191

171192
if not HAS_NUMPY:
172-
print '%s: not calculating derived values: install numpy!' % (self.filename)
173193
return
174194

175195
# convert energy to angle, or vice versa
176196
if ix >= 0 and 'd_spacing' in self.attrs['mono']:
177197
dspace = float(self.attrs['mono']['d_spacing'])
178-
omega = PLANCK_hc/(2*dspace)
198+
if dspace < 0: dspace = 0.001
199+
omega = PLANCK_HC/(2*dspace)
179200
if xname == 'energy' and not hasattr(self, 'angle'):
180201
energy_ev = self.energy
181202
if xunits.lower() == 'kev':

0 commit comments

Comments
 (0)