source: trunk/exports/G2export_pwdr.py @ 2018

Last change on this file since 2018 was 2018, checked in by toby, 8 years ago

fix export bugs; prevent exception if Cancel is used when locating a missing image

  • 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-20 16:37:34 +0000 (Tue, 20 Oct 2015) $
5# $Author: toby $
6# $Revision: 2018 $
7# $URL: trunk/exports/G2export_pwdr.py $
8# $Id: G2export_pwdr.py 2018 2015-10-20 16:37:34Z 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: 2018 $")
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            histblk = self.Histograms[hist]
109            prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0])
110            self.Writer(hist,prmname=prmname)
111            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
112
113
114class ExportPowderXYE(G2IO.ExportBaseclass):
115    '''Used to create a Topas XYE file for a powder data set
116
117    :param wx.Frame G2frame: reference to main GSAS-II frame
118    '''
119    def __init__(self,G2frame):
120        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
121            G2frame=G2frame,
122            formatName = 'Topas XYE file',
123            extension='.xye',
124            longFormatName = 'Export powder data as Topas XYE (column) file'
125            )
126        self.exporttype = ['powder']
127        self.multiple = True
128       
129    def Writer(self,TreeName,filename=None):
130        GSASIIpath.IPyBreak()
131
132        self.OpenFile()
133        histblk = self.Histograms[TreeName]
134        self.Write('/*')    #The ugly c comment delimiter used in topas!
135        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
136        self.Write('*/')
137        x = np.array(histblk['Data'][0])
138        # convert weights to sigmas; use largest weight as minimum esd
139        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
140        s[s==0] = np.max(s)
141        s = 1./s
142        for XYS in zip(x,histblk['Data'][1],s):
143            line = ''
144            for val in XYS:
145                line += G2py3.FormatPadValue(val,(15,6))
146            self.Write(line)
147        self.CloseFile()
148
149    def Exporter(self,event=None):
150        '''Export one or more sets of powder data as XYE file(s)
151        '''
152        # the export process starts here
153        self.InitExport(event)
154        # load all of the tree into a set of dicts
155        self.loadTree()
156        if self.ExportSelect( # set export parameters
157            AskFile='single' # get a file name/directory to save in
158            ): return
159        filenamelist = []
160        for hist in self.histnam:
161            if len(self.histnam) > 1:
162                # multiple files: create a unique name from the histogram
163                fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
164                # create an instrument parameter file
165                self.filename = os.path.join(self.dirname,fileroot + self.extension)
166            else:
167                # use the supplied name, but force the extension
168                self.filename= os.path.splitext(self.filename)[0] + self.extension
169
170            self.Writer(hist)
171            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
Note: See TracBrowser for help on using the repository browser.