1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2014-01-10 14:46:36 -0600 (Fri, 10 Jan 2014) $ |
---|
5 | # $Author: vondreele $ |
---|
6 | # $Revision: 1191 $ |
---|
7 | # $URL: https://subversion.xor.aps.anl.gov/pyGSAS/trunk/exports/G2export_csv.py $ |
---|
8 | # $Id: G2export_csv.py 1191 2014-01-10 20:46:36Z vondreele $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | ''' |
---|
11 | *Module G2export_pwdr: Export powder input files* |
---|
12 | ------------------------------------------------- |
---|
13 | |
---|
14 | Creates files used by GSAS (FXYE) & TOPAS (XYE) as input |
---|
15 | |
---|
16 | ''' |
---|
17 | import os.path |
---|
18 | import numpy as np |
---|
19 | import GSASIIpath |
---|
20 | GSASIIpath.SetVersionNumber("$Revision: 1191 $") |
---|
21 | import GSASIIIO as G2IO |
---|
22 | import GSASIIpy3 as G2py3 |
---|
23 | import GSASIIobj as G2obj |
---|
24 | |
---|
25 | class ExportPowderFXYE(G2IO.ExportBaseclass): |
---|
26 | '''Used to create a FXYE file for a powder data set |
---|
27 | |
---|
28 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
29 | ''' |
---|
30 | def __init__(self,G2frame): |
---|
31 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
32 | G2frame=G2frame, |
---|
33 | formatName = 'GSAS FXYE file', |
---|
34 | extension='.fxye', |
---|
35 | longFormatName = 'Export powder data as GSAS FXYE (column) file' |
---|
36 | ) |
---|
37 | self.exporttype = ['powder'] |
---|
38 | self.multiple = True |
---|
39 | |
---|
40 | def WriteInstFile(self,hist,Inst): |
---|
41 | '''Write an instrument parameter file |
---|
42 | ''' |
---|
43 | prmname = os.path.splitext(self.filename)[0] + '.prm' |
---|
44 | prmname = os.path.join(self.dirname,prmname) |
---|
45 | self.OpenFile(prmname) |
---|
46 | self.Write( ' 123456789012345678901234567890123456789012345678901234567890 ') |
---|
47 | self.Write( 'INS BANK 1 ') |
---|
48 | self.Write(('INS HTYPE %sR ')%(Inst['Type'][0])) |
---|
49 | if 'Lam1' in Inst: #Ka1 & Ka2 |
---|
50 | self.Write(('INS 1 ICONS%10.7f%10.7f 0.0000 0.990 0 0.500 ')%(Inst['Lam1'][0],Inst['Lam2'][0])) |
---|
51 | elif 'Lam' in Inst: #single wavelength |
---|
52 | self.Write(('INS 1 ICONS%10.7f%10.7f 0.0000 0.990 0 0.500 ')%(Inst['Lam'][1],0.0)) |
---|
53 | self.Write( 'INS 1 IRAD 0 ') |
---|
54 | self.Write( 'INS 1I HEAD ') |
---|
55 | self.Write( 'INS 1I ITYP 0 0.0000 180.0000 1 ') |
---|
56 | self.Write(('INS 1DETAZM%10.3f ')%(Inst['Azimuth'][0])) |
---|
57 | self.Write( 'INS 1PRCF1 3 8 0.00100 ') |
---|
58 | self.Write(('INS 1PRCF11 %15.6g%15.6g%15.6g%15.6g ')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0)) |
---|
59 | self.Write(('INS 1PRCF12 %15.6g%15.6g%15.6g%15.6g ')%(Inst['X'][1],Inst['Y'][1],Inst['SH/L'][1]/2.,Inst['SH/L'][1]/2.)) |
---|
60 | self.CloseFile() |
---|
61 | print('Parameters from '+str(hist)+' written to file '+str(prmname)) |
---|
62 | return prmname |
---|
63 | |
---|
64 | def Exporter(self,event=None): |
---|
65 | '''Export one or more sets of powder data as FXYE file(s) |
---|
66 | ''' |
---|
67 | # the export process starts here |
---|
68 | self.InitExport(event) |
---|
69 | # load all of the tree into a set of dicts |
---|
70 | self.loadTree() |
---|
71 | if self.ExportSelect( # set export parameters |
---|
72 | AskFile='single' # get a file name/directory to save in |
---|
73 | ): return |
---|
74 | filenamelist = [] |
---|
75 | for hist in self.histnam: |
---|
76 | if len(self.histnam) > 1: |
---|
77 | # multiple files: create a unique name from the histogram |
---|
78 | fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist) |
---|
79 | # create an instrument parameter file |
---|
80 | self.filename = os.path.join(self.dirname,fileroot + self.extension) |
---|
81 | else: |
---|
82 | # use the supplied name, but force the extension |
---|
83 | self.filename= os.path.splitext(self.filename)[0] + self.extension |
---|
84 | |
---|
85 | histblk = self.Histograms[hist] |
---|
86 | prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0]) |
---|
87 | |
---|
88 | self.OpenFile() |
---|
89 | self.Write(hist[5:]) |
---|
90 | self.Write('Instrument parameter file:'+os.path.split(prmname)[1]) |
---|
91 | x = 100*np.array(histblk['Data'][0]) |
---|
92 | # convert weights to sigmas; use largest weight as minimum esd |
---|
93 | s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2]))) |
---|
94 | s[s==0] = np.max(s) |
---|
95 | s = 1./s |
---|
96 | self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % ( |
---|
97 | len(x),len(x),x[0],(x[1]-x[0]) |
---|
98 | )) |
---|
99 | # for X,Y,S in zip(x,histblk['Data'][1],s): |
---|
100 | # self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S)) |
---|
101 | for XYS in zip(x,histblk['Data'][1],s): |
---|
102 | line = '' |
---|
103 | for val in XYS: |
---|
104 | line += G2py3.FormatPadValue(val,(15,6)) |
---|
105 | self.Write(line) |
---|
106 | self.CloseFile() |
---|
107 | print('Histogram '+str(hist)+' written to file '+str(self.fullpath)) |
---|
108 | |
---|
109 | |
---|
110 | class ExportPowderXYE(G2IO.ExportBaseclass): |
---|
111 | '''Used to create a Topas XYE file for a powder data set |
---|
112 | |
---|
113 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
114 | ''' |
---|
115 | def __init__(self,G2frame): |
---|
116 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
117 | G2frame=G2frame, |
---|
118 | formatName = 'Topas XYE file', |
---|
119 | extension='.xye', |
---|
120 | longFormatName = 'Export powder data as Topas XYE (column) file' |
---|
121 | ) |
---|
122 | self.exporttype = ['powder'] |
---|
123 | self.multiple = True |
---|
124 | |
---|
125 | def Exporter(self,event=None): |
---|
126 | '''Export one or more sets of powder data as XYE file(s) |
---|
127 | ''' |
---|
128 | # the export process starts here |
---|
129 | self.InitExport(event) |
---|
130 | # load all of the tree into a set of dicts |
---|
131 | self.loadTree() |
---|
132 | if self.ExportSelect( # set export parameters |
---|
133 | AskFile='single' # get a file name/directory to save in |
---|
134 | ): return |
---|
135 | filenamelist = [] |
---|
136 | for hist in self.histnam: |
---|
137 | if len(self.histnam) > 1: |
---|
138 | # multiple files: create a unique name from the histogram |
---|
139 | fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist) |
---|
140 | # create an instrument parameter file |
---|
141 | self.filename = os.path.join(self.dirname,fileroot + self.extension) |
---|
142 | else: |
---|
143 | # use the supplied name, but force the extension |
---|
144 | self.filename= os.path.splitext(self.filename)[0] + self.extension |
---|
145 | |
---|
146 | self.OpenFile() |
---|
147 | histblk = self.Histograms[hist] |
---|
148 | self.Write('# '+hist[5:]) |
---|
149 | x = np.array(histblk['Data'][0]) |
---|
150 | # convert weights to sigmas; use largest weight as minimum esd |
---|
151 | s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2]))) |
---|
152 | s[s==0] = np.max(s) |
---|
153 | s = 1./s |
---|
154 | for XYS in zip(x,histblk['Data'][1],s): |
---|
155 | line = '' |
---|
156 | for val in XYS: |
---|
157 | line += G2py3.FormatPadValue(val,(15,6)) |
---|
158 | self.Write(line) |
---|
159 | self.CloseFile() |
---|
160 | print('Histogram '+str(hist)+' written to file '+str(self.fullpath)) |
---|