source: trunk/exports/G2export_csv.py @ 3738

Last change on this file since 3738 was 3738, checked in by vondreele, 3 years ago

fix mag form factor table so Ce & Ce+2 are both seen. Comment section moved to end of dict.
fix export powder data as csv & text - python 3 vs 2 problem
fix p 62'2' operator problem - reverse order of generators
fixes for some incommensurate mag problems - still more to be found

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 19.0 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2018-11-26 16:31:06 +0000 (Mon, 26 Nov 2018) $
5# $Author: vondreele $
6# $Revision: 3738 $
7# $URL: trunk/exports/G2export_csv.py $
8# $Id: G2export_csv.py 3738 2018-11-26 16:31:06Z vondreele $
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: 3738 $")
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        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 Exporter(self,event=None):
296        '''Export a set of powder reflections as a csv file
297        '''
298        self.InitExport(event)
299        # load all of the tree into a set of dicts
300        self.loadTree()
301        if self.ExportSelect(): return  # set export parameters, get file name
302        self.OpenFile()
303        hist = list(self.histnam)[0] # there should only be one histogram, in any case take the 1st
304        histblk = self.Histograms[hist]
305        self.Write('"Histogram"')
306        self.Write('"'+hist+'"')
307        self.Write('')
308        # table of phases
309        self.Write('"Phase name","phase #"')
310        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
311            self.Write('"'+str(phasenam)+'",'+str(i))
312        self.Write('')
313        # note addition of a phase # flag at end (i)
314        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
315            phasDict = histblk['Reflection Lists'][phasenam]
316            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
317            if phasDict.get('Super',False):
318                WriteList(self,("h","k","l","m","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
319                if 'T' in phasDict['Type']:
320                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
321                else:
322                    fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
323                refList = phasDict['RefList']
324                for refItem in refList:
325                    if 'T' in phasDict['Type']:
326                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:17]
327                        FWHM = G2pwd.getgamFW(gam,sig)
328                        self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,i))
329                    else:        #convert to deg       
330                        h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:14]
331                        s = np.sqrt(max(sig,0.0001))/100.   #var -> sig in deg
332                        g = gam/100.    #-> deg
333                        FWHM = G2pwd.getgamFW(g,s)
334                        self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,i))
335            else:
336                WriteList(self,("h","k","l","d-sp",tname,"F_obs","F_calc","phase","mult","sig","gam","FWHM","Prfo","phase #"))
337                if 'T' in phasDict['Type']:
338                    fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.3f},{:.3f},{:.3f},{:.4f},{:d}"
339                else:
340                    fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:.5f},{:.5f},{:.5f},{:.4f},{:d}"
341                refList = phasDict['RefList']
342                for refItem in refList:
343                    if 'T' in phasDict['Type']:
344                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,x,x,x,Prfo = refItem[:16]
345                        FWHM = G2pwd.getgamFW(gam,sig)
346                        self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,sig,gam,FWHM,Prfo,i))
347                    else:        #convert to deg       
348                        h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr,Prfo = refItem[:13]
349                        g = gam/100.
350                        s = np.sqrt(max(sig,0.0001))/100.
351                        FWHM = G2pwd.getgamFW(g,s)
352                        self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,s,g,FWHM,Prfo,i))
353        self.CloseFile()
354        print(hist+' reflections written to file '+self.fullpath)
355
356class ExportSingleCSV(G2IO.ExportBaseclass):
357    '''Used to create a csv file with single crystal reflection data
358
359    :param wx.Frame G2frame: reference to main GSAS-II frame
360    '''
361    def __init__(self,G2frame):
362        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
363            G2frame=G2frame,
364            formatName = 'CSV file',
365            extension='.csv',
366            longFormatName = 'Export reflection list as a comma-separated (csv) file'
367            )
368        self.exporttype = ['single']
369        self.multiple = False # only allow one histogram to be selected
370
371    def Exporter(self,event=None):
372        '''Export a set of single crystal data as a csv file
373        '''
374        # the export process starts here
375        self.InitExport(event)
376        # load all of the tree into a set of dicts
377        self.loadTree()
378        if self.ExportSelect(): return  # set export parameters, get file name
379        self.OpenFile()
380        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
381        histblk = self.Histograms[hist]
382        for i,phasenam in enumerate(sorted(histblk['Reflection Lists'])):
383            phasDict = histblk['Reflection Lists'][phasenam]
384            tname = {'T':'TOF','C':'2-theta'}[phasDict['Type'][2]]
385            if phasDict.get('Super',False):
386                WriteList(self,("h","k","l","m",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #"))
387                fmt = "{:.0f},{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
388                refList = phasDict['RefList']
389                for refItem in refList:
390                    h,k,l,m,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:13]
391                    self.Write(fmt.format(h,k,l,m,dsp,pos,Fobs,Fcalc,phase,mult,i))               
392            else:
393                WriteList(self,("h","k","l",'d-sp',tname,"F_obs","F_calc","phase","mult","phase #"))
394                fmt = "{:.0f},{:.0f},{:.0f},{:.5f},{:.3f},{:.3f},{:.3f},{:.2f},{:.0f},{:d}"
395                refList = phasDict['RefList']
396                for refItem in refList:
397                    h,k,l,mult,dsp,pos,sig,gam,Fobs,Fcalc,phase,Icorr = refItem[:12]
398                    self.Write(fmt.format(h,k,l,dsp,pos,Fobs,Fcalc,phase,mult,i))
399        self.CloseFile()
400        print(hist+' written to file '+self.fullname)                       
401
402class ExportStrainCSV(G2IO.ExportBaseclass):
403    '''Used to create a csv file with single crystal reflection data
404
405    :param wx.Frame G2frame: reference to main GSAS-II frame
406    '''
407    def __init__(self,G2frame):
408        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
409            G2frame=G2frame,
410            formatName = 'Strain CSV file',
411            extension='.csv',
412            longFormatName = 'Export strain results as a comma-separated (csv) file'
413            )
414        self.exporttype = ['image']
415        self.multiple = False # only allow one histogram to be selected
416
417    def Exporter(self,event=None):
418        '''Export a set of single crystal data as a csv file
419        '''
420        # the export process starts here
421        self.InitExport(event)
422        # load all of the tree into a set of dicts
423        self.loadTree()
424        if self.ExportSelect(): return  # set export parameters, get file name
425        self.OpenFile()
426        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
427        histblk = self.Histograms[hist]
428        StrSta = histblk['Stress/Strain']
429        WriteList(self,("Dset","Dcalc","e11","sig(e11)","e12","sig(e12)","e22","sig(e22)"))
430        fmt = 2*"{:.5f},"+6*"{:.0f},"
431        fmt1 = "{:.5f}"
432        fmt2 = "{:.2f},{:.5f},{:.5f}"
433        for item in StrSta['d-zero']:
434            Emat = item['Emat']
435            Esig = item['Esig']
436            self.Write(fmt.format(item['Dset'],item['Dcalc'],Emat[0],Esig[0],Emat[1],Esig[1],Emat[2],Esig[2]))
437        for item in StrSta['d-zero']:
438            WriteList(self,("Azm","dobs","dcalc","Dset="+fmt1.format(item['Dset'])))
439            ring = np.vstack((item['ImtaObs'],item['ImtaCalc']))
440            for dat in ring.T:
441                self.Write(fmt2.format(dat[1],dat[0],dat[2]))           
442        self.CloseFile()
443        print(hist+' written to file '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.