source: trunk/imports/G2sad_xye.py @ 2927

Last change on this file since 2927 was 2817, checked in by vondreele, 8 years ago

major revision - move all importers to GSASIIobj & make them independent of wx so they can be used in a scripting environment.
Still to move are PhaseSelector?, and 3 BlockSelector? dialogs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 14.0 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2017-05-02 15:03:41 +0000 (Tue, 02 May 2017) $
4# $Author: vondreele $
5# $Revision: 2817 $
6# $URL: trunk/imports/G2sad_xye.py $
7# $Id: G2sad_xye.py 2817 2017-05-02 15:03:41Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2sad_xye: read small angle data*
11------------------------------------------------
12
13Routines to read in small angle data from an .xye type file, with
14two-theta or Q steps.
15
16'''
17
18import os.path as ospath
19import numpy as np
20import GSASIIobj as G2obj
21import GSASIIpath
22GSASIIpath.SetVersionNumber("$Revision: 2817 $")
23npasind = lambda x: 180.*np.arcsin(x)/np.pi
24
25class txt_XRayReaderClass(G2obj.ImportSmallAngleData):
26    'Routines to import X-ray q SAXD data from a .xsad or .xdat file'
27    def __init__(self):
28        super(self.__class__,self).__init__( # fancy way to self-reference
29            extensionlist=('.xsad','.xdat'),
30            strictExtension=False,
31            formatName = 'q (A-1) step X-ray QIE data',
32            longFormatName = 'q (A-1) stepped X-ray text data file in Q,I,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            vals = S.split()
41            if len(vals) >= 2:
42                try:
43                    data = [float(val) for val in vals]
44                    Ndata += 1
45                except ValueError:
46                    pass
47        if not Ndata:     
48            self.errors = 'No 2 or more column numeric data found'
49            return False
50        return True # no errors encountered
51
52    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
53        print 'Read a q-step text file'
54        x = []
55        y = []
56        w = []
57        wave = 1.5428   #Cuka default
58        Temperature = 300
59        for i,S in enumerate(filepointer):
60            if len(S) == 1:     #skip blank line
61                continue
62            if '=' in S:
63                self.comments.append(S[:-1])
64                if 'wave' in S.split('=')[0].lower():
65                    try:
66                        wave = float(S.split('=')[1])
67                    except:
68                        pass
69                continue
70            vals = S.split()
71            if len(vals) >= 2:
72                try:
73                    data = [float(val) for val in vals]
74                    x.append(float(data[0]))
75                    f = float(data[1])
76                    if f <= 0.0:
77                        del x[-1]
78                        continue
79                    elif len(vals) > 2:
80                        y.append(float(data[1]))
81                        w.append(1.0/float(data[2])**2)
82                    else:
83                        y.append(float(data[1]))
84                        w.append(1.0/float(data[1]))
85                except ValueError:
86                    msg = 'Error in line '+str(i+1)
87                    print msg
88                    continue
89        N = len(x)
90        for S in self.comments:
91            if 'Temp' in S.split('=')[0]:
92                try:
93                    Temperature = float(S.split('=')[1])
94                except:
95                    pass
96        self.instdict['wave'] = wave
97        self.instdict['type'] = 'LXC'
98        x = np.array(x)
99        self.smallangledata = [
100            x, # x-axis values q
101            np.array(y), # small angle pattern intensities
102            np.array(w), # 1/sig(intensity)^2 values (weights)
103            np.zeros(N), # calc. intensities (zero)
104            np.zeros(N), # obs-calc profiles
105            np.zeros(N), # fix bkg
106            ]
107        self.smallangleentry[0] = filename
108        self.smallangleentry[2] = 1 # xye file only has one bank
109        self.idstring = ospath.basename(filename)
110        # scan comments for temperature
111        self.Sample['Temperature'] = Temperature
112
113        return True
114
115class txt_nmXRayReaderClass(G2obj.ImportSmallAngleData):
116    'Routines to import X-ray q SAXD data from a .xsad or .xdat file, q in nm-1'
117    def __init__(self):
118        super(self.__class__,self).__init__( # fancy way to self-reference
119            extensionlist=('.xsad','.xdat'),
120            strictExtension=False,
121            formatName = 'q (nm-1) step X-ray QIE data',
122            longFormatName = 'q (nm-1) stepped X-ray text data file in Q,I,E order; E optional'
123            )
124
125    # Validate the contents -- make sure we only have valid lines
126    def ContentsValidator(self, filepointer):
127        'Look through the file for expected types of lines in a valid q-step file'
128        Ndata = 0
129        for i,S in enumerate(filepointer):
130            vals = S.split()
131            if len(vals) >= 2:
132                try:
133                    data = [float(val) for val in vals]
134                    Ndata += 1
135                except ValueError:
136                    pass
137        if not Ndata:     
138            self.errors = 'No 2 or more column numeric data found'
139            return False
140        return True # no errors encountered
141
142    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
143        print 'Read a q-step text file'
144        x = []
145        y = []
146        w = []
147        wave = 1.5428   #Cuka default
148        Temperature = 300
149        for i,S in enumerate(filepointer):
150            if len(S) == 1:     #skip blank line
151                continue
152            if '=' in S:
153                self.comments.append(S[:-1])
154                if 'wave' in S.split('=')[0].lower():
155                    try:
156                        wave = float(S.split('=')[1])
157                    except:
158                        pass
159                continue
160            vals = S.split()
161            if len(vals) >= 2:
162                try:
163                    data = [float(val) for val in vals]
164                    x.append(float(data[0])/10.)        #convert nm-1 to A-1
165                    f = float(data[1])
166                    if f <= 0.0:
167                        x.pop()
168                        continue
169                    elif len(vals) > 2:
170                        y.append(float(data[1]))
171                        w.append(1.0/float(data[2])**2)
172                    else:
173                        y.append(float(data[1]))
174                        w.append(1.0/float(data[1]))
175                except ValueError:
176                    msg = 'Error in line '+str(i+1)
177                    print msg
178                    continue
179        N = len(x)
180        for S in self.comments:
181            if 'Temp' in S.split('=')[0]:
182                try:
183                    Temperature = float(S.split('=')[1])
184                except:
185                    pass
186        self.instdict['wave'] = wave
187        self.instdict['type'] = 'LXC'
188        x = np.array(x)
189        self.smallangledata = [
190            x, # x-axis values q
191            np.array(y), # small angle pattern intensities
192            np.array(w), # 1/sig(intensity)^2 values (weights)
193            np.zeros(N), # calc. intensities (zero)
194            np.zeros(N), # obs-calc profiles
195            np.zeros(N), # fix bkg
196            ]
197        self.smallangleentry[0] = filename
198        self.smallangleentry[2] = 1 # xye file only has one bank
199        self.idstring = ospath.basename(filename)
200        # scan comments for temperature
201        self.Sample['Temperature'] = Temperature
202
203        return True
204
205class txt_CWNeutronReaderClass(G2obj.ImportSmallAngleData):
206    'Routines to import neutron CW q SAXD data from a .nsad or .ndat file'
207    def __init__(self):
208        super(self.__class__,self).__init__( # fancy way to self-reference
209            extensionlist=('.nsad','.ndat'),
210            strictExtension=False,
211            formatName = 'q (A-1) step neutron CW QIE data',
212            longFormatName = 'q (A-1) stepped neutron CW text data file in Q,I,E order; E optional'
213            )
214
215    # Validate the contents -- make sure we only have valid lines
216    def ContentsValidator(self, filepointer):
217        'Look through the file for expected types of lines in a valid q-step file'
218        Ndata = 0
219        for i,S in enumerate(filepointer):
220            vals = S.split()
221            if len(vals) >= 2:
222                try:
223                    data = [float(val) for val in vals]
224                    Ndata += 1
225                except ValueError:
226                    pass
227        if not Ndata:     
228            self.errors = 'No 2 or more column numeric data found'
229            return False
230        return True # no errors encountered
231
232    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
233        print 'Read a q-step text file'
234        x = []
235        y = []
236        w = []
237        wave = 1.5428   #Cuka default
238        Temperature = 300
239        for i,S in enumerate(filepointer):
240            if len(S) == 1:     #skip blank line
241                continue
242            if '=' in S:
243                self.comments.append(S[:-1])
244                if 'wave' in S.split('=')[0].lower():
245                    try:
246                        wave = float(S.split('=')[1])
247                    except:
248                        pass
249                continue
250            vals = S.split()
251            if len(vals) >= 2:
252                try:
253                    data = [float(val) for val in vals]
254                    x.append(float(data[0]))
255                    f = float(data[1])
256                    if f <= 0.0:
257                        y.append(0.0)
258                        w.append(1.0)
259                    elif len(vals) > 2:
260                        y.append(float(data[1]))
261                        w.append(1.0/float(data[2])**2)
262                    else:
263                        y.append(float(data[1]))
264                        w.append(1.0/float(data[1]))
265                except ValueError:
266                    msg = 'Error in line '+str(i+1)
267                    print msg
268                    continue
269        N = len(x)
270        for S in self.comments:
271            if 'Temp' in S.split('=')[0]:
272                try:
273                    Temperature = float(S.split('=')[1])
274                except:
275                    pass
276        self.instdict['wave'] = wave
277        self.instdict['type'] = 'LNC'
278        x = np.array(x)
279        if np.any(x > 2.):         #q must be nm-1
280            x /= 10.
281        self.smallangledata = [
282            x, # x-axis values q
283            np.array(y), # small angle pattern intensities
284            np.array(w), # 1/sig(intensity)^2 values (weights)
285            np.zeros(N), # calc. intensities (zero)
286            np.zeros(N), # obs-calc profiles
287            np.zeros(N), # fix bkg
288            ]
289        self.smallangleentry[0] = filename
290        self.smallangleentry[2] = 1 # xye file only has one bank
291        self.idstring = ospath.basename(filename)
292        # scan comments for temperature
293        self.Sample['Temperature'] = Temperature
294
295        return True
296
297class txt_nmCWNeutronReaderClass(G2obj.ImportSmallAngleData):
298    'Routines to import neutron CW q in nm-1 SAXD data from a .nsad or .ndat file'
299    def __init__(self):
300        super(self.__class__,self).__init__( # fancy way to self-reference
301            extensionlist=('.nsad','.ndat'),
302            strictExtension=False,
303            formatName = 'q (nm-1) step neutron CW QIE data',
304            longFormatName = 'q (nm-1) stepped neutron CW text data file in Q,I,E order; E optional'
305            )
306
307    # Validate the contents -- make sure we only have valid lines
308    def ContentsValidator(self, filepointer):
309        'Look through the file for expected types of lines in a valid q-step file'
310        Ndata = 0
311        for i,S in enumerate(filepointer):
312            vals = S.split()
313            if len(vals) >= 2:
314                try:
315                    data = [float(val) for val in vals]
316                    Ndata += 1
317                except ValueError:
318                    pass
319        if not Ndata:     
320            self.errors = 'No 2 or more column numeric data found'
321            return False
322        return True # no errors encountered
323
324    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
325        print 'Read a q-step text file'
326        x = []
327        y = []
328        w = []
329        wave = 1.5428   #Cuka default
330        Temperature = 300
331        for i,S in enumerate(filepointer):
332            if len(S) == 1:     #skip blank line
333                continue
334            if '=' in S:
335                self.comments.append(S[:-1])
336                if 'wave' in S.split('=')[0].lower():
337                    try:
338                        wave = float(S.split('=')[1])
339                    except:
340                        pass
341                continue
342            vals = S.split()
343            if len(vals) >= 2:
344                try:
345                    data = [float(val) for val in vals]
346                    x.append(float(data[0])/10.)    #convert to A-1
347                    f = float(data[1])
348                    if f <= 0.0:
349                        y.append(0.0)
350                        w.append(1.0)
351                    elif len(vals) > 2:
352                        y.append(float(data[1]))
353                        w.append(1.0/float(data[2])**2)
354                    else:
355                        y.append(float(data[1]))
356                        w.append(1.0/float(data[1]))
357                except ValueError:
358                    msg = 'Error in line '+str(i+1)
359                    print msg
360                    continue
361        N = len(x)
362        for S in self.comments:
363            if 'Temp' in S.split('=')[0]:
364                try:
365                    Temperature = float(S.split('=')[1])
366                except:
367                    pass
368        self.instdict['wave'] = wave
369        self.instdict['type'] = 'LNC'
370        x = np.array(x)
371        self.smallangledata = [
372            x, # x-axis values q
373            np.array(y), # small angle pattern intensities
374            np.array(w), # 1/sig(intensity)^2 values (weights)
375            np.zeros(N), # calc. intensities (zero)
376            np.zeros(N), # obs-calc profiles
377            np.zeros(N), # fix bkg
378            ]
379        self.smallangleentry[0] = filename
380        self.smallangleentry[2] = 1 # xye file only has one bank
381        self.idstring = ospath.basename(filename)
382        # scan comments for temperature
383        self.Sample['Temperature'] = Temperature
384
385        return True
Note: See TracBrowser for help on using the repository browser.