source: trunk/exports/G2export_csv.py @ 4119

Last change on this file since 4119 was 4119, checked in by toby, 2 years ago

add reflection output in scripting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 22.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2019-08-29 04:11:22 +0000 (Thu, 29 Aug 2019) $
5# $Author: toby $
6# $Revision: 4119 $
7# $URL: trunk/exports/G2export_csv.py $
8# $Id: G2export_csv.py 4119 2019-08-29 04:11:22Z 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: 4119 $")
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 = '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        WriteList(self,("x","y_obs","weight","y_calc","y_bkg","Q"))
191        digitList = 2*((13,3),) + ((13,5),) + 3*((13,3),)
192        for vallist in zip(histblk['Data'][0],
193                       histblk['Data'][1],
194                       histblk['Data'][2],
195                       histblk['Data'][3],
196                       histblk['Data'][4],
197                       #histblk['Data'][5],
198                       2*np.pi/G2lat.Pos2dsp(Parms,histblk['Data'][0])
199                       ):
200            line = ""
201            for val,digits in zip(vallist,digitList):
202                if line: line += ','
203                line += G2py3.FormatValue(val,digits)
204            self.Write(line)
205        self.CloseFile()
206       
207    def Exporter(self,event=None):
208        '''Export a set of powder data as a csv file
209        '''
210        # the export process starts here
211        self.InitExport(event)
212        # load all of the tree into a set of dicts
213        self.loadTree()
214        if self.ExportSelect( # set export parameters
215            AskFile='single' # get a file name/directory to save in
216            ): return
217        filenamelist = []
218        for hist in self.histnam:
219            if len(self.histnam) == 1:
220                name = self.filename
221            else:    # multiple files: create a unique name from the histogram
222                name = self.MakePWDRfilename(hist)
223            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
224            # create the file
225            self.filename = os.path.join(self.dirname,fileroot + self.extension)
226            self.Writer(hist)
227            print('Histogram '+hist+' written to file '+self.fullpath)
228
229class ExportMultiPowderCSV(G2IO.ExportBaseclass):
230    '''Used to create a csv file for a stack of powder data sets suitable for display
231    purposes only; no y-calc or weights are exported only x & y-obs
232    :param wx.Frame G2frame: reference to main GSAS-II frame
233    '''
234    def __init__(self,G2frame):
235        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
236            G2frame=G2frame,
237            formatName = 'stacked CSV file',
238            extension='.csv',
239            longFormatName = 'Export powder data sets as a (csv) file - x,y-o1,y-o2,... only'
240            )
241        self.exporttype = ['powder']
242        #self.multiple = False # only allow one histogram to be selected
243        self.multiple = True
244
245    def Exporter(self,event=None):
246        '''Export a set of powder data as a single csv file
247        '''
248        # the export process starts here
249        self.InitExport(event)
250        # load all of the tree into a set of dicts
251        self.loadTree()
252        if self.ExportSelect( # set export parameters
253            AskFile='ask' # only one file is ever written
254            ): return
255        csvData = []
256        headList = ["x",]
257        digitList = []
258        self.filename = os.path.join(self.dirname,os.path.splitext(self.filename)[0]
259                                     + self.extension)
260        for ihst,hist in enumerate(self.histnam):
261            histblk = self.Histograms[hist]
262            headList.append('y_obs_'+G2obj.StripUnicode(hist[5:].replace(' ','_')))
263            if not ihst:
264                digitList = [(13,3),]
265                csvData.append(histblk['Data'][0])
266            digitList += [(13,3),]
267            csvData.append(histblk['Data'][1])
268            print('Histogram '+hist+' added to file...')
269        self.OpenFile()
270        WriteList(self,headList)
271        for vallist in np.array(csvData).T:
272            line = ""
273            for val,digits in zip(vallist,digitList):
274                if line: line += ','
275                line += G2py3.FormatValue(val,digits)
276            self.Write(line)
277        self.CloseFile()
278        print('...file '+self.fullpath+' written')
279
280class ExportPowderReflCSV(G2IO.ExportBaseclass):
281    '''Used to create a csv file of reflections from a powder data set
282
283    :param wx.Frame G2frame: reference to main GSAS-II frame
284    '''
285    def __init__(self,G2frame):
286        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
287            G2frame=G2frame,
288            formatName = 'reflection list as CSV',
289            extension='.csv',
290            longFormatName = 'Export powder reflection list as a comma-separated (csv) file'
291            )
292        self.exporttype = ['powder']
293        self.multiple = False # only allow one histogram to be selected
294
295    def Writer(self,TreeName,filename=None):
296        print(filename)
297        self.OpenFile(filename)
298        histblk = self.Histograms[TreeName]
299        self.write(TreeName,histblk)
300        self.CloseFile()
301        print(TreeName+' reflections written to file '+self.fullpath)
302       
303    def Exporter(self,event=None):
304        '''Export a set of powder reflections as a csv file
305        '''
306        self.InitExport(event)
307        # load all of the tree into a set of dicts
308        self.loadTree()
309        if self.ExportSelect(): return  # set export parameters, get file name
310        hist = list(self.histnam)[0] # there should only be one histogram, in any case take the 1st
311        histblk = self.Histograms[hist]
312        self.OpenFile()
313        self.write(hist,histblk)
314        self.CloseFile()
315        print(hist+' reflections written to file '+self.fullpath)
316       
317    def write(self,hist,histblk):
318        self.Write('"Histogram"')
319        self.Write('"'+hist+'"')
320        self.Write('')
321        # table of phases
322        self.Write('"Phase name","phase #"')
323        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
324            self.Write('"'+str(phasenam)+'",'+str(i))
325        self.Write('')
326        # note addition of a phase # flag at end (i)
327        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
328            phasDict = histblk['Reflection Lists'][phasenam]
329            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
330            if phasDict.get('Super',False):
331                WriteList(self,("h","k","l","m","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
332                if 'T' in phasDict['Type']:
333                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
334                else:
335                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
336                refList = phasDict['RefList']
337                for refItem in refList:
338                    if 'T' in phasDict['Type']:
339                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:17]
340                        FWHM = G2pwd.getgamFW(gam,sig)
341                        self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,i))
342                    else:        #convert to deg       
343                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:14]
344                        s = np.sqrt(max(sig,0.0001))/100.   #var -> sig in deg
345                        g = gam/100.    #-> deg
346                        FWHM = G2pwd.getgamFW(g,s)
347                        self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,i))
348            else:
349                WriteList(self,("h","k","l","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
350                if 'T' in phasDict['Type']:
351                    fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
352                else:
353                    fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
354                refList = phasDict['RefList']
355                for refItem in refList:
356                    if 'T' in phasDict['Type']:
357                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:16]
358                        FWHM = G2pwd.getgamFW(gam,sig)
359                        self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,Prfo,i))
360                    else:        #convert to deg       
361                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:13]
362                        g = gam/100.
363                        s = np.sqrt(max(sig,0.0001))/100.
364                        FWHM = G2pwd.getgamFW(g,s)
365                        self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,Prfo,i))
366       
367class ExportSASDCSV(G2IO.ExportBaseclass):
368    '''Used to create a csv file for a small angle data set
369
370    :param wx.Frame G2frame: reference to main GSAS-II frame
371    '''
372    def __init__(self,G2frame):
373        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
374            G2frame=G2frame,
375            formatName = 'CSV file',
376            extension='.csv',
377            longFormatName = 'Export small angle data as comma-separated (csv) file'
378            )
379        self.exporttype = ['sasd']
380        #self.multiple = False # only allow one histogram to be selected
381        self.multiple = True
382
383    def Writer(self,TreeName,filename=None):
384        self.OpenFile(filename)
385        histblk = self.Histograms[TreeName]
386        if len(self.Histograms[TreeName]['Models']['Size']['Distribution']):
387            self.Write('"Size Distribution"')
388            Distr = np.array(self.Histograms[TreeName]['Models']['Size']['Distribution'])
389            WriteList(self,("bin_pos","bin_width","bin_value"))
390            digitList = 2*((13,3),)+((13,4,'g'),)
391            for bindata in Distr.T:
392                line = ""
393                for val,digits in zip(bindata,digitList):
394                    if line: line += ','
395                    line += G2py3.FormatValue(val,digits)
396                self.Write(line)           
397        self.Write('"Small angle data"')
398        Parms = self.Histograms[TreeName]['Instrument Parameters'][0]
399        for parm in Parms:
400            if parm in ['Type','Source',]:
401                line = '"Instparm: %s","%s"'%(parm,Parms[parm][0])
402            elif parm in ['Lam',]:
403                line = '"Instparm: %s",%10.6f'%(parm,Parms[parm][1])
404            else:
405                line = '"Instparm: %s",%10.2f'%(parm,Parms[parm][1])
406            self.Write(line)
407        WriteList(self,("q","y_obs","y_sig","y_calc","y_bkg"))
408        digitList = 5*((13,5,'g'),)
409        for vallist in zip(histblk['Data'][0],
410                       histblk['Data'][1],
411                       1./np.sqrt(histblk['Data'][2]),
412                       histblk['Data'][3],
413                       histblk['Data'][4],
414                       ):
415            line = ""
416            for val,digits in zip(vallist,digitList):
417                if line: line += ','
418                line += G2py3.FormatValue(val,digits)
419            self.Write(line)
420        self.CloseFile()
421       
422    def Exporter(self,event=None):
423        '''Export a set of small angle data as a csv file
424        '''
425        # the export process starts here
426        self.InitExport(event)
427        # load all of the tree into a set of dicts
428        self.loadTree()
429        if self.ExportSelect( # set export parameters
430            AskFile='single' # get a file name/directory to save in
431            ): return
432        filenamelist = []
433        for hist in self.histnam:
434            if len(self.histnam) == 1:
435                name = self.filename
436            else:    # multiple files: create a unique name from the histogram
437                name = self.MakePWDRfilename(hist)
438            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
439            # create the file
440            self.filename = os.path.join(self.dirname,fileroot + self.extension)
441            self.Writer(hist)
442            print('Histogram '+hist+' written to file '+self.fullpath)
443
444class ExportSingleCSV(G2IO.ExportBaseclass):
445    '''Used to create a csv file with single crystal reflection data
446
447    :param wx.Frame G2frame: reference to main GSAS-II frame
448    '''
449    def __init__(self,G2frame):
450        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
451            G2frame=G2frame,
452            formatName = 'CSV file',
453            extension='.csv',
454            longFormatName = 'Export reflection list as a comma-separated (csv) file'
455            )
456        self.exporttype = ['single']
457        self.multiple = False # only allow one histogram to be selected
458
459    def Exporter(self,event=None):
460        '''Export a set of single crystal data as a csv file
461        '''
462        # the export process starts here
463        self.InitExport(event)
464        # load all of the tree into a set of dicts
465        self.loadTree()
466        if self.ExportSelect(): return  # set export parameters, get file name
467        self.OpenFile()
468        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
469        histblk = self.Histograms[hist]
470        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
471            phasDict = histblk['Reflection Lists'][phasenam]
472            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
473            if phasDict.get('Super',False):
474                WriteList(self,("h","k","l","m",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #"))
475                fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
476                refList = phasDict['RefList']
477                for refItem in refList:
478                    h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:13]
479                    self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,i))               
480            else:
481                WriteList(self,("h","k","l",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #"))
482                fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
483                refList = phasDict['RefList']
484                for refItem in refList:
485                    h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:12]
486                    self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,i))
487        self.CloseFile()
488        print(hist+' written to file '+self.fullname)                       
489
490class ExportStrainCSV(G2IO.ExportBaseclass):
491    '''Used to create a csv file with single crystal reflection data
492
493    :param wx.Frame G2frame: reference to main GSAS-II frame
494    '''
495    def __init__(self,G2frame):
496        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
497            G2frame=G2frame,
498            formatName = 'Strain CSV file',
499            extension='.csv',
500            longFormatName = 'Export strain results as a comma-separated (csv) file'
501            )
502        self.exporttype = ['image']
503        self.multiple = False # only allow one histogram to be selected
504
505    def Exporter(self,event=None):
506        '''Export a set of single crystal data as a csv file
507        '''
508        # the export process starts here
509        self.InitExport(event)
510        # load all of the tree into a set of dicts
511        self.loadTree()
512        if self.ExportSelect(): return  # set export parameters, get file name
513        self.OpenFile()
514        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
515        histblk = self.Histograms[hist]
516        StrSta = histblk['Stress/Strain']
517        WriteList(self,("Dset","Dcalc","e11","sig(e11)","e12","sig(e12)","e22","sig(e22)"))
518        fmt = 2*"{:.5f},"+6*"{:.0f},"
519        fmt1 = "{:.5f}"
520        fmt2 = "{:.2f},{:.5f},{:.5f}"
521        for item in StrSta['d-zero']:
522            Emat = item['Emat']
523            Esig = item['Esig']
524            self.Write(fmt.format(item['Dset'],item['Dcalc'],Emat[0],Esig[0],Emat[1],Esig[1],Emat[2],Esig[2]))
525        for item in StrSta['d-zero']:
526            WriteList(self,("Azm","dobs","dcalc","Dset="+fmt1.format(item['Dset'])))
527            ring = np.vstack((item['ImtaObs'],item['ImtaCalc']))
528            for dat in ring.T:
529                self.Write(fmt2.format(dat[1],dat[0],dat[2]))           
530        self.CloseFile()
531        print(hist+' written to file '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.