Changeset 3999 for trunk/GSASIIfiles.py


Ignore:
Timestamp:
May 27, 2019 4:07:46 PM (2 years ago)
Author:
toby
Message:

reorg PDF computation/save as non-GUI code, add PDF to scriptable, doc build fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIfiles.py

    r3974 r3999  
    826826        masks[key] = masks.get(key,[])
    827827        masks[key] = [i for i in masks[key] if len(i)]
     828
     829def PDFWrite(PDFentry,fileroot,PDFsaves,PDFControls,Inst={},Limits=[]):
     830    '''Write PDF-related data (G(r), S(Q),...) into files, as
     831    selected.
     832
     833    :param str PDFentry: name of the PDF entry in the tree. This is
     834      used for comments in the file specifying where it came from;
     835      it can be arbitrary
     836    :param str fileroot: name of file(s) to be written. The extension
     837      will be ignored.
     838    :param list PDFsaves: flags that determine what type of file will be
     839      written:
     840      PDFsaves[0], if True writes a I(Q) file with a .iq extension
     841      PDFsaves[1], if True writes a S(Q) file with a .sq extension
     842      PDFsaves[2], if True writes a F(Q) file with a .fq extension
     843      PDFsaves[3], if True writes a G(r) file with a .gr extension
     844      PDFsaves[4], if True writes G(r) in a pdfGUI input file with
     845      a .gr extension. Note that if PDFsaves[3] and PDFsaves[4] are
     846      both True, the pdfGUI overwrites the G(r) file.
     847    :param dict PDFControls: The PDF parameters and computed results
     848    :param dict Inst: Instrument parameters from the PDWR entry used
     849      to compute the PDF. Needed only when PDFsaves[4] is True.
     850    :param list Limits: Computation limits from the PDWR entry used
     851      to compute the PDF. Needed only when PDFsaves[4] is True.
     852    '''
     853    import scipy.interpolate as scintp
     854    fileroot = os.path.splitext(fileroot)[0]
     855    if PDFsaves[0]:     #I(Q)
     856        iqfilename = fileroot+'.iq'
     857        iqdata = PDFControls['I(Q)'][1]
     858        iqfxn = scintp.interp1d(iqdata[0],iqdata[1],kind='linear')
     859        iqfile = open(iqfilename,'w')
     860        iqfile.write('#T I(Q) %s\n'%(PDFentry))
     861        iqfile.write('#L Q     I(Q)\n')
     862        qnew = np.arange(iqdata[0][0],iqdata[0][-1],0.005)
     863        iqnew = zip(qnew,iqfxn(qnew))
     864        for q,iq in iqnew:
     865            iqfile.write("%15.6g %15.6g\n" % (q,iq))
     866        iqfile.close()
     867        print (' I(Q) saved to: '+iqfilename)
     868
     869    if PDFsaves[1]:     #S(Q)
     870        sqfilename = fileroot+'.sq'
     871        sqdata = PDFControls['S(Q)'][1]
     872        sqfxn = scintp.interp1d(sqdata[0],sqdata[1],kind='linear')
     873        sqfile = open(sqfilename,'w')
     874        sqfile.write('#T S(Q) %s\n'%(PDFentry))
     875        sqfile.write('#L Q     S(Q)\n')
     876        qnew = np.arange(sqdata[0][0],sqdata[0][-1],0.005)
     877        sqnew = zip(qnew,sqfxn(qnew))
     878        for q,sq in sqnew:
     879            sqfile.write("%15.6g %15.6g\n" % (q,sq))
     880        sqfile.close()
     881        print (' S(Q) saved to: '+sqfilename)
     882
     883    if PDFsaves[2]:     #F(Q)
     884        fqfilename = fileroot+'.fq'
     885        fqdata = PDFControls['F(Q)'][1]
     886        fqfxn = scintp.interp1d(fqdata[0],fqdata[1],kind='linear')
     887        fqfile = open(fqfilename,'w')
     888        fqfile.write('#T F(Q) %s\n'%(PDFentry))
     889        fqfile.write('#L Q     F(Q)\n')
     890        qnew = np.arange(fqdata[0][0],fqdata[0][-1],0.005)
     891        fqnew = zip(qnew,fqfxn(qnew))
     892        for q,fq in fqnew:
     893            fqfile.write("%15.6g %15.6g\n" % (q,fq))
     894        fqfile.close()
     895        print (' F(Q) saved to: '+fqfilename)
     896
     897    if PDFsaves[3]:     #G(R)
     898        grfilename = fileroot+'.gr'
     899        grdata = PDFControls['G(R)'][1]
     900        grfxn = scintp.interp1d(grdata[0],grdata[1],kind='linear')
     901        grfile = open(grfilename,'w')
     902        grfile.write('#T G(R) %s\n'%(PDFentry))
     903        grfile.write('#L R     G(R)\n')
     904        rnew = np.arange(grdata[0][0],grdata[0][-1],0.010)
     905        grnew = zip(rnew,grfxn(rnew))
     906        for r,gr in grnew:
     907            grfile.write("%15.6g %15.6g\n" % (r,gr))
     908        grfile.close()
     909        print (' G(R) saved to: '+grfilename)
     910
     911    if PDFsaves[4]: #pdfGUI file for G(R)
     912        import GSASIImath as G2mth
     913        import GSASIIlattice as G2lat       
     914        grfilename = fileroot+'.gr'
     915        grdata = PDFControls['G(R)'][1]
     916        qdata = PDFControls['I(Q)'][1][0]
     917        grfxn = scintp.interp1d(grdata[0],grdata[1],kind='linear')
     918        grfile = open(grfilename,'w')
     919        rnew = np.arange(grdata[0][0],grdata[0][-1],0.010)
     920        grnew = zip(rnew,grfxn(rnew))
     921
     922        grfile.write('[DEFAULT]\n')
     923        grfile.write('\n')
     924        grfile.write('version = GSAS-II-v'+str(GSASIIpath.GetVersionNumber())+'\n')
     925        grfile.write('\n')
     926        grfile.write('# input and output specifications\n')
     927        grfile.write('dataformat = Qnm\n')
     928        grfile.write('inputfile = %s\n'%(PDFControls['Sample']['Name']))
     929        grfile.write('backgroundfile = %s\n'%(PDFControls['Sample Bkg.']['Name']))
     930        grfile.write('outputtype = gr\n')
     931        grfile.write('\n')
     932        grfile.write('# PDF calculation setup\n')
     933        if 'x' in Inst['Type']:
     934            grfile.write('mode = %s\n'%('xray'))
     935        elif 'N' in Inst['Type']:
     936            grfile.write('mode = %s\n'%('neutron'))
     937        wave = G2mth.getMeanWave(Inst)
     938        grfile.write('wavelength = %.5f\n'%(wave))
     939        formula = ''
     940        for el in PDFControls['ElList']:
     941            formula += el
     942            num = PDFControls['ElList'][el]['FormulaNo']
     943            if num == round(num):
     944                formula += '%d'%(int(num))
     945            else:
     946                formula += '%.2f'%(num)
     947        grfile.write('composition = %s\n'%(formula))
     948        grfile.write('bgscale = %.3f\n'%(-PDFControls['Sample Bkg.']['Mult']))
     949        highQ = 2.*np.pi/G2lat.Pos2dsp(Inst,Limits[1][1])
     950        grfile.write('qmaxinst = %.2f\n'%(highQ))
     951        grfile.write('qmin = %.5f\n'%(qdata[0]))
     952        grfile.write('qmax = %.4f\n'%(qdata[-1]))
     953        grfile.write('rmin = %.2f\n'%(PDFControls['Rmin']))
     954        grfile.write('rmax = %.2f\n'%(PDFControls['Rmax']))
     955        grfile.write('rstep = 0.01\n')
     956
     957
     958        grfile.write('\n')
     959        grfile.write('# End of config '+63*'-')
     960        grfile.write('\n')
     961        grfile.write('#### start data\n')
     962        grfile.write('#S 1\n')
     963        grfile.write('#L r($\AA$)  G($\AA^{-2}$)\n')           
     964        for r,gr in grnew:
     965            grfile.write("%15.2F %15.6F\n" % (r,gr))
     966        grfile.close()
     967        print (' G(R) saved to: '+grfilename)
Note: See TracChangeset for help on using the changeset viewer.