source: trunk/exports/G2export_pwdr.py @ 4190

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

fix a missing G2frame.PickId? problem in G2imgGUI
fix SAS plotting issues with background subtraction
fix SAS errors in Spheroid, Schulz & Gauss models
fix of fxye export for TOF data

  • 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: 2019-07-26 13:37:07 +0000 (Fri, 26 Jul 2019) $
5# $Author: vondreele $
6# $Revision: 4072 $
7# $URL: trunk/exports/G2export_pwdr.py $
8# $Id: G2export_pwdr.py 4072 2019-07-26 13:37:07Z 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: 4072 $")
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 = np.array(histblk['Data'][0])
75        if 'C' in histblk['Instrument Parameters'][0]['Type'][0]:
76            x *= 100.
77        else:
78            cw = np.diff(x)
79            x[:-1] += cw
80        # convert weights to sigmas; use largest weight as minimum esd
81        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
82        s[s==0] = np.max(s)
83        s = 1./s
84        self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % (
85            len(x),len(x),x[0],(x[1]-x[0])
86            ))
87#            for X,Y,S in zip(x,histblk['Data'][1],s):
88#                self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S))
89        for XYS in zip(x,histblk['Data'][1],s):
90            line = ''
91            for val in XYS:
92                line += G2py3.FormatPadValue(val,(15,6))
93            self.Write(line)
94        self.CloseFile()
95       
96    def Exporter(self,event=None):
97        '''Export one or more sets of powder data as FXYE file(s)
98        '''
99        # the export process starts here
100        self.InitExport(event)
101        self.loadTree() # load all of the tree into a set of dicts
102        if self.ExportSelect( # set export parameters
103            AskFile='single' # get a file name/directory to save in
104            ): return
105        filenamelist = []
106        for hist in self.histnam:
107            if len(self.histnam) == 1:
108                name = self.filename
109            else:    # multiple files: create a unique name from the histogram
110                name = self.MakePWDRfilename(hist)
111            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
112            self.filename = os.path.join(self.dirname,fileroot + self.extension)
113            histblk = self.Histograms[hist]
114            # create an instrument parameter file
115            prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0])
116            self.Writer(hist,prmname=prmname)
117            print('Histogram '+hist+' written to '+self.fullpath)
118
119class ExportPowderXYE(G2IO.ExportBaseclass):
120    '''Used to create a Topas XYE file for a powder data set
121
122    :param wx.Frame G2frame: reference to main GSAS-II frame
123    '''
124    def __init__(self,G2frame):
125        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
126            G2frame=G2frame,
127            formatName = 'Topas XYE file',
128            extension='.xye',
129            longFormatName = 'Export powder data as Topas XYE (column) file'
130            )
131        self.exporttype = ['powder']
132        self.multiple = True
133       
134    def Writer(self,TreeName,filename=None):
135        self.OpenFile(filename)
136        histblk = self.Histograms[TreeName]
137        self.Write('/*')    #The ugly c comment delimiter used in topas!
138        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
139        self.Write('*/')
140        x = np.array(histblk['Data'][0])
141        # convert weights to sigmas; use largest weight as minimum esd
142        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
143        s[s==0] = np.max(s)
144        s = 1./s
145        for XYS in zip(x,histblk['Data'][1],s):
146            line = ''
147            for val in XYS:
148                line += G2py3.FormatPadValue(val,(15,6))
149            self.Write(line)
150        self.CloseFile()
151
152    def Exporter(self,event=None):
153        '''Export one or more sets of powder data as XYE file(s)
154        '''
155        # the export process starts here
156        self.InitExport(event)
157        # load all of the tree into a set of dicts
158        self.loadTree()
159        if self.ExportSelect( # set export parameters
160            AskFile='single' # get a file name/directory to save in
161            ): return
162        filenamelist = []
163        for hist in self.histnam:
164            if len(self.histnam) == 1:
165                name = self.filename
166            else:    # multiple files: create a unique name from the histogram
167                name = self.MakePWDRfilename(hist)
168            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
169            self.filename = os.path.join(self.dirname,fileroot + self.extension)
170            self.Writer(hist)
171            print('Histogram '+hist+' written to '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.