source: trunk/exports/G2export_pwdr.py @ 3024

Last change on this file since 3024 was 2819, checked in by vondreele, 5 years ago

fix printing of nonascii characters (e.g. "umlaut-o") in file names, etc. in exporters

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 6.9 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2017-05-04 14:14:19 +0000 (Thu, 04 May 2017) $
5# $Author: toby $
6# $Revision: 2819 $
7# $URL: trunk/exports/G2export_pwdr.py $
8# $Id: G2export_pwdr.py 2819 2017-05-04 14:14:19Z 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: 2819 $")
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 '+hist+' written to file '+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)
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            # multiple files: create a unique name from the histogram
100            fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
101            # create an instrument parameter file
102            self.filename = os.path.join(self.dirname,fileroot + self.extension)
103            histblk = self.Histograms[hist]
104            prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0])
105            self.Writer(hist,prmname=prmname)
106            print('Histogram '+hist+' written to file '+self.fullpath)
107
108class ExportPowderXYE(G2IO.ExportBaseclass):
109    '''Used to create a Topas XYE file for a powder data set
110
111    :param wx.Frame G2frame: reference to main GSAS-II frame
112    '''
113    def __init__(self,G2frame):
114        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
115            G2frame=G2frame,
116            formatName = 'Topas XYE file',
117            extension='.xye',
118            longFormatName = 'Export powder data as Topas XYE (column) file'
119            )
120        self.exporttype = ['powder']
121        self.multiple = True
122       
123    def Writer(self,TreeName,filename=None):
124        self.OpenFile(filename)
125        histblk = self.Histograms[TreeName]
126        self.Write('/*')    #The ugly c comment delimiter used in topas!
127        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
128        self.Write('*/')
129        x = np.array(histblk['Data'][0])
130        # convert weights to sigmas; use largest weight as minimum esd
131        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
132        s[s==0] = np.max(s)
133        s = 1./s
134        for XYS in zip(x,histblk['Data'][1],s):
135            line = ''
136            for val in XYS:
137                line += G2py3.FormatPadValue(val,(15,6))
138            self.Write(line)
139        self.CloseFile()
140
141    def Exporter(self,event=None):
142        '''Export one or more sets of powder data as XYE file(s)
143        '''
144        # the export process starts here
145        self.InitExport(event)
146        # load all of the tree into a set of dicts
147        self.loadTree()
148        if self.ExportSelect( # set export parameters
149            AskFile='single' # get a file name/directory to save in
150            ): return
151        filenamelist = []
152        for hist in self.histnam:
153            # multiple files: create a unique name from the histogram
154            fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
155            # create an instrument parameter file
156            self.filename = os.path.join(self.dirname,fileroot + self.extension)
157            self.Writer(hist)
158            print('Histogram '+hist+' written to file '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.