source: trunk/exports/G2export_pwdr.py @ 4071

Last change on this file since 4071 was 4071, checked in by vondreele, 2 years ago

fix bug exporting fxye file for TOF data - can't write iparm file

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2019-07-22 21:06:23 +0000 (Mon, 22 Jul 2019) $
5# $Author: vondreele $
6# $Revision: 4071 $
7# $URL: trunk/exports/G2export_pwdr.py $
8# $Id: G2export_pwdr.py 4071 2019-07-22 21:06:23Z vondreele $
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'''
17from __future__ import division, print_function
18import os.path
19import numpy as np
20import GSASIIpath
21GSASIIpath.SetVersionNumber("$Revision: 4071 $")
22import GSASIIIO as G2IO
23import GSASIIpy3 as G2py3
24import GSASIIobj as G2obj
25
26class ExportPowderFXYE(G2IO.ExportBaseclass):
27    '''Used to create a FXYE file for a powder data set
28
29    :param wx.Frame G2frame: reference to main GSAS-II frame
30    '''
31    def __init__(self,G2frame):
32        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
33            G2frame=G2frame,
34            formatName = 'GSAS FXYE file',
35            extension='.fxye',
36            longFormatName = 'Export powder data as GSAS FXYE (column) file'
37            )
38        self.exporttype = ['powder']
39        self.multiple = True
40
41    def WriteInstFile(self,hist,Inst):
42        '''Write an instrument parameter file
43        '''
44        if 'T' in Inst['Type'][0]:      #can't do TOF iparm files
45            return None
46        prmname = os.path.splitext(self.filename)[0] + '.prm'
47        prmname = os.path.join(self.dirname,prmname)
48        self.OpenFile(prmname)
49        self.Write( '            123456789012345678901234567890123456789012345678901234567890        ')
50        self.Write( 'INS   BANK      1                                                               ')
51        self.Write(('INS   HTYPE   %sR                                                              ')%(Inst['Type'][0]))
52        if 'Lam1' in Inst:              #Ka1 & Ka2
53            self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam1'][0],Inst['Lam2'][0]))
54        elif 'Lam' in Inst:             #single wavelength
55            self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam'][1],0.0))
56        self.Write( 'INS  1 IRAD     0                                                               ')
57        self.Write( 'INS  1I HEAD                                                                    ')
58        self.Write( 'INS  1I ITYP    0    0.0000  180.0000         1                                 ')
59        self.Write(('INS  1DETAZM%10.3f                                                          ')%(Inst['Azimuth'][0]))
60        self.Write( 'INS  1PRCF1     3    8   0.00100                                                ')
61        self.Write(('INS  1PRCF11%15.6e%15.6e%15.6e%15.6e   ')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0))
62        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.))
63        self.CloseFile()
64        print('Parameters from '+hist+' written to '+prmname)
65        return prmname
66
67    def Writer(self,TreeName,filename=None,prmname=''):
68        '''Write a single PWDR entry to a FXYE file
69        '''
70        histblk = self.Histograms[TreeName]
71        self.OpenFile(filename)
72        self.Write(TreeName[5:])
73        if prmname: self.Write('Instrument parameter file:'+os.path.split(prmname)[1])
74        x = 100*np.array(histblk['Data'][0])
75        # convert weights to sigmas; use largest weight as minimum esd
76        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
77        s[s==0] = np.max(s)
78        s = 1./s
79        self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % (
80            len(x),len(x),x[0],(x[1]-x[0])
81            ))
82#            for X,Y,S in zip(x,histblk['Data'][1],s):
83#                self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S))
84        for XYS in zip(x,histblk['Data'][1],s):
85            line = ''
86            for val in XYS:
87                line += G2py3.FormatPadValue(val,(15,6))
88            self.Write(line)
89        self.CloseFile()
90       
91    def Exporter(self,event=None):
92        '''Export one or more sets of powder data as FXYE file(s)
93        '''
94        # the export process starts here
95        self.InitExport(event)
96        self.loadTree() # load all of the tree into a set of dicts
97        if self.ExportSelect( # set export parameters
98            AskFile='single' # get a file name/directory to save in
99            ): return
100        filenamelist = []
101        for hist in self.histnam:
102            if len(self.histnam) == 1:
103                name = self.filename
104            else:    # multiple files: create a unique name from the histogram
105                name = self.MakePWDRfilename(hist)
106            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
107            self.filename = os.path.join(self.dirname,fileroot + self.extension)
108            histblk = self.Histograms[hist]
109            # create an instrument parameter file
110            prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0])
111            self.Writer(hist,prmname=prmname)
112            print('Histogram '+hist+' written to '+self.fullpath)
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        self.OpenFile(filename)
131        histblk = self.Histograms[TreeName]
132        self.Write('/*')    #The ugly c comment delimiter used in topas!
133        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
134        self.Write('*/')
135        x = np.array(histblk['Data'][0])
136        # convert weights to sigmas; use largest weight as minimum esd
137        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
138        s[s==0] = np.max(s)
139        s = 1./s
140        for XYS in zip(x,histblk['Data'][1],s):
141            line = ''
142            for val in XYS:
143                line += G2py3.FormatPadValue(val,(15,6))
144            self.Write(line)
145        self.CloseFile()
146
147    def Exporter(self,event=None):
148        '''Export one or more sets of powder data as XYE file(s)
149        '''
150        # the export process starts here
151        self.InitExport(event)
152        # load all of the tree into a set of dicts
153        self.loadTree()
154        if self.ExportSelect( # set export parameters
155            AskFile='single' # get a file name/directory to save in
156            ): return
157        filenamelist = []
158        for hist in self.histnam:
159            if len(self.histnam) == 1:
160                name = self.filename
161            else:    # multiple files: create a unique name from the histogram
162                name = self.MakePWDRfilename(hist)
163            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
164            self.filename = os.path.join(self.dirname,fileroot + self.extension)
165            self.Writer(hist)
166            print('Histogram '+hist+' written to '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.