1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2013-10-25 19:24:22 +0000 (Fri, 25 Oct 2013) $ |
---|
5 | # $Author: toby $ |
---|
6 | # $Revision: 1123 $ |
---|
7 | # $URL: trunk/exports/G2export_examples.py $ |
---|
8 | # $Id: G2export_examples.py 1123 2013-10-25 19:24:22Z toby $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | ''' |
---|
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 | ''' |
---|
22 | import os.path |
---|
23 | import GSASIIpath |
---|
24 | GSASIIpath.SetVersionNumber("$Revision: 1123 $") |
---|
25 | import GSASIIIO as G2IO |
---|
26 | #import GSASIIgrid as G2gd |
---|
27 | #import GSASIIstrIO as G2stIO |
---|
28 | import GSASIImath as G2mth |
---|
29 | #import GSASIIlattice as G2lat |
---|
30 | #import GSASIIspc as G2spc |
---|
31 | #import GSASIIphsGUI as G2pg |
---|
32 | #import GSASIIstrMain as G2stMn |
---|
33 | |
---|
34 | class ExportPhaseText(G2IO.ExportBaseclass): |
---|
35 | '''Used to create a text file for a phase |
---|
36 | |
---|
37 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
38 | ''' |
---|
39 | def __init__(self,G2frame): |
---|
40 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
41 | G2frame=G2frame, |
---|
42 | formatName = 'Text file', |
---|
43 | extension='.txt', |
---|
44 | longFormatName = 'Export phase as text file' |
---|
45 | ) |
---|
46 | self.exporttype = ['phase'] |
---|
47 | self.multiple = True # allow multiple phases to be selected |
---|
48 | |
---|
49 | def Exporter(self,event=None): |
---|
50 | '''Export a phase as a text file |
---|
51 | ''' |
---|
52 | # the export process starts here |
---|
53 | self.InitExport(event) |
---|
54 | # load all of the tree into a set of dicts |
---|
55 | self.loadTree() |
---|
56 | # create a dict with refined values and their uncertainties |
---|
57 | self.loadParmDict() |
---|
58 | if self.ExportSelect( # set export parameters |
---|
59 | AskFile=True # prompt the user for a file name |
---|
60 | ): return |
---|
61 | self.OpenFile(self.filename) |
---|
62 | # if more than one format is selected, put them into a single file |
---|
63 | for phasenam in self.phasenam: |
---|
64 | phasedict = self.Phases[phasenam] # pointer to current phase info |
---|
65 | i = self.Phases[phasenam]['pId'] |
---|
66 | self.Write('\n'+80*'=') |
---|
67 | self.Write("Phase "+str(phasenam)+" from "+str(self.G2frame.GSASprojectfile)) |
---|
68 | self.Write("\nSpace group = "+str(phasedict['General']['SGData']['SpGrp'].strip())) |
---|
69 | # get cell parameters & print them |
---|
70 | cellList,cellSig = self.GetCell(phasenam) |
---|
71 | prevsig = 0 |
---|
72 | for lbl,defsig,val,sig in zip( |
---|
73 | ['a','b','c','alpha','beta ','gamma','volume'], |
---|
74 | 3*[-0.00001] + 3*[-0.001] + [-0.01], # sign values to use when no sigma |
---|
75 | cellList,cellSig |
---|
76 | ): |
---|
77 | if sig: |
---|
78 | txt = G2mth.ValEsd(val,sig) |
---|
79 | prevsig = -sig # use this as the significance for next value |
---|
80 | else: |
---|
81 | txt = G2mth.ValEsd(val,min(defsig,prevsig),True) |
---|
82 | self.Write(lbl+' = '+txt) |
---|
83 | # get atoms and print them in nice columns |
---|
84 | AtomsList = self.GetAtoms(phasenam) |
---|
85 | fmt = "{:8s} {:4s} {:4s} {:12s} {:12s} {:12s} {:10s} {:10s}" |
---|
86 | self.Write('\nAtoms\n'+80*'-') |
---|
87 | self.Write(fmt.format("label","elem","mult","x","y","z","frac","Uiso")) |
---|
88 | self.Write(80*'-') |
---|
89 | aniso = False |
---|
90 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
91 | vals = [lbl,typ,str(mult)] |
---|
92 | if xyz[3][0] == 0: continue |
---|
93 | for val,sig in xyz: |
---|
94 | vals.append(G2mth.ValEsd(val,sig)) |
---|
95 | if len(td) == 1: |
---|
96 | vals.append(G2mth.ValEsd(td[0][0],td[0][1])) |
---|
97 | else: |
---|
98 | vals.append("aniso") |
---|
99 | aniso = True |
---|
100 | self.Write(fmt.format(*vals)) |
---|
101 | # print anisotropic values, if any |
---|
102 | if aniso: |
---|
103 | self.Write('\nAnisotropic parameters') |
---|
104 | self.Write(80*'-') |
---|
105 | fmt = "{:8s} {:4s} {:10s} {:10s} {:10s} {:10s} {:10s} {:10s}" |
---|
106 | self.Write(fmt.format("label","elem",'U11','U22','U33','U12','U13','U23')) |
---|
107 | self.Write(80*'-') |
---|
108 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
109 | if len(td) == 1: continue |
---|
110 | if xyz[3][0] == 0: continue |
---|
111 | vals = [lbl,typ] |
---|
112 | for val,sig in td: |
---|
113 | vals.append(G2mth.ValEsd(val,sig)) |
---|
114 | self.Write(fmt.format(*vals)) |
---|
115 | print('Phase '+str(phasenam)+' written to file '+str(self.filename)) |
---|
116 | self.CloseFile() |
---|
117 | |
---|
118 | class ExportPowderText(G2IO.ExportBaseclass): |
---|
119 | '''Used to create a text file for a powder data set |
---|
120 | |
---|
121 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
122 | ''' |
---|
123 | def __init__(self,G2frame): |
---|
124 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
125 | G2frame=G2frame, |
---|
126 | formatName = 'Text file', |
---|
127 | extension='.txt', |
---|
128 | longFormatName = 'Export powder data as text file' |
---|
129 | ) |
---|
130 | self.exporttype = ['powder'] |
---|
131 | self.multiple = False # only allow one histogram to be selected |
---|
132 | |
---|
133 | def Exporter(self,event=None): |
---|
134 | '''Export a set of powder data as a text file |
---|
135 | ''' |
---|
136 | # the export process starts here |
---|
137 | self.InitExport(event) |
---|
138 | # load all of the tree into a set of dicts |
---|
139 | self.loadTree() |
---|
140 | if self.ExportSelect( # set export parameters |
---|
141 | AskFile=False # use the default file name |
---|
142 | #AskFile=True |
---|
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] |
---|
147 | fmt = 2*"{:12.3f} " + "{:12.5f} " + 2*"{:12.3f} " |
---|
148 | hfmt = 5*"{:>12s} " |
---|
149 | self.Write(hfmt.format("x","y_obs","weight","y_calc","y_bkg")) |
---|
150 | for x,yobs,yw,ycalc,ybkg,obsmcalc in zip(histblk['Data'][0], |
---|
151 | histblk['Data'][1], |
---|
152 | histblk['Data'][2], |
---|
153 | histblk['Data'][3], |
---|
154 | histblk['Data'][4], |
---|
155 | histblk['Data'][5], |
---|
156 | ): |
---|
157 | self.Write(fmt.format(x,yobs,yw,ycalc,ybkg)) |
---|
158 | self.CloseFile() |
---|
159 | print(str(hist)+' written to file '+str(self.filename)) |
---|
160 | class ExportPowderReflText(G2IO.ExportBaseclass): |
---|
161 | '''Used to create a text file of reflections from a powder data set |
---|
162 | |
---|
163 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
164 | ''' |
---|
165 | def __init__(self,G2frame): |
---|
166 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
167 | G2frame=G2frame, |
---|
168 | formatName = 'reflection list as text', |
---|
169 | extension='.txt', |
---|
170 | longFormatName = 'Export powder reflection list as a text file' |
---|
171 | ) |
---|
172 | self.exporttype = ['powder'] |
---|
173 | self.multiple = False # only allow one histogram to be selected |
---|
174 | |
---|
175 | def Exporter(self,event=None): |
---|
176 | '''Export a set of powder reflections as a text file |
---|
177 | ''' |
---|
178 | self.InitExport(event) |
---|
179 | # load all of the tree into a set of dicts |
---|
180 | self.loadTree() |
---|
181 | if self.ExportSelect( # set export parameters |
---|
182 | AskFile=False # use the default file name |
---|
183 | #AskFile=True |
---|
184 | ): return |
---|
185 | self.OpenFile() |
---|
186 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
187 | histblk = self.Histograms[hist] |
---|
188 | hklfmt = "{:.0f},{:.0f},{:.0f}" |
---|
189 | hfmt = "{:>8s} {:>8s} {:>12s} {:>12s} {:>7s} {:>6s}" |
---|
190 | fmt = "{:>8s} {:8.3f} {:12.3f} {:12.3f} {:7.2f} {:6.0f}" |
---|
191 | for phasenam in histblk['Reflection Lists']: |
---|
192 | self.Write('\nPhase '+str(phasenam)) |
---|
193 | self.Write(80*'=') |
---|
194 | self.Write(hfmt.format("h,k,l","2-theta","F_obs","F_calc","phase","mult")) |
---|
195 | self.Write(80*'=') |
---|
196 | for ( |
---|
197 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,eqlist,phaselist,Icorr,FFdict |
---|
198 | ) in histblk['Reflection Lists'][phasenam]: |
---|
199 | self.Write(fmt.format(hklfmt.format(h,k,l),pos,Fobs,Fcalc,phase,mult)) |
---|
200 | self.CloseFile() |
---|
201 | print(str(hist)+'reflections written to file '+str(self.filename)) |
---|
202 | |
---|
203 | class ExportSingleText(G2IO.ExportBaseclass): |
---|
204 | '''Used to create a text file with single crystal reflection data |
---|
205 | |
---|
206 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
207 | ''' |
---|
208 | def __init__(self,G2frame): |
---|
209 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
210 | G2frame=G2frame, |
---|
211 | formatName = 'Text file', |
---|
212 | extension='.txt', |
---|
213 | longFormatName = 'Export reflection list as a text file' |
---|
214 | ) |
---|
215 | self.exporttype = ['single'] |
---|
216 | self.multiple = False # only allow one histogram to be selected |
---|
217 | |
---|
218 | def Exporter(self,event=None): |
---|
219 | '''Export a set of single crystal data as a text file |
---|
220 | ''' |
---|
221 | # the export process starts here |
---|
222 | self.InitExport(event) |
---|
223 | # load all of the tree into a set of dicts |
---|
224 | self.loadTree() |
---|
225 | if self.ExportSelect( # set export parameters |
---|
226 | AskFile=False # use the default file name |
---|
227 | #AskFile=True |
---|
228 | ): return |
---|
229 | self.OpenFile() |
---|
230 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
231 | histblk = self.Histograms[hist] |
---|
232 | hklfmt = "{:.0f},{:.0f},{:.0f}" |
---|
233 | hfmt = "{:>10s} {:>8s} {:>12s} {:>12s} {:>12s} {:>7s} {:>6s}" |
---|
234 | fmt = "{:>10s} {:8.3f} {:12.2f} {:12.4f} {:12.2f} {:7.2f} {:6.0f}" |
---|
235 | self.Write(80*'=') |
---|
236 | self.Write(hfmt.format("h,k,l","d-space","F_obs","sig(Fobs)","F_calc","phase","mult")) |
---|
237 | self.Write(80*'=') |
---|
238 | for ( |
---|
239 | h,k,l,mult,dsp,Fobs,sigFobs,Fcalc,FobsT,FcalcT,phase,eqlist,phaselist,Icorr,FFdict |
---|
240 | ) in histblk['Data']: |
---|
241 | self.Write(fmt.format(hklfmt.format(h,k,l),dsp,Fobs,sigFobs,Fcalc,phase,mult)) |
---|
242 | self.CloseFile() |
---|
243 | print(str(hist)+' written to file '+str(self.filename)) |
---|
244 | |
---|