[1084] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | ########### SVN repository information ################### |
---|
[1102] | 4 | # $Date: 2015-06-22 16:42:30 +0000 (Mon, 22 Jun 2015) $ |
---|
| 5 | # $Author: vondreele $ |
---|
| 6 | # $Revision: 1902 $ |
---|
| 7 | # $URL: trunk/exports/G2export_examples.py $ |
---|
| 8 | # $Id: G2export_examples.py 1902 2015-06-22 16:42:30Z vondreele $ |
---|
[1084] | 9 | ########### SVN repository information ################### |
---|
| 10 | ''' |
---|
[1123] | 11 | *Module G2export_examples: Examples* |
---|
| 12 | ------------------------------------------- |
---|
| 13 | |
---|
| 14 | Code to demonstrate how GSAS-II data export routines are created. The |
---|
| 15 | classes defined here, :class:`ExportPhaseText`, |
---|
| 16 | :class:`ExportSingleText`, :class:`ExportPowderReflText`, |
---|
| 17 | and :class:`ExportPowderText` each demonstrate a different type |
---|
| 18 | of export. Also see :class:`G2export_map.ExportMapASCII` for an |
---|
| 19 | example of a map export. |
---|
| 20 | |
---|
| 21 | ''' |
---|
[1084] | 22 | import os.path |
---|
[1791] | 23 | import numpy as np |
---|
[1084] | 24 | import GSASIIpath |
---|
[1102] | 25 | GSASIIpath.SetVersionNumber("$Revision: 1902 $") |
---|
[1084] | 26 | import GSASIIIO as G2IO |
---|
[1236] | 27 | import GSASIIpy3 as G2py3 |
---|
[1084] | 28 | #import GSASIIgrid as G2gd |
---|
[1102] | 29 | #import GSASIIstrIO as G2stIO |
---|
| 30 | import GSASIImath as G2mth |
---|
[1773] | 31 | import GSASIIpwd as G2pwd |
---|
[1102] | 32 | #import GSASIIlattice as G2lat |
---|
| 33 | #import GSASIIspc as G2spc |
---|
[1084] | 34 | #import GSASIIphsGUI as G2pg |
---|
| 35 | #import GSASIIstrMain as G2stMn |
---|
| 36 | |
---|
[1102] | 37 | class ExportPhaseText(G2IO.ExportBaseclass): |
---|
| 38 | '''Used to create a text file for a phase |
---|
[1084] | 39 | |
---|
| 40 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
| 41 | ''' |
---|
| 42 | def __init__(self,G2frame): |
---|
| 43 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
| 44 | G2frame=G2frame, |
---|
[1115] | 45 | formatName = 'Text file', |
---|
[1102] | 46 | extension='.txt', |
---|
| 47 | longFormatName = 'Export phase as text file' |
---|
[1084] | 48 | ) |
---|
| 49 | self.exporttype = ['phase'] |
---|
[1102] | 50 | self.multiple = True # allow multiple phases to be selected |
---|
[1084] | 51 | |
---|
| 52 | def Exporter(self,event=None): |
---|
[1102] | 53 | '''Export a phase as a text file |
---|
[1084] | 54 | ''' |
---|
| 55 | # the export process starts here |
---|
[1115] | 56 | self.InitExport(event) |
---|
[1084] | 57 | # load all of the tree into a set of dicts |
---|
| 58 | self.loadTree() |
---|
| 59 | # create a dict with refined values and their uncertainties |
---|
| 60 | self.loadParmDict() |
---|
[1261] | 61 | if self.ExportSelect(): return # set export parameters; prompt for file name |
---|
| 62 | self.OpenFile() |
---|
[1102] | 63 | # if more than one format is selected, put them into a single file |
---|
[1084] | 64 | for phasenam in self.phasenam: |
---|
| 65 | phasedict = self.Phases[phasenam] # pointer to current phase info |
---|
| 66 | i = self.Phases[phasenam]['pId'] |
---|
[1102] | 67 | self.Write('\n'+80*'=') |
---|
| 68 | self.Write("Phase "+str(phasenam)+" from "+str(self.G2frame.GSASprojectfile)) |
---|
| 69 | self.Write("\nSpace group = "+str(phasedict['General']['SGData']['SpGrp'].strip())) |
---|
| 70 | # get cell parameters & print them |
---|
| 71 | cellList,cellSig = self.GetCell(phasenam) |
---|
| 72 | prevsig = 0 |
---|
| 73 | for lbl,defsig,val,sig in zip( |
---|
| 74 | ['a','b','c','alpha','beta ','gamma','volume'], |
---|
| 75 | 3*[-0.00001] + 3*[-0.001] + [-0.01], # sign values to use when no sigma |
---|
| 76 | cellList,cellSig |
---|
| 77 | ): |
---|
| 78 | if sig: |
---|
| 79 | txt = G2mth.ValEsd(val,sig) |
---|
| 80 | prevsig = -sig # use this as the significance for next value |
---|
| 81 | else: |
---|
| 82 | txt = G2mth.ValEsd(val,min(defsig,prevsig),True) |
---|
| 83 | self.Write(lbl+' = '+txt) |
---|
| 84 | # get atoms and print them in nice columns |
---|
| 85 | AtomsList = self.GetAtoms(phasenam) |
---|
| 86 | fmt = "{:8s} {:4s} {:4s} {:12s} {:12s} {:12s} {:10s} {:10s}" |
---|
| 87 | self.Write('\nAtoms\n'+80*'-') |
---|
| 88 | self.Write(fmt.format("label","elem","mult","x","y","z","frac","Uiso")) |
---|
| 89 | self.Write(80*'-') |
---|
| 90 | aniso = False |
---|
| 91 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
| 92 | vals = [lbl,typ,str(mult)] |
---|
| 93 | if xyz[3][0] == 0: continue |
---|
| 94 | for val,sig in xyz: |
---|
| 95 | vals.append(G2mth.ValEsd(val,sig)) |
---|
| 96 | if len(td) == 1: |
---|
| 97 | vals.append(G2mth.ValEsd(td[0][0],td[0][1])) |
---|
| 98 | else: |
---|
| 99 | vals.append("aniso") |
---|
| 100 | aniso = True |
---|
| 101 | self.Write(fmt.format(*vals)) |
---|
| 102 | # print anisotropic values, if any |
---|
| 103 | if aniso: |
---|
| 104 | self.Write('\nAnisotropic parameters') |
---|
| 105 | self.Write(80*'-') |
---|
| 106 | fmt = "{:8s} {:4s} {:10s} {:10s} {:10s} {:10s} {:10s} {:10s}" |
---|
| 107 | self.Write(fmt.format("label","elem",'U11','U22','U33','U12','U13','U23')) |
---|
| 108 | self.Write(80*'-') |
---|
| 109 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
| 110 | if len(td) == 1: continue |
---|
| 111 | if xyz[3][0] == 0: continue |
---|
| 112 | vals = [lbl,typ] |
---|
| 113 | for val,sig in td: |
---|
| 114 | vals.append(G2mth.ValEsd(val,sig)) |
---|
| 115 | self.Write(fmt.format(*vals)) |
---|
[1261] | 116 | print('Phase '+str(phasenam)+' written to file '+str(self.fullpath)) |
---|
[1102] | 117 | self.CloseFile() |
---|
[1084] | 118 | |
---|
[1102] | 119 | class ExportPowderText(G2IO.ExportBaseclass): |
---|
| 120 | '''Used to create a text file for a powder data set |
---|
| 121 | |
---|
| 122 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
| 123 | ''' |
---|
| 124 | def __init__(self,G2frame): |
---|
| 125 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
| 126 | G2frame=G2frame, |
---|
[1115] | 127 | formatName = 'Text file', |
---|
[1102] | 128 | extension='.txt', |
---|
| 129 | longFormatName = 'Export powder data as text file' |
---|
| 130 | ) |
---|
| 131 | self.exporttype = ['powder'] |
---|
| 132 | self.multiple = False # only allow one histogram to be selected |
---|
| 133 | |
---|
| 134 | def Exporter(self,event=None): |
---|
| 135 | '''Export a set of powder data as a text file |
---|
| 136 | ''' |
---|
| 137 | # the export process starts here |
---|
[1115] | 138 | self.InitExport(event) |
---|
[1102] | 139 | # load all of the tree into a set of dicts |
---|
| 140 | self.loadTree() |
---|
[1115] | 141 | if self.ExportSelect( # set export parameters |
---|
[1261] | 142 | AskFile='default' # base name on the GPX file name |
---|
[1102] | 143 | ): return |
---|
| 144 | self.OpenFile() |
---|
| 145 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
| 146 | histblk = self.Histograms[hist] |
---|
[1236] | 147 | hfmt = 5*"{:12s} " |
---|
| 148 | digitList = 2*((13,3),) + ((13,5),) + 2*((13,3),) |
---|
| 149 | |
---|
[1102] | 150 | self.Write(hfmt.format("x","y_obs","weight","y_calc","y_bkg")) |
---|
[1236] | 151 | for vallist in zip(histblk['Data'][0], |
---|
| 152 | histblk['Data'][1], |
---|
| 153 | histblk['Data'][2], |
---|
| 154 | histblk['Data'][3], |
---|
| 155 | histblk['Data'][4], |
---|
| 156 | #histblk['Data'][5], |
---|
| 157 | ): |
---|
| 158 | strg = '' |
---|
| 159 | for val,digits in zip(vallist,digitList): |
---|
| 160 | strg += G2py3.FormatPadValue(val,digits) |
---|
| 161 | self.Write(strg) |
---|
[1102] | 162 | self.CloseFile() |
---|
[1261] | 163 | print(str(hist)+' written to file '+str(self.fullpath)) |
---|
[1236] | 164 | |
---|
[1102] | 165 | class ExportPowderReflText(G2IO.ExportBaseclass): |
---|
| 166 | '''Used to create a text file of reflections from a powder data set |
---|
| 167 | |
---|
| 168 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
| 169 | ''' |
---|
| 170 | def __init__(self,G2frame): |
---|
| 171 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
| 172 | G2frame=G2frame, |
---|
| 173 | formatName = 'reflection list as text', |
---|
| 174 | extension='.txt', |
---|
| 175 | longFormatName = 'Export powder reflection list as a text file' |
---|
| 176 | ) |
---|
| 177 | self.exporttype = ['powder'] |
---|
| 178 | self.multiple = False # only allow one histogram to be selected |
---|
| 179 | |
---|
| 180 | def Exporter(self,event=None): |
---|
| 181 | '''Export a set of powder reflections as a text file |
---|
| 182 | ''' |
---|
[1115] | 183 | self.InitExport(event) |
---|
[1102] | 184 | # load all of the tree into a set of dicts |
---|
| 185 | self.loadTree() |
---|
[1115] | 186 | if self.ExportSelect( # set export parameters |
---|
[1261] | 187 | AskFile='default' # base name on the GPX file name |
---|
[1102] | 188 | ): return |
---|
| 189 | self.OpenFile() |
---|
| 190 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
[1836] | 191 | self.Write('\nHistogram '+hist) |
---|
[1102] | 192 | histblk = self.Histograms[hist] |
---|
| 193 | for phasenam in histblk['Reflection Lists']: |
---|
[1762] | 194 | phasDict = histblk['Reflection Lists'][phasenam] |
---|
| 195 | tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]] |
---|
[1102] | 196 | self.Write('\nPhase '+str(phasenam)) |
---|
[1762] | 197 | if phasDict.get('Super',False): |
---|
[1836] | 198 | self.Write(96*'=') |
---|
[1762] | 199 | hklfmt = "{:.0f},{:.0f},{:.0f},{:.0f}" |
---|
[1836] | 200 | hfmt = "{:>10s} {:>8s} {:>12s} {:>12s} {:>7s} {:>6s} {:>8s} {:>8s} {:>8s} {:>8s}" |
---|
[1791] | 201 | if 'T' in phasDict['Type']: |
---|
[1836] | 202 | fmt = "{:>10s} {:8.3f} {:12.3f} {:12.3f} {:7.2f} {:6.0f} {:8.3f} {:8.3f} {:8.3f} {:8.4f}" |
---|
[1791] | 203 | else: |
---|
[1836] | 204 | fmt = "{:>10s} {:8.3f} {:12.3f} {:12.3f} {:7.2f} {:6.0f} {:8.5f} {:8.5f} {:8.5f} {:8.4f}" |
---|
| 205 | self.Write(hfmt.format("h,k,l,m",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo")) |
---|
| 206 | self.Write(96*'=') |
---|
[1762] | 207 | refList = phasDict['RefList'] |
---|
| 208 | for refItem in refList: |
---|
[1791] | 209 | if 'T' in phasDict['Type']: |
---|
[1836] | 210 | h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,x,x,x,x,prfo = refItem[:17] |
---|
[1902] | 211 | FWHM = 2.*G2pwd.getgamFW(gam,sig) |
---|
[1836] | 212 | self.Write(fmt.format(hklfmt.format(h,k,l,m),pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,prfo)) |
---|
[1791] | 213 | else: |
---|
[1836] | 214 | h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,x,prfo = refItem[:14] |
---|
[1902] | 215 | FWHM = 2.*G2pwd.getgamFW(gam,sig) |
---|
[1791] | 216 | self.Write(fmt.format(hklfmt.format(h,k,l,m),pos,Fobs,Fcalc,phase,mult, \ |
---|
[1836] | 217 | np.sqrt(max(sig,0.0001))/100.,gam/100.,FWHM/100.,prfo)) |
---|
[1762] | 218 | else: |
---|
[1836] | 219 | self.Write(94*'=') |
---|
[1762] | 220 | hklfmt = "{:.0f},{:.0f},{:.0f}" |
---|
[1836] | 221 | hfmt = "{:>8s} {:>8s} {:>12s} {:>12s} {:>7s} {:>6s} {:>8s} {:>8s} {:>8s} {:>8s}" |
---|
[1791] | 222 | if 'T' in phasDict['Type']: |
---|
[1836] | 223 | fmt = "{:>8s} {:8.3f} {:12.3f} {:12.3f} {:7.2f} {:6.0f} {:8.3f} {:8.3f} {:8.3f} {:8.4f}" |
---|
[1791] | 224 | else: |
---|
[1836] | 225 | fmt = "{:>8s} {:8.3f} {:12.3f} {:12.3f} {:7.2f} {:6.0f} {:8.5f} {:8.5f} {:8.5f} {:8.4f}" |
---|
| 226 | self.Write(hfmt.format("h,k,l",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo")) |
---|
| 227 | self.Write(94*'=') |
---|
[1762] | 228 | refList = phasDict['RefList'] |
---|
| 229 | for refItem in refList: |
---|
[1791] | 230 | if 'T' in phasDict['Type']: |
---|
[1836] | 231 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,x,x,x,x,prfo = refItem[:16] |
---|
[1902] | 232 | FWHM = 2.*G2pwd.getgamFW(gam,sig) |
---|
| 233 | self.Write(fmt.format(hklfmt.format(h,k,l),pos,Fobs,Fcalc,phase,mult,np.sqrt(max(sig,0.0001)),gam,FWHM,prfo)) |
---|
[1791] | 234 | else: |
---|
[1836] | 235 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,x,prfo = refItem[:13] |
---|
[1902] | 236 | FWHM = 2.*G2pwd.getgamFW(gam,sig) |
---|
[1791] | 237 | self.Write(fmt.format(hklfmt.format(h,k,l),pos,Fobs,Fcalc,phase,mult, \ |
---|
[1836] | 238 | np.sqrt(max(sig,0.0001))/100.,gam/100.,FWHM/100.,prfo)) |
---|
[1102] | 239 | self.CloseFile() |
---|
[1261] | 240 | print(str(hist)+'reflections written to file '+str(self.fullpath)) |
---|
[1102] | 241 | |
---|
| 242 | class ExportSingleText(G2IO.ExportBaseclass): |
---|
| 243 | '''Used to create a text file with single crystal reflection data |
---|
| 244 | |
---|
| 245 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
| 246 | ''' |
---|
| 247 | def __init__(self,G2frame): |
---|
| 248 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
| 249 | G2frame=G2frame, |
---|
[1115] | 250 | formatName = 'Text file', |
---|
[1102] | 251 | extension='.txt', |
---|
| 252 | longFormatName = 'Export reflection list as a text file' |
---|
| 253 | ) |
---|
| 254 | self.exporttype = ['single'] |
---|
| 255 | self.multiple = False # only allow one histogram to be selected |
---|
| 256 | |
---|
| 257 | def Exporter(self,event=None): |
---|
| 258 | '''Export a set of single crystal data as a text file |
---|
| 259 | ''' |
---|
| 260 | # the export process starts here |
---|
[1115] | 261 | self.InitExport(event) |
---|
[1102] | 262 | # load all of the tree into a set of dicts |
---|
| 263 | self.loadTree() |
---|
[1115] | 264 | if self.ExportSelect( # set export parameters |
---|
[1261] | 265 | AskFile='default' # base name on the GPX file name |
---|
[1102] | 266 | ): return |
---|
| 267 | self.OpenFile() |
---|
| 268 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
| 269 | histblk = self.Histograms[hist] |
---|
| 270 | hklfmt = "{:.0f},{:.0f},{:.0f}" |
---|
| 271 | hfmt = "{:>10s} {:>8s} {:>12s} {:>12s} {:>12s} {:>7s} {:>6s}" |
---|
| 272 | fmt = "{:>10s} {:8.3f} {:12.2f} {:12.4f} {:12.2f} {:7.2f} {:6.0f}" |
---|
| 273 | self.Write(80*'=') |
---|
| 274 | self.Write(hfmt.format("h,k,l","d-space","F_obs","sig(Fobs)","F_calc","phase","mult")) |
---|
| 275 | self.Write(80*'=') |
---|
| 276 | for ( |
---|
[1127] | 277 | h,k,l,mult,dsp,Fobs,sigFobs,Fcalc,FobsT,FcalcT,phase,Icorr |
---|
| 278 | ) in histblk['Data']['RefList']: |
---|
[1102] | 279 | self.Write(fmt.format(hklfmt.format(h,k,l),dsp,Fobs,sigFobs,Fcalc,phase,mult)) |
---|
| 280 | self.CloseFile() |
---|
[1261] | 281 | print(str(hist)+' written to file '+str(self.fullpath)) |
---|
[1102] | 282 | |
---|