source: trunk/exports/G2export_pwdr.py @ 3260

Last change on this file since 3260 was 3223, checked in by toby, 4 years ago

fix histogram export file naming

  • 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: 2018-01-14 17:28:10 +0000 (Sun, 14 Jan 2018) $
5# $Author: toby $
6# $Revision: 3223 $
7# $URL: trunk/exports/G2export_pwdr.py $
8# $Id: G2export_pwdr.py 3223 2018-01-14 17:28:10Z 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'''
17from __future__ import division, print_function
18import os.path
19import numpy as np
20import GSASIIpath
21GSASIIpath.SetVersionNumber("$Revision: 3223 $")
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        prmname = os.path.splitext(self.filename)[0] + '.prm'
45        prmname = os.path.join(self.dirname,prmname)
46        self.OpenFile(prmname)
47        self.Write( '            123456789012345678901234567890123456789012345678901234567890        ')
48        self.Write( 'INS   BANK      1                                                               ')
49        self.Write(('INS   HTYPE   %sR                                                              ')%(Inst['Type'][0]))
50        if 'Lam1' in Inst:              #Ka1 & Ka2
51            self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam1'][0],Inst['Lam2'][0]))
52        elif 'Lam' in Inst:             #single wavelength
53            self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam'][1],0.0))
54        self.Write( 'INS  1 IRAD     0                                                               ')
55        self.Write( 'INS  1I HEAD                                                                    ')
56        self.Write( 'INS  1I ITYP    0    0.0000  180.0000         1                                 ')
57        self.Write(('INS  1DETAZM%10.3f                                                          ')%(Inst['Azimuth'][0]))
58        self.Write( 'INS  1PRCF1     3    8   0.00100                                                ')
59        self.Write(('INS  1PRCF11%15.6e%15.6e%15.6e%15.6e   ')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0))
60        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.))
61        self.CloseFile()
62        print('Parameters from '+hist+' written to '+prmname)
63        return prmname
64
65    def Writer(self,TreeName,filename=None,prmname=''):
66        '''Write a single PWDR entry to a FXYE file
67        '''
68        histblk = self.Histograms[TreeName]
69        self.OpenFile(filename)
70        self.Write(TreeName[5:])
71        if prmname: self.Write('Instrument parameter file:'+os.path.split(prmname)[1])
72        x = 100*np.array(histblk['Data'][0])
73        # convert weights to sigmas; use largest weight as minimum esd
74        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
75        s[s==0] = np.max(s)
76        s = 1./s
77        self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % (
78            len(x),len(x),x[0],(x[1]-x[0])
79            ))
80#            for X,Y,S in zip(x,histblk['Data'][1],s):
81#                self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S))
82        for XYS in zip(x,histblk['Data'][1],s):
83            line = ''
84            for val in XYS:
85                line += G2py3.FormatPadValue(val,(15,6))
86            self.Write(line)
87        self.CloseFile()
88       
89    def Exporter(self,event=None):
90        '''Export one or more sets of powder data as FXYE file(s)
91        '''
92        # the export process starts here
93        self.InitExport(event)
94        self.loadTree() # load all of the tree into a set of dicts
95        if self.ExportSelect( # set export parameters
96            AskFile='single' # get a file name/directory to save in
97            ): return
98        filenamelist = []
99        for hist in self.histnam:
100            if len(self.histnam) == 1:
101                name = self.filename
102            else:    # multiple files: create a unique name from the histogram
103                name = self.MakePWDRfilename(hist)
104            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
105            self.filename = os.path.join(self.dirname,fileroot + self.extension)
106            histblk = self.Histograms[hist]
107            # create an instrument parameter file
108            prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0])
109            self.Writer(hist,prmname=prmname)
110            print('Histogram '+hist+' written to '+self.fullpath)
111
112class ExportPowderXYE(G2IO.ExportBaseclass):
113    '''Used to create a Topas XYE file for a powder data set
114
115    :param wx.Frame G2frame: reference to main GSAS-II frame
116    '''
117    def __init__(self,G2frame):
118        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
119            G2frame=G2frame,
120            formatName = 'Topas XYE file',
121            extension='.xye',
122            longFormatName = 'Export powder data as Topas XYE (column) file'
123            )
124        self.exporttype = ['powder']
125        self.multiple = True
126       
127    def Writer(self,TreeName,filename=None):
128        self.OpenFile(filename)
129        histblk = self.Histograms[TreeName]
130        self.Write('/*')    #The ugly c comment delimiter used in topas!
131        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
132        self.Write('*/')
133        x = np.array(histblk['Data'][0])
134        # convert weights to sigmas; use largest weight as minimum esd
135        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
136        s[s==0] = np.max(s)
137        s = 1./s
138        for XYS in zip(x,histblk['Data'][1],s):
139            line = ''
140            for val in XYS:
141                line += G2py3.FormatPadValue(val,(15,6))
142            self.Write(line)
143        self.CloseFile()
144
145    def Exporter(self,event=None):
146        '''Export one or more sets of powder data as XYE file(s)
147        '''
148        # the export process starts here
149        self.InitExport(event)
150        # load all of the tree into a set of dicts
151        self.loadTree()
152        if self.ExportSelect( # set export parameters
153            AskFile='single' # get a file name/directory to save in
154            ): return
155        filenamelist = []
156        for hist in self.histnam:
157            if len(self.histnam) == 1:
158                name = self.filename
159            else:    # multiple files: create a unique name from the histogram
160                name = self.MakePWDRfilename(hist)
161            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
162            self.filename = os.path.join(self.dirname,fileroot + self.extension)
163            self.Writer(hist)
164            print('Histogram '+hist+' written to '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.