source: trunk/exports/G2export_csv.py @ 2133

Last change on this file since 2133 was 2132, checked in by vondreele, 6 years ago

more rationalization of FWHM calculations

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 16.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2016-01-22 16:10:43 +0000 (Fri, 22 Jan 2016) $
5# $Author: toby $
6# $Revision: 2132 $
7# $URL: trunk/exports/G2export_csv.py $
8# $Id: G2export_csv.py 2132 2016-01-22 16:10:43Z toby $
9########### SVN repository information ###################
10'''
11*Module G2export_csv: Spreadsheet export*
12-------------------------------------------
13
14Code to create .csv (comma-separated variable) files for
15GSAS-II data export to a spreadsheet program, etc.
16
17'''
18import os.path
19import numpy as np
20import GSASIIpath
21GSASIIpath.SetVersionNumber("$Revision: 2132 $")
22import GSASIIIO as G2IO
23import GSASIIpy3 as G2py3
24import GSASIIobj as G2obj
25import GSASIImath as G2mth
26import GSASIIpwd as G2pwd
27
28def WriteList(obj,headerItems):
29    '''Write a CSV header
30
31    :param object obj: Exporter object
32    :param list headerItems: items to write as a header
33    '''
34    line = ''
35    for lbl in headerItems:
36        if line: line += ','
37        line += '"'+lbl+'"'
38    obj.Write(line)
39
40class ExportPhaseCSV(G2IO.ExportBaseclass):
41    '''Used to create a csv file for a phase
42
43    :param wx.Frame G2frame: reference to main GSAS-II frame
44    '''
45    def __init__(self,G2frame):
46        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
47            G2frame=G2frame,
48            formatName = 'CSV file',
49            extension='.csv',
50            longFormatName = 'Export phase as comma-separated (csv) file'
51            )
52        self.exporttype = ['phase']
53        self.multiple = True # allow multiple phases to be selected
54
55    def Exporter(self,event=None):
56        '''Export a phase as a csv file
57        '''
58        # the export process starts here
59        self.InitExport(event)
60        # load all of the tree into a set of dicts
61        self.loadTree()
62        # create a dict with refined values and their uncertainties
63        self.loadParmDict()
64        if self.ExportSelect(): return # set export parameters; get file name
65        self.OpenFile()
66        # if more than one format is selected, put them into a single file
67        for phasenam in self.phasenam:
68            phasedict = self.Phases[phasenam] # pointer to current phase info           
69            i = self.Phases[phasenam]['pId']
70            self.Write('"'+"Phase "+str(phasenam)+" from "+str(self.G2frame.GSASprojectfile)+'"')
71            self.Write('\n"Space group:","'+str(phasedict['General']['SGData']['SpGrp'].strip())+'"')
72            # get cell parameters & print them
73            cellList,cellSig = self.GetCell(phasenam)
74            WriteList(self,['a','b','c','alpha','beta','gamma','volume'])
75
76            line = ''
77            for defsig,val in zip(
78                3*[-0.00001] + 3*[-0.001] + [-0.01], # sign values to use when no sigma
79                cellList
80                ):
81                txt = G2mth.ValEsd(val,defsig)
82                if line: line += ','
83                line += txt
84            self.Write(line)
85               
86            # get atoms and print separated by commas
87            AtomsList = self.GetAtoms(phasenam)
88            # check for aniso atoms
89            aniso = False
90            for lbl,typ,mult,xyz,td in AtomsList:
91                if len(td) != 1: aniso = True               
92            lbllist = ["label","elem","mult","x","y","z","frac","Uiso"]
93            if aniso: lbllist += ['U11','U22','U33','U12','U13','U23']
94            WriteList(self,lbllist)
95               
96            for lbl,typ,mult,xyz,td in AtomsList:
97                line = '"' + lbl + '","' + typ + '",' + str(mult) + ','
98                for val,sig in xyz:
99                    line += G2mth.ValEsd(val,-abs(sig))
100                    line += ","
101                if len(td) == 1:
102                    line += G2mth.ValEsd(td[0][0],-abs(td[0][1]))
103                else:
104                    line += ","
105                    for val,sig in td:
106                        line += G2mth.ValEsd(val,-abs(sig))
107                        line += ","
108                self.Write(line)
109            print('Phase '+str(phasenam)+' written to file '+str(self.fullpath))
110        self.CloseFile()
111
112class ExportPowderCSV(G2IO.ExportBaseclass):
113    '''Used to create a csv file for a powder data set
114
115    :param wx.Frame G2frame: reference to main GSAS-II frame
116    '''
117    def __init__(self,G2frame):
118        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
119            G2frame=G2frame,
120            formatName = 'CSV file',
121            extension='.csv',
122            longFormatName = 'Export powder data as comma-separated (csv) file'
123            )
124        self.exporttype = ['powder']
125        #self.multiple = False # only allow one histogram to be selected
126        self.multiple = True
127
128    def Writer(self,TreeName,filename=None):
129        self.OpenFile(filename)
130        histblk = self.Histograms[TreeName]
131        WriteList(self,("x","y_obs","weight","y_calc","y_bkg"))
132        digitList = 2*((13,3),) + ((13,5),) + 2*((13,3),)
133        for vallist in zip(histblk['Data'][0],
134                       histblk['Data'][1],
135                       histblk['Data'][2],
136                       histblk['Data'][3],
137                       histblk['Data'][4],
138                       #histblk['Data'][5],
139                       ):
140            line = ""
141            for val,digits in zip(vallist,digitList):
142                if line: line += ','
143                line += G2py3.FormatValue(val,digits)
144            self.Write(line)
145        self.CloseFile()
146       
147    def Exporter(self,event=None):
148        '''Export a set of powder data as a csv file
149        '''
150        # the export process starts here
151        self.InitExport(event)
152        # load all of the tree into a set of dicts
153        self.loadTree()
154        if self.ExportSelect( # set export parameters
155            AskFile='single' # get a file name/directory to save in
156            ): return
157        filenamelist = []
158        for hist in self.histnam:
159            #if len(self.histnam) > 1:
160            #    # multiple files: create a unique name from the histogram
161            #    fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
162            #    # create an instrument parameter file
163            #    self.filename = os.path.join(self.dirname,fileroot + self.extension)
164            self.Writer(hist)
165            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
166
167class ExportMultiPowderCSV(G2IO.ExportBaseclass):
168    '''Used to create a csv file for a stack of powder data sets suitable for display
169    purposes only; no y-calc or weights are exported only x & y-obs
170    :param wx.Frame G2frame: reference to main GSAS-II frame
171    '''
172    def __init__(self,G2frame):
173        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
174            G2frame=G2frame,
175            formatName = 'stacked CSV file',
176            extension='.csv',
177            longFormatName = 'Export powder data sets as a (csv) file - x,y-o1,y-o2,... only'
178            )
179        self.exporttype = ['powder']
180        #self.multiple = False # only allow one histogram to be selected
181        self.multiple = True
182
183    def Exporter(self,event=None):
184        '''Export a set of powder data as a csv file
185        '''
186        # the export process starts here
187        self.InitExport(event)
188        # load all of the tree into a set of dicts
189        self.loadTree()
190        if self.ExportSelect( # set export parameters
191            AskFile='single' # get a file name/directory to save in
192            ): return
193        filenamelist = []
194        csvData = []
195        headList = ["x",]
196        digitList = []
197        fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(self.histnam[0]),filenamelist)
198        # create a file
199        self.filename = os.path.join(self.dirname,fileroot + self.extension)
200        for ihst,hist in enumerate(self.histnam):
201            histblk = self.Histograms[hist]
202            headList.append('y_obs_'+str(ihst))
203            if not ihst:
204                digitList = [(13,3),]
205                csvData.append(histblk['Data'][0])
206            digitList += [(13,3),]
207            csvData.append(histblk['Data'][1])
208            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
209        self.OpenFile()
210        WriteList(self,headList)
211        for vallist in np.array(csvData).T:
212            line = ""
213            for val,digits in zip(vallist,digitList):
214                if line: line += ','
215                line += G2py3.FormatValue(val,digits)
216            self.Write(line)
217        self.CloseFile()
218
219class ExportPowderReflCSV(G2IO.ExportBaseclass):
220    '''Used to create a csv file of reflections from a powder data set
221
222    :param wx.Frame G2frame: reference to main GSAS-II frame
223    '''
224    def __init__(self,G2frame):
225        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
226            G2frame=G2frame,
227            formatName = 'reflection list as CSV',
228            extension='.csv',
229            longFormatName = 'Export powder reflection list as a comma-separated (csv) file'
230            )
231        self.exporttype = ['powder']
232        self.multiple = False # only allow one histogram to be selected
233
234    def Exporter(self,event=None):
235        '''Export a set of powder reflections as a csv file
236        '''
237        self.InitExport(event)
238        # load all of the tree into a set of dicts
239        self.loadTree()
240        if self.ExportSelect(): return  # set export parameters, get file name
241        self.OpenFile()
242        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
243        histblk = self.Histograms[hist]
244        self.Write('"Histogram"')
245        self.Write('"'+hist+'"')
246        self.Write('')
247        # table of phases
248        self.Write('"Phase name","phase #"')
249        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
250            self.Write('"'+str(phasenam)+'",'+str(i))
251        self.Write('')
252        # note addition of a phase # flag at end (i)
253        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
254            phasDict = histblk['Reflection Lists'][phasenam]
255            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
256            if phasDict.get('Super',False):
257                WriteList(self,("h","k","l","m",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
258                if 'T' in phasDict['Type']:
259                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
260                else:
261                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
262                refList = phasDict['RefList']
263                for refItem in refList:
264                    if 'T' in phasDict['Type']:
265                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:17]
266                        FWHM = G2pwd.getgamFW(gam,sig)
267                        self.Write(fmt.format(h,k,l,m,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,i))
268                    else:        #convert to deg       
269                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:14]
270                        s = np.sqrt(max(sig,0.0001))/100.   #var -> sig in deg
271                        g = gam/100.    #-> deg
272                        FWHM = G2pwd.getgamFW(g,s)
273                        self.Write(fmt.format(h,k,l,m,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,i))
274            else:
275                WriteList(self,("h","k","l",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
276                if 'T' in phasDict['Type']:
277                    fmt = "{:.0f},{:.0f},{:.0f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
278                else:
279                    fmt = "{:.0f},{:.0f},{:.0f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
280                refList = phasDict['RefList']
281                for refItem in refList:
282                    if 'T' in phasDict['Type']:
283                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:16]
284                        FWHM = G2pwd.getgamFW(gam,sig)
285                        self.Write(fmt.format(h,k,l,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,Prfo,i))
286                    else:        #convert to deg       
287                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:13]
288                        g = gam/100.
289                        s = np.sqrt(max(sig,0.0001))/100.
290                        FWHM = G2pwd.getgamFW(g,s)
291                        self.Write(fmt.format(h,k,l,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,Prfo,i))
292        self.CloseFile()
293        print(str(hist)+'reflections written to file '+str(self.fullpath))
294
295class ExportSingleCSV(G2IO.ExportBaseclass):
296    '''Used to create a csv file with single crystal reflection data
297
298    :param wx.Frame G2frame: reference to main GSAS-II frame
299    '''
300    def __init__(self,G2frame):
301        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
302            G2frame=G2frame,
303            formatName = 'CSV file',
304            extension='.csv',
305            longFormatName = 'Export reflection list as a comma-separated (csv) file'
306            )
307        self.exporttype = ['single']
308        self.multiple = False # only allow one histogram to be selected
309
310    def Exporter(self,event=None):
311        '''Export a set of single crystal data as a csv file
312        '''
313        # the export process starts here
314        self.InitExport(event)
315        # load all of the tree into a set of dicts
316        self.loadTree()
317        if self.ExportSelect(): return  # set export parameters, get file name
318        self.OpenFile()
319        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
320        histblk = self.Histograms[hist]
321        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
322            phasDict = histblk['Reflection Lists'][phasenam]
323            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
324            if phasDict.get('Super',False):
325                WriteList(self,("h","k","l","m",tname,"F_obs","F_calc","phase","mult","phase #"))
326                fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
327                refList = phasDict['RefList']
328                for refItem in refList:
329                    h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:13]
330                    self.Write(fmt.format(h,k,l,m,pos,Fobs,Fcalc,phase,mult,i))               
331            else:
332                WriteList(self,("h","k","l",tname,"F_obs","F_calc","phase","mult","phase #"))
333                fmt = "{:.0f},{:.0f},{:.0f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
334                refList = phasDict['RefList']
335                for refItem in refList:
336                    h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:12]
337                    self.Write(fmt.format(h,k,l,pos,Fobs,Fcalc,phase,mult,i))
338        self.CloseFile()
339        print(str(hist)+' written to file '+str(self.fullname))                       
340
341class ExportStrainCSV(G2IO.ExportBaseclass):
342    '''Used to create a csv file with single crystal reflection data
343
344    :param wx.Frame G2frame: reference to main GSAS-II frame
345    '''
346    def __init__(self,G2frame):
347        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
348            G2frame=G2frame,
349            formatName = 'Strain CSV file',
350            extension='.csv',
351            longFormatName = 'Export strain results as a comma-separated (csv) file'
352            )
353        self.exporttype = ['image']
354        self.multiple = False # only allow one histogram to be selected
355
356    def Exporter(self,event=None):
357        '''Export a set of single crystal data as a csv file
358        '''
359        # the export process starts here
360        self.InitExport(event)
361        # load all of the tree into a set of dicts
362        self.loadTree()
363        if self.ExportSelect(): return  # set export parameters, get file name
364        self.OpenFile()
365        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
366        histblk = self.Histograms[hist]
367        StrSta = histblk['Stress/Strain']
368        WriteList(self,("Dset","Dcalc","e11","sig(e11)","e12","sig(e12)","e22","sig(e22)"))
369        fmt = 2*"{:.5f},"+6*"{:.0f},"
370        fmt1 = "{:.5f}"
371        fmt2 = "{:.2f},{:.5f},{:.5f}"
372        for item in StrSta['d-zero']:
373            Emat = item['Emat']
374            Esig = item['Esig']
375            self.Write(fmt.format(item['Dset'],item['Dcalc'],Emat[0],Esig[0],Emat[1],Esig[1],Emat[2],Esig[2]))
376        for item in StrSta['d-zero']:
377            WriteList(self,("Azm","dobs","dcalc","Dset="+fmt1.format(item['Dset'])))
378            ring = np.vstack((item['ImtaObs'],item['ImtaCalc']))
379            for dat in ring.T:
380                self.Write(fmt2.format(dat[1],dat[0],dat[2]))           
381        self.CloseFile()
382        print(str(hist)+' written to file '+str(self.fullpath))
Note: See TracBrowser for help on using the repository browser.