source: trunk/exports/G2export_csv.py @ 3299

Last change on this file since 3299 was 3299, checked in by toby, 4 years ago

Add Q to powder CSV export

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 18.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2018-03-01 05:59:19 +0000 (Thu, 01 Mar 2018) $
5# $Author: toby $
6# $Revision: 3299 $
7# $URL: trunk/exports/G2export_csv.py $
8# $Id: G2export_csv.py 3299 2018-03-01 05:59:19Z 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'''
18from __future__ import division, print_function
19import os.path
20import numpy as np
21import GSASIIpath
22GSASIIpath.SetVersionNumber("$Revision: 3299 $")
23import GSASIIIO as G2IO
24import GSASIIpy3 as G2py3
25import GSASIIobj as G2obj
26import GSASIImath as G2mth
27import GSASIIpwd as G2pwd
28import GSASIIlattice as G2lat
29
30def 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
42class 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
161class 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 = '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        WriteList(self,("x","y_obs","weight","y_calc","y_bkg","Q"))
183        digitList = 2*((13,3),) + ((13,5),) + 3*((13,3),)
184        for vallist in zip(histblk['Data'][0],
185                       histblk['Data'][1],
186                       histblk['Data'][2],
187                       histblk['Data'][3],
188                       histblk['Data'][4],
189                       #histblk['Data'][5],
190                       2*np.pi/G2lat.Pos2dsp(Parms,histblk['Data'][0])
191                       ):
192            line = ""
193            for val,digits in zip(vallist,digitList):
194                if line: line += ','
195                line += G2py3.FormatValue(val,digits)
196            self.Write(line)
197        self.CloseFile()
198       
199    def Exporter(self,event=None):
200        '''Export a set of powder data as a csv file
201        '''
202        # the export process starts here
203        self.InitExport(event)
204        # load all of the tree into a set of dicts
205        self.loadTree()
206        if self.ExportSelect( # set export parameters
207            AskFile='single' # get a file name/directory to save in
208            ): return
209        filenamelist = []
210        for hist in self.histnam:
211            if len(self.histnam) == 1:
212                name = self.filename
213            else:    # multiple files: create a unique name from the histogram
214                name = self.MakePWDRfilename(hist)
215            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
216            # create the file
217            self.filename = os.path.join(self.dirname,fileroot + self.extension)
218            self.Writer(hist)
219            print('Histogram '+hist+' written to file '+self.fullpath)
220
221class ExportMultiPowderCSV(G2IO.ExportBaseclass):
222    '''Used to create a csv file for a stack of powder data sets suitable for display
223    purposes only; no y-calc or weights are exported only x & y-obs
224    :param wx.Frame G2frame: reference to main GSAS-II frame
225    '''
226    def __init__(self,G2frame):
227        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
228            G2frame=G2frame,
229            formatName = 'stacked CSV file',
230            extension='.csv',
231            longFormatName = 'Export powder data sets as a (csv) file - x,y-o1,y-o2,... only'
232            )
233        self.exporttype = ['powder']
234        #self.multiple = False # only allow one histogram to be selected
235        self.multiple = True
236
237    def Exporter(self,event=None):
238        '''Export a set of powder data as a single csv file
239        '''
240        # the export process starts here
241        self.InitExport(event)
242        # load all of the tree into a set of dicts
243        self.loadTree()
244        if self.ExportSelect( # set export parameters
245            AskFile='ask' # only one file is ever written
246            ): return
247        csvData = []
248        headList = ["x",]
249        digitList = []
250        self.filename = os.path.join(self.dirname,os.path.splitext(self.filename)[0]
251                                     + self.extension)
252        for ihst,hist in enumerate(self.histnam):
253            histblk = self.Histograms[hist]
254            headList.append('y_obs_'+G2obj.StripUnicode(hist[5:].replace(' ','_')))
255            if not ihst:
256                digitList = [(13,3),]
257                csvData.append(histblk['Data'][0])
258            digitList += [(13,3),]
259            csvData.append(histblk['Data'][1])
260            print('Histogram '+hist+' added to file...')
261        self.OpenFile()
262        WriteList(self,headList)
263        for vallist in np.array(csvData).T:
264            line = ""
265            for val,digits in zip(vallist,digitList):
266                if line: line += ','
267                line += G2py3.FormatValue(val,digits)
268            self.Write(line)
269        self.CloseFile()
270        print('...file '+self.fullpath+' written')
271
272class ExportPowderReflCSV(G2IO.ExportBaseclass):
273    '''Used to create a csv file of reflections from a powder data set
274
275    :param wx.Frame G2frame: reference to main GSAS-II frame
276    '''
277    def __init__(self,G2frame):
278        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
279            G2frame=G2frame,
280            formatName = 'reflection list as CSV',
281            extension='.csv',
282            longFormatName = 'Export powder reflection list as a comma-separated (csv) file'
283            )
284        self.exporttype = ['powder']
285        self.multiple = False # only allow one histogram to be selected
286
287    def Exporter(self,event=None):
288        '''Export a set of powder reflections as a csv file
289        '''
290        self.InitExport(event)
291        # load all of the tree into a set of dicts
292        self.loadTree()
293        if self.ExportSelect(): return  # set export parameters, get file name
294        self.OpenFile()
295        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
296        histblk = self.Histograms[hist]
297        self.Write('"Histogram"')
298        self.Write('"'+hist+'"')
299        self.Write('')
300        # table of phases
301        self.Write('"Phase name","phase #"')
302        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
303            self.Write('"'+str(phasenam)+'",'+str(i))
304        self.Write('')
305        # note addition of a phase # flag at end (i)
306        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
307            phasDict = histblk['Reflection Lists'][phasenam]
308            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
309            if phasDict.get('Super',False):
310                WriteList(self,("h","k","l","m","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
311                if 'T' in phasDict['Type']:
312                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
313                else:
314                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
315                refList = phasDict['RefList']
316                for refItem in refList:
317                    if 'T' in phasDict['Type']:
318                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:17]
319                        FWHM = G2pwd.getgamFW(gam,sig)
320                        self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,i))
321                    else:        #convert to deg       
322                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:14]
323                        s = np.sqrt(max(sig,0.0001))/100.   #var -> sig in deg
324                        g = gam/100.    #-> deg
325                        FWHM = G2pwd.getgamFW(g,s)
326                        self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,i))
327            else:
328                WriteList(self,("h","k","l","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
329                if 'T' in phasDict['Type']:
330                    fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
331                else:
332                    fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
333                refList = phasDict['RefList']
334                for refItem in refList:
335                    if 'T' in phasDict['Type']:
336                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:16]
337                        FWHM = G2pwd.getgamFW(gam,sig)
338                        self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,Prfo,i))
339                    else:        #convert to deg       
340                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:13]
341                        g = gam/100.
342                        s = np.sqrt(max(sig,0.0001))/100.
343                        FWHM = G2pwd.getgamFW(g,s)
344                        self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,Prfo,i))
345        self.CloseFile()
346        print(hist+' reflections written to file '+self.fullpath)
347
348class ExportSingleCSV(G2IO.ExportBaseclass):
349    '''Used to create a csv file with single crystal reflection data
350
351    :param wx.Frame G2frame: reference to main GSAS-II frame
352    '''
353    def __init__(self,G2frame):
354        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
355            G2frame=G2frame,
356            formatName = 'CSV file',
357            extension='.csv',
358            longFormatName = 'Export reflection list as a comma-separated (csv) file'
359            )
360        self.exporttype = ['single']
361        self.multiple = False # only allow one histogram to be selected
362
363    def Exporter(self,event=None):
364        '''Export a set of single crystal data as a csv file
365        '''
366        # the export process starts here
367        self.InitExport(event)
368        # load all of the tree into a set of dicts
369        self.loadTree()
370        if self.ExportSelect(): return  # set export parameters, get file name
371        self.OpenFile()
372        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
373        histblk = self.Histograms[hist]
374        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
375            phasDict = histblk['Reflection Lists'][phasenam]
376            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
377            if phasDict.get('Super',False):
378                WriteList(self,("h","k","l","m",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #"))
379                fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
380                refList = phasDict['RefList']
381                for refItem in refList:
382                    h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:13]
383                    self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,i))               
384            else:
385                WriteList(self,("h","k","l",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #"))
386                fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
387                refList = phasDict['RefList']
388                for refItem in refList:
389                    h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:12]
390                    self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,i))
391        self.CloseFile()
392        print(hist+' written to file '+self.fullname)                       
393
394class ExportStrainCSV(G2IO.ExportBaseclass):
395    '''Used to create a csv file with single crystal reflection data
396
397    :param wx.Frame G2frame: reference to main GSAS-II frame
398    '''
399    def __init__(self,G2frame):
400        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
401            G2frame=G2frame,
402            formatName = 'Strain CSV file',
403            extension='.csv',
404            longFormatName = 'Export strain results as a comma-separated (csv) file'
405            )
406        self.exporttype = ['image']
407        self.multiple = False # only allow one histogram to be selected
408
409    def Exporter(self,event=None):
410        '''Export a set of single crystal data as a csv file
411        '''
412        # the export process starts here
413        self.InitExport(event)
414        # load all of the tree into a set of dicts
415        self.loadTree()
416        if self.ExportSelect(): return  # set export parameters, get file name
417        self.OpenFile()
418        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
419        histblk = self.Histograms[hist]
420        StrSta = histblk['Stress/Strain']
421        WriteList(self,("Dset","Dcalc","e11","sig(e11)","e12","sig(e12)","e22","sig(e22)"))
422        fmt = 2*"{:.5f},"+6*"{:.0f},"
423        fmt1 = "{:.5f}"
424        fmt2 = "{:.2f},{:.5f},{:.5f}"
425        for item in StrSta['d-zero']:
426            Emat = item['Emat']
427            Esig = item['Esig']
428            self.Write(fmt.format(item['Dset'],item['Dcalc'],Emat[0],Esig[0],Emat[1],Esig[1],Emat[2],Esig[2]))
429        for item in StrSta['d-zero']:
430            WriteList(self,("Azm","dobs","dcalc","Dset="+fmt1.format(item['Dset'])))
431            ring = np.vstack((item['ImtaObs'],item['ImtaCalc']))
432            for dat in ring.T:
433                self.Write(fmt2.format(dat[1],dat[0],dat[2]))           
434        self.CloseFile()
435        print(hist+' written to file '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.