6
6
import ctypes
7
7
import ctypes .util
8
8
9
- __version__ = '1.1 .0'
9
+ __version__ = '1.2 .0'
10
10
11
- try :
12
- from numpy import array , exp , log , sin , arcsin
13
- HAS_NUMPY = True
14
- except ImportError :
15
- HAS_NUMPY = False
11
+ from numpy import array , exp , log , sin , arcsin
16
12
17
13
PI = 3.14159265358979323846
18
14
RAD2DEG = 180.0 / PI
@@ -38,8 +34,9 @@ class XDIFileStruct(ctypes.Structure):
38
34
('edge' , ctypes .c_char_p ),
39
35
('comments' , ctypes .c_char_p ),
40
36
('error_line' , ctypes .c_char_p ),
37
+ ('error_message' , ctypes .c_char_p ),
41
38
('array_labels' , ctypes .c_void_p ),
42
- ('outer_label' , ctypes .c_char_p ),
39
+ ('outer_label' , ctypes .c_char_p ),
43
40
('array_units' , ctypes .c_void_p ),
44
41
('meta_families' , ctypes .c_void_p ),
45
42
('meta_keywords' , ctypes .c_void_p ),
@@ -62,7 +59,6 @@ def get_xdilib():
62
59
"""make initial connection to XDI dll"""
63
60
global XDILIB
64
61
if XDILIB is None :
65
- add_dot2path ()
66
62
dllpath = ctypes .util .find_library ('xdifile' )
67
63
load_dll = ctypes .cdll .LoadLibrary
68
64
if os .name == 'nt' :
@@ -71,6 +67,7 @@ def get_xdilib():
71
67
XDILIB .XDI_errorstring .restype = ctypes .c_char_p
72
68
return XDILIB
73
69
70
+
74
71
class XDIFileException (Exception ):
75
72
"""XDI File Exception: General Errors"""
76
73
def __init__ (self , msg , ** kws ):
@@ -107,29 +104,36 @@ def __init__(self, filename=None):
107
104
108
105
def write (self , filename ):
109
106
"write out an XDI File"
110
- print 'Writing XDI file not currently supported'
107
+ print ( 'Writing XDI file not currently supported' )
111
108
112
109
def read (self , filename = None ):
113
110
"""read validate and parse an XDI datafile into python structures
114
111
"""
115
112
if filename is None and self .filename is not None :
116
113
filename = self .filename
117
-
118
114
pxdi = ctypes .pointer (XDIFileStruct ())
119
115
self .status = out = self .xdilib .XDI_readfile (filename , pxdi )
120
116
if out < 0 :
121
- msg = self .xdilib .XDI_errorstring (out )
117
+ msg = self .xdilib .XDI_errorstring (out )
118
+ self .xdilib .XDI_cleanup (pxdi , out )
122
119
msg = 'Error reading XDIFile %s\n %s' % (filename , msg )
123
120
raise XDIFileException (msg )
124
121
125
122
xdi = pxdi .contents
126
123
for attr in dict (xdi ._fields_ ):
127
124
setattr (self , attr , getattr (xdi , attr ))
128
-
129
125
pchar = ctypes .c_char_p
130
126
self .array_labels = (self .narrays * pchar ).from_address (xdi .array_labels )[:]
131
- self .array_units = (self .narrays * pchar ).from_address (xdi .array_units )[:]
132
-
127
+ arr_units = (self .narrays * pchar ).from_address (xdi .array_units )[:]
128
+ self .array_units = []
129
+ self .array_addrs = []
130
+ for unit in arr_units :
131
+ addr = ''
132
+ if '||' in unit :
133
+ unit , addr = [x .strip () for x in unit .split ('||' , 1 )]
134
+ self .array_units .append (unit )
135
+ self .array_addrs .append (addr )
136
+
133
137
mfams = (self .nmetadata * pchar ).from_address (xdi .meta_families )[:]
134
138
mkeys = (self .nmetadata * pchar ).from_address (xdi .meta_keywords )[:]
135
139
mvals = (self .nmetadata * pchar ).from_address (xdi .meta_values )[:]
@@ -143,7 +147,7 @@ def read(self, filename=None):
143
147
144
148
parrays = (xdi .narrays * ctypes .c_void_p ).from_address (xdi .array )[:]
145
149
rawdata = [(xdi .npts * ctypes .c_double ).from_address (p )[:] for p in parrays ]
146
-
150
+
147
151
nout = xdi .nouter
148
152
outer , breaks = [], []
149
153
if nout > 1 :
@@ -153,17 +157,16 @@ def read(self, filename=None):
153
157
delattr (self , attr )
154
158
self .outer_array = array (outer )
155
159
self .outer_breakpts = array (breaks )
156
-
157
-
158
- if HAS_NUMPY :
159
- rawdata = array (rawdata )
160
- rawdata .shape = (self .narrays , self .npts )
160
+
161
+
162
+ rawdata = array (rawdata )
163
+ rawdata .shape = (self .narrays , self .npts )
161
164
self .rawdata = rawdata
162
165
self ._assign_arrays ()
163
-
164
166
for attr in ('nmetadata' , 'narray_labels' , 'meta_families' ,
165
167
'meta_keywords' , 'meta_values' , 'array' ):
166
168
delattr (self , attr )
169
+ self .xdilib .XDI_cleanup (pxdi , 0 )
167
170
168
171
def _assign_arrays (self ):
169
172
"""assign data arrays for principle data attributes:
@@ -173,14 +176,10 @@ def _assign_arrays(self):
173
176
xunits = 'eV'
174
177
xname = None
175
178
ix = - 1
176
- if HAS_NUMPY :
177
- self .rawdata = array (self .rawdata )
179
+ self .rawdata = array (self .rawdata )
178
180
179
181
for idx , name in enumerate (self .array_labels ):
180
- if HAS_NUMPY :
181
- dat = self .rawdata [idx ,:]
182
- else :
183
- dat = [d [idx ] for d in self .rawdata ]
182
+ dat = self .rawdata [idx ,:]
184
183
setattr (self , name , dat )
185
184
if name in ('energy' , 'angle' ):
186
185
ix = idx
@@ -189,12 +188,15 @@ def _assign_arrays(self):
189
188
if units is not None :
190
189
xunits = units
191
190
192
- if not HAS_NUMPY :
193
- return
194
-
195
191
# convert energy to angle, or vice versa
196
- if ix >= 0 and 'd_spacing' in self .attrs ['mono' ]:
197
- dspace = float (self .attrs ['mono' ]['d_spacing' ])
192
+ monodat = {}
193
+ if 'mono' in self .attrs :
194
+ monodat = self .attrs ['mono' ]
195
+ elif 'monochromator' in self .attrs :
196
+ monodat = self .attrs ['monochromator' ]
197
+
198
+ if ix >= 0 and 'd_spacing' in monodat :
199
+ dspace = float (monodat ['d_spacing' ])
198
200
if dspace < 0 : dspace = 0.001
199
201
omega = PLANCK_HC / (2 * dspace )
200
202
if xname == 'energy' and not hasattr (self , 'angle' ):
0 commit comments