1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2017-12-07 22:00:04 +0000 (Thu, 07 Dec 2017) $ |
---|
5 | # $Author: toby $ |
---|
6 | # $Revision: 3180 $ |
---|
7 | # $URL: trunk/exports/G2export_FIT2D.py $ |
---|
8 | # $Id: G2export_FIT2D.py 3180 2017-12-07 22:00:04Z toby $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | ''' |
---|
11 | *Module G2export_FIT2D: Fit2D "Chi" export* |
---|
12 | ------------------------------------------- |
---|
13 | |
---|
14 | Code to create .chi (Fit2D like) files for GSAS-II powder data export |
---|
15 | |
---|
16 | ''' |
---|
17 | from __future__ import division, print_function |
---|
18 | import os.path |
---|
19 | import numpy as np |
---|
20 | import GSASIIpath |
---|
21 | GSASIIpath.SetVersionNumber("$Revision: 3180 $") |
---|
22 | import GSASIIIO as G2IO |
---|
23 | import GSASIIobj as G2obj |
---|
24 | |
---|
25 | class 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 | # multiple files: create a unique name from the histogram |
---|
66 | fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist) |
---|
67 | # create an instrument parameter file |
---|
68 | self.filename = os.path.join(self.dirname,fileroot + self.extension) |
---|
69 | self.Writer(hist) |
---|
70 | print('Histogram '+hist+' written to file '+self.fullpath) |
---|
71 | |
---|
72 | class ExportPowderQCHI(G2IO.ExportBaseclass): |
---|
73 | '''Used to create a q-binned CHI file for a powder data set |
---|
74 | |
---|
75 | :param wx.Frame G2frame: reference to main GSAS-II frame |
---|
76 | ''' |
---|
77 | def __init__(self,G2frame): |
---|
78 | super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ |
---|
79 | G2frame=G2frame, |
---|
80 | formatName = 'Fit2D q-bin chi file', |
---|
81 | extension='.qchi', |
---|
82 | longFormatName = 'Export powder data as q-bin Fit2D .qchi file' |
---|
83 | ) |
---|
84 | self.exporttype = ['powder'] |
---|
85 | self.multiple = True |
---|
86 | |
---|
87 | def Writer(self,TreeName,filename=None): |
---|
88 | import GSASIIlattice as G2lat |
---|
89 | self.OpenFile(filename) |
---|
90 | histblk = self.Histograms[TreeName] |
---|
91 | inst = histblk['Instrument Parameters'][0] |
---|
92 | self.Write(str(TreeName)[5:]) # drop 'PWDR ' |
---|
93 | self.Write("Q") |
---|
94 | self.Write("Intensity") |
---|
95 | self.Write(" "+str(len(histblk['Data'][0]))) |
---|
96 | for X,Y in zip(histblk['Data'][0],histblk['Data'][1]): |
---|
97 | line = " %5.7e" % (2.*np.pi/G2lat.Pos2dsp(inst,X)) |
---|
98 | line += " %5.7e" % Y |
---|
99 | self.Write(line) |
---|
100 | self.CloseFile() |
---|
101 | |
---|
102 | def Exporter(self,event=None): |
---|
103 | '''Export a set of powder data as a q-bin Fit2D .qchi file |
---|
104 | ''' |
---|
105 | # the export process starts here |
---|
106 | self.InitExport(event) |
---|
107 | # load all of the tree into a set of dicts |
---|
108 | self.loadTree() |
---|
109 | if self.ExportSelect( # set export parameters |
---|
110 | AskFile='single' # get a file name/directory to save in |
---|
111 | ): return |
---|
112 | filenamelist = [] |
---|
113 | for hist in self.histnam: |
---|
114 | # multiple files: create a unique name from the histogram |
---|
115 | fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist) |
---|
116 | # create an instrument parameter file |
---|
117 | self.filename = os.path.join(self.dirname,fileroot + self.extension) |
---|
118 | self.Writer(hist) |
---|
119 | print('Histogram '+hist+' written to file '+self.fullpath) |
---|