1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2017-03-04 08:34:05 -0600 (Sat, 04 Mar 2017) $ |
---|
4 | # $Author: vondreele $ |
---|
5 | # $Revision: 2738 $ |
---|
6 | # $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/imports/G2sad_xye.py $ |
---|
7 | # $Id: G2sad_xye.py 2738 2017-03-04 14:34:05Z vondreele $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2rfd_xye: read reflectometry data* |
---|
11 | ------------------------------------------------ |
---|
12 | |
---|
13 | Routines to read in reflectometry data from an .xye type file, with |
---|
14 | two-theta or Q steps. |
---|
15 | |
---|
16 | ''' |
---|
17 | |
---|
18 | import os.path as ospath |
---|
19 | import numpy as np |
---|
20 | import GSASIIIO as G2IO |
---|
21 | import GSASIIpath |
---|
22 | GSASIIpath.SetVersionNumber("$Revision: 2738 $") |
---|
23 | npasind = lambda x: 180.*np.arcsin(x)/np.pi |
---|
24 | |
---|
25 | class txt_XRayReaderClass(G2IO.ImportReflectometryData): |
---|
26 | 'Routines to import X-ray q REFD data from a .xrfd or .xdat file' |
---|
27 | def __init__(self): |
---|
28 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
29 | extensionlist=('.xrfd','.xdat'), |
---|
30 | strictExtension=False, |
---|
31 | formatName = 'q (A-1) step X-ray QRE data', |
---|
32 | longFormatName = 'q (A-1) stepped X-ray text data file in Q,R,E order; E optional' |
---|
33 | ) |
---|
34 | |
---|
35 | # Validate the contents -- make sure we only have valid lines |
---|
36 | def ContentsValidator(self, filepointer): |
---|
37 | 'Look through the file for expected types of lines in a valid q-step file' |
---|
38 | Ndata = 0 |
---|
39 | for i,S in enumerate(filepointer): |
---|
40 | if '#' in S[0]: |
---|
41 | continue |
---|
42 | vals = S.split() |
---|
43 | if len(vals) >= 2: |
---|
44 | try: |
---|
45 | data = [float(val) for val in vals] |
---|
46 | Ndata += 1 |
---|
47 | except ValueError: |
---|
48 | pass |
---|
49 | if not Ndata: |
---|
50 | self.errors = 'No 2 or more column numeric data found' |
---|
51 | return False |
---|
52 | return True # no errors encountered |
---|
53 | |
---|
54 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
55 | print 'Read a q-step text file' |
---|
56 | x = [] |
---|
57 | y = [] |
---|
58 | w = [] |
---|
59 | sq = [] |
---|
60 | wave = 1.5428 #Cuka default |
---|
61 | Temperature = 300 |
---|
62 | for i,S in enumerate(filepointer): |
---|
63 | if len(S) == 1: #skip blank line |
---|
64 | continue |
---|
65 | if '=' in S: |
---|
66 | self.comments.append(S[:-1]) |
---|
67 | if 'wave' in S.split('=')[0].lower(): |
---|
68 | try: |
---|
69 | wave = float(S.split('=')[1]) |
---|
70 | except: |
---|
71 | pass |
---|
72 | continue |
---|
73 | if '#' in S[0]: |
---|
74 | continue |
---|
75 | vals = S.split() |
---|
76 | if len(vals) >= 2: |
---|
77 | try: |
---|
78 | data = [float(val) for val in vals] |
---|
79 | x.append(float(data[0])) |
---|
80 | f = float(data[1]) |
---|
81 | if f <= 0.0: |
---|
82 | del x[-1] |
---|
83 | continue |
---|
84 | elif len(vals) > 2: |
---|
85 | y.append(float(data[1])) |
---|
86 | w.append(1.0/float(data[2])**2) |
---|
87 | if len(vals) == 4: |
---|
88 | sq.append(float(data[3])) |
---|
89 | else: |
---|
90 | sq.append(0.) |
---|
91 | else: |
---|
92 | y.append(float(data[1])) |
---|
93 | w.append(1.0/(0.02*float(data[1]))**2) |
---|
94 | except ValueError: |
---|
95 | msg = 'Error in line '+str(i+1) |
---|
96 | print msg |
---|
97 | continue |
---|
98 | N = len(x) |
---|
99 | for S in self.comments: |
---|
100 | if 'Temp' in S.split('=')[0]: |
---|
101 | try: |
---|
102 | Temperature = float(S.split('=')[1]) |
---|
103 | except: |
---|
104 | pass |
---|
105 | self.instdict['wave'] = wave |
---|
106 | self.instdict['type'] = 'RXC' |
---|
107 | x = np.array(x) |
---|
108 | self.reflectometrydata = [ |
---|
109 | x, # x-axis values q |
---|
110 | np.array(y), # small angle pattern intensities |
---|
111 | np.array(w), # 1/sig(intensity)^2 values (weights) |
---|
112 | np.zeros(N), # calc. intensities (zero) |
---|
113 | np.zeros(N), # obs-calc profiles |
---|
114 | np.array(sq), # fix bkg |
---|
115 | ] |
---|
116 | self.reflectometryentry[0] = filename |
---|
117 | self.reflectometryentry[2] = 1 # xye file only has one bank |
---|
118 | self.idstring = ospath.basename(filename) |
---|
119 | # scan comments for temperature |
---|
120 | self.Sample['Temperature'] = Temperature |
---|
121 | |
---|
122 | return True |
---|
123 | |
---|
124 | class txt_NeutronReaderClass(G2IO.ImportReflectometryData): |
---|
125 | 'Routines to import neutron q REFD data from a .nrfd or .ndat file' |
---|
126 | def __init__(self): |
---|
127 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
128 | extensionlist=('.nrfd','.ndat'), |
---|
129 | strictExtension=False, |
---|
130 | formatName = 'q (A-1) step neutron QRE data', |
---|
131 | longFormatName = 'q (A-1) stepped neutron text data file in Q,R,E order; E optional' |
---|
132 | ) |
---|
133 | |
---|
134 | # Validate the contents -- make sure we only have valid lines |
---|
135 | def ContentsValidator(self, filepointer): |
---|
136 | 'Look through the file for expected types of lines in a valid q-step file' |
---|
137 | Ndata = 0 |
---|
138 | for i,S in enumerate(filepointer): |
---|
139 | if '#' in S[0]: |
---|
140 | continue |
---|
141 | vals = S.split() |
---|
142 | if len(vals) >= 2: |
---|
143 | try: |
---|
144 | data = [float(val) for val in vals] |
---|
145 | Ndata += 1 |
---|
146 | except ValueError: |
---|
147 | pass |
---|
148 | if not Ndata: |
---|
149 | self.errors = 'No 2 or more column numeric data found' |
---|
150 | return False |
---|
151 | return True # no errors encountered |
---|
152 | |
---|
153 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
154 | print 'Read a q-step text file' |
---|
155 | x = [] |
---|
156 | y = [] |
---|
157 | w = [] |
---|
158 | sq = [] |
---|
159 | wave = 1.5428 #Cuka default |
---|
160 | Temperature = 300 |
---|
161 | for i,S in enumerate(filepointer): |
---|
162 | if len(S) == 1: #skip blank line |
---|
163 | continue |
---|
164 | if '=' in S: |
---|
165 | self.comments.append(S[:-1]) |
---|
166 | if 'wave' in S.split('=')[0].lower(): |
---|
167 | try: |
---|
168 | wave = float(S.split('=')[1]) |
---|
169 | except: |
---|
170 | pass |
---|
171 | continue |
---|
172 | if '#' in S[0]: |
---|
173 | continue |
---|
174 | vals = S.split() |
---|
175 | if len(vals) >= 2: |
---|
176 | try: |
---|
177 | data = [float(val) for val in vals] |
---|
178 | x.append(float(data[0])) |
---|
179 | f = float(data[1]) |
---|
180 | if f <= 0.0: |
---|
181 | del x[-1] |
---|
182 | continue |
---|
183 | elif len(vals) > 2: |
---|
184 | y.append(float(data[1])) |
---|
185 | w.append(1.0/float(data[2])**2) |
---|
186 | if len(vals) == 4: |
---|
187 | sq.append(float(data[3])) |
---|
188 | else: |
---|
189 | sq.append(0.) |
---|
190 | else: |
---|
191 | y.append(float(data[1])) |
---|
192 | w.append(1.0/(0.02*float(data[1]))**2) |
---|
193 | except ValueError: |
---|
194 | msg = 'Error in line '+str(i+1) |
---|
195 | print msg |
---|
196 | continue |
---|
197 | N = len(x) |
---|
198 | for S in self.comments: |
---|
199 | if 'Temp' in S.split('=')[0]: |
---|
200 | try: |
---|
201 | Temperature = float(S.split('=')[1]) |
---|
202 | except: |
---|
203 | pass |
---|
204 | self.instdict['wave'] = wave |
---|
205 | self.instdict['type'] = 'RNC' |
---|
206 | x = np.array(x) |
---|
207 | self.reflectometrydata = [ |
---|
208 | x, # x-axis values q |
---|
209 | np.array(y), # small angle pattern intensities |
---|
210 | np.array(w), # 1/sig(intensity)^2 values (weights) |
---|
211 | np.zeros(N), # calc. intensities (zero) |
---|
212 | np.zeros(N), # obs-calc profiles |
---|
213 | np.array(sq), # Q FWHM |
---|
214 | ] |
---|
215 | self.reflectometryentry[0] = filename |
---|
216 | self.reflectometryentry[2] = 1 # xye file only has one bank |
---|
217 | self.idstring = ospath.basename(filename) |
---|
218 | # scan comments for temperature |
---|
219 | self.Sample['Temperature'] = Temperature |
---|
220 | |
---|
221 | return True |
---|
222 | |
---|