1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2018-03-01 20:30:45 +0000 (Thu, 01 Mar 2018) $ |
---|
4 | # $Author: vondreele $ |
---|
5 | # $Revision: 3300 $ |
---|
6 | # $URL: trunk/imports/G2phase.py $ |
---|
7 | # $Id: G2phase.py 3300 2018-03-01 20:30:45Z vondreele $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | # |
---|
10 | ''' |
---|
11 | *Module G2phase: PDB, .EXP & JANA m40,m50* |
---|
12 | ------------------------------------------- |
---|
13 | |
---|
14 | A set of short routines to read in phases using routines that were |
---|
15 | previously implemented in GSAS-II: PDB, GSAS .EXP and JANA m40-m50 file formats |
---|
16 | |
---|
17 | ''' |
---|
18 | |
---|
19 | from __future__ import division, print_function |
---|
20 | import sys |
---|
21 | import os.path |
---|
22 | import math |
---|
23 | import random as ran |
---|
24 | import numpy as np |
---|
25 | try: |
---|
26 | import wx |
---|
27 | except ImportError: |
---|
28 | wx = None |
---|
29 | import GSASIIobj as G2obj |
---|
30 | import GSASIIspc as G2spc |
---|
31 | import GSASIIlattice as G2lat |
---|
32 | import GSASIIpath |
---|
33 | GSASIIpath.SetVersionNumber("$Revision: 3300 $") |
---|
34 | R2pisq = 1./(2.*np.pi**2) |
---|
35 | |
---|
36 | class PDB_ReaderClass(G2obj.ImportPhase): |
---|
37 | 'Routine to import Phase information from a PDB file' |
---|
38 | def __init__(self): |
---|
39 | super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__ |
---|
40 | extensionlist=('.pdb','.ent','.PDB','.ENT'), |
---|
41 | strictExtension=True, |
---|
42 | formatName = 'PDB', |
---|
43 | longFormatName = 'Original Protein Data Bank (.pdb file) import' |
---|
44 | ) |
---|
45 | def ContentsValidator(self, filename): |
---|
46 | '''Taking a stab a validating a PDB file |
---|
47 | (look for cell & at least one atom) |
---|
48 | ''' |
---|
49 | fp = open(filename,'r') |
---|
50 | for i,l in enumerate(fp): |
---|
51 | if l.startswith('CRYST1'): |
---|
52 | break |
---|
53 | else: |
---|
54 | self.errors = 'no CRYST1 record found' |
---|
55 | fp.close() |
---|
56 | return False |
---|
57 | for i,l in enumerate(fp): |
---|
58 | if l.startswith('ATOM'): |
---|
59 | fp.close() |
---|
60 | return True |
---|
61 | self.errors = 'no ATOM records found after CRYST1 record' |
---|
62 | fp.close() |
---|
63 | return False |
---|
64 | |
---|
65 | def Reader(self,filename, ParentFrame=None, **unused): |
---|
66 | 'Read a PDF file using :meth:`ReadPDBPhase`' |
---|
67 | self.Phase = self.ReadPDBPhase(filename, ParentFrame) |
---|
68 | return True |
---|
69 | |
---|
70 | def ReadPDBPhase(self,filename,parent=None): |
---|
71 | '''Read a phase from a PDB file. |
---|
72 | ''' |
---|
73 | EightPiSq = 8.*math.pi**2 |
---|
74 | self.errors = 'Error opening file' |
---|
75 | file = open(filename, 'Ur') |
---|
76 | Phase = {} |
---|
77 | Title = '' |
---|
78 | Compnd = '' |
---|
79 | Atoms = [] |
---|
80 | A = np.zeros(shape=(3,3)) |
---|
81 | S = file.readline() |
---|
82 | line = 1 |
---|
83 | SGData = None |
---|
84 | cell = None |
---|
85 | while S: |
---|
86 | self.errors = 'Error reading at line '+str(line) |
---|
87 | Atom = [] |
---|
88 | if 'TITLE' in S[:5]: |
---|
89 | Title = S[10:72].strip() |
---|
90 | elif 'COMPND ' in S[:10]: |
---|
91 | Compnd = S[10:72].strip() |
---|
92 | elif 'CRYST' in S[:5]: |
---|
93 | abc = S[7:34].split() |
---|
94 | angles = S[34:55].split() |
---|
95 | cell=[float(abc[0]),float(abc[1]),float(abc[2]), |
---|
96 | float(angles[0]),float(angles[1]),float(angles[2])] |
---|
97 | Volume = G2lat.calc_V(G2lat.cell2A(cell)) |
---|
98 | AA,AB = G2lat.cell2AB(cell) |
---|
99 | SpGrp = S[55:65] |
---|
100 | E,SGData = G2spc.SpcGroup(SpGrp) |
---|
101 | # space group processing failed, try to look up name in table |
---|
102 | if E: |
---|
103 | SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) |
---|
104 | if SpGrpNorm: |
---|
105 | E,SGData = G2spc.SpcGroup(SpGrpNorm) |
---|
106 | while E: |
---|
107 | dlg = wx.TextEntryDialog(parent, |
---|
108 | SpGrp[:-1]+' is invalid \nN.B.: make sure spaces separate axial fields in symbol', |
---|
109 | 'ERROR in space group symbol','',style=wx.OK) |
---|
110 | if dlg.ShowModal() == wx.ID_OK: |
---|
111 | SpGrp = dlg.GetValue() |
---|
112 | E,SGData = G2spc.SpcGroup(SpGrp) |
---|
113 | else: |
---|
114 | SGData = G2obj.P1SGData # P 1 |
---|
115 | self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' |
---|
116 | self.warnings += "Change this in phase's General tab." |
---|
117 | dlg.Destroy() |
---|
118 | # SGlines = G2spc.SGPrint(SGData) |
---|
119 | # for l in SGlines: print (l) |
---|
120 | elif 'SCALE' in S[:5]: |
---|
121 | V = S[10:41].split() |
---|
122 | A[int(S[5])-1] = [float(V[0]),float(V[1]),float(V[2])] |
---|
123 | elif 'ATOM' in S[:4] or 'HETATM' in S[:6]: |
---|
124 | if not SGData: |
---|
125 | self.warnings += '\nThe space group was not read before atoms and has been set to "P 1". ' |
---|
126 | self.warnings += "Change this in phase's General tab." |
---|
127 | SGData = G2obj.P1SGData # P 1 |
---|
128 | XYZ = [float(S[31:39]),float(S[39:47]),float(S[47:55])] |
---|
129 | XYZ = np.inner(AB,XYZ) |
---|
130 | XYZ = np.where(abs(XYZ)<0.00001,0,XYZ) |
---|
131 | SytSym,Mult = G2spc.SytSym(XYZ,SGData)[:2] |
---|
132 | Uiso = float(S[61:67])/EightPiSq |
---|
133 | Type = S[76:78].lower() |
---|
134 | Atom = [S[22:27].strip(),S[17:20].upper(),S[20:22], |
---|
135 | S[12:17].strip(),Type.strip().capitalize(),'',XYZ[0],XYZ[1],XYZ[2], |
---|
136 | float(S[55:61]),SytSym,Mult,'I',Uiso,0,0,0,0,0,0] |
---|
137 | if S[16] in [' ','A','B']: #remove disorered residues - can't handle them just now |
---|
138 | # Atom[3] = Atom[3][:3] |
---|
139 | Atom.append(ran.randint(0,sys.maxsize)) |
---|
140 | Atoms.append(Atom) |
---|
141 | elif 'ANISOU' in S[:6]: |
---|
142 | Uij = S[30:72].split() |
---|
143 | Uij = [float(Uij[0])/10000.,float(Uij[1])/10000.,float(Uij[2])/10000., |
---|
144 | float(Uij[3])/10000.,float(Uij[4])/10000.,float(Uij[5])/10000.] |
---|
145 | Atoms[-1] = Atoms[-1][:14]+Uij |
---|
146 | Atoms[-1][12] = 'A' |
---|
147 | Atoms[-1].append(ran.randint(0,sys.maxsize)) |
---|
148 | S = file.readline() |
---|
149 | line += 1 |
---|
150 | file.close() |
---|
151 | self.errors = 'Error after read complete' |
---|
152 | if Title: |
---|
153 | PhaseName = Title |
---|
154 | elif Compnd: |
---|
155 | PhaseName = Compnd |
---|
156 | else: |
---|
157 | PhaseName = 'None' |
---|
158 | if not SGData: |
---|
159 | raise self.ImportException("No space group (CRYST entry) found") |
---|
160 | if not cell: |
---|
161 | raise self.ImportException("No cell (CRYST entry) found") |
---|
162 | Phase = G2obj.SetNewPhase(Name=PhaseName,SGData=SGData,cell=cell+[Volume,]) |
---|
163 | Phase['General']['Type'] = 'macromolecular' |
---|
164 | Phase['General']['AtomPtrs'] = [6,4,10,12] |
---|
165 | Phase['Atoms'] = Atoms |
---|
166 | return Phase |
---|
167 | |
---|
168 | class EXP_ReaderClass(G2obj.ImportPhase): |
---|
169 | 'Routine to import Phase information from GSAS .EXP files' |
---|
170 | def __init__(self): |
---|
171 | super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__ |
---|
172 | extensionlist=('.EXP','.exp'), |
---|
173 | strictExtension=True, |
---|
174 | formatName = 'GSAS .EXP', |
---|
175 | longFormatName = 'GSAS Experiment (.EXP file) import' |
---|
176 | ) |
---|
177 | |
---|
178 | def ContentsValidator(self, filename): |
---|
179 | 'Look for a VERSION tag in 1st line' |
---|
180 | fp = open(filename,'r') |
---|
181 | if fp.read(13) == ' VERSION ': |
---|
182 | fp.close() |
---|
183 | return True |
---|
184 | self.errors = 'File does not begin with VERSION tag' |
---|
185 | fp.close() |
---|
186 | return False |
---|
187 | |
---|
188 | def Reader(self,filename,ParentFrame=None,usedRanIdList=[],**unused): |
---|
189 | 'Read a phase from a GSAS .EXP file using :meth:`ReadEXPPhase`' |
---|
190 | self.Phase = G2obj.SetNewPhase(Name='new phase') # create a new empty phase dict |
---|
191 | while self.Phase['ranId'] in usedRanIdList: |
---|
192 | self.Phase['ranId'] = ran.randint(0,sys.maxsize) |
---|
193 | # make sure the ranId is really unique! |
---|
194 | self.MPhase = G2obj.SetNewPhase(Name='new phase') # create a new empty phase dict |
---|
195 | while self.MPhase['ranId'] in usedRanIdList: |
---|
196 | self.MPhase['ranId'] = ran.randint(0,sys.maxsize) |
---|
197 | fp = open(filename,'r') |
---|
198 | self.ReadEXPPhase(ParentFrame, fp) |
---|
199 | fp.close() |
---|
200 | return True |
---|
201 | |
---|
202 | def ReadEXPPhase(self, G2frame,filepointer): |
---|
203 | '''Read a phase from a GSAS .EXP file. |
---|
204 | ''' |
---|
205 | shModels = ['cylindrical','none','shear - 2/m','rolling - mmm'] |
---|
206 | textureData = {'Order':0,'Model':'cylindrical','Sample omega':[False,0.0], |
---|
207 | 'Sample chi':[False,0.0],'Sample phi':[False,0.0],'SH Coeff':[False,{}], |
---|
208 | 'SHShow':False,'PFhkl':[0,0,1],'PFxyz':[0,0,1],'PlotType':'Pole figure'} |
---|
209 | shNcof = 0 |
---|
210 | S = 1 |
---|
211 | NPhas = [] |
---|
212 | Expr = [{},{},{},{},{},{},{},{},{}] # GSAS can have at most 9 phases |
---|
213 | for line,S in enumerate(filepointer): |
---|
214 | self.errors = 'Error reading at line '+str(line+1) |
---|
215 | if 'EXPR NPHAS' in S[:12]: |
---|
216 | NPhas = S[12:-1].split() |
---|
217 | if 'CRS' in S[:3]: |
---|
218 | N = int(S[3:4])-1 |
---|
219 | Expr[N][S[:12]] = S[12:-1] |
---|
220 | PNames = [] |
---|
221 | if not NPhas: |
---|
222 | raise self.ImportException("No EXPR NPHAS record found") |
---|
223 | self.errors = 'Error interpreting file' |
---|
224 | for n,N in enumerate(NPhas): |
---|
225 | if N != '0': |
---|
226 | result = n |
---|
227 | key = 'CRS'+str(n+1)+' PNAM' |
---|
228 | PNames.append(Expr[n][key]) |
---|
229 | if len(PNames) == 0: |
---|
230 | raise self.ImportException("No phases found") |
---|
231 | elif len(PNames) > 1: |
---|
232 | dlg = wx.SingleChoiceDialog(G2frame, 'Which phase to read?', 'Read phase data', PNames, wx.CHOICEDLG_STYLE) |
---|
233 | try: |
---|
234 | if dlg.ShowModal() == wx.ID_OK: |
---|
235 | result = dlg.GetSelection() # I think this breaks is there are skipped phases. Cant this happen? |
---|
236 | finally: |
---|
237 | dlg.Destroy() |
---|
238 | EXPphase = Expr[result] |
---|
239 | keyList = list(EXPphase.keys()) |
---|
240 | keyList.sort() |
---|
241 | SGData = {} |
---|
242 | MPtype = '' |
---|
243 | if NPhas[result] == '1': |
---|
244 | Ptype = 'nuclear' |
---|
245 | elif NPhas[result] =='2': |
---|
246 | Ptype = 'nuclear' |
---|
247 | MPtype = 'magnetic' |
---|
248 | MagDmin = 1.0 |
---|
249 | elif NPhas[result] =='3': |
---|
250 | Ptype = 'magnetic' |
---|
251 | MagDmin = 1.0 |
---|
252 | elif NPhas[result] == '4': |
---|
253 | Ptype = 'macromolecular' |
---|
254 | elif NPhas[result] == '10': |
---|
255 | Ptype = 'Pawley' |
---|
256 | else: |
---|
257 | raise self.ImportException("Phase type not recognized") |
---|
258 | |
---|
259 | for key in keyList: |
---|
260 | if 'PNAM' in key: |
---|
261 | PhaseName = EXPphase[key].strip() |
---|
262 | elif 'ABC ' in key: |
---|
263 | abc = [float(EXPphase[key][:10]),float(EXPphase[key][10:20]),float(EXPphase[key][20:30])] |
---|
264 | elif 'ANGLES' in key: |
---|
265 | angles = [float(EXPphase[key][:10]),float(EXPphase[key][10:20]),float(EXPphase[key][20:30])] |
---|
266 | elif 'SG SYM' in key: |
---|
267 | SpGrp = EXPphase[key][:15].strip() |
---|
268 | E,SGData = G2spc.SpcGroup(SpGrp) |
---|
269 | if E: |
---|
270 | SGData = G2obj.P1SGData # P 1 -- unlikely to need this! |
---|
271 | self.warnings += '\nThe GSAS space group was not interpreted(!) and has been set to "P 1".' |
---|
272 | self.warnings += "Change this in phase's General tab." |
---|
273 | elif 'SPNFLP' in key: |
---|
274 | SpnFlp = np.array([int(float(s)) for s in EXPphase[key].split()]) |
---|
275 | SpnFlp = np.where(SpnFlp==0,1,SpnFlp) |
---|
276 | SpnFlp = [1,]+list(SpnFlp) |
---|
277 | if SGData['SpGrp'][0] in ['A','B','C','I','R','F']: |
---|
278 | SpnFlp = list(SpnFlp)+[1,1,1,1] |
---|
279 | elif 'MXDSTR' in key: |
---|
280 | MagDmin = float(EXPphase[key][:10]) |
---|
281 | elif 'OD ' in key: |
---|
282 | SHdata = EXPphase[key].split() # may not have all 9 values |
---|
283 | SHvals = 9*[0] |
---|
284 | for i in range(9): |
---|
285 | try: |
---|
286 | float(SHdata[i]) |
---|
287 | SHvals[i] = SHdata[i] |
---|
288 | except: |
---|
289 | pass |
---|
290 | textureData['Order'] = int(SHvals[0]) |
---|
291 | textureData['Model'] = shModels[int(SHvals[2])] |
---|
292 | textureData['Sample omega'] = [False,float(SHvals[6])] |
---|
293 | textureData['Sample chi'] = [False,float(SHvals[7])] |
---|
294 | textureData['Sample phi'] = [False,float(SHvals[8])] |
---|
295 | shNcof = int(SHvals[1]) |
---|
296 | Volume = G2lat.calc_V(G2lat.cell2A(abc+angles)) |
---|
297 | |
---|
298 | Atoms = [] |
---|
299 | MAtoms = [] |
---|
300 | Bmat = G2lat.cell2AB(abc+angles)[1] |
---|
301 | if Ptype == 'macromolecular': |
---|
302 | for key in keyList: |
---|
303 | if 'AT' in key[6:8]: |
---|
304 | S = EXPphase[key] |
---|
305 | Atom = [S[56:60].strip(),S[50:54].strip().upper(),S[54:56], |
---|
306 | S[46:51].strip(),S[:8].strip().capitalize(),'', |
---|
307 | float(S[16:24]),float(S[24:32]),float(S[32:40]), |
---|
308 | float(S[8:16]),'1',1,'I',float(S[40:46]),0,0,0,0,0,0] |
---|
309 | XYZ = Atom[6:9] |
---|
310 | Atom[10],Atom[11] = G2spc.SytSym(XYZ,SGData)[:2] |
---|
311 | Atom.append(ran.randint(0,sys.maxsize)) |
---|
312 | Atoms.append(Atom) |
---|
313 | else: |
---|
314 | for key in keyList: |
---|
315 | if 'AT' in key: |
---|
316 | if key[11:] == 'A': |
---|
317 | S = EXPphase[key] |
---|
318 | elif key[11:] == 'B': |
---|
319 | S1 = EXPphase[key] |
---|
320 | Atom = [S[50:58].strip(),S[:10].strip().capitalize(),'', |
---|
321 | float(S[10:20]),float(S[20:30]),float(S[30:40]), |
---|
322 | float(S[40:50]),'',int(S[60:62]),S1[62:63]] |
---|
323 | #float(S[40:50]),'',int(S[60:62]),S1[130:131]] |
---|
324 | if Atom[9] == 'I': |
---|
325 | Atom += [float(S1[0:10]),0.,0.,0.,0.,0.,0.] |
---|
326 | elif Atom[9] == 'A': |
---|
327 | Atom += [0.0, |
---|
328 | float(S1[ 0:10]),float(S1[10:20]), |
---|
329 | float(S1[20:30]),float(S1[30:40]), |
---|
330 | float(S1[40:50]),float(S1[50:60])] |
---|
331 | else: |
---|
332 | print('Error in line with key: '+key) |
---|
333 | Atom += [0.,0.,0.,0.,0.,0.,0.] |
---|
334 | XYZ = Atom[3:6] |
---|
335 | Atom[7],Atom[8] = G2spc.SytSym(XYZ,SGData)[:2] |
---|
336 | Atom.append(ran.randint(0,sys.maxsize)) |
---|
337 | Atoms.append(Atom) |
---|
338 | elif key[11:] == 'M' and key[6:8] == 'AT': |
---|
339 | S = EXPphase[key] |
---|
340 | mom = np.array([float(S[:10]),float(S[10:20]),float(S[20:30])]) |
---|
341 | mag = np.sqrt(np.sum(mom**2)) |
---|
342 | mom = np.inner(Bmat,mom)*mag |
---|
343 | MAtoms.append(Atom) |
---|
344 | MAtoms[-1] = Atom[:7]+list(mom)+Atom[7:] |
---|
345 | |
---|
346 | if shNcof: |
---|
347 | shCoef = {} |
---|
348 | nRec = [i+1 for i in range((shNcof-1)//6+1)] |
---|
349 | for irec in nRec: |
---|
350 | ODkey = keyList[0][:6]+'OD'+'%3dA'%(irec) |
---|
351 | indx = EXPphase[ODkey].split() |
---|
352 | ODkey = ODkey[:-1]+'B' |
---|
353 | vals = EXPphase[ODkey].split() |
---|
354 | for i,val in enumerate(vals): |
---|
355 | key = 'C(%s,%s,%s)'%(indx[3*i],indx[3*i+1],indx[3*i+2]) |
---|
356 | shCoef[key] = float(val) |
---|
357 | textureData['SH Coeff'] = [False,shCoef] |
---|
358 | |
---|
359 | if not SGData: |
---|
360 | raise self.ImportException("No space group found in phase") |
---|
361 | if not abc: |
---|
362 | raise self.ImportException("No cell lengths found in phase") |
---|
363 | if not angles: |
---|
364 | raise self.ImportException("No cell angles found in phase") |
---|
365 | if not Atoms: |
---|
366 | raise self.ImportException("No atoms found in phase") |
---|
367 | |
---|
368 | self.Phase['General'].update({'Type':Ptype,'Name':PhaseName,'Cell':[False,]+abc+angles+[Volume,],'SGData':SGData}) |
---|
369 | if MPtype == 'magnetic': |
---|
370 | self.MPhase['General'].update({'Type':'magnetic','Name':PhaseName+' mag','Cell':[False,]+abc+angles+[Volume,],'SGData':SGData}) |
---|
371 | else: |
---|
372 | self.MPhase = None |
---|
373 | |
---|
374 | if Ptype =='macromolecular': |
---|
375 | self.Phase['General']['AtomPtrs'] = [6,4,10,12] |
---|
376 | self.Phase['Atoms'] = Atoms |
---|
377 | elif Ptype == 'magnetic': |
---|
378 | self.Phase['General']['AtomPtrs'] = [3,1,10,12] |
---|
379 | self.Phase['General']['SGData']['SGSpin'] = SpnFlp |
---|
380 | self.Phase['General']['MagDmin'] = MagDmin |
---|
381 | self.Phase['Atoms'] = MAtoms |
---|
382 | else: #nuclear |
---|
383 | self.Phase['General']['AtomPtrs'] = [3,1,7,9] |
---|
384 | self.Phase['General']['SH Texture'] = textureData |
---|
385 | self.Phase['Atoms'] = Atoms |
---|
386 | if MPtype =='magnetic': |
---|
387 | self.MPhase['General']['AtomPtrs'] = [3,1,10,12] |
---|
388 | self.MPhase['General']['SGData']['SGSpin'] = SpnFlp |
---|
389 | self.MPhase['General']['MagDmin'] = MagDmin |
---|
390 | self.MPhase['Atoms'] = MAtoms |
---|
391 | |
---|
392 | class JANA_ReaderClass(G2obj.ImportPhase): |
---|
393 | 'Routine to import Phase information from a JANA2006 file' |
---|
394 | def __init__(self): |
---|
395 | super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__ |
---|
396 | extensionlist=('.m50','.M50'), |
---|
397 | strictExtension=True, |
---|
398 | formatName = 'JANA m50', |
---|
399 | longFormatName = 'JANA2006 phase import' |
---|
400 | ) |
---|
401 | def ContentsValidator(self, filename): |
---|
402 | '''Taking a stab a validating a .m50 file |
---|
403 | (look for cell & at least one atom) |
---|
404 | ''' |
---|
405 | fp = open(filename,'r') |
---|
406 | for i,l in enumerate(fp): |
---|
407 | if l.startswith('cell'): |
---|
408 | break |
---|
409 | else: |
---|
410 | self.errors = 'no cell record found' |
---|
411 | fp.close() |
---|
412 | return False |
---|
413 | for i,l in enumerate(fp): |
---|
414 | if l.startswith('spgroup'): |
---|
415 | fp.close() |
---|
416 | return True |
---|
417 | self.errors = 'no spgroup record found after cell record' |
---|
418 | fp.close() |
---|
419 | return False |
---|
420 | |
---|
421 | def Reader(self,filename, ParentFrame=None, **unused): |
---|
422 | 'Read a m50 file using :meth:`ReadJANAPhase`' |
---|
423 | self.Phase = self.ReadJANAPhase(filename, ParentFrame) |
---|
424 | return True |
---|
425 | |
---|
426 | def ReadJANAPhase(self,filename,parent=None): |
---|
427 | '''Read a phase from a JANA2006 m50 & m40 files. |
---|
428 | ''' |
---|
429 | self.errors = 'Error opening file' |
---|
430 | fp = open(filename, 'Ur') #contains only cell & spcgroup |
---|
431 | Phase = {} |
---|
432 | Title = os.path.basename(filename) |
---|
433 | Type = 'nuclear' |
---|
434 | Atoms = [] |
---|
435 | Atypes = [] |
---|
436 | SuperVec = [[0,0,.1],False,4] |
---|
437 | S = fp.readline() |
---|
438 | line = 1 |
---|
439 | SGData = None |
---|
440 | SuperSg = '' |
---|
441 | cell = None |
---|
442 | nqi = 0 |
---|
443 | version = '2000' |
---|
444 | while S: |
---|
445 | self.errors = 'Error reading at line '+str(line) |
---|
446 | if 'title' in S and S != 'title\n': |
---|
447 | Title = S.split()[1] |
---|
448 | elif 'Jana2006' in S: |
---|
449 | self.warnings += '\nJana2006 file detected' |
---|
450 | version = '2006' |
---|
451 | elif 'cell' in S[:4]: |
---|
452 | cell = S[5:].split() |
---|
453 | cell=[float(cell[0]),float(cell[1]),float(cell[2]), |
---|
454 | float(cell[3]),float(cell[4]),float(cell[5])] |
---|
455 | Volume = G2lat.calc_V(G2lat.cell2A(cell)) |
---|
456 | G,g = G2lat.cell2Gmat(cell) |
---|
457 | ast = np.sqrt(np.diag(G)) |
---|
458 | Mast = np.multiply.outer(ast,ast) |
---|
459 | |
---|
460 | elif 'spgroup' in S: |
---|
461 | if 'X' in S: |
---|
462 | raise self.ImportException("Ad hoc Supersymmetry centering "+S+" not allowed in GSAS-II") |
---|
463 | SpGrp = S.split()[1] |
---|
464 | SuperSg = '' |
---|
465 | if '(' in SpGrp: #supercell symmetry - split in 2 |
---|
466 | SuperStr = SpGrp.split('(') |
---|
467 | SpGrp = SuperStr[0] |
---|
468 | SuperSg = '('+SuperStr[1] |
---|
469 | SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) |
---|
470 | E,SGData = G2spc.SpcGroup(SpGrpNorm) |
---|
471 | # space group processing failed, try to look up name in table |
---|
472 | while E: |
---|
473 | print (G2spc.SGErrors(E)) |
---|
474 | dlg = wx.TextEntryDialog(parent, |
---|
475 | SpGrp[:-1]+' is invalid \nN.B.: make sure spaces separate axial fields in symbol', |
---|
476 | 'ERROR in space group symbol','',style=wx.OK) |
---|
477 | if dlg.ShowModal() == wx.ID_OK: |
---|
478 | SpGrp = dlg.GetValue() |
---|
479 | E,SGData = G2spc.SpcGroup(SpGrp) |
---|
480 | else: |
---|
481 | SGData = G2obj.P1SGData # P 1 |
---|
482 | self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' |
---|
483 | self.warnings += "Change this in phase's General tab." |
---|
484 | dlg.Destroy() |
---|
485 | G2spc.SGPrint(SGData) #silent check of space group symbol |
---|
486 | elif 'qi' in S[:2]: |
---|
487 | if nqi: |
---|
488 | raise self.ImportException("Supersymmetry too high; GSAS-II limited to (3+1) supersymmetry") |
---|
489 | vec = S.split()[1:] |
---|
490 | SuperVec = [[float(vec[i]) for i in range(3)],False,4] |
---|
491 | nqi += 1 |
---|
492 | elif 'atom' in S[:4]: |
---|
493 | Atypes.append(S.split()[1]) |
---|
494 | S = fp.readline() |
---|
495 | line += 1 |
---|
496 | fp.close() |
---|
497 | #read atoms from m40 file |
---|
498 | if not SGData: |
---|
499 | self.warnings += '\nThe space group was not read before atoms and has been set to "P 1". ' |
---|
500 | self.warnings += "Change this in phase's General tab." |
---|
501 | SGData = G2obj.P1SGData # P 1 |
---|
502 | waveTypes = ['Fourier','Sawtooth','ZigZag',] |
---|
503 | filename2 = os.path.splitext(filename)[0]+'.m40' |
---|
504 | file2 = open(filename2,'Ur') |
---|
505 | S = file2.readline() |
---|
506 | line = 1 |
---|
507 | self.errors = 'Error reading at line '+str(line) |
---|
508 | nAtoms = int(S.split()[0]) |
---|
509 | for i in range(4): |
---|
510 | S = file2.readline() |
---|
511 | for i in range(nAtoms): |
---|
512 | S1 = file2.readline() |
---|
513 | S1N = S1.split()[-3:] # no. occ, no. pos waves, no. ADP waves |
---|
514 | S1N = [int(i) for i in S1N] |
---|
515 | S1T = list(S1[60:63]) |
---|
516 | waveType = waveTypes[int(S1T[1])] |
---|
517 | Spos = [] |
---|
518 | Sadp = [] |
---|
519 | Sfrac = [] |
---|
520 | Smag = [] |
---|
521 | XYZ = [float(S1[27:36]),float(S1[36:45]),float(S1[45:54])] |
---|
522 | SytSym,Mult = G2spc.SytSym(XYZ,SGData)[:2] |
---|
523 | aType = Atypes[int(S1[9:11])-1] |
---|
524 | Name = S1[:8].strip() |
---|
525 | if S1[11:15].strip() == '1': |
---|
526 | S2 = file2.readline() |
---|
527 | Uiso = S2[:9] |
---|
528 | if version == '2000': |
---|
529 | Uiso = R2pisq*float(Uiso)/4. #Biso -> Uiso |
---|
530 | Uij = [0,0,0,0,0,0] |
---|
531 | IA = 'I' |
---|
532 | elif S1[11:15].strip() == '2': |
---|
533 | S2 = file2.readline() |
---|
534 | IA = 'A' |
---|
535 | Uiso = 0. |
---|
536 | Uij = [float(S2[:9]),float(S2[9:18]),float(S2[18:27]), |
---|
537 | float(S2[27:36]),float(S2[36:45]),float(S2[45:54])] #Uij in Jana2006! |
---|
538 | if version == '2000': |
---|
539 | Uij = R2pisq*G2lat.UijtoU6(G2lat.U6toUij(Uij)/Mast) #these things are betaij in Jana2000! need to convert to Uij |
---|
540 | for i in range(S1N[0]): |
---|
541 | if not i: |
---|
542 | FS = file2.readline() |
---|
543 | Sfrac.append(FS[:9]) #'O' or 'delta' = 'length' for crenel |
---|
544 | if int(S1T[0]): #"", "Legendre" or "Xharm" in 18:27 for "crenel"! |
---|
545 | waveType = 'Crenel/Fourier' #all waves 'Fourier' no other choice |
---|
546 | Sfrac.append(file2.readline()[:18]) #if not crenel = Osin & Ocos |
---|
547 | # else Osin & Ocos except last one is X40 = 'Center' |
---|
548 | for i in range(S1N[1]): |
---|
549 | Spos.append(file2.readline()[:54]) |
---|
550 | for i in range(S1N[2]): |
---|
551 | Sadp.append(file2.readline()[:54]+file2.readline()) |
---|
552 | if sum(S1N): #if any waves: skip mystery line? |
---|
553 | file2.readline() |
---|
554 | for i,it in enumerate(Sfrac): |
---|
555 | print (i,it) |
---|
556 | if not i: |
---|
557 | if 'Crenel' in waveType: |
---|
558 | vals = [float(it),float(Sfrac[-1][:9])] |
---|
559 | else: |
---|
560 | vals = [float(it),] |
---|
561 | else: |
---|
562 | vals = [float(it[:9]),float(it[9:18])] |
---|
563 | if 'Crenel' in waveType and i == len(Sfrac)-1: |
---|
564 | del Sfrac[-1] |
---|
565 | break |
---|
566 | Sfrac[i] = [vals,False] |
---|
567 | print (Sfrac[i]) |
---|
568 | for i,it in enumerate(Spos): |
---|
569 | if waveType in ['Sawtooth',] and not i: |
---|
570 | vals = [float(it[:9]),float(it[9:18]),float(it[18:27]),float(it[27:36])] |
---|
571 | else: |
---|
572 | vals = [float(it[:9]),float(it[9:18]),float(it[18:27]),float(it[27:36]),float(it[36:45]),float(it[45:54])] |
---|
573 | Spos[i] = [vals,False] |
---|
574 | for i,it in enumerate(Sadp): |
---|
575 | vals = [float(it[:9]),float(it[9:18]),float(it[18:27]),float(it[27:36]),float(it[36:45]),float(it[45:54]), |
---|
576 | float(it[54:63]),float(it[63:72]),float(it[72:81]),float(it[81:90]),float(it[90:99]),float(it[99:108])] |
---|
577 | #these are betaij modulations in Jana2000! need to convert to Uij modulations |
---|
578 | if version == '2000': |
---|
579 | vals[:6] = R2pisq*G2lat.UijtoU6(G2lat.U6toUij(vals[:6])/Mast) #convert sin bij to Uij |
---|
580 | vals[6:] = R2pisq*G2lat.UijtoU6(G2lat.U6toUij(vals[6:])/Mast) #convert cos bij to Uij |
---|
581 | Sadp[i] = [vals,False] |
---|
582 | Atom = [Name,aType,'',XYZ[0],XYZ[1],XYZ[2],1.0,SytSym,Mult,IA,Uiso] |
---|
583 | Atom += Uij |
---|
584 | Atom.append(ran.randint(0,sys.maxsize)) |
---|
585 | Atom.append([]) |
---|
586 | Atom.append([]) |
---|
587 | Atom.append({'SS1':{'Sfrac':[waveType,]+Sfrac,'Spos':[waveType,]+Spos,'Sadp':['Fourier',]+Sadp,'Smag':['Fourier',]+Smag}}) #SS2 is for (3+2), etc. |
---|
588 | Atoms.append(Atom) |
---|
589 | file2.close() |
---|
590 | self.errors = 'Error after read complete' |
---|
591 | if not SGData: |
---|
592 | raise self.ImportException("No space group (spcgroup entry) found") |
---|
593 | if not cell: |
---|
594 | raise self.ImportException("No cell found") |
---|
595 | Phase = G2obj.SetNewPhase(Name=Title,SGData=SGData,cell=cell+[Volume,]) |
---|
596 | Phase['General']['Type'] = Type |
---|
597 | Phase['General']['Modulated'] = True |
---|
598 | Phase['General']['Super'] = nqi |
---|
599 | Phase['General']['SuperVec'] = SuperVec |
---|
600 | Phase['General']['SuperSg'] = SuperSg |
---|
601 | if SuperSg: |
---|
602 | Phase['General']['SSGData'] = G2spc.SSpcGroup(SGData,SuperSg)[1] |
---|
603 | Phase['General']['AtomPtrs'] = [3,1,7,9] |
---|
604 | Phase['Atoms'] = Atoms |
---|
605 | return Phase |
---|
606 | |
---|
607 | class PDF_ReaderClass(G2obj.ImportPhase): |
---|
608 | 'Routine to import Phase information from ICDD PDF Card files' |
---|
609 | def __init__(self): |
---|
610 | super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__ |
---|
611 | extensionlist=('.str',), |
---|
612 | strictExtension=True, |
---|
613 | formatName = 'ICDD .str', |
---|
614 | longFormatName = 'ICDD PDF Card (.str file) import' |
---|
615 | ) |
---|
616 | |
---|
617 | def ContentsValidator(self, filename): |
---|
618 | 'Look for a str tag in 1st line' |
---|
619 | fp = open(filename,'r') |
---|
620 | if fp.read(3) == 'str': |
---|
621 | fp.close() |
---|
622 | return True |
---|
623 | self.errors = 'File does not begin with str tag' |
---|
624 | fp.close() |
---|
625 | return False |
---|
626 | |
---|
627 | def Reader(self,filename, ParentFrame=None, **unused): |
---|
628 | 'Read phase from a ICDD .str file using :meth:`ReadPDFPhase`' |
---|
629 | fp = open(filename,'r') |
---|
630 | self.Phase = self.ReadPDFPhase(ParentFrame, fp) |
---|
631 | fp.close() |
---|
632 | return True |
---|
633 | |
---|
634 | def ReadPDFPhase(self, G2frame,fp): |
---|
635 | '''Read a phase from a ICDD .str file. |
---|
636 | ''' |
---|
637 | EightPiSq = 8.*math.pi**2 |
---|
638 | self.errors = 'Error opening file' |
---|
639 | Phase = {} |
---|
640 | Atoms = [] |
---|
641 | S = fp.readline() |
---|
642 | line = 1 |
---|
643 | SGData = None |
---|
644 | cell = [] |
---|
645 | cellkey = [] |
---|
646 | while S: |
---|
647 | if 'space_group' in S: |
---|
648 | break |
---|
649 | S = fp.readline() |
---|
650 | while S: |
---|
651 | self.errors = 'Error reading at line '+str(line) |
---|
652 | if 'phase_name' in S: |
---|
653 | Title = S.split('"')[1] |
---|
654 | elif 'Space group (HMS)' in S: |
---|
655 | SpGrp = S.split()[-1] |
---|
656 | SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) |
---|
657 | E,SGData = G2spc.SpcGroup(SpGrpNorm) |
---|
658 | # space group processing failed, try to look up name in table |
---|
659 | while E: |
---|
660 | print (G2spc.SGErrors(E)) |
---|
661 | dlg = wx.TextEntryDialog(G2frame, |
---|
662 | SpGrp[:-1]+' is invalid \nN.B.: make sure spaces separate axial fields in symbol', |
---|
663 | 'ERROR in space group symbol','',style=wx.OK) |
---|
664 | if dlg.ShowModal() == wx.ID_OK: |
---|
665 | SpGrp = dlg.GetValue() |
---|
666 | E,SGData = G2spc.SpcGroup(SpGrp) |
---|
667 | else: |
---|
668 | SGData = G2obj.P1SGData # P 1 |
---|
669 | self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' |
---|
670 | self.warnings += "Change this in phase's General tab." |
---|
671 | dlg.Destroy() |
---|
672 | G2spc.SGPrint(SGData) #silent check of space group symbol |
---|
673 | elif 'a a_' in S[:7]: |
---|
674 | data = S.split() |
---|
675 | cell.append(float(data[2])) |
---|
676 | cellkey.append(data[1]) |
---|
677 | elif 'b b_' in S[:7]: |
---|
678 | data = S.split() |
---|
679 | cell.append(float(data[2])) |
---|
680 | cellkey.append(data[1]) |
---|
681 | elif 'b =' in S[:6]: |
---|
682 | data = S.split('=') |
---|
683 | indx = cellkey.index(data[1].split(';')[0]) |
---|
684 | cell.append(cell[indx]) |
---|
685 | elif 'c c_' in S[:7]: |
---|
686 | data = S.split() |
---|
687 | cell.append(float(data[2])) |
---|
688 | elif 'c =' in S[:6]: |
---|
689 | data = S.split('=') |
---|
690 | indx = cellkey.index(data[1].split(';')[0]) |
---|
691 | cell.append(cell[indx]) |
---|
692 | elif 'al' in S[:5]: |
---|
693 | cell.append(float(S.split()[1])) |
---|
694 | elif 'be' in S[:5]: |
---|
695 | cell.append(float(S.split()[1])) |
---|
696 | elif 'ga' in S[:5]: |
---|
697 | cell.append(float(S.split()[1])) |
---|
698 | Volume = G2lat.calc_V(G2lat.cell2A(cell)) |
---|
699 | break |
---|
700 | S = fp.readline() |
---|
701 | S = fp.readline() |
---|
702 | while S: |
---|
703 | if '/*' in S[:5]: |
---|
704 | break |
---|
705 | if 'site' in S[:7]: |
---|
706 | atom = [] |
---|
707 | xyzkey = [] |
---|
708 | data = S.split() |
---|
709 | atom.append(data[1]) #name |
---|
710 | pos = data.index('occ')+1 |
---|
711 | atom.append(data[pos]) #type |
---|
712 | atom.append('') #refine |
---|
713 | for xid in ['x =','y =','z =']: |
---|
714 | if xid in S: |
---|
715 | xpos = S.index(xid)+3 |
---|
716 | xend = xpos+S[xpos:].index(';') |
---|
717 | if S[xpos:xend] in xyzkey: |
---|
718 | atom.append(atom[3+xyzkey.index(S[xpos:xend])]) |
---|
719 | else: |
---|
720 | atom.append(eval(S[xpos:xend]+'.')) |
---|
721 | else: |
---|
722 | xpos = data.index(xid[0])+2 |
---|
723 | xyzkey.append(data[xpos-1][1:]) |
---|
724 | atom.append(float(data[xpos])) |
---|
725 | atom.append(float(data[pos+2])) |
---|
726 | SytSym,Mult = G2spc.SytSym(np.array(atom[3:6]),SGData)[:2] |
---|
727 | atom.append(SytSym) |
---|
728 | atom.append(Mult) |
---|
729 | if 'beq' in S: |
---|
730 | atom.append('I') |
---|
731 | upos = data.index('beq') |
---|
732 | atom.append(float(data[upos+2])/EightPiSq) |
---|
733 | atom += [0.,0.,0.,0.,0.,0.,] |
---|
734 | elif 'ADPs' in S: |
---|
735 | upos = data.index('ADPs') |
---|
736 | atom.append('A') |
---|
737 | atom.append(0.0) |
---|
738 | for uid in ['Bani11','Bani22','Bani33','Bani12','Bani13','Bani23']: |
---|
739 | upos = data.index(uid)+1 |
---|
740 | atom.append(float(data[upos])/EightPiSq) |
---|
741 | else: |
---|
742 | atom.append('I') |
---|
743 | atom += [0.02,0.,0.,0.,0.,0.,0.,] |
---|
744 | atom.append(ran.randint(0,sys.maxsize)) |
---|
745 | Atoms.append(atom) |
---|
746 | S = fp.readline() |
---|
747 | fp.close() |
---|
748 | self.errors = 'Error after read complete' |
---|
749 | if not SGData: |
---|
750 | raise self.ImportException("No space group (spcgroup entry) found") |
---|
751 | if not cell: |
---|
752 | raise self.ImportException("No cell found") |
---|
753 | Phase = G2obj.SetNewPhase(Name=Title,SGData=SGData,cell=cell+[Volume,]) |
---|
754 | Phase['General']['Type'] = 'nuclear' |
---|
755 | Phase['General']['AtomPtrs'] = [3,1,7,9] |
---|
756 | Phase['Atoms'] = Atoms |
---|
757 | return Phase |
---|