1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2020-07-16 14:30:05 +0000 (Thu, 16 Jul 2020) $ |
---|
5 | # $Author: vondreele $ |
---|
6 | # $Revision: 4522 $ |
---|
7 | # $URL: trunk/exports/G2export_csv.py $ |
---|
8 | # $Id: G2export_csv.py 4522 2020-07-16 14:30:05Z vondreele $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | ''' |
---|
11 | *Module G2export_csv: Spreadsheet export* |
---|
12 | ------------------------------------------- |
---|
13 | |
---|
14 | Code to create .csv (comma-separated variable) files for |
---|
15 | GSAS-II data export to a spreadsheet program, etc. |
---|
16 | |
---|
17 | ''' |
---|
18 | from __future__ import division, print_function |
---|
19 | import os.path |
---|
20 | import numpy as np |
---|
21 | import GSASIIpath |
---|
22 | GSASIIpath.SetVersionNumber("$Revision: 4522 $") |
---|
23 | import GSASIIIO as G2IO |
---|
24 | import GSASIIpy3 as G2py3 |
---|
25 | import GSASIIobj as G2obj |
---|
26 | import GSASIImath as G2mth |
---|
27 | import GSASIIpwd as G2pwd |
---|
28 | import GSASIIlattice as G2lat |
---|
29 | |
---|
30 | def WriteList(obj,headerItems): |
---|
31 | '''Write a CSV header |
---|
32 | |
---|
33 | :param object obj: Exporter object |
---|
34 | :param list headerItems: items to write as a header |
---|
35 | ''' |
---|
36 | line = '' |
---|
37 | for lbl in headerItems: |
---|
38 | if line: line += ',' |
---|
39 | line += '"'+lbl+'"' |
---|
40 | obj.Write(line) |
---|
41 | |
---|
42 | class ExportPhaseCSV(G2IO.ExportBaseclass): |
---|
43 | '''Used to create a csv file for a phase |
---|
44 | |
---|
45 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
46 | ''' |
---|
47 | def __init__(self,G2frame): |
---|
48 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
49 | G2frame=G2frame, |
---|
50 | formatName = 'CSV file', |
---|
51 | extension='.csv', |
---|
52 | longFormatName = 'Export phase as comma-separated (csv) file' |
---|
53 | ) |
---|
54 | self.exporttype = ['phase'] |
---|
55 | self.multiple = True # allow multiple phases to be selected |
---|
56 | |
---|
57 | def Writer(self,hist,phasenam,mode='w'): |
---|
58 | self.OpenFile(mode=mode) |
---|
59 | # test for aniso atoms |
---|
60 | aniso = False |
---|
61 | AtomsList = self.GetAtoms(phasenam) |
---|
62 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
63 | if len(td) != 1: |
---|
64 | aniso = True |
---|
65 | break |
---|
66 | if mode == 'w': |
---|
67 | lbllist = ['hist','phase','a','b','c','alpha','beta','gamma','volume'] |
---|
68 | lbllist += ["atm label","elem","mult","x","y","z","frac","Uiso"] |
---|
69 | if aniso: lbllist += ['U11','U22','U33','U12','U13','U23'] |
---|
70 | WriteList(self,lbllist) |
---|
71 | |
---|
72 | cellList,cellSig = self.GetCell(phasenam) |
---|
73 | line = '"' + str(hist)+ '","' + str(phasenam) + '"' |
---|
74 | for defsig,val in zip( |
---|
75 | 3*[-0.00001] + 3*[-0.001] + [-0.01], # sets sig. figs. |
---|
76 | cellList |
---|
77 | ): |
---|
78 | txt = G2mth.ValEsd(val,defsig) |
---|
79 | if line: line += ',' |
---|
80 | line += txt |
---|
81 | self.Write(line) |
---|
82 | |
---|
83 | # get atoms and print separated by commas |
---|
84 | AtomsList = self.GetAtoms(phasenam) |
---|
85 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
86 | line = ",,,,,,,,," |
---|
87 | line += '"' + lbl + '","' + typ + '",' + str(mult) + ',' |
---|
88 | for val,sig in xyz: |
---|
89 | line += G2mth.ValEsd(val,-abs(sig)) |
---|
90 | line += "," |
---|
91 | if len(td) == 1: |
---|
92 | line += G2mth.ValEsd(td[0][0],-abs(td[0][1])) |
---|
93 | else: |
---|
94 | line += "," |
---|
95 | for val,sig in td: |
---|
96 | line += G2mth.ValEsd(val,-abs(sig)) |
---|
97 | line += "," |
---|
98 | self.Write(line) |
---|
99 | |
---|
100 | if mode == 'w': |
---|
101 | print('Phase '+phasenam+' written to file '+self.fullpath) |
---|
102 | self.CloseFile() |
---|
103 | |
---|
104 | def Exporter(self,event=None): |
---|
105 | '''Export a phase as a csv file |
---|
106 | ''' |
---|
107 | # the export process starts here |
---|
108 | self.InitExport(event) |
---|
109 | # load all of the tree into a set of dicts |
---|
110 | self.loadTree() |
---|
111 | # create a dict with refined values and their uncertainties |
---|
112 | self.loadParmDict() |
---|
113 | if self.ExportSelect(): return # set export parameters; get file name |
---|
114 | self.OpenFile() |
---|
115 | # if more than one phase is selected, put them into a single file |
---|
116 | for phasenam in self.phasenam: |
---|
117 | phasedict = self.Phases[phasenam] # pointer to current phase info |
---|
118 | i = self.Phases[phasenam]['pId'] |
---|
119 | self.Write('"'+"Phase "+str(phasenam)+" from "+str(self.G2frame.GSASprojectfile)+'"') |
---|
120 | self.Write('\n"Space group:","'+str(phasedict['General']['SGData']['SpGrp'].strip())+'"') |
---|
121 | # get cell parameters & print them |
---|
122 | cellList,cellSig = self.GetCell(phasenam) |
---|
123 | WriteList(self,['a','b','c','alpha','beta','gamma','volume']) |
---|
124 | |
---|
125 | line = '' |
---|
126 | for defsig,val in zip( |
---|
127 | 3*[-0.00001] + 3*[-0.001] + [-0.01], # sign values to use when no sigma |
---|
128 | cellList |
---|
129 | ): |
---|
130 | txt = G2mth.ValEsd(val,defsig) |
---|
131 | if line: line += ',' |
---|
132 | line += txt |
---|
133 | self.Write(line) |
---|
134 | |
---|
135 | # get atoms and print separated by commas |
---|
136 | AtomsList = self.GetAtoms(phasenam) |
---|
137 | # check for aniso atoms |
---|
138 | aniso = False |
---|
139 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
140 | if len(td) != 1: aniso = True |
---|
141 | lbllist = ["label","elem","mult","x","y","z","frac","Uiso"] |
---|
142 | if aniso: lbllist += ['U11','U22','U33','U12','U13','U23'] |
---|
143 | WriteList(self,lbllist) |
---|
144 | |
---|
145 | for lbl,typ,mult,xyz,td in AtomsList: |
---|
146 | line = '"' + lbl + '","' + typ + '",' + str(mult) + ',' |
---|
147 | for val,sig in xyz: |
---|
148 | line += G2mth.ValEsd(val,-abs(sig)) |
---|
149 | line += "," |
---|
150 | if len(td) == 1: |
---|
151 | line += G2mth.ValEsd(td[0][0],-abs(td[0][1])) |
---|
152 | else: |
---|
153 | line += "," |
---|
154 | for val,sig in td: |
---|
155 | line += G2mth.ValEsd(val,-abs(sig)) |
---|
156 | line += "," |
---|
157 | self.Write(line) |
---|
158 | print('Phase '+phasenam+' written to file '+self.fullpath) |
---|
159 | self.CloseFile() |
---|
160 | |
---|
161 | class ExportPowderCSV(G2IO.ExportBaseclass): |
---|
162 | '''Used to create a csv file for a powder data set |
---|
163 | |
---|
164 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
165 | ''' |
---|
166 | def __init__(self,G2frame): |
---|
167 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
168 | G2frame=G2frame, |
---|
169 | formatName = 'histogram CSV file', |
---|
170 | extension='.csv', |
---|
171 | longFormatName = 'Export powder data as comma-separated (csv) file' |
---|
172 | ) |
---|
173 | self.exporttype = ['powder'] |
---|
174 | #self.multiple = False # only allow one histogram to be selected |
---|
175 | self.multiple = True |
---|
176 | |
---|
177 | def Writer(self,TreeName,filename=None): |
---|
178 | #print filename |
---|
179 | self.OpenFile(filename) |
---|
180 | histblk = self.Histograms[TreeName] |
---|
181 | Parms = self.Histograms[TreeName]['Instrument Parameters'][0] |
---|
182 | for parm in Parms: |
---|
183 | if parm in ['Type','Source',]: |
---|
184 | line = '"Instparm: %s","%s"'%(parm,Parms[parm][0]) |
---|
185 | elif parm in ['Lam','Zero',]: |
---|
186 | line = '"Instparm: %s",%10.6f'%(parm,Parms[parm][1]) |
---|
187 | else: |
---|
188 | line = '"Instparm: %s",%10.2f'%(parm,Parms[parm][1]) |
---|
189 | self.Write(line) |
---|
190 | Samp = self.Histograms[TreeName]['Sample Parameters'] |
---|
191 | for samp in Samp: |
---|
192 | if samp in ['InstrName','Type']: |
---|
193 | line = '"Samparm: %s",%s'%(samp,Samp[samp]) |
---|
194 | elif samp in ['Azimuth','Chi','Gonio. radius','Omega','Phi','Pressure','Temperature','Time']: |
---|
195 | line = '"Samparm: %s",%10.2f'%(samp,Samp[samp]) |
---|
196 | elif samp in ['DisplaceX','DisplaceY','Scale','Shift','SurfRoughA','SurfRoughB','Transparency']: |
---|
197 | line = '"Samparm: %s",%10.2f'%(samp,Samp[samp][0]) |
---|
198 | else: |
---|
199 | continue |
---|
200 | self.Write(line) |
---|
201 | WriteList(self,("x","y_obs","weight","y_calc","y_bkg","Q")) |
---|
202 | digitList = 2*((13,3),) + ((13,5),) + 3*((13,3),) |
---|
203 | for vallist in zip(histblk['Data'][0], |
---|
204 | histblk['Data'][1], |
---|
205 | histblk['Data'][2], |
---|
206 | histblk['Data'][3], |
---|
207 | histblk['Data'][4], |
---|
208 | #histblk['Data'][5], |
---|
209 | 2*np.pi/G2lat.Pos2dsp(Parms,histblk['Data'][0]) |
---|
210 | ): |
---|
211 | line = "" |
---|
212 | for val,digits in zip(vallist,digitList): |
---|
213 | if line: line += ',' |
---|
214 | line += G2py3.FormatValue(val,digits) |
---|
215 | self.Write(line) |
---|
216 | self.CloseFile() |
---|
217 | |
---|
218 | def Exporter(self,event=None): |
---|
219 | '''Export a set of powder data as a csv 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='single' # get a file name/directory to save in |
---|
227 | ): return |
---|
228 | filenamelist = [] |
---|
229 | for hist in self.histnam: |
---|
230 | if len(self.histnam) == 1: |
---|
231 | name = self.filename |
---|
232 | else: # multiple files: create a unique name from the histogram |
---|
233 | name = self.MakePWDRfilename(hist) |
---|
234 | fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0] |
---|
235 | # create the file |
---|
236 | self.filename = os.path.join(self.dirname,fileroot + self.extension) |
---|
237 | self.Writer(hist) |
---|
238 | print('Histogram '+hist+' written to file '+self.fullpath) |
---|
239 | |
---|
240 | class ExportMultiPowderCSV(G2IO.ExportBaseclass): |
---|
241 | '''Used to create a csv file for a stack of powder data sets suitable for display |
---|
242 | purposes only; no y-calc or weights are exported only x & y-obs |
---|
243 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
244 | ''' |
---|
245 | def __init__(self,G2frame): |
---|
246 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
247 | G2frame=G2frame, |
---|
248 | formatName = 'stacked CSV file', |
---|
249 | extension='.csv', |
---|
250 | longFormatName = 'Export powder data sets as a (csv) file - x,y-o1,y-o2,... only' |
---|
251 | ) |
---|
252 | self.exporttype = ['powder'] |
---|
253 | #self.multiple = False # only allow one histogram to be selected |
---|
254 | self.multiple = True |
---|
255 | |
---|
256 | def Exporter(self,event=None): |
---|
257 | '''Export a set of powder data as a single csv file |
---|
258 | ''' |
---|
259 | # the export process starts here |
---|
260 | self.InitExport(event) |
---|
261 | # load all of the tree into a set of dicts |
---|
262 | self.loadTree() |
---|
263 | if self.ExportSelect( # set export parameters |
---|
264 | AskFile='ask' # only one file is ever written |
---|
265 | ): return |
---|
266 | csvData = [] |
---|
267 | headList = ["x",] |
---|
268 | digitList = [] |
---|
269 | self.filename = os.path.join(self.dirname,os.path.splitext(self.filename)[0] |
---|
270 | + self.extension) |
---|
271 | for ihst,hist in enumerate(self.histnam): |
---|
272 | histblk = self.Histograms[hist] |
---|
273 | headList.append('y_obs_'+G2obj.StripUnicode(hist[5:].replace(' ','_'))) |
---|
274 | if not ihst: |
---|
275 | digitList = [(13,3),] |
---|
276 | csvData.append(histblk['Data'][0]) |
---|
277 | digitList += [(13,3),] |
---|
278 | csvData.append(histblk['Data'][1]) |
---|
279 | print('Histogram '+hist+' added to file...') |
---|
280 | self.OpenFile() |
---|
281 | WriteList(self,headList) |
---|
282 | for vallist in np.array(csvData).T: |
---|
283 | line = "" |
---|
284 | for val,digits in zip(vallist,digitList): |
---|
285 | if line: line += ',' |
---|
286 | line += G2py3.FormatValue(val,digits) |
---|
287 | self.Write(line) |
---|
288 | self.CloseFile() |
---|
289 | print('...file '+self.fullpath+' written') |
---|
290 | |
---|
291 | class ExportPowderReflCSV(G2IO.ExportBaseclass): |
---|
292 | '''Used to create a csv file of reflections from a powder data set |
---|
293 | |
---|
294 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
295 | ''' |
---|
296 | def __init__(self,G2frame): |
---|
297 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
298 | G2frame=G2frame, |
---|
299 | formatName = 'reflection list as CSV', |
---|
300 | extension='.csv', |
---|
301 | longFormatName = 'Export powder reflection list as a comma-separated (csv) file' |
---|
302 | ) |
---|
303 | self.exporttype = ['powder'] |
---|
304 | self.multiple = False # only allow one histogram to be selected |
---|
305 | |
---|
306 | def Writer(self,TreeName,filename=None): |
---|
307 | print(filename) |
---|
308 | self.OpenFile(filename) |
---|
309 | histblk = self.Histograms[TreeName] |
---|
310 | self.write(TreeName,histblk) |
---|
311 | self.CloseFile() |
---|
312 | print(TreeName+' reflections written to file '+self.fullpath) |
---|
313 | |
---|
314 | def Exporter(self,event=None): |
---|
315 | '''Export a set of powder reflections as a csv file |
---|
316 | ''' |
---|
317 | self.InitExport(event) |
---|
318 | # load all of the tree into a set of dicts |
---|
319 | self.loadTree() |
---|
320 | if self.ExportSelect(): return # set export parameters, get file name |
---|
321 | hist = list(self.histnam)[0] # there should only be one histogram, in any case take the 1st |
---|
322 | histblk = self.Histograms[hist] |
---|
323 | self.OpenFile() |
---|
324 | self.write(hist,histblk) |
---|
325 | self.CloseFile() |
---|
326 | print(hist+' reflections written to file '+self.fullpath) |
---|
327 | |
---|
328 | def write(self,hist,histblk): |
---|
329 | self.Write('"Histogram"') |
---|
330 | self.Write('"'+hist+'"') |
---|
331 | self.Write('') |
---|
332 | # table of phases |
---|
333 | self.Write('"Phase name","phase #"') |
---|
334 | for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])): |
---|
335 | self.Write('"'+str(phasenam)+'",'+str(i)) |
---|
336 | self.Write('') |
---|
337 | # note addition of a phase # flag at end (i) |
---|
338 | for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])): |
---|
339 | phasDict = histblk['Reflection Lists'][phasenam] |
---|
340 | tname = {'T':'TOF','C':'2-theta','B':'2-theta'}[phasDict['Type'][2]] |
---|
341 | if phasDict.get('Super',False): |
---|
342 | WriteList(self,("h","k","l","m","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #")) |
---|
343 | if 'T' in phasDict['Type']: |
---|
344 | fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}" |
---|
345 | else: |
---|
346 | fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}" |
---|
347 | refList = phasDict['RefList'] |
---|
348 | for refItem in refList: |
---|
349 | if 'T' in phasDict['Type']: |
---|
350 | h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:17] |
---|
351 | FWHM = G2pwd.getgamFW(gam,sig) |
---|
352 | self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,i)) |
---|
353 | elif 'C' in phasDict['Type']: #convert to deg |
---|
354 | h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:14] |
---|
355 | s = np.sqrt(max(sig,0.0001))/100. #var -> sig in deg |
---|
356 | g = gam/100. #-> deg |
---|
357 | FWHM = G2pwd.getgamFW(g,s) |
---|
358 | self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,i)) |
---|
359 | elif 'B' in phasDict['Type']: #convert to deg |
---|
360 | h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:17] |
---|
361 | s = np.sqrt(max(sig,0.0001))/100. #var -> sig in deg |
---|
362 | g = gam/100. #-> deg |
---|
363 | FWHM = G2pwd.getgamFW(g,s) |
---|
364 | self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,i)) |
---|
365 | else: |
---|
366 | WriteList(self,("h","k","l","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #")) |
---|
367 | if 'T' in phasDict['Type']: |
---|
368 | fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}" |
---|
369 | else: |
---|
370 | fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}" |
---|
371 | refList = phasDict['RefList'] |
---|
372 | for refItem in refList: |
---|
373 | if 'T' in phasDict['Type']: |
---|
374 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:16] |
---|
375 | FWHM = G2pwd.getgamFW(gam,sig) |
---|
376 | self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,Prfo,i)) |
---|
377 | elif 'C' in phasDict['Type']: #convert to deg |
---|
378 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:13] |
---|
379 | g = gam/100. |
---|
380 | s = np.sqrt(max(sig,0.0001))/100. |
---|
381 | FWHM = G2pwd.getgamFW(g,s) |
---|
382 | self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,Prfo,i)) |
---|
383 | elif 'B' in phasDict['Type']: #convert to deg |
---|
384 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:16] |
---|
385 | g = gam/100. |
---|
386 | s = np.sqrt(max(sig,0.0001))/100. |
---|
387 | FWHM = G2pwd.getgamFW(g,s) |
---|
388 | self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,Prfo,i)) |
---|
389 | |
---|
390 | class ExportSASDCSV(G2IO.ExportBaseclass): |
---|
391 | '''Used to create a csv file for a small angle data set |
---|
392 | |
---|
393 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
394 | ''' |
---|
395 | def __init__(self,G2frame): |
---|
396 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
397 | G2frame=G2frame, |
---|
398 | formatName = 'CSV file', |
---|
399 | extension='.csv', |
---|
400 | longFormatName = 'Export small angle data as comma-separated (csv) file' |
---|
401 | ) |
---|
402 | self.exporttype = ['sasd'] |
---|
403 | #self.multiple = False # only allow one histogram to be selected |
---|
404 | self.multiple = True |
---|
405 | |
---|
406 | def Writer(self,TreeName,filename=None): |
---|
407 | self.OpenFile(filename) |
---|
408 | histblk = self.Histograms[TreeName] |
---|
409 | if len(self.Histograms[TreeName]['Models']['Size']['Distribution']): |
---|
410 | self.Write('"Size Distribution"') |
---|
411 | Distr = np.array(self.Histograms[TreeName]['Models']['Size']['Distribution']) |
---|
412 | WriteList(self,("bin_pos","bin_width","bin_value")) |
---|
413 | digitList = 2*((13,3),)+((13,4,'g'),) |
---|
414 | for bindata in Distr.T: |
---|
415 | line = "" |
---|
416 | for val,digits in zip(bindata,digitList): |
---|
417 | if line: line += ',' |
---|
418 | line += G2py3.FormatValue(val,digits) |
---|
419 | self.Write(line) |
---|
420 | self.Write('"Small angle data"') |
---|
421 | Parms = self.Histograms[TreeName]['Instrument Parameters'][0] |
---|
422 | for parm in Parms: |
---|
423 | if parm in ['Type','Source',]: |
---|
424 | line = '"Instparm: %s","%s"'%(parm,Parms[parm][0]) |
---|
425 | elif parm in ['Lam',]: |
---|
426 | line = '"Instparm: %s",%10.6f'%(parm,Parms[parm][1]) |
---|
427 | else: |
---|
428 | line = '"Instparm: %s",%10.2f'%(parm,Parms[parm][1]) |
---|
429 | self.Write(line) |
---|
430 | WriteList(self,("q","y_obs","y_sig","y_calc","y_bkg")) |
---|
431 | digitList = 5*((13,5,'g'),) |
---|
432 | for vallist in zip(histblk['Data'][0], |
---|
433 | histblk['Data'][1], |
---|
434 | 1./np.sqrt(histblk['Data'][2]), |
---|
435 | histblk['Data'][3], |
---|
436 | histblk['Data'][4], |
---|
437 | ): |
---|
438 | line = "" |
---|
439 | for val,digits in zip(vallist,digitList): |
---|
440 | if line: line += ',' |
---|
441 | line += G2py3.FormatValue(val,digits) |
---|
442 | self.Write(line) |
---|
443 | self.CloseFile() |
---|
444 | |
---|
445 | def Exporter(self,event=None): |
---|
446 | '''Export a set of small angle data as a csv file |
---|
447 | ''' |
---|
448 | # the export process starts here |
---|
449 | self.InitExport(event) |
---|
450 | # load all of the tree into a set of dicts |
---|
451 | self.loadTree() |
---|
452 | if self.ExportSelect( # set export parameters |
---|
453 | AskFile='single' # get a file name/directory to save in |
---|
454 | ): return |
---|
455 | filenamelist = [] |
---|
456 | for hist in self.histnam: |
---|
457 | if len(self.histnam) == 1: |
---|
458 | name = self.filename |
---|
459 | else: # multiple files: create a unique name from the histogram |
---|
460 | name = self.MakePWDRfilename(hist) |
---|
461 | fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0] |
---|
462 | # create the file |
---|
463 | self.filename = os.path.join(self.dirname,fileroot + self.extension) |
---|
464 | self.Writer(hist) |
---|
465 | print('Histogram '+hist+' written to file '+self.fullpath) |
---|
466 | |
---|
467 | class ExportSingleCSV(G2IO.ExportBaseclass): |
---|
468 | '''Used to create a csv file with single crystal reflection data |
---|
469 | |
---|
470 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
471 | ''' |
---|
472 | def __init__(self,G2frame): |
---|
473 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
474 | G2frame=G2frame, |
---|
475 | formatName = 'CSV file', |
---|
476 | extension='.csv', |
---|
477 | longFormatName = 'Export reflection list as a comma-separated (csv) file' |
---|
478 | ) |
---|
479 | self.exporttype = ['single'] |
---|
480 | self.multiple = False # only allow one histogram to be selected |
---|
481 | |
---|
482 | def Exporter(self,event=None): |
---|
483 | '''Export a set of single crystal data as a csv file |
---|
484 | ''' |
---|
485 | # the export process starts here |
---|
486 | self.InitExport(event) |
---|
487 | # load all of the tree into a set of dicts |
---|
488 | self.loadTree() |
---|
489 | if self.ExportSelect(): return # set export parameters, get file name |
---|
490 | self.OpenFile() |
---|
491 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
492 | histblk = self.Histograms[hist] |
---|
493 | for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])): |
---|
494 | phasDict = histblk['Reflection Lists'][phasenam] |
---|
495 | tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]] |
---|
496 | if phasDict.get('Super',False): |
---|
497 | WriteList(self,("h","k","l","m",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #")) |
---|
498 | fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}" |
---|
499 | refList = phasDict['RefList'] |
---|
500 | for refItem in refList: |
---|
501 | h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:13] |
---|
502 | self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,i)) |
---|
503 | else: |
---|
504 | WriteList(self,("h","k","l",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #")) |
---|
505 | fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}" |
---|
506 | refList = phasDict['RefList'] |
---|
507 | for refItem in refList: |
---|
508 | h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:12] |
---|
509 | self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,i)) |
---|
510 | self.CloseFile() |
---|
511 | print(hist+' written to file '+self.fullname) |
---|
512 | |
---|
513 | class ExportStrainCSV(G2IO.ExportBaseclass): |
---|
514 | '''Used to create a csv file with single crystal reflection data |
---|
515 | |
---|
516 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
517 | ''' |
---|
518 | def __init__(self,G2frame): |
---|
519 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
520 | G2frame=G2frame, |
---|
521 | formatName = 'Strain CSV file', |
---|
522 | extension='.csv', |
---|
523 | longFormatName = 'Export strain results as a comma-separated (csv) file' |
---|
524 | ) |
---|
525 | self.exporttype = ['image'] |
---|
526 | self.multiple = False # only allow one histogram to be selected |
---|
527 | |
---|
528 | def Exporter(self,event=None): |
---|
529 | '''Export a set of single crystal data as a csv file |
---|
530 | ''' |
---|
531 | # the export process starts here |
---|
532 | self.InitExport(event) |
---|
533 | # load all of the tree into a set of dicts |
---|
534 | self.loadTree() |
---|
535 | if self.ExportSelect(): return # set export parameters, get file name |
---|
536 | self.OpenFile() |
---|
537 | hist = self.histnam[0] # there should only be one histogram, in any case take the 1st |
---|
538 | histblk = self.Histograms[hist] |
---|
539 | StrSta = histblk['Stress/Strain'] |
---|
540 | WriteList(self,("Dset","Dcalc","e11","sig(e11)","e12","sig(e12)","e22","sig(e22)")) |
---|
541 | fmt = 2*"{:.5f},"+6*"{:.0f}," |
---|
542 | fmt1 = "{:.5f}" |
---|
543 | fmt2 = "{:.2f},{:.5f},{:.5f}" |
---|
544 | for item in StrSta['d-zero']: |
---|
545 | Emat = item['Emat'] |
---|
546 | Esig = item['Esig'] |
---|
547 | self.Write(fmt.format(item['Dset'],item['Dcalc'],Emat[0],Esig[0],Emat[1],Esig[1],Emat[2],Esig[2])) |
---|
548 | for item in StrSta['d-zero']: |
---|
549 | WriteList(self,("Azm","dobs","dcalc","Dset="+fmt1.format(item['Dset']))) |
---|
550 | ring = np.vstack((item['ImtaObs'],item['ImtaCalc'])) |
---|
551 | for dat in ring.T: |
---|
552 | self.Write(fmt2.format(dat[1],dat[0],dat[2])) |
---|
553 | self.CloseFile() |
---|
554 | print(hist+' written to file '+self.fullpath) |
---|