def monoCellReduce(ibrav,A): 'needs a doc string' a,b,c,alp,bet,gam = G2lat.A2cell(A) G,g = G2lat.A2Gmat(A) if ibrav in [11]: u = [0,0,-1] v = [1,0,2] anew = math.sqrt(np.dot(np.dot(v,g),v)) if anew < a: cang = np.dot(np.dot(u,g),v)/(anew*c) beta = acosd(-abs(cang)) A = G2lat.cell2A([anew,b,c,90,beta,90]) else: u = [-1,0,0] v = [1,0,1] cnew = math.sqrt(np.dot(np.dot(v,g),v)) if cnew < c: cang = np.dot(np.dot(u,g),v)/(a*cnew) beta = acosd(-abs(cang)) A = G2lat.cell2A([a,b,cnew,90,beta,90]) return A
def monoCellReduce(ibrav,A): 'needs a doc string' a,b,c,alp,bet,gam = G2lat.A2cell(A) G,g = G2lat.A2Gmat(A) if ibrav in [13]: u = [0,0,-1] v = [1,0,2] anew = math.sqrt(np.dot(np.dot(v,g),v)) if anew < a: cang = np.dot(np.dot(u,g),v)/(anew*c) beta = acosd(-abs(cang)) A = G2lat.cell2A([anew,b,c,90,beta,90]) else: u = [-1,0,0] v = [1,0,1] cnew = math.sqrt(np.dot(np.dot(v,g),v)) if cnew < c: cang = np.dot(np.dot(u,g),v)/(a*cnew) beta = acosd(-abs(cang)) A = G2lat.cell2A([a,b,cnew,90,beta,90]) return A
def convert_lattice(self, paramList, params): ''' Convert between A and unit cell parameters. A is defined in GSAS-II as A = [G11, G22, G33, 2*G12, 2*G13, 2*G23] with G as the reciprocal metric tensor elements. ''' latparamList = [ '0::A0', '1::A0', '2::A0', '3::A0', '4::A0', '5::A0', '6::A0', '7::A0', '8::A0', '9::A0', '10::A0' ] check = any(item in paramList for item in latparamList) if check is True: return G2latt.A2cell(params) else: return G2latt.cell2A(params)
def findMV(peaks,controls,ssopt,Inst,dlg): def Val2Vec(vec,Vref,values): Vec = [] i = 0 for j,r in enumerate(Vref): if r: if values.size > 1: Vec.append(max(0.0,min(1.0,values[i]))) else: Vec.append(max(0.0,min(1.0,values))) i += 1 else: Vec.append(vec[j]) return np.array(Vec) def ZSSfunc(values,peaks,dmin,Inst,SGData,SSGData,vec,Vref,maxH,A,wave,Z,dlg=None): Vec = Val2Vec(vec,Vref,values) HKL = G2pwd.getHKLMpeak(dmin,Inst,SGData,SSGData,Vec,maxH,A) Peaks = np.array(IndexSSPeaks(peaks,HKL)[1]).T Qo = 1./Peaks[-2]**2 Qc = G2lat.calc_rDsqZSS(Peaks[4:8],A,Vec,Z,Peaks[0],wave) chi = np.sum((Qo-Qc)**2) if dlg: dlg.Pulse() return chi if 'C' in Inst['Type'][0]: wave = G2mth.getWave(Inst) else: difC = Inst['difC'][1] SGData = G2spc.SpcGroup(controls[13])[1] SSGData = G2spc.SSpcGroup(SGData,ssopt['ssSymb'])[1] A = G2lat.cell2A(controls[6:12]) Z = controls[1] Vref = [True if x in ssopt['ssSymb'] else False for x in ['a','b','g']] values = [] ranges = [] for v,r in zip(ssopt['ModVec'],Vref): if r: ranges += [slice(.02,.98,.05),] values += [v,] dmin = getDmin(peaks)-0.005 Peaks = np.copy(np.array(peaks).T) result = so.brute(ZSSfunc,ranges,finish=so.fmin_cg, args=(peaks,dmin,Inst,SGData,SSGData,ssopt['ModVec'],Vref,ssopt['maxH'],A,wave,Z,dlg)) return Val2Vec(ssopt['ModVec'],Vref,result)
def test0(): if NeedTestData: TestData() ibrav, cell, bestcell, Pwr, peaks = TestData print('best cell:', bestcell) print('old cell:', cell) Peaks = np.array(peaks) HKL = Peaks[4:7] print(calc_M20(peaks, HKL)) A = G2lat.cell2A(cell) OK, smin, A, result = FitHKL(ibrav, peaks, A, Pwr) print('new cell:', G2lat.A2cell(A)) print('x:', result[0]) print('cov_x:', result[1]) print('infodict:') for item in result[2]: print(item, result[2][item]) print('msg:', result[3]) print('ier:', result[4]) result = refinePeaks(peaks, ibrav, A) N, M20, X20, A = result print('refinePeaks:', N, M20, X20, G2lat.A2cell(A)) print('compare bestcell:', bestcell)
def SHPenalty(POData): def OnHKLList(event): dlg = G2G.G2MultiChoiceDialog(G2frame, 'Select penalty hkls', 'Penalty hkls',hkls,filterBox=False) try: if dlg.ShowModal() == wx.ID_OK: POData[6] = [hkls[i] for i in dlg.GetSelections()] if not POData[6]: POData[6] = ['',] else: return finally: dlg.Destroy() wx.CallLater(100,RepaintHistogramInfo) def OnshToler(event): try: value = float(shToler.GetValue()) POData[7] = value except ValueError: pass shToler.SetValue('%.2f'%(POData[7])) A = G2lat.cell2A(generalData['Cell'][1:7]) hkls = G2lat.GenPfHKLs(10,SGData,A) shPenalty = wx.BoxSizer(wx.HORIZONTAL) shPenalty.Add(wx.StaticText(DData,wx.ID_ANY,' Negative MRD penalty list: '),0,WACV) shPenalty.Add(wx.ComboBox(DData,value=POData[6][0],choices=POData[6], style=wx.CB_DROPDOWN),0,WACV) hklList = wx.Button(DData,label='Select penalty hkls') hklList.Bind(wx.EVT_BUTTON,OnHKLList) shPenalty.Add(hklList,0,WACV) shPenalty.Add(wx.StaticText(DData,wx.ID_ANY,' Zero MRD tolerance: '),0,WACV) shToler = wx.TextCtrl(DData,wx.ID_ANY,'%.2f'%(POData[7]),style=wx.TE_PROCESS_ENTER) shToler.Bind(wx.EVT_TEXT_ENTER,OnshToler) shToler.Bind(wx.EVT_KILL_FOCUS,OnshToler) shPenalty.Add(shToler,0,WACV) return shPenalty
def test0(): if NeedTestData: TestData() msg = 'test FitHKL' ibrav, cell, bestcell, Pwr, peaks = TestData print 'best cell:', bestcell print 'old cell:', cell Peaks = np.array(peaks) HKL = Peaks[4:7] print calc_M20(peaks, HKL) A = G2lat.cell2A(cell) OK, smin, A, result = FitHKL(ibrav, peaks, A, Pwr) print 'new cell:', G2lat.A2cell(A) print 'x:', result[0] print 'cov_x:', result[1] print 'infodict:' for item in result[2]: print item, result[2][item] print 'msg:', result[3] print 'ier:', result[4] result = refinePeaks(peaks, ibrav, A) N, M20, X20, A = result print 'refinePeaks:', N, M20, X20, G2lat.A2cell(A) print 'compare bestcell:', bestcell
def test0(): if NeedTestData: TestData() msg = 'test FitHKL' ibrav,cell,bestcell,Pwr,peaks = TestData print 'best cell:',bestcell print 'old cell:',cell Peaks = np.array(peaks) HKL = Peaks[4:7] print calc_M20(peaks,HKL) A = G2lat.cell2A(cell) OK,smin,A,result = FitHKL(ibrav,peaks,A,Pwr) print 'new cell:',G2lat.A2cell(A) print 'x:',result[0] print 'cov_x:',result[1] print 'infodict:' for item in result[2]: print item,result[2][item] print 'msg:',result[3] print 'ier:',result[4] result = refinePeaks(peaks,ibrav,A) N,M20,X20,A = result print 'refinePeaks:',N,M20,X20,G2lat.A2cell(A) print 'compare bestcell:',bestcell
def ReadPDFPhase(self, G2frame, fp): '''Read a phase from a ICDD .str file. ''' EightPiSq = 8. * math.pi**2 self.errors = 'Error opening file' Phase = {} Atoms = [] S = fp.readline() line = 1 SGData = None cell = [] cellkey = [] while S: if 'space_group' in S: break S = fp.readline() while S: self.errors = 'Error reading at line ' + str(line) if 'phase_name' in S: Title = S.split('"')[1] elif 'Space group (HMS)' in S: SpGrp = S.split()[-1] SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) E, SGData = G2spc.SpcGroup(SpGrpNorm) # space group processing failed, try to look up name in table while E: print(G2spc.SGErrors(E)) dlg = wx.TextEntryDialog( G2frame, SpGrp[:-1] + ' is invalid \nN.B.: make sure spaces separate axial fields in symbol', 'ERROR in space group symbol', '', style=wx.OK) if dlg.ShowModal() == wx.ID_OK: SpGrp = dlg.GetValue() E, SGData = G2spc.SpcGroup(SpGrp) else: SGData = G2obj.P1SGData # P 1 self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' self.warnings += "Change this in phase's General tab." dlg.Destroy() G2spc.SGPrint(SGData) #silent check of space group symbol elif 'a a_' in S[:7]: data = S.split() cell.append(float(data[2])) cellkey.append(data[1]) elif 'b b_' in S[:7]: data = S.split() cell.append(float(data[2])) cellkey.append(data[1]) elif 'b =' in S[:6]: data = S.split('=') indx = cellkey.index(data[1].split(';')[0]) cell.append(cell[indx]) elif 'c c_' in S[:7]: data = S.split() cell.append(float(data[2])) elif 'c =' in S[:6]: data = S.split('=') indx = cellkey.index(data[1].split(';')[0]) cell.append(cell[indx]) elif 'al' in S[:5]: cell.append(float(S.split()[1])) elif 'be' in S[:5]: cell.append(float(S.split()[1])) elif 'ga' in S[:5]: cell.append(float(S.split()[1])) Volume = G2lat.calc_V(G2lat.cell2A(cell)) break S = fp.readline() S = fp.readline() while S: if '/*' in S[:5]: break if 'site' in S[:7]: atom = [] xyzkey = [] data = S.split() atom.append(data[1]) #name pos = data.index('occ') + 1 atom.append(data[pos]) #type atom.append('') #refine for xid in ['x =', 'y =', 'z =']: if xid in S: xpos = S.index(xid) + 3 xend = xpos + S[xpos:].index(';') if S[xpos:xend] in xyzkey: atom.append(atom[3 + xyzkey.index(S[xpos:xend])]) else: atom.append(eval(S[xpos:xend] + '.')) else: xpos = data.index(xid[0]) + 2 xyzkey.append(data[xpos - 1][1:]) atom.append(float(data[xpos])) atom.append(float(data[pos + 2])) SytSym, Mult = G2spc.SytSym(np.array(atom[3:6]), SGData)[:2] atom.append(SytSym) atom.append(Mult) if 'beq' in S: atom.append('I') upos = data.index('beq') atom.append(float(data[upos + 2]) / EightPiSq) atom += [ 0., 0., 0., 0., 0., 0., ] elif 'ADPs' in S: upos = data.index('ADPs') atom.append('A') atom.append(0.0) for uid in [ 'Bani11', 'Bani22', 'Bani33', 'Bani12', 'Bani13', 'Bani23' ]: upos = data.index(uid) + 1 atom.append(float(data[upos]) / EightPiSq) else: atom.append('I') atom += [ 0.02, 0., 0., 0., 0., 0., 0., ] atom.append(ran.randint(0, sys.maxsize)) Atoms.append(atom) S = fp.readline() fp.close() self.errors = 'Error after read complete' if not SGData: raise self.ImportException("No space group (spcgroup entry) found") if not cell: raise self.ImportException("No cell found") Phase = G2obj.SetNewPhase(Name=Title, SGData=SGData, cell=cell + [ Volume, ]) Phase['General']['Type'] = 'nuclear' Phase['General']['AtomPtrs'] = [3, 1, 7, 9] Phase['Atoms'] = Atoms return Phase
def ReadEXPPhase(self, G2frame, filepointer): '''Read a phase from a GSAS .EXP file. ''' shModels = ['cylindrical', 'none', 'shear - 2/m', 'rolling - mmm'] textureData = { 'Order': 0, 'Model': 'cylindrical', 'Sample omega': [False, 0.0], 'Sample chi': [False, 0.0], 'Sample phi': [False, 0.0], 'SH Coeff': [False, {}], 'SHShow': False, 'PFhkl': [0, 0, 1], 'PFxyz': [0, 0, 1], 'PlotType': 'Pole figure' } shNcof = 0 S = 1 NPhas = [] Expr = [{}, {}, {}, {}, {}, {}, {}, {}, {}] # GSAS can have at most 9 phases for line, S in enumerate(filepointer): self.errors = 'Error reading at line ' + str(line + 1) if 'EXPR NPHAS' in S[:12]: NPhas = S[12:-1].split() if 'CRS' in S[:3]: N = int(S[3:4]) - 1 Expr[N][S[:12]] = S[12:-1] PNames = [] if not NPhas: raise self.ImportException("No EXPR NPHAS record found") self.errors = 'Error interpreting file' for n, N in enumerate(NPhas): if N != '0': result = n key = 'CRS' + str(n + 1) + ' PNAM' PNames.append(Expr[n][key]) if len(PNames) == 0: raise self.ImportException("No phases found") elif len(PNames) > 1: dlg = wx.SingleChoiceDialog(G2frame, 'Which phase to read?', 'Read phase data', PNames, wx.CHOICEDLG_STYLE) try: if dlg.ShowModal() == wx.ID_OK: result = dlg.GetSelection( ) # I think this breaks is there are skipped phases. Cant this happen? finally: dlg.Destroy() EXPphase = Expr[result] keyList = list(EXPphase.keys()) keyList.sort() SGData = {} MPtype = '' if NPhas[result] == '1': Ptype = 'nuclear' elif NPhas[result] == '2': Ptype = 'nuclear' MPtype = 'magnetic' MagDmin = 1.0 elif NPhas[result] == '3': Ptype = 'magnetic' MagDmin = 1.0 elif NPhas[result] == '4': Ptype = 'macromolecular' elif NPhas[result] == '10': Ptype = 'Pawley' else: raise self.ImportException("Phase type not recognized") for key in keyList: if 'PNAM' in key: PhaseName = EXPphase[key].strip() elif 'ABC ' in key: abc = [ float(EXPphase[key][:10]), float(EXPphase[key][10:20]), float(EXPphase[key][20:30]) ] elif 'ANGLES' in key: angles = [ float(EXPphase[key][:10]), float(EXPphase[key][10:20]), float(EXPphase[key][20:30]) ] elif 'SG SYM' in key: SpGrp = EXPphase[key][:15].strip() E, SGData = G2spc.SpcGroup(SpGrp) if E: SGData = G2obj.P1SGData # P 1 -- unlikely to need this! self.warnings += '\nThe GSAS space group was not interpreted(!) and has been set to "P 1".' self.warnings += "Change this in phase's General tab." elif 'SPNFLP' in key: SpnFlp = np.array( [int(float(s)) for s in EXPphase[key].split()]) SpnFlp = np.where(SpnFlp == 0, 1, SpnFlp) SpnFlp = [ 1, ] + list(SpnFlp) if SGData['SpGrp'][0] in ['A', 'B', 'C', 'I', 'R', 'F']: SpnFlp = list(SpnFlp) + [1, 1, 1, 1] elif 'MXDSTR' in key: MagDmin = float(EXPphase[key][:10]) elif 'OD ' in key: SHdata = EXPphase[key].split() # may not have all 9 values SHvals = 9 * [0] for i in range(9): try: float(SHdata[i]) SHvals[i] = SHdata[i] except: pass textureData['Order'] = int(SHvals[0]) textureData['Model'] = shModels[int(SHvals[2])] textureData['Sample omega'] = [False, float(SHvals[6])] textureData['Sample chi'] = [False, float(SHvals[7])] textureData['Sample phi'] = [False, float(SHvals[8])] shNcof = int(SHvals[1]) Volume = G2lat.calc_V(G2lat.cell2A(abc + angles)) Atoms = [] MAtoms = [] Bmat = G2lat.cell2AB(abc + angles)[1] if Ptype == 'macromolecular': for key in keyList: if 'AT' in key[6:8]: S = EXPphase[key] Atom = [ S[56:60].strip(), S[50:54].strip().upper(), S[54:56], S[46:51].strip(), S[:8].strip().capitalize(), '', float(S[16:24]), float(S[24:32]), float(S[32:40]), float(S[8:16]), '1', 1, 'I', float(S[40:46]), 0, 0, 0, 0, 0, 0 ] XYZ = Atom[6:9] Atom[10], Atom[11] = G2spc.SytSym(XYZ, SGData)[:2] Atom.append(ran.randint(0, sys.maxsize)) Atoms.append(Atom) else: for key in keyList: if 'AT' in key: if key[11:] == 'A': S = EXPphase[key] elif key[11:] == 'B': S1 = EXPphase[key] Atom = [ S[50:58].strip(), S[:10].strip().capitalize(), '', float(S[10:20]), float(S[20:30]), float(S[30:40]), float(S[40:50]), '', int(S[60:62]), S1[62:63] ] #float(S[40:50]),'',int(S[60:62]),S1[130:131]] if Atom[9] == 'I': Atom += [float(S1[0:10]), 0., 0., 0., 0., 0., 0.] elif Atom[9] == 'A': Atom += [ 0.0, float(S1[0:10]), float(S1[10:20]), float(S1[20:30]), float(S1[30:40]), float(S1[40:50]), float(S1[50:60]) ] else: print('Error in line with key: ' + key) Atom += [0., 0., 0., 0., 0., 0., 0.] XYZ = Atom[3:6] Atom[7], Atom[8] = G2spc.SytSym(XYZ, SGData)[:2] Atom.append(ran.randint(0, sys.maxsize)) Atoms.append(Atom) elif key[11:] == 'M' and key[6:8] == 'AT': S = EXPphase[key] mom = np.array( [float(S[:10]), float(S[10:20]), float(S[20:30])]) mag = np.sqrt(np.sum(mom**2)) mom = np.inner(Bmat, mom) * mag MAtoms.append(Atom) MAtoms[-1] = Atom[:7] + list(mom) + Atom[7:] if shNcof: shCoef = {} nRec = [i + 1 for i in range((shNcof - 1) // 6 + 1)] for irec in nRec: ODkey = keyList[0][:6] + 'OD' + '%3dA' % (irec) indx = EXPphase[ODkey].split() ODkey = ODkey[:-1] + 'B' vals = EXPphase[ODkey].split() for i, val in enumerate(vals): key = 'C(%s,%s,%s)' % (indx[3 * i], indx[3 * i + 1], indx[3 * i + 2]) shCoef[key] = float(val) textureData['SH Coeff'] = [False, shCoef] if not SGData: raise self.ImportException("No space group found in phase") if not abc: raise self.ImportException("No cell lengths found in phase") if not angles: raise self.ImportException("No cell angles found in phase") if not Atoms: raise self.ImportException("No atoms found in phase") self.Phase['General'].update({ 'Type': Ptype, 'Name': PhaseName, 'Cell': [ False, ] + abc + angles + [ Volume, ], 'SGData': SGData }) if MPtype == 'magnetic': self.MPhase['General'].update({ 'Type': 'magnetic', 'Name': PhaseName + ' mag', 'Cell': [ False, ] + abc + angles + [ Volume, ], 'SGData': SGData }) else: self.MPhase = None if Ptype == 'macromolecular': self.Phase['General']['AtomPtrs'] = [6, 4, 10, 12] self.Phase['Atoms'] = Atoms elif Ptype == 'magnetic': self.Phase['General']['AtomPtrs'] = [3, 1, 10, 12] self.Phase['General']['SGData']['SGSpin'] = SpnFlp self.Phase['General']['MagDmin'] = MagDmin self.Phase['Atoms'] = MAtoms else: #nuclear self.Phase['General']['AtomPtrs'] = [3, 1, 7, 9] self.Phase['General']['SH Texture'] = textureData self.Phase['Atoms'] = Atoms if MPtype == 'magnetic': self.MPhase['General']['AtomPtrs'] = [3, 1, 10, 12] self.MPhase['General']['SGData']['SGSpin'] = SpnFlp self.MPhase['General']['MagDmin'] = MagDmin self.MPhase['Atoms'] = MAtoms
def ReadPDBPhase(self,filename,parent=None): '''Read a phase from a PDB file. ''' EightPiSq = 8.*math.pi**2 self.errors = 'Error opening file' file = open(filename, 'Ur') Phase = {} Title = '' Compnd = '' Atoms = [] A = np.zeros(shape=(3,3)) S = file.readline() line = 1 SGData = None cell = None while S: self.errors = 'Error reading at line '+str(line) Atom = [] if 'TITLE' in S[:5]: Title = S[10:72].strip() elif 'COMPND ' in S[:10]: Compnd = S[10:72].strip() elif 'CRYST' in S[:5]: abc = S[7:34].split() angles = S[34:55].split() cell=[float(abc[0]),float(abc[1]),float(abc[2]), float(angles[0]),float(angles[1]),float(angles[2])] Volume = G2lat.calc_V(G2lat.cell2A(cell)) AA,AB = G2lat.cell2AB(cell) SpGrp = S[55:65] E,SGData = G2spc.SpcGroup(SpGrp) # space group processing failed, try to look up name in table if E: SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) if SpGrpNorm: E,SGData = G2spc.SpcGroup(SpGrpNorm) while E: print G2spc.SGErrors(E) dlg = wx.TextEntryDialog(parent, SpGrp[:-1]+' is invalid \nN.B.: make sure spaces separate axial fields in symbol', 'ERROR in space group symbol','',style=wx.OK) if dlg.ShowModal() == wx.ID_OK: SpGrp = dlg.GetValue() E,SGData = G2spc.SpcGroup(SpGrp) else: SGData = G2IO.SGData # P 1 self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' self.warnings += "Change this in phase's General tab." dlg.Destroy() SGlines = G2spc.SGPrint(SGData) for l in SGlines: print l elif 'SCALE' in S[:5]: V = S[10:41].split() A[int(S[5])-1] = [float(V[0]),float(V[1]),float(V[2])] elif 'ATOM' in S[:4] or 'HETATM' in S[:6]: if not SGData: self.warnings += '\nThe space group was not read before atoms and has been set to "P 1". ' self.warnings += "Change this in phase's General tab." SGData = G2IO.SGData # P 1 XYZ = [float(S[31:39]),float(S[39:47]),float(S[47:55])] XYZ = np.inner(AB,XYZ) XYZ = np.where(abs(XYZ)<0.00001,0,XYZ) SytSym,Mult = G2spc.SytSym(XYZ,SGData) Uiso = float(S[61:67])/EightPiSq Type = S[12:14].lower() if Type[0] in '123456789': Type = Type[1:] Atom = [S[22:27].strip(),S[17:20].upper(),S[20:22], S[12:17].strip(),Type.strip().capitalize(),'',XYZ[0],XYZ[1],XYZ[2], float(S[55:61]),SytSym,Mult,'I',Uiso,0,0,0,0,0,0] S = file.readline() line += 1 if 'ANISOU' in S[:6]: Uij = S[30:72].split() Uij = [float(Uij[0])/10000.,float(Uij[1])/10000.,float(Uij[2])/10000., float(Uij[3])/10000.,float(Uij[4])/10000.,float(Uij[5])/10000.] Atom = Atom[:14]+Uij Atom[12] = 'A' Atom.append(ran.randint(0,sys.maxint)) Atoms.append(Atom) S = file.readline() line += 1 file.close() self.errors = 'Error after read complete' if Title: PhaseName = Title elif Compnd: PhaseName = Compnd else: PhaseName = 'None' if not SGData: raise self.ImportException("No space group (CRYST entry) found") if not cell: raise self.ImportException("No cell (CRYST entry) found") Phase = G2IO.SetNewPhase(Name=PhaseName,SGData=SGData,cell=cell+[Volume,]) Phase['General']['Type'] = 'macromolecular' Phase['General']['AtomPtrs'] = [6,4,10,12] Phase['Atoms'] = Atoms return Phase
def ReadJANAPhase(self,filename,parent=None): '''Read a phase from a JANA2006 m50 & m40 files. ''' self.errors = 'Error opening file' file = open(filename, 'Ur') #contains only cell & spcgroup Phase = {} Title = os.path.basename(filename) Compnd = '' Type = 'nuclear' Atoms = [] Atypes = [] SuperVec = [[0,0,.1],False,4] S = file.readline() line = 1 SGData = None SuperSg = '' cell = None nqi = 0 while S: self.errors = 'Error reading at line '+str(line) if 'title' in S and S != 'title\n': Title = S.split()[1] elif 'cell' in S[:4]: cell = S[5:].split() cell=[float(cell[0]),float(cell[1]),float(cell[2]), float(cell[3]),float(cell[4]),float(cell[5])] Volume = G2lat.calc_V(G2lat.cell2A(cell)) elif 'spgroup' in S: if 'X' in S: raise self.ImportException("Supersymmetry "+S+" too high; GSAS-II limited to (3+1) supersymmetry") SpGrp = S.split()[1] SuperSg = '' if '(' in SpGrp: #supercell symmetry - split in 2 SuperStr = SpGrp.split('(') SpGrp = SuperStr[0] SuperSg = '('+SuperStr[1] SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) E,SGData = G2spc.SpcGroup(SpGrpNorm) # space group processing failed, try to look up name in table while E: print G2spc.SGErrors(E) dlg = wx.TextEntryDialog(parent, SpGrp[:-1]+' is invalid \nN.B.: make sure spaces separate axial fields in symbol', 'ERROR in space group symbol','',style=wx.OK) if dlg.ShowModal() == wx.ID_OK: SpGrp = dlg.GetValue() E,SGData = G2spc.SpcGroup(SpGrp) else: SGData = G2IO.SGData # P 1 self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' self.warnings += "Change this in phase's General tab." dlg.Destroy() SGlines = G2spc.SGPrint(SGData) elif 'qi' in S[:2]: if nqi: raise self.ImportException("Supersymmetry too high; GSAS-II limited to (3+1) supersymmetry") Type = 'modulated' vec = S.split()[1:] SuperVec = [[float(vec[i]) for i in range(3)],False,4] nqi += 1 elif 'atom' in S[:4]: Atypes.append(S.split()[1]) S = file.readline() line += 1 file.close() #read atoms from m40 file if not SGData: self.warnings += '\nThe space group was not read before atoms and has been set to "P 1". ' self.warnings += "Change this in phase's General tab." SGData = G2IO.SGData # P 1 waveTypes = ['Fourier','Sawtooth','ZigZag',] filename2 = os.path.splitext(filename)[0]+'.m40' file2 = open(filename2,'Ur') S = file2.readline() line = 1 self.errors = 'Error reading at line '+str(line) nAtoms = int(S.split()[0]) for i in range(4): S = file2.readline() for i in range(nAtoms): S1 = file2.readline() S1N = S1.split()[-3:] # no. occ, no. pos waves, no. ADP waves S1N = [int(i) for i in S1N] S1T = list(S1[60:63]) waveType = waveTypes[int(S1T[1])] crenelType = '' Spos = [] Sadp = [] Sfrac = [] Smag = [] XYZ = [float(S1[27:36]),float(S1[36:45]),float(S1[45:54])] SytSym,Mult = G2spc.SytSym(XYZ,SGData) aType = Atypes[int(S1[9:11])-1] Name = S1[:8].strip() if S1[11:15].strip() == '1': S2 = file2.readline() Uiso = float(S2[:9]) Uij = [0,0,0,0,0,0] IA = 'I' elif S1[11:15].strip() == '2': S2 = file2.readline() IA = 'A' Uiso = 0. Uij = [float(S2[:9]),float(S2[9:18]),float(S2[18:27]), float(S2[27:36]),float(S2[36:45]),float(S2[45:54])] for i in range(S1N[0]): if not i: FS = file2.readline() Sfrac.append(FS[:9]) #'O' or 'delta' = 'length' for crenel if int(S1T[0]): #"", "Legendre" or "Xharm" in 18:27 for "crenel"! waveType = 'Crenel/Fourier' #all waves 'Fourier' no other choice crenelType = FS[18:27] Sfrac.append(file2.readline()[:18]) #if not crenel = Osin & Ocos # else Osin & Ocos except last one is X40 = 'Center' for i in range(S1N[1]): Spos.append(file2.readline()[:54]) for i in range(S1N[2]): Sadp.append(file2.readline()[:54]+file2.readline()) if sum(S1N): #if any waves: skip mystery line? file2.readline() for i,it in enumerate(Sfrac): print i,it if not i: if 'Crenel' in waveType: vals = [float(it),float(Sfrac[-1][:9])] else: vals = [float(it),] else: vals = [float(it[:9]),float(it[9:18])] if 'Crenel' in waveType and i == len(Sfrac)-1: del Sfrac[-1] break Sfrac[i] = [vals,False] print Sfrac[i] for i,it in enumerate(Spos): if waveType in ['ZigZag','Sawtooth'] and not i: vals = [float(it[:9]),float(it[9:18]),float(it[18:27]),float(it[27:36])] else: vals = [float(it[:9]),float(it[9:18]),float(it[18:27]),float(it[27:36]),float(it[36:45]),float(it[45:54])] Spos[i] = [vals,False] for i,it in enumerate(Sadp): vals = [float(it[:9]),float(it[9:18]),float(it[18:27]),float(it[27:36]),float(it[36:45]),float(it[45:54]), float(it[54:63]),float(it[63:72]),float(it[72:81]),float(it[81:90]),float(it[90:99]),float(it[99:108])] Sadp[i] = [vals,False] Atom = [Name,aType,'',XYZ[0],XYZ[1],XYZ[2],1.0,SytSym,Mult,IA,Uiso] Atom += Uij Atom.append(ran.randint(0,sys.maxint)) Atom.append([]) Atom.append([]) Atom.append({'SS1':{'waveType':waveType,'crenelType':crenelType,'Sfrac':Sfrac,'Spos':Spos,'Sadp':Sadp,'Smag':Smag}}) #SS2 is for (3+2), etc. Atoms.append(Atom) file2.close() self.errors = 'Error after read complete' if not SGData: raise self.ImportException("No space group (spcgroup entry) found") if not cell: raise self.ImportException("No cell found") Phase = G2IO.SetNewPhase(Name=Title,SGData=SGData,cell=cell+[Volume,]) Phase['General']['Type'] = Type Phase['General']['Super'] = nqi Phase['General']['SuperVec'] = SuperVec Phase['General']['SuperSg'] = SuperSg if SuperSg: Phase['General']['SSGData'] = G2spc.SSpcGroup(SGData,SuperSg)[1] Phase['General']['AtomPtrs'] = [3,1,7,9] Phase['Atoms'] = Atoms return Phase
def PrintDistAngle(DisAglCtls,DisAglData,out=sys.stdout): '''Print distances and angles :param dict DisAglCtls: contains distance/angle radii usually defined using :func:`GSASIIctrlGUI.DisAglDialog` :param dict DisAglData: contains phase data: Items 'OrigAtoms' and 'TargAtoms' contain the atoms to be used for distance/angle origins and atoms to be used as targets. Item 'SGData' has the space group information (see :ref:`Space Group object<SGData_table>`) :param file out: file object for output. Defaults to sys.stdout. ''' def MyPrint(s): out.write(s+'\n') # print(s,file=out) # use in Python 3 def ShowBanner(name): MyPrint(80*'*') MyPrint(' Interatomic Distances and Angles for phase '+name) MyPrint((80*'*')+'\n') ShowBanner(DisAglCtls['Name']) SGData = DisAglData['SGData'] SGtext,SGtable = G2spc.SGPrint(SGData) for line in SGtext: MyPrint(line) if len(SGtable) > 1: for i,item in enumerate(SGtable[::2]): if 2*i+1 == len(SGtable): line = ' %s'%(item.ljust(30)) else: line = ' %s %s'%(item.ljust(30),SGtable[2*i+1].ljust(30)) MyPrint(line) else: MyPrint(' ( 1) %s'%(SGtable[0])) #triclinic case Cell = DisAglData['Cell'] Amat,Bmat = G2lat.cell2AB(Cell[:6]) covData = {} if 'covData' in DisAglData: covData = DisAglData['covData'] pfx = str(DisAglData['pId'])+'::' A = G2lat.cell2A(Cell[:6]) cellSig = G2stIO.getCellEsd(pfx,SGData,A,covData) names = [' a = ',' b = ',' c = ',' alpha = ',' beta = ',' gamma = ',' Volume = '] valEsd = [G2mth.ValEsd(Cell[i],cellSig[i],True) for i in range(7)] line = '\n Unit cell:' for name,vals in zip(names,valEsd): line += name+vals MyPrint(line) else: MyPrint('\n Unit cell: a = '+('%.5f'%Cell[0])+' b = '+('%.5f'%Cell[1])+' c = '+('%.5f'%Cell[2])+ ' alpha = '+('%.3f'%Cell[3])+' beta = '+('%.3f'%Cell[4])+' gamma = '+ ('%.3f'%Cell[5])+' Volume = '+('%.3f'%Cell[6])) AtomLabels,DistArray,AngArray = RetDistAngle(DisAglCtls,DisAglData) origAtoms = DisAglData['OrigAtoms'] for Oatom in origAtoms: i = Oatom[0] Dist = DistArray[i] nDist = len(Dist) angles = np.zeros((nDist,nDist)) angsig = np.zeros((nDist,nDist)) for k,j,tup in AngArray[i]: angles[k][j],angsig[k][j] = angles[j][k],angsig[j][k] = tup line = '' for i,x in enumerate(Oatom[3:6]): line += ('%12.5f'%x).rstrip('0') MyPrint('\n Distances & angles for '+Oatom[1]+' at '+line.rstrip()) MyPrint(80*'*') line = '' for dist in Dist[:-1]: line += '%12s'%(AtomLabels[dist[0]].center(12)) MyPrint(' To cell +(sym. op.) dist. '+line.rstrip()) for i,dist in enumerate(Dist): line = '' for j,angle in enumerate(angles[i][0:i]): sig = angsig[i][j] if angle: if sig: line += '%12s'%(G2mth.ValEsd(angle,sig,True).center(12)) else: val = '%.3f'%(angle) line += '%12s'%(val.center(12)) else: line += 12*' ' if dist[4]: #sig exists! val = G2mth.ValEsd(dist[3],dist[4]) else: val = '%8.4f'%(dist[3]) tunit = '[%2d%2d%2d]'% dist[1] MyPrint((' %8s%10s+(%4d) %12s'%(AtomLabels[dist[0]].ljust(8),tunit.ljust(10),dist[2],val.center(12)))+line.rstrip())
def ReadJANAPhase(self, filename, parent=None): '''Read a phase from a JANA2006 m50 & m40 files. ''' self.errors = 'Error opening file' file = open(filename, 'Ur') #contains only cell & spcgroup Phase = {} Title = os.path.basename(filename) Compnd = '' Type = 'nuclear' Atoms = [] Atypes = [] SuperVec = [[0, 0, .1], False, 4] S = file.readline() line = 1 SGData = None SuperSg = '' cell = None nqi = 0 while S: self.errors = 'Error reading at line ' + str(line) if 'title' in S and S != 'title\n': Title = S.split()[1] elif 'cell' in S[:4]: cell = S[5:].split() cell = [ float(cell[0]), float(cell[1]), float(cell[2]), float(cell[3]), float(cell[4]), float(cell[5]) ] Volume = G2lat.calc_V(G2lat.cell2A(cell)) elif 'spgroup' in S: if 'X' in S: raise self.ImportException( "Supersymmetry " + S + " too high; GSAS-II limited to (3+1) supersymmetry") SpGrp = S.split()[1] SuperSg = '' if '(' in SpGrp: #supercell symmetry - split in 2 SuperStr = SpGrp.split('(') SpGrp = SuperStr[0] SuperSg = '(' + SuperStr[1] SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) E, SGData = G2spc.SpcGroup(SpGrpNorm) # space group processing failed, try to look up name in table while E: print G2spc.SGErrors(E) dlg = wx.TextEntryDialog( parent, SpGrp[:-1] + ' is invalid \nN.B.: make sure spaces separate axial fields in symbol', 'ERROR in space group symbol', '', style=wx.OK) if dlg.ShowModal() == wx.ID_OK: SpGrp = dlg.GetValue() E, SGData = G2spc.SpcGroup(SpGrp) else: SGData = G2IO.SGData # P 1 self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' self.warnings += "Change this in phase's General tab." dlg.Destroy() SGlines = G2spc.SGPrint(SGData) elif 'qi' in S[:2]: if nqi: raise self.ImportException( "Supersymmetry too high; GSAS-II limited to (3+1) supersymmetry" ) Type = 'modulated' vec = S.split()[1:] SuperVec = [[float(vec[i]) for i in range(3)], False, 4] nqi += 1 elif 'atom' in S[:4]: Atypes.append(S.split()[1]) S = file.readline() line += 1 file.close() #read atoms from m40 file if not SGData: self.warnings += '\nThe space group was not read before atoms and has been set to "P 1". ' self.warnings += "Change this in phase's General tab." SGData = G2IO.SGData # P 1 waveTypes = [ 'Fourier', 'Sawtooth', 'ZigZag', ] filename2 = os.path.splitext(filename)[0] + '.m40' file2 = open(filename2, 'Ur') S = file2.readline() line = 1 self.errors = 'Error reading at line ' + str(line) nAtoms = int(S.split()[0]) for i in range(4): S = file2.readline() for i in range(nAtoms): S1 = file2.readline() S1N = S1.split()[-3:] # no. occ, no. pos waves, no. ADP waves S1N = [int(i) for i in S1N] S1T = list(S1[60:63]) waveType = waveTypes[int(S1T[1])] crenelType = '' Spos = [] Sadp = [] Sfrac = [] Smag = [] XYZ = [float(S1[27:36]), float(S1[36:45]), float(S1[45:54])] SytSym, Mult = G2spc.SytSym(XYZ, SGData) aType = Atypes[int(S1[9:11]) - 1] Name = S1[:8].strip() if S1[11:15].strip() == '1': S2 = file2.readline() Uiso = float(S2[:9]) Uij = [0, 0, 0, 0, 0, 0] IA = 'I' elif S1[11:15].strip() == '2': S2 = file2.readline() IA = 'A' Uiso = 0. Uij = [ float(S2[:9]), float(S2[9:18]), float(S2[18:27]), float(S2[27:36]), float(S2[36:45]), float(S2[45:54]) ] for i in range(S1N[0]): if not i: FS = file2.readline() Sfrac.append(FS[:9]) #'O' or 'delta' = 'length' for crenel if int( S1T[0] ): #"", "Legendre" or "Xharm" in 18:27 for "crenel"! waveType = 'Crenel/Fourier' #all waves 'Fourier' no other choice crenelType = FS[18:27] Sfrac.append( file2.readline()[:18]) #if not crenel = Osin & Ocos # else Osin & Ocos except last one is X40 = 'Center' for i in range(S1N[1]): Spos.append(file2.readline()[:54]) for i in range(S1N[2]): Sadp.append(file2.readline()[:54] + file2.readline()) if sum(S1N): #if any waves: skip mystery line? file2.readline() for i, it in enumerate(Sfrac): print i, it if not i: if 'Crenel' in waveType: vals = [float(it), float(Sfrac[-1][:9])] else: vals = [ float(it), ] else: vals = [float(it[:9]), float(it[9:18])] if 'Crenel' in waveType and i == len(Sfrac) - 1: del Sfrac[-1] break Sfrac[i] = [vals, False] print Sfrac[i] for i, it in enumerate(Spos): if waveType in ['ZigZag', 'Sawtooth'] and not i: vals = [ float(it[:9]), float(it[9:18]), float(it[18:27]), float(it[27:36]) ] else: vals = [ float(it[:9]), float(it[9:18]), float(it[18:27]), float(it[27:36]), float(it[36:45]), float(it[45:54]) ] Spos[i] = [vals, False] for i, it in enumerate(Sadp): vals = [ float(it[:9]), float(it[9:18]), float(it[18:27]), float(it[27:36]), float(it[36:45]), float(it[45:54]), float(it[54:63]), float(it[63:72]), float(it[72:81]), float(it[81:90]), float(it[90:99]), float(it[99:108]) ] Sadp[i] = [vals, False] Atom = [ Name, aType, '', XYZ[0], XYZ[1], XYZ[2], 1.0, SytSym, Mult, IA, Uiso ] Atom += Uij Atom.append(ran.randint(0, sys.maxint)) Atom.append([]) Atom.append([]) Atom.append({ 'SS1': { 'waveType': waveType, 'crenelType': crenelType, 'Sfrac': Sfrac, 'Spos': Spos, 'Sadp': Sadp, 'Smag': Smag } }) #SS2 is for (3+2), etc. Atoms.append(Atom) file2.close() self.errors = 'Error after read complete' if not SGData: raise self.ImportException("No space group (spcgroup entry) found") if not cell: raise self.ImportException("No cell found") Phase = G2IO.SetNewPhase(Name=Title, SGData=SGData, cell=cell + [ Volume, ]) Phase['General']['Type'] = Type Phase['General']['Super'] = nqi Phase['General']['SuperVec'] = SuperVec Phase['General']['SuperSg'] = SuperSg if SuperSg: Phase['General']['SSGData'] = G2spc.SSpcGroup(SGData, SuperSg)[1] Phase['General']['AtomPtrs'] = [3, 1, 7, 9] Phase['Atoms'] = Atoms return Phase
def ReadINSPhase(self, filename, parent=None): '''Read a phase from a INS file. ''' Shelx = [ 'TITL', 'CELL', 'ZERR', 'LATT', 'SYMM', 'SFAC', 'DISP', 'UNIT', 'LAUE', 'EADP', 'MORE', 'TIME', 'HKLF', 'OMIT', 'SHEL', 'BASF', 'TWIN', 'EXTI', 'SWAT', 'HOPE', 'MERG', 'SPEC', 'RESI', 'RTAB', 'MPLA', 'HFIX', 'MOVE', 'ANIS', 'AFIX', 'FRAG', 'FEND', 'EXYZ', 'EDAP', 'EQIV', 'CONN', 'PART', 'BIND', 'FREE', 'DFIX', 'DANG', 'BUMP', 'SAME', 'SADI', 'CHIV', 'FLAT', 'DELU', 'SIMU', 'DEFS', 'ISOR', 'NCSY', 'SUMP', 'L.S.', 'CGLS', 'BLOC', 'DAMP', 'STIR', 'WGHT', 'FVAR', 'BOND', 'CONF', 'MPLA', 'HTAB', 'LIST', 'ACTA', 'SIZE', 'TEMP', 'WPDB', 'FMAP', 'GRID', 'PLAN', 'MOLE' ] self.errors = 'Error opening file' fp = open(filename, 'Ur') Phase = {} Title = '' Atoms = [] aTypes = [] S = fp.readline() line = 1 SGData = None cell = None while S: if '!' in S: S = S.split('!')[0] self.errors = 'Error reading at line ' + str(line) Atom = [] if 'TITL' in S[:4].upper(): Title = S[4:72].strip() elif not S.strip(): pass elif 'CELL' in S[:4].upper(): cellRec = S.split() abc = cellRec[2:5] angles = cellRec[5:8] cell = [ float(abc[0]), float(abc[1]), float(abc[2]), float(angles[0]), float(angles[1]), float(angles[2]) ] Volume = G2lat.calc_V(G2lat.cell2A(cell)) AA, AB = G2lat.cell2AB(cell) SGData = G2obj.P1SGData # P 1 self.warnings += '\nThe space group is not given in an ins file and has been set to "P 1".' self.warnings += "\nChange this in phase's General tab; NB: it might be in the Phase name." elif S[:4].upper() in 'SFAC': aTypes = S[4:].split() if 'H' in aTypes: self.warnings += '\n\nHydrogen atoms found; consider replacing them with stereochemically tied ones' self.warnings += '\nas Shelx constraints & HFIX commands are ignored.' self.warnings += "\nDo 'Edit/Insert H atoms' in this phase's Atoms tab after deleting the old ones." elif S[0] == 'Q': pass elif '\x1a' in S[:4]: pass elif S[:3].upper() == 'REM': pass elif S[:3].upper() == 'END': pass elif S[:4].strip().upper( ) not in Shelx: #this will find an atom record! AtRec = S.split() Atype = aTypes[int(AtRec[1]) - 1] Aname = AtRec[0] Afrac = abs(float(AtRec[5])) % 10. x, y, z = AtRec[2:5] XYZ = np.array([float(x), float(y), float(z)]) XYZ = np.where(np.abs(XYZ) < 0.00001, 0, XYZ) SytSym, Mult = G2spc.SytSym(XYZ, SGData)[:2] if '=' not in S: IA = 'I' Uiso = float(AtRec[6]) if Uiso < 0. or Uiso > 1.0: Uiso = 0.025 Uij = [0. for i in range(6)] else: IA = 'A' Uiso = 0. Ustr = AtRec[6:8] S = fp.readline() if '!' in S: S = S.split('!')[0] AtRec = S.split() line += 1 Ustr += AtRec Uij = [float(Ustr[i]) for i in range(6)] Uij = Uij[0:3] + [Uij[5], Uij[4], Uij[3]] Atom = [ Aname, Atype, '', XYZ[0], XYZ[1], XYZ[2], Afrac, SytSym, Mult, IA, Uiso ] Atom += Uij Atom.append(ran.randint(0, sys.maxsize)) Atoms.append(Atom) S = fp.readline() line += 1 fp.close() self.errors = 'Error after read complete' Phase = G2obj.SetNewPhase(Name='ShelX phase', SGData=SGData, cell=cell + [ Volume, ]) Phase['General']['Name'] = Title Phase['General']['Type'] = 'nuclear' Phase['General']['AtomPtrs'] = [3, 1, 7, 9] Phase['Atoms'] = Atoms return Phase
def findMV(peaks, controls, ssopt, Inst, dlg): def Val2Vec(vec, Vref, values): Vec = [] i = 0 for j, r in enumerate(Vref): if r: if values.size > 1: Vec.append(max(-1., min(1.0, values[i]))) else: Vec.append(max(0.0, min(1.0, values))) i += 1 else: Vec.append(vec[j]) return np.array(Vec) def ZSSfunc(values, peaks, dmin, Inst, SGData, SSGData, vec, Vref, maxH, A, wave, Z, dlg=None): Vec = Val2Vec(vec, Vref, values) HKL = G2pwd.getHKLMpeak(dmin, Inst, SGData, SSGData, Vec, maxH, A) Peaks = np.array(IndexSSPeaks(peaks, HKL)[1]).T Qo = 1. / Peaks[-2]**2 Qc = G2lat.calc_rDsqZSS(Peaks[4:8], A, Vec, Z, Peaks[0], wave) chi = np.sum((Qo - Qc)**2) if dlg: dlg.Pulse() return chi def TSSfunc(values, peaks, dmin, Inst, SGData, SSGData, vec, Vref, maxH, A, difC, Z, dlg=None): Vec = Val2Vec(vec, Vref, values) HKL = G2pwd.getHKLMpeak(dmin, Inst, SGData, SSGData, Vec, maxH, A) Peaks = np.array(IndexSSPeaks(peaks, HKL)[1]).T Qo = 1. / Peaks[-2]**2 Qc = G2lat.calc_rDsqTSS(Peaks[4:8], A, Vec, Z, Peaks[0], difC) chi = np.sum((Qo - Qc)**2) if dlg: dlg.Pulse() return chi if 'T' in Inst['Type'][0]: difC = Inst['difC'][1] else: wave = G2mth.getWave(Inst) SGData = G2spc.SpcGroup(controls[13])[1] SSGData = G2spc.SSpcGroup(SGData, ssopt['ssSymb'])[1] A = G2lat.cell2A(controls[6:12]) Z = controls[1] Vref = [True if x in ssopt['ssSymb'] else False for x in ['a', 'b', 'g']] values = [] ranges = [] dT = 0.01 #seems to be a good choice for v, r in zip(ssopt['ModVec'], Vref): if r: ranges += [ slice(dT, 1. - dT, dT), ] #NB: unique part for (00g) & (a0g); (abg)? values += [ v, ] dmin = getDmin(peaks) - 0.005 if 'T' in Inst['Type'][0]: result = so.brute(TSSfunc, ranges, finish=so.fmin_cg, full_output=True, args=(peaks, dmin, Inst, SGData, SSGData, ssopt['ModVec'], Vref, 1, A, difC, Z, dlg)) else: result = so.brute(ZSSfunc, ranges, finish=so.fmin_cg, full_output=True, args=(peaks, dmin, Inst, SGData, SSGData, ssopt['ModVec'], Vref, 1, A, wave, Z, dlg)) return Val2Vec(ssopt['ModVec'], Vref, result[0]), result
def Reader(self,filename,filepointer, ParentFrame=None, usedRanIdList=[], **unused): self.isodistort_warnings = '' self.Phase = G2IO.SetNewPhase(Name='new phase',SGData=G2IO.P1SGData) # create a new empty phase dict # make sure the ranId is really unique! while self.Phase['ranId'] in usedRanIdList: self.Phase['ranId'] = ran.randint(0,sys.maxint) returnstat = False cellitems = ( '_cell_length_a','_cell_length_b','_cell_length_c', '_cell_angle_alpha','_cell_angle_beta','_cell_angle_gamma',) cellwaveitems = ( '_cell_wave_vector_seq_id', '_cell_wave_vector_x','_cell_wave_vector_y','_cell_wave_vector_z') reqitems = ( '_atom_site_fract_x', '_atom_site_fract_y', '_atom_site_fract_z', ) phasenamefields = ( '_chemical_name_common', '_pd_phase_name', '_chemical_formula_sum' ) try: self.ShowBusy() # this can take a while try: cf = G2IO.ReadCIF(filename) except Exception as detail: self.errors = "Parse or reading of file failed in pyCifRW; check syntax of file in enCIFer or CheckCIF" return False finally: self.DoneBusy() # scan blocks for structural info self.errors = 'Error during scan of blocks for datasets' str_blklist = [] for blk in cf.keys(): for r in reqitems+cellitems: if r not in cf[blk].keys(): break else: str_blklist.append(blk) if not str_blklist: selblk = None # no block to choose elif len(str_blklist) == 1: # only one choice selblk = 0 else: # choose from options choice = [] for blknm in str_blklist: choice.append('') # accumumlate some info about this phase choice[-1] += blknm + ': ' for i in phasenamefields: # get a name for the phase name = cf[blknm].get(i).strip() if name is None or name == '?' or name == '.': continue else: choice[-1] += name.strip()[:20] + ', ' break na = len(cf[blknm].get("_atom_site_fract_x")) if na == 1: choice[-1] += '1 atom' else: choice[-1] += ('%d' % nd) + ' atoms' choice[-1] += ', cell: ' fmt = "%.2f," for i,key in enumerate(cellitems): if i == 3: fmt = "%.f," if i == 5: fmt = "%.f" choice[-1] += fmt % cif.get_number_with_esd( cf[blknm].get(key))[0] sg = cf[blknm].get("_symmetry_space_group_name_H-M",'') if not sg: sg = cf[blknm].get("_space_group_name_H-M_alt",'') if sg: choice[-1] += ', (' + sg.strip() + ')' selblk = self.PhaseSelector( choice, ParentFrame=ParentFrame, title= 'Select a phase from one the CIF data_ blocks below', size=(600,100) ) self.errors = 'Error during reading of selected block' if selblk is None: returnstat = False # no block selected or available else: blknm = str_blklist[selblk] blk = cf[str_blklist[selblk]] E = True Super = False SpGrp = blk.get("_symmetry_space_group_name_H-M",'') if not SpGrp: SpGrp = blk.get("_space_group_name_H-M_alt",'') if not SpGrp: sspgrp = blk.get("_space_group_ssg_name",'').split('(') SpGrp = sspgrp[0] SuperSg = '('+sspgrp[1].replace('\\','') Super = True SuperVec = [[0,0,.1],False,4] # try normalizing the space group, to see if we can pick the space group out of a table SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) if SpGrpNorm: E,SGData = G2spc.SpcGroup(SpGrpNorm) # nope, try the space group "out of the Box" if E and SpGrp: E,SGData = G2spc.SpcGroup(SpGrp) if E: if not SpGrp: self.warnings += 'No space group name was found in the CIF.' self.warnings += '\nThe space group has been set to "P 1". ' self.warnings += "Change this in phase's General tab." else: self.warnings += 'ERROR in space group symbol '+SpGrp self.warnings += '\nThe space group has been set to "P 1". ' self.warnings += "Change this in phase's General tab." self.warnings += '\nAre there spaces separating axial fields?\n\nError msg: ' self.warnings += G2spc.SGErrors(E) SGData = G2IO.SGData # P 1 self.Phase['General']['SGData'] = SGData # cell parameters cell = [] for lbl in cellitems: cell.append(cif.get_number_with_esd(blk[lbl])[0]) Volume = G2lat.calc_V(G2lat.cell2A(cell)) self.Phase['General']['Cell'] = [False,]+cell+[Volume,] # read in atoms self.errors = 'Error during reading of atoms' atomlbllist = [] # table to look up atom IDs atomloop = blk.GetLoop('_atom_site_label') atomkeys = [i.lower() for i in atomloop.keys()] if not blk.get('_atom_site_type_symbol'): self.isodistort_warnings += '\nlack of atom types prevents ISODISTORT processing' if blk.get('_atom_site_aniso_label'): anisoloop = blk.GetLoop('_atom_site_aniso_label') anisokeys = [i.lower() for i in anisoloop.keys()] else: anisoloop = None anisokeys = [] self.Phase['Atoms'] = [] G2AtomDict = { '_atom_site_type_symbol' : 1, '_atom_site_label' : 0, '_atom_site_fract_x' : 3, '_atom_site_fract_y' : 4, '_atom_site_fract_z' : 5, '_atom_site_occupancy' : 6, '_atom_site_aniso_u_11' : 11, '_atom_site_aniso_u_22' : 12, '_atom_site_aniso_u_33' : 13, '_atom_site_aniso_u_12' : 14, '_atom_site_aniso_u_13' : 15, '_atom_site_aniso_u_23' : 16, } ranIdlookup = {} for aitem in atomloop: atomlist = ['','','',0,0,0,1.0,'',0,'I',0.01,0,0,0,0,0,0,0] atomlist[-1] = ran.randint(0,sys.maxint) # add a random Id while atomlist[-1] in ranIdlookup: atomlist[-1] = ran.randint(0,sys.maxint) # make it unique for val,key in zip(aitem,atomkeys): col = G2AtomDict.get(key) if col >= 3: atomlist[col] = cif.get_number_with_esd(val)[0] elif col is not None: atomlist[col] = val elif key in ('_atom_site_thermal_displace_type', '_atom_site_adp_type'): #Iso or Aniso? if val.lower() == 'uani': atomlist[9] = 'A' elif key == '_atom_site_u_iso_or_equiv': atomlist[10] =cif.get_number_with_esd(val)[0] if not atomlist[1] and atomlist[0]: for i in range(2,0,-1): typ = atomlist[0].strip()[:i] if G2elem.CheckElement(typ): atomlist[1] = typ if not atomlist[1]: atomlist[1] = 'Xe' ulbl = '_atom_site_aniso_label' if atomlist[9] == 'A' and atomlist[0] in blk.get(ulbl): for val,key in zip(anisoloop.GetKeyedPacket(ulbl,atomlist[0]), anisokeys): col = G2AtomDict.get(key) if col: atomlist[col] = cif.get_number_with_esd(val)[0] atomlist[7],atomlist[8] = G2spc.SytSym(atomlist[3:6],SGData) atomlist[1] = G2elem.FixValence(atomlist[1]) self.Phase['Atoms'].append(atomlist) ranIdlookup[atomlist[0]] = atomlist[-1] if atomlist[0] in atomlbllist: self.warnings += ' ERROR: repeated atom label: '+atomlist[0] else: atomlbllist.append(atomlist[0]) if len(atomlbllist) != len(self.Phase['Atoms']): self.isodistort_warnings += '\nRepeated atom labels prevents ISODISTORT decode' for lbl in phasenamefields: # get a name for the phase name = blk.get(lbl) if name is None: continue name = name.strip() if name == '?' or name == '.': continue else: break else: # no name found, use block name for lack of a better choice name = blknm self.Phase['General']['Name'] = name.strip()[:20] self.Phase['General']['Super'] = Super if Super: self.Phase['General']['Type'] = 'modulated' self.Phase['General']['SuperVec'] = SuperVec self.Phase['General']['SuperSg'] = SuperSg self.Phase['General']['SSGData'] = G2spc.SSpcGroup(SGData,SuperSg)[1] if not self.isodistort_warnings: if blk.get('_iso_displacivemode_label') or blk.get('_iso_occupancymode_label'): self.errors = "Error while processing ISODISTORT constraints" self.ISODISTORT_proc(blk,atomlbllist,ranIdlookup) else: self.warnings += self.isodistort_warnings returnstat = True except Exception as detail: self.errors += '\n '+str(detail) print 'CIF error:',detail # for testing print sys.exc_info()[0] # for testing import traceback print traceback.format_exc() returnstat = False return returnstat
def findMV(peaks, controls, ssopt, Inst, dlg): def Val2Vec(vec, Vref, values): Vec = [] i = 0 for j, r in enumerate(Vref): if r: if values.size > 1: Vec.append(max(0.0, min(1.0, values[i]))) else: Vec.append(max(0.0, min(1.0, values))) i += 1 else: Vec.append(vec[j]) return np.array(Vec) def ZSSfunc(values, peaks, dmin, Inst, SGData, SSGData, vec, Vref, maxH, A, wave, Z, dlg=None): Vec = Val2Vec(vec, Vref, values) HKL = G2pwd.getHKLMpeak(dmin, Inst, SGData, SSGData, Vec, maxH, A) Peaks = np.array(IndexSSPeaks(peaks, HKL)[1]).T Qo = 1. / Peaks[-2]**2 Qc = G2lat.calc_rDsqZSS(Peaks[4:8], A, Vec, Z, Peaks[0], wave) chi = np.sum((Qo - Qc)**2) if dlg: dlg.Pulse() return chi if 'C' in Inst['Type'][0]: wave = G2mth.getWave(Inst) else: difC = Inst['difC'][1] SGData = G2spc.SpcGroup(controls[13])[1] SSGData = G2spc.SSpcGroup(SGData, ssopt['ssSymb'])[1] A = G2lat.cell2A(controls[6:12]) Z = controls[1] Vref = [True if x in ssopt['ssSymb'] else False for x in ['a', 'b', 'g']] values = [] ranges = [] for v, r in zip(ssopt['ModVec'], Vref): if r: ranges += [ slice(.02, .98, .05), ] values += [ v, ] dmin = getDmin(peaks) - 0.005 Peaks = np.copy(np.array(peaks).T) result = so.brute(ZSSfunc, ranges, finish=so.fmin_cg, args=(peaks, dmin, Inst, SGData, SSGData, ssopt['ModVec'], Vref, ssopt['maxH'], A, wave, Z, dlg)) return Val2Vec(ssopt['ModVec'], Vref, result)
def ImageRecalibrate(self,data,masks): 'Needs a doc string' import ImageCalibrants as calFile print 'Image recalibration:' time0 = time.time() pixelSize = data['pixelSize'] scalex = 1000./pixelSize[0] scaley = 1000./pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] data['rings'] = [] data['ellipses'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] Bravais,SGs,Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais,sg,cell in zip(Bravais,SGs,Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin,SGData,A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin,bravais,A) HKL += hkl HKL = G2lat.sortHKLd(HKL,True,False) varyList = [item for item in data['varyList'] if data['varyList'][item]] parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1], 'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']} Found = False wave = data['wavelength'] frame = masks['Frames'] tam = ma.make_mask_none(self.ImageZ.shape) if frame: tam = ma.mask_or(tam,MakeFrameMask(data,frame)) for iH,H in enumerate(HKL): if debug: print H dsp = H[3] tth = 2.0*asind(wave/(2.*dsp)) if tth+abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break ellipse = GetEllipse(dsp,data) Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,ma.array(self.ImageZ,mask=tam)) if Ring: if iH >= skip: data['rings'].append(np.array(Ring)) data['ellipses'].append(copy.deepcopy(ellipse+('r',))) Found = True elif not Found: #skipping inner rings, keep looking until ring found continue else: #no more rings beyond edge of detector data['ellipses'].append([]) continue # break rings = np.concatenate((data['rings']),axis=0) chisq = FitDetector(rings,varyList,parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'],parmDict['det-Y']] data['rotation'] = np.mod(parmDict['phi'],360.0) data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq N = len(data['ellipses']) data['ellipses'] = [] #clear away individual ellipse fits for H in HKL[:N]: ellipse = GetEllipse(H[3],data) data['ellipses'].append(copy.deepcopy(ellipse+('b',))) print 'calibration time = ',time.time()-time0 G2plt.PlotImage(self,newImage=True) return True
def ImageCalibrate(self,data): 'Needs a doc string' import copy import ImageCalibrants as calFile print 'Image calibration:' time0 = time.time() ring = data['ring'] pixelSize = data['pixelSize'] scalex = 1000./pixelSize[0] scaley = 1000./pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] varyDict = data['varyList'] if varyDict['dist'] and varyDict['wave']: print 'ERROR - you can not simultaneously calibrate distance and wavelength' return False if len(ring) < 5: print 'ERROR - not enough inner ring points for ellipse' return False #fit start points on inner ring data['ellipses'] = [] data['rings'] = [] outE = FitEllipse(ring) fmt = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f' fmt2 = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f, chi**2: %.3f, Np: %d' if outE: print fmt%('start ellipse: ',outE[0][0],outE[0][1],outE[1],outE[2][0],outE[2][1]) ellipse = outE else: return False #setup 360 points on that ring for "good" fit data['ellipses'].append(ellipse[:]+('g',)) Ring = makeRing(1.0,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) if Ring: ellipse = FitEllipse(Ring) Ring = makeRing(1.0,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) #do again ellipse = FitEllipse(Ring) else: print '1st ring not sufficiently complete to proceed' return False if debug: print fmt2%('inner ring: ',ellipse[0][0],ellipse[0][1],ellipse[1], ellipse[2][0],ellipse[2][1],0.,len(Ring)) #cent,phi,radii data['ellipses'].append(ellipse[:]+('r',)) data['rings'].append(np.array(Ring)) G2plt.PlotImage(self,newImage=True) #setup for calibration data['rings'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] #generate reflection set Bravais,SGs,Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais,sg,cell in zip(Bravais,SGs,Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin,SGData,A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin,bravais,A) HKL += hkl HKL = G2lat.sortHKLd(HKL,True,False)[skip:] #set up 1st ring elcent,phi,radii = ellipse #from fit of 1st ring dsp = HKL[0][3] print '1st ring: try %.4f'%(dsp) if varyDict['dist']: wave = data['wavelength'] tth = 2.0*asind(wave/(2.*dsp)) else: #varyDict['wave']! dist = data['distance'] tth = npatan2d(radii[0],dist) data['wavelength'] = wave = 2.0*dsp*sind(tth/2.0) Ring0 = makeRing(dsp,ellipse,3,cutoff,scalex,scaley,self.ImageZ) ttth = nptand(tth) stth = npsind(tth) ctth = npcosd(tth) #1st estimate of tilt; assume ellipse - don't know sign though if varyDict['tilt']: tilt = npasind(np.sqrt(max(0.,1.-(radii[0]/radii[1])**2))*ctth) if not tilt: print 'WARNING - selected ring was fitted as a circle' print ' - if detector was tilted we suggest you skip this ring - WARNING' else: tilt = data['tilt'] #1st estimate of dist: sample to detector normal to plane if varyDict['dist']: data['distance'] = dist = radii[0]**2/(ttth*radii[1]) else: dist = data['distance'] if varyDict['tilt']: #ellipse to cone axis (x-ray beam); 2 choices depending on sign of tilt zdisp = radii[1]*ttth*tand(tilt) zdism = radii[1]*ttth*tand(-tilt) #cone axis position; 2 choices. Which is right? #NB: zdisp is || to major axis & phi is rotation of minor axis #thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)] centp = [elcent[0]+zdisp*sind(phi),elcent[1]-zdisp*cosd(phi)] centm = [elcent[0]+zdism*sind(phi),elcent[1]-zdism*cosd(phi)] #check get same ellipse parms either way #now do next ring; estimate either way & do a FitDetector each way; best fit is correct one fail = True i2 = 1 while fail: dsp = HKL[i2][3] print '2nd ring: try %.4f'%(dsp) tth = 2.0*asind(wave/(2.*dsp)) ellipsep = GetEllipse2(tth,0.,dist,centp,tilt,phi) print fmt%('plus ellipse :',ellipsep[0][0],ellipsep[0][1],ellipsep[1],ellipsep[2][0],ellipsep[2][1]) Ringp = makeRing(dsp,ellipsep,3,cutoff,scalex,scaley,self.ImageZ) parmDict = {'dist':dist,'det-X':centp[0],'det-Y':centp[1], 'tilt':tilt,'phi':phi,'wave':wave,'dep':0.0} varyList = [item for item in varyDict if varyDict[item]] if len(Ringp) > 10: chip = FitDetector(np.array(Ring0+Ringp),varyList,parmDict,True) tiltp = parmDict['tilt'] phip = parmDict['phi'] centp = [parmDict['det-X'],parmDict['det-Y']] fail = False else: chip = 1e6 ellipsem = GetEllipse2(tth,0.,dist,centm,-tilt,phi) print fmt%('minus ellipse:',ellipsem[0][0],ellipsem[0][1],ellipsem[1],ellipsem[2][0],ellipsem[2][1]) Ringm = makeRing(dsp,ellipsem,3,cutoff,scalex,scaley,self.ImageZ) if len(Ringm) > 10: parmDict['tilt'] *= -1 chim = FitDetector(np.array(Ring0+Ringm),varyList,parmDict,True) tiltm = parmDict['tilt'] phim = parmDict['phi'] centm = [parmDict['det-X'],parmDict['det-Y']] fail = False else: chim = 1e6 if fail: i2 += 1 if chip < chim: data['tilt'] = tiltp data['center'] = centp data['rotation'] = phip else: data['tilt'] = tiltm data['center'] = centm data['rotation'] = phim data['ellipses'].append(ellipsep[:]+('b',)) data['rings'].append(np.array(Ringp)) data['ellipses'].append(ellipsem[:]+('r',)) data['rings'].append(np.array(Ringm)) G2plt.PlotImage(self,newImage=True) parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1], 'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']} varyList = [item for item in varyDict if varyDict[item]] data['rings'] = [] data['ellipses'] = [] for i,H in enumerate(HKL): dsp = H[3] tth = 2.0*asind(wave/(2.*dsp)) if tth+abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break if debug: print 'HKLD:',H[:4],'2-theta: %.4f'%(tth) elcent,phi,radii = ellipse = GetEllipse(dsp,data) data['ellipses'].append(copy.deepcopy(ellipse+('g',))) if debug: print fmt%('predicted ellipse:',elcent[0],elcent[1],phi,radii[0],radii[1]) Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) if Ring: data['rings'].append(np.array(Ring)) rings = np.concatenate((data['rings']),axis=0) if i: chisq = FitDetector(rings,varyList,parmDict,False) data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'],parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq elcent,phi,radii = ellipse = GetEllipse(dsp,data) if debug: print fmt2%('fitted ellipse: ',elcent[0],elcent[1],phi,radii[0],radii[1],chisq,len(rings)) data['ellipses'].append(copy.deepcopy(ellipse+('r',))) # G2plt.PlotImage(self,newImage=True) else: if debug: print 'insufficient number of points in this ellipse to fit' # break G2plt.PlotImage(self,newImage=True) fullSize = len(self.ImageZ)/scalex if 2*radii[1] < .9*fullSize: print 'Are all usable rings (>25% visible) used? Try reducing Min ring I/Ib' N = len(data['ellipses']) if N > 2: FitDetector(rings,varyList,parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'],parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] for H in HKL[:N]: ellipse = GetEllipse(H[3],data) data['ellipses'].append(copy.deepcopy(ellipse+('b',))) print 'calibration time = ',time.time()-time0 G2plt.PlotImage(self,newImage=True) return True
def UpdateLattice(self, parmVarDict, phase_index): ''' Update the lattice parameters in the current model for all phases ''' # Symmetry Options: # cubic, tetragonal, hexagonal, rhombohedral, trigonal, # orthorhombic, monoclinic, triclinic if self._symmetry[self._phase[phase_index]] == 'cubic': a = parmVarDict[str(phase_index) + '::a'] b = a c = a alpha = 90. beta = 90. gamma = 90. elif self._symmetry[self._phase[phase_index]] == 'tetragonal': a = parmVarDict[str(phase_index) + '::a'] b = a c = parmVarDict[str(phase_index) + '::c'] alpha = 90. beta = 90. gamma = 90. elif self._symmetry[self._phase[phase_index]] == 'hexagonal': a = parmVarDict[str(phase_index) + '::a'] b = a c = parmVarDict[str(phase_index) + '::c'] alpha = 90. beta = 90. gamma = 120. elif self._symmetry[self._phase[phase_index]] == 'trigonal': a = parmVarDict[str(phase_index) + '::a'] b = a c = parmVarDict[str(phase_index) + '::c'] alpha = 90. beta = 90. gamma = 120. elif self._symmetry[self._phase[phase_index]] == 'rhombohedral': a = parmVarDict[str(phase_index) + '::a'] b = a c = a alpha = parmVarDict[str(phase_index) + '::alpha'] beta = alpha gamma = alpha elif self._symmetry[self._phase[phase_index]] == 'orthorhombic': a = parmVarDict[str(phase_index) + '::a'] b = parmVarDict[str(phase_index) + '::b'] c = parmVarDict[str(phase_index) + '::c'] alpha = 90. beta = 90. gamma = 90. elif self._symmetry[self._phase[phase_index]] == 'monoclinic': a = parmVarDict[str(phase_index) + '::a'] b = parmVarDict[str(phase_index) + '::b'] c = parmVarDict[str(phase_index) + '::c'] beta = parmVarDict[str(phase_index) + '::beta'] alpha = 90. gamma = 90. elif self._symmetry[self._phase[phase_index]] == 'triclinic': a = parmVarDict[str(phase_index) + '::a'] b = parmVarDict[str(phase_index) + '::b'] c = parmVarDict[str(phase_index) + '::c'] alpha = parmVarDict[str(phase_index) + '::alpha'] beta = parmVarDict[str(phase_index) + '::beta'] gamma = parmVarDict[str(phase_index) + '::gamma'] A = G2latt.cell2A([a, b, c, alpha, beta, gamma]) LatticeUpdate = { str(phase_index) + '::A0': A[0], str(phase_index) + '::A1': A[1], str(phase_index) + '::A2': A[2], str(phase_index) + '::A3': A[3], str(phase_index) + '::A4': A[4], str(phase_index) + '::A5': A[5] } self._parmDict.update(LatticeUpdate) return parmVarDict
def ReadEXPPhase(self, G2frame, filepointer): '''Read a phase from a GSAS .EXP file. ''' shModels = ['cylindrical', 'none', 'shear - 2/m', 'rolling - mmm'] textureData = { 'Order': 0, 'Model': 'cylindrical', 'Sample omega': [False, 0.0], 'Sample chi': [False, 0.0], 'Sample phi': [False, 0.0], 'SH Coeff': [False, {}], 'SHShow': False, 'PFhkl': [0, 0, 1], 'PFxyz': [0, 0, 1], 'PlotType': 'Pole figure' } shNcof = 0 S = 1 NPhas = [] Expr = [{}, {}, {}, {}, {}, {}, {}, {}, {}] # GSAS can have at most 9 phases for line, S in enumerate(filepointer): self.errors = 'Error reading at line ' + str(line + 1) if 'EXPR NPHAS' in S[:12]: Num = S[12:-1].count('0') NPhas = S[12:-1].split() if 'CRS' in S[:3]: N = int(S[3:4]) - 1 Expr[N][S[:12]] = S[12:-1] PNames = [] if not NPhas: raise self.ImportException("No EXPR NPHAS record found") self.errors = 'Error interpreting file' for n, N in enumerate(NPhas): if N != '0': result = n key = 'CRS' + str(n + 1) + ' PNAM' PNames.append(Expr[n][key]) if len(PNames) == 0: raise self.ImportException("No phases found") elif len(PNames) > 1: dlg = wx.SingleChoiceDialog(G2frame, 'Which phase to read?', 'Read phase data', PNames, wx.CHOICEDLG_STYLE) try: if dlg.ShowModal() == wx.ID_OK: result = dlg.GetSelection( ) # I think this breaks is there are skipped phases. Cant this happen? finally: dlg.Destroy() EXPphase = Expr[result] keyList = EXPphase.keys() keyList.sort() SGData = {} if NPhas[result] == '1': Ptype = 'nuclear' elif NPhas[result] in ['2', '3']: Ptype = 'magnetic' elif NPhas[result] == '4': Ptype = 'macromolecular' elif NPhas[result] == '10': Ptype = 'Pawley' else: raise self.ImportException("Phase type not recognized") for key in keyList: if 'PNAM' in key: PhaseName = EXPphase[key].strip() elif 'ABC ' in key: abc = [ float(EXPphase[key][:10]), float(EXPphase[key][10:20]), float(EXPphase[key][20:30]) ] elif 'ANGLES' in key: angles = [ float(EXPphase[key][:10]), float(EXPphase[key][10:20]), float(EXPphase[key][20:30]) ] elif 'SG SYM' in key: SpGrp = EXPphase[key][:15].strip() E, SGData = G2spc.SpcGroup(SpGrp) if E: SGData = G2IO.SGData # P 1 -- unlikely to need this! self.warnings += '\nThe GSAS space group was not interpreted(!) and has been set to "P 1".' self.warnings += "Change this in phase's General tab." elif 'OD ' in key: SHdata = EXPphase[key].split() # may not have all 9 values SHvals = 9 * [0] for i in range(9): try: float(SHdata[i]) SHvals[i] = SHdata[i] except: pass textureData['Order'] = int(SHvals[0]) textureData['Model'] = shModels[int(SHvals[2])] textureData['Sample omega'] = [False, float(SHvals[6])] textureData['Sample chi'] = [False, float(SHvals[7])] textureData['Sample phi'] = [False, float(SHvals[8])] shNcof = int(SHvals[1]) Atoms = [] if Ptype == 'nuclear': for key in keyList: if 'AT' in key: if key[11:] == 'A': S = EXPphase[key] elif key[11:] == 'B': S += EXPphase[key] Atom = [ S[50:58].strip(), S[:10].strip().capitalize(), '', float(S[10:20]), float(S[20:30]), float(S[30:40]), float(S[40:50]), '', int(S[60:62]), S[130:131] ] if Atom[9] == 'I': Atom += [float(S[68:78]), 0., 0., 0., 0., 0., 0.] elif Atom[9] == 'A': Atom += [ 0.0, float(S[68:78]), float(S[78:88]), float(S[88:98]), float(S[98:108]), float(S[108:118]), float(S[118:128]) ] XYZ = Atom[3:6] Atom[7], Atom[8] = G2spc.SytSym(XYZ, SGData) Atom.append(ran.randint(0, sys.maxint)) Atoms.append(Atom) elif Ptype == 'macromolecular': for key in keyList: if 'AT' in key[6:8]: S = EXPphase[key] Atom = [ S[56:60], S[50:54].strip().upper(), S[54:56], S[46:51].strip(), S[:8].strip().capitalize(), '', float(S[16:24]), float(S[24:32]), float(S[32:40]), float(S[8:16]), '1', 1, 'I', float(S[40:46]), 0, 0, 0, 0, 0, 0 ] XYZ = Atom[6:9] Atom[10], Atom[11] = G2spc.SytSym(XYZ, SGData) Atom.append(ran.randint(0, sys.maxint)) Atoms.append(Atom) Volume = G2lat.calc_V(G2lat.cell2A(abc + angles)) if shNcof: shCoef = {} nRec = [i + 1 for i in range((shNcof - 1) / 6 + 1)] for irec in nRec: ODkey = keyList[0][:6] + 'OD' + '%3dA' % (irec) indx = EXPphase[ODkey].split() ODkey = ODkey[:-1] + 'B' vals = EXPphase[ODkey].split() for i, val in enumerate(vals): key = 'C(%s,%s,%s)' % (indx[3 * i], indx[3 * i + 1], indx[3 * i + 2]) shCoef[key] = float(val) textureData['SH Coeff'] = [False, shCoef] if not SGData: raise self.ImportException("No space group found in phase") if not abc: raise self.ImportException("No cell lengths found in phase") if not angles: raise self.ImportException("No cell angles found in phase") if not Atoms: raise self.ImportException("No atoms found in phase") Phase = G2IO.SetNewPhase(Name=PhaseName, SGData=SGData, cell=abc + angles + [ Volume, ]) general = Phase['General'] general['Type'] = Ptype if general['Type'] == 'macromolecular': general['AtomPtrs'] = [6, 4, 10, 12] else: general['AtomPtrs'] = [3, 1, 7, 9] general['SH Texture'] = textureData Phase['Atoms'] = Atoms return Phase
def ImageRecalibrate(self, data, masks): 'Needs a doc string' import ImageCalibrants as calFile print 'Image recalibration:' time0 = time.time() pixelSize = data['pixelSize'] scalex = 1000. / pixelSize[0] scaley = 1000. / pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] data['rings'] = [] data['ellipses'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] Bravais, SGs, Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais, sg, cell in zip(Bravais, SGs, Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin, SGData, A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin, bravais, A) HKL += hkl HKL = G2lat.sortHKLd(HKL, True, False) varyList = [item for item in data['varyList'] if data['varyList'][item]] parmDict = { 'dist': data['distance'], 'det-X': data['center'][0], 'det-Y': data['center'][1], 'tilt': data['tilt'], 'phi': data['rotation'], 'wave': data['wavelength'], 'dep': data['DetDepth'] } Found = False wave = data['wavelength'] frame = masks['Frames'] tam = ma.make_mask_none(self.ImageZ.shape) if frame: tam = ma.mask_or(tam, MakeFrameMask(data, frame)) for iH, H in enumerate(HKL): if debug: print H dsp = H[3] tth = 2.0 * asind(wave / (2. * dsp)) if tth + abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break ellipse = GetEllipse(dsp, data) Ring = makeRing(dsp, ellipse, pixLimit, cutoff, scalex, scaley, ma.array(self.ImageZ, mask=tam)) if Ring: if iH >= skip: data['rings'].append(np.array(Ring)) data['ellipses'].append(copy.deepcopy(ellipse + ('r', ))) Found = True elif not Found: #skipping inner rings, keep looking until ring found continue else: #no more rings beyond edge of detector data['ellipses'].append([]) continue # break rings = np.concatenate((data['rings']), axis=0) chisq = FitDetector(rings, varyList, parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'], parmDict['det-Y']] data['rotation'] = np.mod(parmDict['phi'], 360.0) data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq N = len(data['ellipses']) data['ellipses'] = [] #clear away individual ellipse fits for H in HKL[:N]: ellipse = GetEllipse(H[3], data) data['ellipses'].append(copy.deepcopy(ellipse + ('b', ))) print 'calibration time = ', time.time() - time0 G2plt.PlotImage(self, newImage=True) return True
def ReadPDBPhase(self, filename, parent=None): '''Read a phase from a PDB file. ''' EightPiSq = 8. * math.pi**2 self.errors = 'Error opening file' file = open(filename, 'Ur') Phase = {} Title = '' Compnd = '' Atoms = [] A = np.zeros(shape=(3, 3)) S = file.readline() line = 1 SGData = None cell = None while S: self.errors = 'Error reading at line ' + str(line) Atom = [] if 'TITLE' in S[:5]: Title = S[10:72].strip() elif 'COMPND ' in S[:10]: Compnd = S[10:72].strip() elif 'CRYST' in S[:5]: abc = S[7:34].split() angles = S[34:55].split() cell = [ float(abc[0]), float(abc[1]), float(abc[2]), float(angles[0]), float(angles[1]), float(angles[2]) ] Volume = G2lat.calc_V(G2lat.cell2A(cell)) AA, AB = G2lat.cell2AB(cell) SpGrp = S[55:65] E, SGData = G2spc.SpcGroup(SpGrp) # space group processing failed, try to look up name in table if E: SpGrpNorm = G2spc.StandardizeSpcName(SpGrp) if SpGrpNorm: E, SGData = G2spc.SpcGroup(SpGrpNorm) while E: print G2spc.SGErrors(E) dlg = wx.TextEntryDialog( parent, SpGrp[:-1] + ' is invalid \nN.B.: make sure spaces separate axial fields in symbol', 'ERROR in space group symbol', '', style=wx.OK) if dlg.ShowModal() == wx.ID_OK: SpGrp = dlg.GetValue() E, SGData = G2spc.SpcGroup(SpGrp) else: SGData = G2IO.SGData # P 1 self.warnings += '\nThe space group was not interpreted and has been set to "P 1".' self.warnings += "Change this in phase's General tab." dlg.Destroy() SGlines = G2spc.SGPrint(SGData) for l in SGlines: print l elif 'SCALE' in S[:5]: V = S[10:41].split() A[int(S[5]) - 1] = [float(V[0]), float(V[1]), float(V[2])] elif 'ATOM' in S[:4] or 'HETATM' in S[:6]: if not SGData: self.warnings += '\nThe space group was not read before atoms and has been set to "P 1". ' self.warnings += "Change this in phase's General tab." SGData = G2IO.SGData # P 1 XYZ = [float(S[31:39]), float(S[39:47]), float(S[47:55])] XYZ = np.inner(AB, XYZ) XYZ = np.where(abs(XYZ) < 0.00001, 0, XYZ) SytSym, Mult = G2spc.SytSym(XYZ, SGData) Uiso = float(S[61:67]) / EightPiSq Type = S[12:14].lower() if Type[0] in '123456789': Type = Type[1:] Atom = [ S[22:27].strip(), S[17:20].upper(), S[20:22], S[12:17].strip(), Type.strip().capitalize(), '', XYZ[0], XYZ[1], XYZ[2], float(S[55:61]), SytSym, Mult, 'I', Uiso, 0, 0, 0, 0, 0, 0 ] S = file.readline() line += 1 if 'ANISOU' in S[:6]: Uij = S[30:72].split() Uij = [ float(Uij[0]) / 10000., float(Uij[1]) / 10000., float(Uij[2]) / 10000., float(Uij[3]) / 10000., float(Uij[4]) / 10000., float(Uij[5]) / 10000. ] Atom = Atom[:14] + Uij Atom[12] = 'A' Atom.append(ran.randint(0, sys.maxint)) Atoms.append(Atom) S = file.readline() line += 1 file.close() self.errors = 'Error after read complete' if Title: PhaseName = Title elif Compnd: PhaseName = Compnd else: PhaseName = 'None' if not SGData: raise self.ImportException("No space group (CRYST entry) found") if not cell: raise self.ImportException("No cell (CRYST entry) found") Phase = G2IO.SetNewPhase(Name=PhaseName, SGData=SGData, cell=cell + [ Volume, ]) Phase['General']['Type'] = 'macromolecular' Phase['General']['AtomPtrs'] = [6, 4, 10, 12] Phase['Atoms'] = Atoms return Phase
def RetDistAngle(DisAglCtls,DisAglData): '''Compute and return distances and angles :param dict DisAglCtls: contains distance/angle radii usually defined using :func:`GSASIIgrid.DisAglDialog` :param dict DisAglData: contains phase data: Items 'OrigAtoms' and 'TargAtoms' contain the atoms to be used for distance/angle origins and atoms to be used as targets. Item 'SGData' has the space group information (see :ref:`Space Group object<SGData_table>`) :returns: AtomLabels,DistArray,AngArray where: **AtomLabels** is a dict of atom labels, keys are the atom number **DistArray** is a dict keyed by the origin atom number where the value is a list of distance entries. The value for each distance is a list containing: 0) the target atom number (int); 1) the unit cell offsets added to x,y & z (tuple of int values) 2) the symmetry operator number (which may be modified to indicate centering and center of symmetry) 3) an interatomic distance in A (float) 4) an uncertainty on the distance in A or 0.0 (float) **AngArray** is a dict keyed by the origin (central) atom number where the value is a list of angle entries. The value for each angle entry consists of three values: 0) a distance item reference for one neighbor (int) 1) a distance item reference for a second neighbor (int) 2) a angle, uncertainty pair; the s.u. may be zero (tuple of two floats) The AngArray distance reference items refer directly to the index of the items in the DistArray item for the list of distances for the central atom. ''' import numpy.ma as ma SGData = DisAglData['SGData'] Cell = DisAglData['Cell'] Amat,Bmat = G2lat.cell2AB(Cell[:6]) covData = {} if 'covData' in DisAglData: covData = DisAglData['covData'] covMatrix = covData['covMatrix'] varyList = covData['varyList'] pfx = str(DisAglData['pId'])+'::' A = G2lat.cell2A(Cell[:6]) cellSig = G2stIO.getCellEsd(pfx,SGData,A,covData) names = [' a = ',' b = ',' c = ',' alpha = ',' beta = ',' gamma = ',' Volume = '] valEsd = [G2mth.ValEsd(Cell[i],cellSig[i],True) for i in range(7)] Factor = DisAglCtls['Factors'] Radii = dict(zip(DisAglCtls['AtomTypes'],zip(DisAglCtls['BondRadii'],DisAglCtls['AngleRadii']))) indices = (-1,0,1) Units = np.array([[h,k,l] for h in indices for k in indices for l in indices]) origAtoms = DisAglData['OrigAtoms'] targAtoms = DisAglData['TargAtoms'] AtomLabels = {} for Oatom in origAtoms: AtomLabels[Oatom[0]] = Oatom[1] for Oatom in targAtoms: AtomLabels[Oatom[0]] = Oatom[1] DistArray = {} AngArray = {} for Oatom in origAtoms: DistArray[Oatom[0]] = [] AngArray[Oatom[0]] = [] OxyzNames = '' IndBlist = [] Dist = [] Vect = [] VectA = [] angles = [] for Tatom in targAtoms: Xvcov = [] TxyzNames = '' if 'covData' in DisAglData: OxyzNames = [pfx+'dAx:%d'%(Oatom[0]),pfx+'dAy:%d'%(Oatom[0]),pfx+'dAz:%d'%(Oatom[0])] TxyzNames = [pfx+'dAx:%d'%(Tatom[0]),pfx+'dAy:%d'%(Tatom[0]),pfx+'dAz:%d'%(Tatom[0])] Xvcov = G2mth.getVCov(OxyzNames+TxyzNames,varyList,covMatrix) result = G2spc.GenAtom(Tatom[3:6],SGData,False,Move=False) BsumR = (Radii[Oatom[2]][0]+Radii[Tatom[2]][0])*Factor[0] AsumR = (Radii[Oatom[2]][1]+Radii[Tatom[2]][1])*Factor[1] for Txyz,Top,Tunit in result: Dx = (Txyz-np.array(Oatom[3:6]))+Units dx = np.inner(Amat,Dx) dist = ma.masked_less(np.sqrt(np.sum(dx**2,axis=0)),0.5) IndB = ma.nonzero(ma.masked_greater(dist-BsumR,0.)) if np.any(IndB): for indb in IndB: for i in range(len(indb)): if str(dx.T[indb][i]) not in IndBlist: IndBlist.append(str(dx.T[indb][i])) unit = Units[indb][i] tunit = (unit[0]+Tunit[0],unit[1]+Tunit[1],unit[2]+Tunit[2]) pdpx = G2mth.getDistDerv(Oatom[3:6],Tatom[3:6],Amat,unit,Top,SGData) sig = 0.0 if len(Xvcov): sig = np.sqrt(np.inner(pdpx,np.inner(Xvcov,pdpx))) Dist.append([Oatom[0],Tatom[0],tunit,Top,ma.getdata(dist[indb])[i],sig]) if (Dist[-1][-2]-AsumR) <= 0.: Vect.append(dx.T[indb][i]/Dist[-1][-2]) VectA.append([OxyzNames,np.array(Oatom[3:6]),TxyzNames,np.array(Tatom[3:6]),unit,Top]) else: Vect.append([0.,0.,0.]) VectA.append([]) for D in Dist: DistArray[Oatom[0]].append(D[1:]) Vect = np.array(Vect) angles = np.zeros((len(Vect),len(Vect))) angsig = np.zeros((len(Vect),len(Vect))) for i,veca in enumerate(Vect): if np.any(veca): for j,vecb in enumerate(Vect): if np.any(vecb): angles[i][j],angsig[i][j] = G2mth.getAngSig(VectA[i],VectA[j],Amat,SGData,covData) if i <= j: continue AngArray[Oatom[0]].append((i,j, G2mth.getAngSig(VectA[i],VectA[j],Amat,SGData,covData))) return AtomLabels,DistArray,AngArray
def PrintDistAngle(DisAglCtls,DisAglData,out=sys.stdout): '''Print distances and angles :param dict DisAglCtls: contains distance/angle radii usually defined using :func:`GSASIIgrid.DisAglDialog` :param dict DisAglData: contains phase data: Items 'OrigAtoms' and 'TargAtoms' contain the atoms to be used for distance/angle origins and atoms to be used as targets. Item 'SGData' has the space group information (see :ref:`Space Group object<SGData_table>`) :param file out: file object for output. Defaults to sys.stdout. ''' import numpy.ma as ma def MyPrint(s): out.write(s+'\n') # print(s,file=out) # use in Python 3 def ShowBanner(name): MyPrint(80*'*') MyPrint(' Interatomic Distances and Angles for phase '+name) MyPrint((80*'*')+'\n') ShowBanner(DisAglCtls['Name']) SGData = DisAglData['SGData'] SGtext,SGtable = G2spc.SGPrint(SGData) for line in SGtext: MyPrint(line) if len(SGtable): for i,item in enumerate(SGtable[::2]): line = ' %s %s'%(item.ljust(30),SGtable[2*i+1].ljust(30)) MyPrint(line) else: MyPrint(' ( 1) %s'%(SGtable[0])) Cell = DisAglData['Cell'] Amat,Bmat = G2lat.cell2AB(Cell[:6]) covData = {} if 'covData' in DisAglData: covData = DisAglData['covData'] covMatrix = covData['covMatrix'] varyList = covData['varyList'] pfx = str(DisAglData['pId'])+'::' A = G2lat.cell2A(Cell[:6]) cellSig = G2stIO.getCellEsd(pfx,SGData,A,covData) names = [' a = ',' b = ',' c = ',' alpha = ',' beta = ',' gamma = ',' Volume = '] valEsd = [G2mth.ValEsd(Cell[i],cellSig[i],True) for i in range(7)] line = '\n Unit cell:' for name,vals in zip(names,valEsd): line += name+vals MyPrint(line) else: MyPrint('\n Unit cell: a = '+('%.5f'%Cell[0])+' b = '+('%.5f'%Cell[1])+' c = '+('%.5f'%Cell[2])+ ' alpha = '+('%.3f'%Cell[3])+' beta = '+('%.3f'%Cell[4])+' gamma = '+ ('%.3f'%Cell[5])+' volume = '+('%.3f'%Cell[6])) AtomLabels,DistArray,AngArray = RetDistAngle(DisAglCtls,DisAglData) origAtoms = DisAglData['OrigAtoms'] targAtoms = DisAglData['TargAtoms'] for Oatom in origAtoms: i = Oatom[0] Dist = DistArray[i] nDist = len(Dist) angles = np.zeros((nDist,nDist)) angsig = np.zeros((nDist,nDist)) for k,j,tup in AngArray[i]: angles[k][j],angsig[k][j] = angles[j][k],angsig[j][k] = tup line = '' for i,x in enumerate(Oatom[3:6]): line += ('%12.5f'%x).rstrip('0') MyPrint('\n Distances & angles for '+Oatom[1]+' at '+line.rstrip()) MyPrint(80*'*') line = '' for dist in Dist[:-1]: line += '%12s'%(AtomLabels[dist[0]].center(12)) MyPrint(' To cell +(sym. op.) dist. '+line.rstrip()) for i,dist in enumerate(Dist): line = '' for j,angle in enumerate(angles[i][0:i]): sig = angsig[i][j] if angle: if sig: line += '%12s'%(G2mth.ValEsd(angle,sig,True).center(12)) else: val = '%.3f'%(angle) line += '%12s'%(val.center(12)) else: line += 12*' ' if dist[4]: #sig exists! val = G2mth.ValEsd(dist[3],dist[4]) else: val = '%8.4f'%(dist[3]) tunit = '[%2d%2d%2d]'% dist[1] MyPrint((' %8s%10s+(%4d) %12s'%(AtomLabels[dist[0]].ljust(8),tunit.ljust(10),dist[2],val.center(12)))+line.rstrip())
def ReadEXPPhase(self, G2frame,filepointer): '''Read a phase from a GSAS .EXP file. ''' shModels = ['cylindrical','none','shear - 2/m','rolling - mmm'] textureData = {'Order':0,'Model':'cylindrical','Sample omega':[False,0.0], 'Sample chi':[False,0.0],'Sample phi':[False,0.0],'SH Coeff':[False,{}], 'SHShow':False,'PFhkl':[0,0,1],'PFxyz':[0,0,1],'PlotType':'Pole figure'} shNcof = 0 S = 1 NPhas = [] Expr = [{},{},{},{},{},{},{},{},{}] # GSAS can have at most 9 phases for line,S in enumerate(filepointer): self.errors = 'Error reading at line '+str(line+1) if 'EXPR NPHAS' in S[:12]: Num = S[12:-1].count('0') NPhas = S[12:-1].split() if 'CRS' in S[:3]: N = int(S[3:4])-1 Expr[N][S[:12]] = S[12:-1] PNames = [] if not NPhas: raise self.ImportException("No EXPR NPHAS record found") self.errors = 'Error interpreting file' for n,N in enumerate(NPhas): if N != '0': result = n key = 'CRS'+str(n+1)+' PNAM' PNames.append(Expr[n][key]) if len(PNames) == 0: raise self.ImportException("No phases found") elif len(PNames) > 1: dlg = wx.SingleChoiceDialog(G2frame, 'Which phase to read?', 'Read phase data', PNames, wx.CHOICEDLG_STYLE) try: if dlg.ShowModal() == wx.ID_OK: result = dlg.GetSelection() # I think this breaks is there are skipped phases. Cant this happen? finally: dlg.Destroy() EXPphase = Expr[result] keyList = EXPphase.keys() keyList.sort() SGData = {} if NPhas[result] == '1': Ptype = 'nuclear' elif NPhas[result] in ['2','3']: Ptype = 'magnetic' elif NPhas[result] == '4': Ptype = 'macromolecular' elif NPhas[result] == '10': Ptype = 'Pawley' else: raise self.ImportException("Phase type not recognized") for key in keyList: if 'PNAM' in key: PhaseName = EXPphase[key].strip() elif 'ABC ' in key: abc = [float(EXPphase[key][:10]),float(EXPphase[key][10:20]),float(EXPphase[key][20:30])] elif 'ANGLES' in key: angles = [float(EXPphase[key][:10]),float(EXPphase[key][10:20]),float(EXPphase[key][20:30])] elif 'SG SYM' in key: SpGrp = EXPphase[key][:15].strip() E,SGData = G2spc.SpcGroup(SpGrp) if E: SGData = G2IO.SGData # P 1 -- unlikely to need this! self.warnings += '\nThe GSAS space group was not interpreted(!) and has been set to "P 1".' self.warnings += "Change this in phase's General tab." elif 'OD ' in key: SHdata = EXPphase[key].split() # may not have all 9 values SHvals = 9*[0] for i in range(9): try: float(SHdata[i]) SHvals[i] = SHdata[i] except: pass textureData['Order'] = int(SHvals[0]) textureData['Model'] = shModels[int(SHvals[2])] textureData['Sample omega'] = [False,float(SHvals[6])] textureData['Sample chi'] = [False,float(SHvals[7])] textureData['Sample phi'] = [False,float(SHvals[8])] shNcof = int(SHvals[1]) Atoms = [] if Ptype == 'nuclear': for key in keyList: if 'AT' in key: if key[11:] == 'A': S = EXPphase[key] elif key[11:] == 'B': S += EXPphase[key] Atom = [S[50:58].strip(),S[:10].strip().capitalize(),'', float(S[10:20]),float(S[20:30]),float(S[30:40]), float(S[40:50]),'',int(S[60:62]),S[130:131]] if Atom[9] == 'I': Atom += [float(S[68:78]),0.,0.,0.,0.,0.,0.] elif Atom[9] == 'A': Atom += [0.0,float(S[68:78]),float(S[78:88]), float(S[88:98]),float(S[98:108]), float(S[108:118]),float(S[118:128])] XYZ = Atom[3:6] Atom[7],Atom[8] = G2spc.SytSym(XYZ,SGData) Atom.append(ran.randint(0,sys.maxint)) Atoms.append(Atom) elif Ptype == 'macromolecular': for key in keyList: if 'AT' in key[6:8]: S = EXPphase[key] Atom = [S[56:60],S[50:54].strip().upper(),S[54:56], S[46:51].strip(),S[:8].strip().capitalize(),'', float(S[16:24]),float(S[24:32]),float(S[32:40]), float(S[8:16]),'1',1,'I',float(S[40:46]),0,0,0,0,0,0] XYZ = Atom[6:9] Atom[10],Atom[11] = G2spc.SytSym(XYZ,SGData) Atom.append(ran.randint(0,sys.maxint)) Atoms.append(Atom) Volume = G2lat.calc_V(G2lat.cell2A(abc+angles)) if shNcof: shCoef = {} nRec = [i+1 for i in range((shNcof-1)/6+1)] for irec in nRec: ODkey = keyList[0][:6]+'OD'+'%3dA'%(irec) indx = EXPphase[ODkey].split() ODkey = ODkey[:-1]+'B' vals = EXPphase[ODkey].split() for i,val in enumerate(vals): key = 'C(%s,%s,%s)'%(indx[3*i],indx[3*i+1],indx[3*i+2]) shCoef[key] = float(val) textureData['SH Coeff'] = [False,shCoef] if not SGData: raise self.ImportException("No space group found in phase") if not abc: raise self.ImportException("No cell lengths found in phase") if not angles: raise self.ImportException("No cell angles found in phase") if not Atoms: raise self.ImportException("No atoms found in phase") Phase = G2IO.SetNewPhase(Name=PhaseName,SGData=SGData,cell=abc+angles+[Volume,]) general = Phase['General'] general['Type'] = Ptype if general['Type'] =='macromolecular': general['AtomPtrs'] = [6,4,10,12] else: general['AtomPtrs'] = [3,1,7,9] general['SH Texture'] = textureData Phase['Atoms'] = Atoms return Phase
def ImageCalibrate(self, data): 'Needs a doc string' import copy import ImageCalibrants as calFile print 'Image calibration:' time0 = time.time() ring = data['ring'] pixelSize = data['pixelSize'] scalex = 1000. / pixelSize[0] scaley = 1000. / pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] varyDict = data['varyList'] if varyDict['dist'] and varyDict['wave']: print 'ERROR - you can not simultaneously calibrate distance and wavelength' return False if len(ring) < 5: print 'ERROR - not enough inner ring points for ellipse' return False #fit start points on inner ring data['ellipses'] = [] data['rings'] = [] outE = FitEllipse(ring) fmt = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f' fmt2 = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f, chi**2: %.3f, Np: %d' if outE: print fmt % ('start ellipse: ', outE[0][0], outE[0][1], outE[1], outE[2][0], outE[2][1]) ellipse = outE else: return False #setup 360 points on that ring for "good" fit data['ellipses'].append(ellipse[:] + ('g', )) Ring = makeRing(1.0, ellipse, pixLimit, cutoff, scalex, scaley, self.ImageZ) if Ring: ellipse = FitEllipse(Ring) Ring = makeRing(1.0, ellipse, pixLimit, cutoff, scalex, scaley, self.ImageZ) #do again ellipse = FitEllipse(Ring) else: print '1st ring not sufficiently complete to proceed' return False if debug: print fmt2 % ('inner ring: ', ellipse[0][0], ellipse[0][1], ellipse[1], ellipse[2][0], ellipse[2][1], 0., len(Ring) ) #cent,phi,radii data['ellipses'].append(ellipse[:] + ('r', )) data['rings'].append(np.array(Ring)) G2plt.PlotImage(self, newImage=True) #setup for calibration data['rings'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] #generate reflection set Bravais, SGs, Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais, sg, cell in zip(Bravais, SGs, Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin, SGData, A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin, bravais, A) HKL += hkl HKL = G2lat.sortHKLd(HKL, True, False)[skip:] #set up 1st ring elcent, phi, radii = ellipse #from fit of 1st ring dsp = HKL[0][3] print '1st ring: try %.4f' % (dsp) if varyDict['dist']: wave = data['wavelength'] tth = 2.0 * asind(wave / (2. * dsp)) else: #varyDict['wave']! dist = data['distance'] tth = npatan2d(radii[0], dist) data['wavelength'] = wave = 2.0 * dsp * sind(tth / 2.0) Ring0 = makeRing(dsp, ellipse, 3, cutoff, scalex, scaley, self.ImageZ) ttth = nptand(tth) stth = npsind(tth) ctth = npcosd(tth) #1st estimate of tilt; assume ellipse - don't know sign though if varyDict['tilt']: tilt = npasind(np.sqrt(max(0., 1. - (radii[0] / radii[1])**2)) * ctth) if not tilt: print 'WARNING - selected ring was fitted as a circle' print ' - if detector was tilted we suggest you skip this ring - WARNING' else: tilt = data['tilt'] #1st estimate of dist: sample to detector normal to plane if varyDict['dist']: data['distance'] = dist = radii[0]**2 / (ttth * radii[1]) else: dist = data['distance'] if varyDict['tilt']: #ellipse to cone axis (x-ray beam); 2 choices depending on sign of tilt zdisp = radii[1] * ttth * tand(tilt) zdism = radii[1] * ttth * tand(-tilt) #cone axis position; 2 choices. Which is right? #NB: zdisp is || to major axis & phi is rotation of minor axis #thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)] centp = [elcent[0] + zdisp * sind(phi), elcent[1] - zdisp * cosd(phi)] centm = [elcent[0] + zdism * sind(phi), elcent[1] - zdism * cosd(phi)] #check get same ellipse parms either way #now do next ring; estimate either way & do a FitDetector each way; best fit is correct one fail = True i2 = 1 while fail: dsp = HKL[i2][3] print '2nd ring: try %.4f' % (dsp) tth = 2.0 * asind(wave / (2. * dsp)) ellipsep = GetEllipse2(tth, 0., dist, centp, tilt, phi) print fmt % ('plus ellipse :', ellipsep[0][0], ellipsep[0][1], ellipsep[1], ellipsep[2][0], ellipsep[2][1]) Ringp = makeRing(dsp, ellipsep, 3, cutoff, scalex, scaley, self.ImageZ) parmDict = { 'dist': dist, 'det-X': centp[0], 'det-Y': centp[1], 'tilt': tilt, 'phi': phi, 'wave': wave, 'dep': 0.0 } varyList = [item for item in varyDict if varyDict[item]] if len(Ringp) > 10: chip = FitDetector(np.array(Ring0 + Ringp), varyList, parmDict, True) tiltp = parmDict['tilt'] phip = parmDict['phi'] centp = [parmDict['det-X'], parmDict['det-Y']] fail = False else: chip = 1e6 ellipsem = GetEllipse2(tth, 0., dist, centm, -tilt, phi) print fmt % ('minus ellipse:', ellipsem[0][0], ellipsem[0][1], ellipsem[1], ellipsem[2][0], ellipsem[2][1]) Ringm = makeRing(dsp, ellipsem, 3, cutoff, scalex, scaley, self.ImageZ) if len(Ringm) > 10: parmDict['tilt'] *= -1 chim = FitDetector(np.array(Ring0 + Ringm), varyList, parmDict, True) tiltm = parmDict['tilt'] phim = parmDict['phi'] centm = [parmDict['det-X'], parmDict['det-Y']] fail = False else: chim = 1e6 if fail: i2 += 1 if chip < chim: data['tilt'] = tiltp data['center'] = centp data['rotation'] = phip else: data['tilt'] = tiltm data['center'] = centm data['rotation'] = phim data['ellipses'].append(ellipsep[:] + ('b', )) data['rings'].append(np.array(Ringp)) data['ellipses'].append(ellipsem[:] + ('r', )) data['rings'].append(np.array(Ringm)) G2plt.PlotImage(self, newImage=True) parmDict = { 'dist': data['distance'], 'det-X': data['center'][0], 'det-Y': data['center'][1], 'tilt': data['tilt'], 'phi': data['rotation'], 'wave': data['wavelength'], 'dep': data['DetDepth'] } varyList = [item for item in varyDict if varyDict[item]] data['rings'] = [] data['ellipses'] = [] for i, H in enumerate(HKL): dsp = H[3] tth = 2.0 * asind(wave / (2. * dsp)) if tth + abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break if debug: print 'HKLD:', H[:4], '2-theta: %.4f' % (tth) elcent, phi, radii = ellipse = GetEllipse(dsp, data) data['ellipses'].append(copy.deepcopy(ellipse + ('g', ))) if debug: print fmt % ('predicted ellipse:', elcent[0], elcent[1], phi, radii[0], radii[1]) Ring = makeRing(dsp, ellipse, pixLimit, cutoff, scalex, scaley, self.ImageZ) if Ring: data['rings'].append(np.array(Ring)) rings = np.concatenate((data['rings']), axis=0) if i: chisq = FitDetector(rings, varyList, parmDict, False) data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'], parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq elcent, phi, radii = ellipse = GetEllipse(dsp, data) if debug: print fmt2 % ('fitted ellipse: ', elcent[0], elcent[1], phi, radii[0], radii[1], chisq, len(rings)) data['ellipses'].append(copy.deepcopy(ellipse + ('r', ))) # G2plt.PlotImage(self,newImage=True) else: if debug: print 'insufficient number of points in this ellipse to fit' # break G2plt.PlotImage(self, newImage=True) fullSize = len(self.ImageZ) / scalex if 2 * radii[1] < .9 * fullSize: print 'Are all usable rings (>25% visible) used? Try reducing Min ring I/Ib' N = len(data['ellipses']) if N > 2: FitDetector(rings, varyList, parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'], parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] for H in HKL[:N]: ellipse = GetEllipse(H[3], data) data['ellipses'].append(copy.deepcopy(ellipse + ('b', ))) print 'calibration time = ', time.time() - time0 G2plt.PlotImage(self, newImage=True) return True
def Readrmc6fPhase(self, filename, parent=None): '''Read a phase from a rmc6f file. ''' self.errors = 'Error opening file' fp = open(filename, 'Ur') Phase = {} Title = os.path.split(filename) G2G.SaveGPXdirectory(Title[0]) Title = os.path.splitext(Title[1])[0] Atoms = [] S = fp.readline() line = 1 SGData = None cell = None IA = 'I' Uiso = 0.01 Uij = [0. for i in range(6)] while S: self.errors = 'Error reading at line ' + str(line) Atom = [] if 'Cell' in S[:4]: cellRec = S.split(':')[1].split() abc = cellRec[:3] angles = cellRec[3:] cell = [ float(abc[0]), float(abc[1]), float(abc[2]), float(angles[0]), float(angles[1]), float(angles[2]) ] Volume = G2lat.calc_V(G2lat.cell2A(cell)) AA, AB = G2lat.cell2AB(cell) SGData = G2obj.P1SGData # P 1 elif 'Atoms' in S[:5]: S = fp.readline()[:-1] AtRec = S.split() for ix, s in enumerate(AtRec): if '.' in s: break #is points at x while S: AtRec = S.split() Atype = AtRec[1] Aname = Atype + AtRec[0] Afrac = 1.0 x, y, z = AtRec[ix:ix + 3] XYZ = np.array([float(x), float(y), float(z)]) SytSym, Mult = '1', 1 Atom = [ Aname, Atype, '', XYZ[0], XYZ[1], XYZ[2], Afrac, SytSym, Mult, IA, Uiso ] Atom += Uij Atom.append(ran.randint(0, sys.maxsize)) Atoms.append(Atom) S = fp.readline()[:-1] S = fp.readline() line += 1 fp.close() self.errors = 'Error after read complete' Phase = G2obj.SetNewPhase(Name='RMCProfile phase', SGData=SGData, cell=cell + [ Volume, ]) Phase['General']['Name'] = Title Phase['General']['Type'] = 'nuclear' Phase['General']['AtomPtrs'] = [3, 1, 7, 9] Phase['Atoms'] = Atoms return Phase