source: trunk/exports/G2export_FIT2D.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: 4.5 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_FIT2D.py $
8# $Id: G2export_FIT2D.py 3223 2018-01-14 17:28:10Z toby $
9########### SVN repository information ###################
10'''
11*Module G2export_FIT2D: Fit2D "Chi" export*
12-------------------------------------------
13
14Code to create .chi (Fit2D like) files for GSAS-II powder data export
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 GSASIIobj as G2obj
24
25class ExportPowderCHI(G2IO.ExportBaseclass):
26    '''Used to create a CHI 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 = 'Fit2D chi file',
34            extension='.chi',
35            longFormatName = 'Export powder data as Fit2D .chi file'
36            )
37        self.exporttype = ['powder']
38        self.multiple = True
39
40    def Writer(self,TreeName,filename=None):
41        self.OpenFile(filename)
42        histblk = self.Histograms[TreeName]
43        self.Write(str(TreeName)[5:]) # drop 'PWDR '
44        self.Write("2-Theta Angle (Degrees)")
45        self.Write("Intensity")
46        self.Write("       "+str(len(histblk['Data'][0])))
47        for X,Y in zip(histblk['Data'][0],histblk['Data'][1]):
48            line = " %5.7e" % X
49            line += "   %5.7e" % Y
50            self.Write(line)
51        self.CloseFile()
52       
53    def Exporter(self,event=None):
54        '''Export a set of powder data as a Fit2D .qchi file
55        '''
56        # the export process starts here
57        self.InitExport(event)
58        # load all of the tree into a set of dicts
59        self.loadTree()
60        if self.ExportSelect( # set export parameters
61            AskFile='single' # get a file name/directory to save in
62            ): return
63        filenamelist = []
64        for hist in self.histnam:
65            if len(self.histnam) == 1:
66                name = self.filename
67            else:    # multiple files: create a unique name from the histogram
68                name = self.MakePWDRfilename(hist)
69            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
70            self.filename = os.path.join(self.dirname,fileroot + self.extension)
71            self.Writer(hist)
72            print('Histogram '+hist+' written to file '+self.fullpath)
73
74class ExportPowderQCHI(G2IO.ExportBaseclass):
75    '''Used to create a q-binned CHI file for a powder data set
76
77    :param wx.Frame G2frame: reference to main GSAS-II frame
78    '''
79    def __init__(self,G2frame):
80        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
81            G2frame=G2frame,
82            formatName = 'Fit2D q-bin chi file',
83            extension='.qchi',
84            longFormatName = 'Export powder data as q-bin Fit2D .qchi file'
85            )
86        self.exporttype = ['powder']
87        self.multiple = True
88
89    def Writer(self,TreeName,filename=None):
90        import GSASIIlattice as G2lat
91        self.OpenFile(filename)
92        histblk = self.Histograms[TreeName]
93        inst = histblk['Instrument Parameters'][0]
94        self.Write(str(TreeName)[5:]) # drop 'PWDR '
95        self.Write("Q")
96        self.Write("Intensity")
97        self.Write("       "+str(len(histblk['Data'][0])))
98        for X,Y in zip(histblk['Data'][0],histblk['Data'][1]):
99            line = " %5.7e" % (2.*np.pi/G2lat.Pos2dsp(inst,X))
100            line += "   %5.7e" % Y
101            self.Write(line)
102        self.CloseFile()
103       
104    def Exporter(self,event=None):
105        '''Export a set of powder data as a q-bin Fit2D .qchi file
106        '''
107        # the export process starts here
108        self.InitExport(event)
109        # load all of the tree into a set of dicts
110        self.loadTree()
111        if self.ExportSelect( # set export parameters
112            AskFile='single' # get a file name/directory to save in
113            ): return
114        filenamelist = []
115        for hist in self.histnam:
116            if len(self.histnam) == 1:
117                name = self.filename
118            else:    # multiple files: create a unique name from the histogram
119                name = self.MakePWDRfilename(hist)
120            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
121            self.filename = os.path.join(self.dirname,fileroot + self.extension)
122            self.Writer(hist)
123            print('Histogram '+hist+' written to file '+self.fullpath)
Note: See TracBrowser for help on using the repository browser.