1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2014-09-26 14:41:33 +0000 (Fri, 26 Sep 2014) $ |
---|
4 | # $Author: vondreele $ |
---|
5 | # $Revision: 1506 $ |
---|
6 | # $URL: trunk/imports/G2sfact.py $ |
---|
7 | # $Id: G2sfact.py 1506 2014-09-26 14:41:33Z vondreele $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2sfact: simple HKL import* |
---|
11 | ----------------------------------- |
---|
12 | Read structure factors from a simple hkl file. Two routines are |
---|
13 | provided to read from files containing F or F\ :sup:`2` values. |
---|
14 | |
---|
15 | ''' |
---|
16 | import sys |
---|
17 | import numpy as np |
---|
18 | import copy |
---|
19 | import GSASIIIO as G2IO |
---|
20 | import GSASIIpath |
---|
21 | GSASIIpath.SetVersionNumber("$Revision: 1506 $") |
---|
22 | |
---|
23 | def ColumnValidator(parent, filepointer,nCol=5): |
---|
24 | 'Validate a file to check that it contains columns of numbers' |
---|
25 | l = filepointer.readline() |
---|
26 | line = 1 |
---|
27 | while l[0] in ['#','(']: #get past comments & fortran formats, if any |
---|
28 | l = filepointer.readline() |
---|
29 | line += 1 |
---|
30 | for i in range(10): # scan a few lines |
---|
31 | S = l.split() |
---|
32 | if len(S) < nCol: |
---|
33 | parent.errors = 'line '+str(line)+': invalid input\n'+l |
---|
34 | return False |
---|
35 | for v in S[:nCol]: |
---|
36 | try: |
---|
37 | float(v) |
---|
38 | except ValueError: |
---|
39 | parent.errors = 'line '+str(line)+': string found where a number is expected\n'+l |
---|
40 | return False |
---|
41 | l = filepointer.readline() |
---|
42 | line += 1 |
---|
43 | return True |
---|
44 | |
---|
45 | |
---|
46 | class HKLF_ReaderClass(G2IO.ImportStructFactor): |
---|
47 | 'Routines to import F, sig(F) reflections from a HKLF file' |
---|
48 | def __init__(self): |
---|
49 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
50 | extensionlist=('.hkl','.HKL'), |
---|
51 | strictExtension=False, |
---|
52 | formatName = 'HKL F', |
---|
53 | longFormatName = 'Simple [hkl, Fo, sig(Fo)] Structure factor text file' |
---|
54 | ) |
---|
55 | |
---|
56 | def ContentsValidator(self, filepointer): |
---|
57 | 'Make sure file contains the expected columns on numbers' |
---|
58 | return ColumnValidator(self, filepointer) |
---|
59 | |
---|
60 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
61 | 'Read the file' |
---|
62 | try: |
---|
63 | for line,S in enumerate(filepointer): |
---|
64 | self.errors = ' Error reading line '+str(line+1) |
---|
65 | if S[0] == '#': continue #ignore comments, if any |
---|
66 | h,k,l,Fo,sigFo = S.split() |
---|
67 | h,k,l = [int(h),int(k),int(l)] |
---|
68 | if not any([h,k,l]): |
---|
69 | break |
---|
70 | Fo = float(Fo) |
---|
71 | sigFo = float(sigFo) |
---|
72 | # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,... |
---|
73 | self.RefDict['RefList'].append([h,k,l,0,0,Fo**2,2.*Fo*sigFo,0,Fo**2,0,0,0]) |
---|
74 | #self.RefDict['FF'].append({}) # now done in OnImportSfact |
---|
75 | self.errors = 'Error after reading reflections (unexpected!)' |
---|
76 | self.RefDict['RefList'] = np.array(self.RefDict['RefList']) |
---|
77 | self.RefDict['Type'] = 'SXC' |
---|
78 | self.RefDict['Super'] = 0 |
---|
79 | self.UpdateParameters(Type='SXC',Wave=None) # histogram type |
---|
80 | return True |
---|
81 | except Exception as detail: |
---|
82 | self.errors += '\n '+str(detail) |
---|
83 | print '\n\n'+self.formatName+' read error: '+str(detail) # for testing |
---|
84 | import traceback |
---|
85 | traceback.print_exc(file=sys.stdout) |
---|
86 | return False |
---|
87 | |
---|
88 | class HKLF2_ReaderClass(G2IO.ImportStructFactor): |
---|
89 | 'Routines to import F**2, sig(F**2) reflections from a HKLF file' |
---|
90 | def __init__(self): |
---|
91 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
92 | extensionlist=('.hkl','.HKL'), |
---|
93 | strictExtension=False, |
---|
94 | formatName = u'HKL F\u00b2', |
---|
95 | longFormatName = u'Simple [hkl, Fo\u00b2, sig(Fo\u00b2)] Structure factor text file' |
---|
96 | ) |
---|
97 | |
---|
98 | def ContentsValidator(self, filepointer): |
---|
99 | 'Make sure file contains the expected columns on numbers' |
---|
100 | return ColumnValidator(self, filepointer) |
---|
101 | |
---|
102 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
103 | 'Read the file' |
---|
104 | try: |
---|
105 | for line,S in enumerate(filepointer): |
---|
106 | self.errors = ' Error reading line '+str(line+1) |
---|
107 | if S[0] == '#': continue #ignore comments, if any |
---|
108 | h,k,l,Fo,sigFo = S.split() |
---|
109 | h,k,l = [int(h),int(k),int(l)] |
---|
110 | if not any([h,k,l]): |
---|
111 | break |
---|
112 | Fo = float(Fo) |
---|
113 | sigFo = float(sigFo) |
---|
114 | # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,... |
---|
115 | self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
116 | #self.RefDict['FF'].append({}) # now done in OnImportSfact |
---|
117 | self.errors = 'Error after reading reflections (unexpected!)' |
---|
118 | self.RefDict['RefList'] = np.array(self.RefDict['RefList']) |
---|
119 | self.RefDict['Type'] = 'SXC' |
---|
120 | self.RefDict['Super'] = 0 |
---|
121 | self.UpdateParameters(Type='SXC',Wave=None) # histogram type |
---|
122 | return True |
---|
123 | except Exception as detail: |
---|
124 | self.errors += '\n '+str(detail) |
---|
125 | print '\n\n'+self.formatName+' read error: '+str(detail) # for testing |
---|
126 | import traceback |
---|
127 | traceback.print_exc(file=sys.stdout) |
---|
128 | return False |
---|
129 | |
---|
130 | class M90_ReaderClass(G2IO.ImportStructFactor): |
---|
131 | 'Routines to import F**2, sig(F**2) reflections from a JANA M90 file' |
---|
132 | def __init__(self): |
---|
133 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
134 | extensionlist=('.m90','.M90'), |
---|
135 | strictExtension=False, |
---|
136 | formatName = u'JANA M90', |
---|
137 | longFormatName = u'Simple [hkl, Fo\u00b2, sig(Fo\u00b2)] Structure factor text file' |
---|
138 | ) |
---|
139 | self.Super = 0 |
---|
140 | |
---|
141 | def ContentsValidator(self, filepointer): |
---|
142 | 'Discover how many columns are in the m90 file - could be 9-12 depending on satellites' |
---|
143 | numCols = 0 |
---|
144 | for i,line in enumerate(filepointer): |
---|
145 | if 'Data' in line: |
---|
146 | startData = i |
---|
147 | break |
---|
148 | for i,line in enumerate(filepointer): |
---|
149 | if i > startData: |
---|
150 | numCols = max(numCols,len(line.split())) |
---|
151 | if i > startData+20: |
---|
152 | break |
---|
153 | self.Super = numCols-9 #= 0,1,2,or 3 |
---|
154 | return True #ColumnValidator(self, filepointer) |
---|
155 | |
---|
156 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
157 | 'Read the file' |
---|
158 | try: |
---|
159 | for line,S in enumerate(filepointer): |
---|
160 | self.errors = ' Error reading line '+str(line+1) |
---|
161 | if S[0] == '#': continue #ignore comments, if any |
---|
162 | try: |
---|
163 | if self.Super == 0: |
---|
164 | h,k,l,Fo,sigFo = S.split()[:5] |
---|
165 | h,k,l = [int(h),int(k),int(l)] |
---|
166 | elif self.Super == 1: |
---|
167 | h,k,l,m1,Fo,sigFo = S.split()[:6] |
---|
168 | h,k,l,m1 = [int(h),int(k),int(l),int(m1)] |
---|
169 | elif self.Super == 2: |
---|
170 | h,k,l,m1,m2,Fo,sigFo = S.split()[:7] |
---|
171 | h,k,l,m1,m2 = [int(h),int(k),int(l),int(m1),int(m2)] |
---|
172 | elif self.Super == 3: |
---|
173 | h,k,l,m1,m2,m3,Fo,sigFo = S.split()[:8] |
---|
174 | h,k,l,m1,m2,m3 = [int(h),int(k),int(l),int(m1),int(m2),int(m3)] |
---|
175 | except ValueError: #skipping text at front |
---|
176 | text = S.split() |
---|
177 | if text[0] == 'lambda': |
---|
178 | wave = float(text[1]) |
---|
179 | continue |
---|
180 | Fo = float(Fo) |
---|
181 | sigFo = float(sigFo) |
---|
182 | # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,... |
---|
183 | if self.Super == 0: |
---|
184 | self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
185 | elif self.Super == 1: |
---|
186 | self.RefDict['RefList'].append([h,k,l,m1,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
187 | elif self.Super == 2: |
---|
188 | self.RefDict['RefList'].append([h,k,l,m1,m2,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
189 | elif self.Super == 3: |
---|
190 | self.RefDict['RefList'].append([h,k,l,m1,m2,m3,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
191 | self.errors = 'Error after reading reflections (unexpected!)' |
---|
192 | self.RefDict['RefList'] = np.array(self.RefDict['RefList']) |
---|
193 | self.RefDict['Type'] = 'SXC' |
---|
194 | self.RefDict['Super'] = self.Super |
---|
195 | self.UpdateParameters(Type='SXC',Wave=wave) # histogram type |
---|
196 | return True |
---|
197 | except Exception as detail: |
---|
198 | self.errors += '\n '+str(detail) |
---|
199 | print '\n\n'+self.formatName+' read error: '+str(detail) # for testing |
---|
200 | import traceback |
---|
201 | traceback.print_exc(file=sys.stdout) |
---|
202 | return False |
---|
203 | |
---|
204 | class SHELX5_ReaderClass(G2IO.ImportStructFactor): |
---|
205 | 'Routines to import F**2, sig(F**2) reflections from a fixed format SHELX HKLF5 file' |
---|
206 | def __init__(self): |
---|
207 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
208 | extensionlist=('.hkl','.HKL'), |
---|
209 | strictExtension=False, |
---|
210 | formatName = u'SHELX HKL F\u00b2', |
---|
211 | longFormatName = u'SHELX HKLF5 [hkl, Fo\u00b2, sig(Fo\u00b2)] Structure factor text file' |
---|
212 | ) |
---|
213 | self.Super = 0 |
---|
214 | |
---|
215 | def ContentsValidator(self, filepointer): |
---|
216 | 'Discover how many characters are in the SHELX file - could be 32-44 depending on satellites' |
---|
217 | numCols = 0 |
---|
218 | for i,line in enumerate(filepointer): |
---|
219 | numCols = max(numCols,len(line)) |
---|
220 | if i > 20: |
---|
221 | break |
---|
222 | self.Super = (numCols-33)/4 #= 0,1,2,or 3 |
---|
223 | print numCols,self.Super |
---|
224 | return True #ColumnValidator(self, filepointer) |
---|
225 | |
---|
226 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
227 | 'Read the file' |
---|
228 | try: |
---|
229 | for line,S in enumerate(filepointer): |
---|
230 | self.errors = ' Error reading line '+str(line+1) |
---|
231 | if self.Super == 0: |
---|
232 | h,k,l,Fo,sigFo = S[:4],S[4:8],S[8:12],S[12:20],S[20:28] |
---|
233 | h,k,l = [int(h),int(k),int(l)] |
---|
234 | elif self.Super == 1: |
---|
235 | h,k,l,m1,Fo,sigFo = S[:4],S[4:8],S[8:12],S[12:16],S[16:24],S[24:32] |
---|
236 | h,k,l,m1 = [int(h),int(k),int(l),int(m1)] |
---|
237 | elif self.Super == 2: |
---|
238 | h,k,l,m1,m2,Fo,sigFo = S[:4],S[4:8],S[8:12],S[12:16],S[16:20],S[20:28],S[28:36] |
---|
239 | h,k,l,m1,m2 = [int(h),int(k),int(l),int(m1),int(m2)] |
---|
240 | elif self.Super == 3: |
---|
241 | h,k,l,m1,m2,m3,Fo,sigFo = S[:4],S[4:8],S[8:12],S[12:16],S[16:20],S[20:24],S[24:32],S[32:40] |
---|
242 | h,k,l,m1,m2,m3 = [int(h),int(k),int(l),int(m1),int(m2),int(m3)] |
---|
243 | if not any([h,k,l]): |
---|
244 | break |
---|
245 | Fo = float(Fo) |
---|
246 | sigFo = float(sigFo) |
---|
247 | # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,... |
---|
248 | if self.Super == 0: |
---|
249 | self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
250 | elif self.Super == 1: |
---|
251 | self.RefDict['RefList'].append([h,k,l,m1,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
252 | elif self.Super == 2: |
---|
253 | self.RefDict['RefList'].append([h,k,l,m1,m2,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
254 | elif self.Super == 3: |
---|
255 | self.RefDict['RefList'].append([h,k,l,m1,m2,m3,0,0,Fo,sigFo,0,Fo,0,0,0]) |
---|
256 | #self.RefDict['FF'].append({}) # now done in OnImportSfact |
---|
257 | self.errors = 'Error after reading reflections (unexpected!)' |
---|
258 | self.RefDict['RefList'] = np.array(self.RefDict['RefList']) |
---|
259 | self.RefDict['Type'] = 'SXC' |
---|
260 | self.RefDict['Super'] = self.Super |
---|
261 | self.UpdateParameters(Type='SXC',Wave=None) # histogram type |
---|
262 | return True |
---|
263 | except Exception as detail: |
---|
264 | self.errors += '\n '+str(detail) |
---|
265 | print '\n\n'+self.formatName+' read error: '+str(detail) # for testing |
---|
266 | import traceback |
---|
267 | traceback.print_exc(file=sys.stdout) |
---|
268 | return False |
---|
269 | |
---|
270 | class NT_HKLF2_ReaderClass(G2IO.ImportStructFactor): |
---|
271 | 'Routines to import neutron TOF F**2, sig(F**2) reflections from a HKLF file' |
---|
272 | def __init__(self): |
---|
273 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
274 | extensionlist=('.hkl','.HKL'), |
---|
275 | strictExtension=False, |
---|
276 | formatName = u'Neutron TOF HKL F\u00b2', |
---|
277 | longFormatName = u'Neutron TOF [hkl, Fo\u00b2, sig(Fo\u00b2),...] Structure factor text file' |
---|
278 | ) |
---|
279 | |
---|
280 | def ContentsValidator(self, filepointer): |
---|
281 | 'Make sure file contains the expected columns on numbers & count number of data blocks - "Banks"' |
---|
282 | oldNo = -1 |
---|
283 | for line,S in enumerate(filepointer): |
---|
284 | if not S: #empty line terminates read |
---|
285 | break |
---|
286 | if S[0] == '#': continue #ignore comments, if any |
---|
287 | if S.split()[:3] == ['0','0','0']: |
---|
288 | break |
---|
289 | bankNo = S.split()[5] |
---|
290 | if bankNo != oldNo: |
---|
291 | self.Banks.append({'RefDict':{'RefList':[],}}) |
---|
292 | oldNo = bankNo |
---|
293 | filepointer.seek(0) |
---|
294 | return ColumnValidator(self, filepointer,nCol=8) |
---|
295 | |
---|
296 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
297 | 'Read the file' |
---|
298 | filepointer.seek(0) |
---|
299 | try: |
---|
300 | for line,S in enumerate(filepointer): |
---|
301 | self.errors = ' Error reading line '+str(line+1) |
---|
302 | if S[0] == '#': continue #ignore comments, if any |
---|
303 | data = S.split() |
---|
304 | h,k,l,Fo,sigFo,bN,wave,tbar = data[:8] #bN = 1..., 6 dir cos next |
---|
305 | h,k,l = [int(h),int(k),int(l)] |
---|
306 | if not any([h,k,l]): |
---|
307 | break |
---|
308 | Fo = float(Fo) |
---|
309 | sigFo = float(sigFo) |
---|
310 | wave = float(wave) |
---|
311 | tbar = float(tbar) |
---|
312 | if len(self.Banks): |
---|
313 | self.Banks[int(bN)-1]['RefDict']['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0,wave,tbar]) |
---|
314 | else: |
---|
315 | # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,... |
---|
316 | self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0,wave,tbar]) |
---|
317 | if len(self.Banks): |
---|
318 | self.UpdateParameters(Type='SNT',Wave=None) # histogram type |
---|
319 | for Bank in self.Banks: |
---|
320 | Bank['RefDict']['RefList'] = np.array(Bank['RefDict']['RefList']) |
---|
321 | Bank['RefDict']['Type'] = 'SNT' |
---|
322 | Bank['RefDict']['Super'] = 0 |
---|
323 | else: |
---|
324 | self.RefDict['RefList'] = np.array(self.RefDict['RefList']) |
---|
325 | self.RefDict['Type'] = 'SNT' |
---|
326 | self.RefDict['Super'] = 0 |
---|
327 | self.errors = 'Error after reading reflections (unexpected!)' |
---|
328 | self.UpdateParameters(Type='SNT',Wave=None) # histogram type |
---|
329 | return True |
---|
330 | except Exception as detail: |
---|
331 | self.errors += '\n '+str(detail) |
---|
332 | print '\n\n'+self.formatName+' read error: '+str(detail) # for testing |
---|
333 | import traceback |
---|
334 | traceback.print_exc(file=sys.stdout) |
---|
335 | return False |
---|
336 | |
---|
337 | class NT_JANA2K_ReaderClass(G2IO.ImportStructFactor): |
---|
338 | 'Routines to import neutron TOF F**2, sig(F**2) reflections from a JANA2000 file' |
---|
339 | def __init__(self): |
---|
340 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
341 | extensionlist=('.int','.INT'), |
---|
342 | strictExtension=False, |
---|
343 | formatName = u'Neutron TOF JANA2000 F\u00b2', |
---|
344 | longFormatName = u'Neutron TOF [hkl, Fo\u00b2, sig(Fo\u00b2),...] Structure factor text file' |
---|
345 | ) |
---|
346 | |
---|
347 | def ContentsValidator(self, filepointer): |
---|
348 | 'Make sure file contains the expected columns on numbers & count number of data blocks - "Banks"' |
---|
349 | oldNo = -1 |
---|
350 | for line,S in enumerate(filepointer): |
---|
351 | if not S: #empty line terminates read |
---|
352 | break |
---|
353 | if S[0] in ['#','(']: continue #ignore comments & fortran format line |
---|
354 | bankNo = S.split()[5] |
---|
355 | if bankNo != oldNo: |
---|
356 | self.Banks.append({'RefDict':{'RefList':[],}}) |
---|
357 | oldNo = bankNo |
---|
358 | filepointer.seek(0) |
---|
359 | return ColumnValidator(self, filepointer,nCol=10) |
---|
360 | |
---|
361 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
362 | 'Read the file' |
---|
363 | filepointer.seek(0) |
---|
364 | try: |
---|
365 | for line,S in enumerate(filepointer): |
---|
366 | self.errors = ' Error reading line '+str(line+1) |
---|
367 | if S[0] in ['#','(']: continue #ignore comments & fortran format line |
---|
368 | data = S.split() |
---|
369 | h,k,l,Fo,sigFo,bN,wave,x,x,tbar = data[:10] #bN = 1..., 6 dir cos next |
---|
370 | h,k,l = [int(h),int(k),int(l)] |
---|
371 | if not any([h,k,l]): |
---|
372 | break |
---|
373 | Fo = float(Fo) |
---|
374 | sigFo = float(sigFo) |
---|
375 | wave = float(wave) |
---|
376 | tbar = float(tbar) |
---|
377 | if len(self.Banks): |
---|
378 | self.Banks[int(bN)-1]['RefDict']['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0,wave,tbar]) |
---|
379 | else: |
---|
380 | # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,... |
---|
381 | self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0,wave,tbar]) |
---|
382 | if len(self.Banks): |
---|
383 | self.UpdateParameters(Type='SNT',Wave=None) # histogram type |
---|
384 | for Bank in self.Banks: |
---|
385 | Bank['RefDict']['RefList'] = np.array(Bank['RefDict']['RefList']) |
---|
386 | Bank['RefDict']['Type'] = 'SNT' |
---|
387 | Bank['RefDict']['Super'] = 0 #for now |
---|
388 | else: |
---|
389 | self.RefDict['RefList'] = np.array(self.RefDict['RefList']) |
---|
390 | self.RefDict['Type'] = 'SNT' |
---|
391 | self.RefDict['Super'] = 0 #for now |
---|
392 | self.errors = 'Error after reading reflections (unexpected!)' |
---|
393 | self.UpdateParameters(Type='SNT',Wave=None) # histogram type |
---|
394 | return True |
---|
395 | except Exception as detail: |
---|
396 | self.errors += '\n '+str(detail) |
---|
397 | print '\n\n'+self.formatName+' read error: '+str(detail) # for testing |
---|
398 | import traceback |
---|
399 | traceback.print_exc(file=sys.stdout) |
---|
400 | return False |
---|
401 | |
---|