source: trunk/exports/G2export_pwdr.py @ 1997

Last change on this file since 1997 was 1997, checked in by toby, 7 years ago

Add API for direct image read (G2IO.ExportPowderList?) and powder exports w/o GUI (G2IO.ExportPowderList?); redo export to add new method (Writer)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.4 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2015-10-10 16:58:12 +0000 (Sat, 10 Oct 2015) $
5# $Author: toby $
6# $Revision: 1997 $
7# $URL: trunk/exports/G2export_pwdr.py $
8# $Id: G2export_pwdr.py 1997 2015-10-10 16:58:12Z toby $
9########### SVN repository information ###################
10'''
11*Module G2export_pwdr: Export powder input files*
12-------------------------------------------------
13
14Creates files used by GSAS (FXYE) & TOPAS (XYE) as input
15
16'''
17import os.path
18import numpy as np
19import GSASIIpath
20GSASIIpath.SetVersionNumber("$Revision: 1997 $")
21import GSASIIIO as G2IO
22import GSASIIpy3 as G2py3
23import GSASIIobj as G2obj
24
25class 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.6e%15.6e%15.6e%15.6e   ')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0))
59        self.Write(('INS  1PRCF12%15.6e%15.6e%15.6e%15.6e   ')%(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 Writer(self,TreeName,filename=None,prmname=''):
65        '''Write a single PWDR entry to a FXYE file
66        '''
67        histblk = self.Histograms[TreeName]
68        self.OpenFile(filename) # ***rethink
69        self.Write(TreeName[5:])
70        if prmname: self.Write('Instrument parameter file:'+os.path.split(prmname)[1])
71        x = 100*np.array(histblk['Data'][0])
72        # convert weights to sigmas; use largest weight as minimum esd
73        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
74        s[s==0] = np.max(s)
75        s = 1./s
76        self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % (
77            len(x),len(x),x[0],(x[1]-x[0])
78            ))
79#            for X,Y,S in zip(x,histblk['Data'][1],s):
80#                self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S))
81        for XYS in zip(x,histblk['Data'][1],s):
82            line = ''
83            for val in XYS:
84                line += G2py3.FormatPadValue(val,(15,6))
85            self.Write(line)
86        self.CloseFile()
87       
88    def Exporter(self,event=None):
89        '''Export one or more sets of powder data as FXYE file(s)
90        '''
91        # the export process starts here
92        self.InitExport(event)
93        self.loadTree() # load all of the tree into a set of dicts
94        if self.ExportSelect( # set export parameters
95            AskFile='single' # get a file name/directory to save in
96            ): return
97        filenamelist = []
98        for hist in self.histnam:
99            if len(self.histnam) > 1:
100                # multiple files: create a unique name from the histogram
101                fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
102                # create an instrument parameter file
103                self.filename = os.path.join(self.dirname,fileroot + self.extension)
104            else:
105                # use the supplied name, but force the extension
106                self.filename= os.path.splitext(self.filename)[0] + self.extension
107
108            prmname = self.WriteInstFile(TreeName,histblk['Instrument Parameters'][0])
109            self.Writer(hist,prmname=prmname)
110            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
111
112
113class ExportPowderXYE(G2IO.ExportBaseclass):
114    '''Used to create a Topas XYE file for a powder data set
115
116    :param wx.Frame G2frame: reference to main GSAS-II frame
117    '''
118    def __init__(self,G2frame):
119        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
120            G2frame=G2frame,
121            formatName = 'Topas XYE file',
122            extension='.xye',
123            longFormatName = 'Export powder data as Topas XYE (column) file'
124            )
125        self.exporttype = ['powder']
126        self.multiple = True
127       
128    def Writer(self,TreeName,filename=None):
129        GSASIIpath.IPyBreak()
130
131        self.OpenFile()
132        histblk = self.Histograms[TreeName]
133        self.Write('/*')    #The ugly c comment delimiter used in topas!
134        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
135        self.Write('*/')
136        x = np.array(histblk['Data'][0])
137        # convert weights to sigmas; use largest weight as minimum esd
138        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
139        s[s==0] = np.max(s)
140        s = 1./s
141        for XYS in zip(x,histblk['Data'][1],s):
142            line = ''
143            for val in XYS:
144                line += G2py3.FormatPadValue(val,(15,6))
145            self.Write(line)
146        self.CloseFile()
147
148    def Exporter(self,event=None):
149        '''Export one or more sets of powder data as XYE file(s)
150        '''
151        # the export process starts here
152        self.InitExport(event)
153        # load all of the tree into a set of dicts
154        self.loadTree()
155        if self.ExportSelect( # set export parameters
156            AskFile='single' # get a file name/directory to save in
157            ): return
158        filenamelist = []
159        for hist in self.histnam:
160            if len(self.histnam) > 1:
161                # multiple files: create a unique name from the histogram
162                fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
163                # create an instrument parameter file
164                self.filename = os.path.join(self.dirname,fileroot + self.extension)
165            else:
166                # use the supplied name, but force the extension
167                self.filename= os.path.splitext(self.filename)[0] + self.extension
168
169            self.Writer(hist)
170            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
Note: See TracBrowser for help on using the repository browser.