source: sasview/DataLoader/readers/IgorReader.py @ 579ba85

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 579ba85 was b99ac227, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Updates and tests for readers

  • Property mode set to 100644
File size: 8.6 KB
Line 
1"""
2    IGOR 2D reduced file reader
3"""
4
5"""
6This software was developed by the University of Tennessee as part of the
7Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
8project funded by the US National Science Foundation.
9
10If you use DANSE applications to do scientific research that leads to
11publication, we ask that you acknowledge the use of the software with the
12following sentence:
13
14"This work benefited from DANSE software developed under NSF award DMR-0520547."
15
16copyright 2008, University of Tennessee
17"""
18
19import os, sys
20import numpy
21import math, logging
22from DataLoader.data_info import Data2D, Detector
23
24# Look for unit converter
25has_converter = True
26try:
27    from data_util.nxsunit import Converter
28except:
29    has_converter = False
30
31class Reader:
32    """ Simple data reader for Igor data files """
33    ## File type
34    type = ["IGOR 2D files (*.ASC)|*.ASC"]
35    ## Extension
36    ext=['.ASC', '.asc']
37
38    def read(self,filename=None):
39        """ Read file """
40        if not os.path.isfile(filename):
41            raise ValueError, \
42            "Specified file %s is not a regular file" % filename
43       
44        # Read file
45        f = open(filename,'r')
46        buf = f.read()
47       
48        # Instantiate data object
49        output = Data2D()
50        output.filename = os.path.basename(filename)
51        detector = Detector()
52        if len(output.detector)>0: print str(output.detector[0])
53        output.detector.append(detector)
54       
55        size_x = 128
56        size_y = 128
57        output.data = numpy.zeros([size_x,size_y])
58        output.err_data = numpy.zeros([size_x,size_y])
59       
60        data_conv_q = None
61        data_conv_i = None
62       
63        if has_converter == True and output.Q_unit != '1/A':
64            data_conv_q = Converter('1/A')
65            # Test it
66            data_conv_q(1.0, output.Q_unit)
67           
68        if has_converter == True and output.I_unit != '1/cm':
69            data_conv_i = Converter('1/cm')
70            # Test it
71            data_conv_i(1.0, output.I_unit)           
72   
73       
74        # Get content
75        dataStarted = False
76       
77       
78        lines = buf.split('\n')
79        itot = 0
80        x = []
81        y = []
82       
83        ncounts = 0
84       
85        xmin = None
86        xmax = None
87        ymin = None
88        ymax = None
89       
90        i_x = 0
91        i_y = -1
92       
93        isInfo = False
94        isCenter = False
95        for line in lines:
96           
97            # Find setup info line
98            if isInfo:
99                isInfo = False
100                line_toks = line.split()
101                # Wavelength in Angstrom
102                try:
103                    wavelength = float(line_toks[1])
104                except:
105                    raise ValueError,"IgorReader: can't read this file, missing wavelength"
106                # Distance in meters
107                try:
108                    distance = float(line_toks[3])
109                except:
110                    raise ValueError,"IgorReader: can't read this file, missing distance"
111               
112                # Distance in meters
113                try:
114                    transmission = float(line_toks[4])
115                except:
116                    raise ValueError,"IgorReader: can't read this file, missing transmission"
117               
118            if line.count("LAMBDA")>0:
119                isInfo = True
120               
121            # Find center info line
122            if isCenter:
123                isCenter = False               
124                line_toks = line.split()
125                # Center in bin number
126                center_x = float(line_toks[0])
127                center_y = float(line_toks[1])
128
129            if line.count("BCENT")>0:
130                isCenter = True
131               
132       
133            # Find data start
134            if line.count("***")>0:
135                dataStarted = True
136               
137                # Check that we have all the info
138                if wavelength == None \
139                    or distance == None \
140                    or center_x == None \
141                    or center_y == None:
142                    raise ValueError, "IgorReader:Missing information in data file"
143               
144            if dataStarted == True:
145                try:
146                    value = float(line)
147                except:
148                    # Found a non-float entry, skip it
149                    continue
150               
151                # Get bin number
152                if math.fmod(itot, 128)==0:
153                    i_x = 0
154                    i_y += 1
155                else:
156                    i_x += 1
157                   
158                output.data[i_y][i_x] = value
159                ncounts += 1 
160               
161                # Det 640 x 640 mm
162                # Q = 4pi/lambda sin(theta/2)
163                # Bin size is 0.5 cm
164                theta = (i_x-center_x+1)*0.5 / distance / 100.0
165                qx = 4.0*math.pi/wavelength * math.sin(theta/2.0)
166
167                if has_converter == True and output.Q_unit != '1/A':
168                    qx = data_conv_q(qx, units=output.Q_unit)
169
170                if xmin==None or qx<xmin:
171                    xmin = qx
172                if xmax==None or qx>xmax:
173                    xmax = qx
174               
175                theta = (i_y-center_y+1)*0.5 / distance / 100.0
176                qy = 4.0*math.pi/wavelength * math.sin(theta/2.0)
177
178                if has_converter == True and output.Q_unit != '1/A':
179                    qy = data_conv_q(qy, units=output.Q_unit)
180               
181                if ymin==None or qy<ymin:
182                    ymin = qy
183                if ymax==None or qy>ymax:
184                    ymax = qy
185               
186                if not qx in x:
187                    x.append(qx)
188                if not qy in y:
189                    y.append(qy)
190               
191                itot += 1
192                 
193                 
194        theta = 0.25 / distance / 100.0
195        xstep = 4.0*math.pi/wavelength * math.sin(theta/2.0)
196       
197        theta = 0.25 / distance / 100.0
198        ystep = 4.0*math.pi/wavelength * math.sin(theta/2.0)
199       
200        # Store all data ######################################
201        # Store wavelength
202        if has_converter==True and output.source.wavelength_unit != 'A':
203            conv = Converter('A')
204            wavelength = conv(wavelength, units=output.source.wavelength_unit)
205        output.source.wavelength = wavelength
206
207        # Store distance
208        if has_converter==True and detector.distance_unit != 'm':
209            conv = Converter('m')
210            distance = conv(distance, units=detector.distance_unit)
211        detector.distance = distance
212 
213        # Store transmission
214        output.sample.transmission = transmission
215       
216        # Store pixel size
217        pixel = 5.0
218        if has_converter==True and detector.pixel_size_unit != 'mm':
219            conv = Converter('mm')
220            pixel = conv(pixel, units=detector.pixel_size_unit)
221        detector.pixel_size.x = pixel
222        detector.pixel_size.y = pixel
223 
224        # Store beam center in distance units
225        detector.beam_center.x = center_x*pixel
226        detector.beam_center.y = center_y*pixel
227 
228       
229        # Store limits of the image (2D array)
230        xmin    =xmin-xstep/2.0
231        xmax    =xmax+xstep/2.0
232        ymin    =ymin-ystep/2.0
233        ymax    =ymax+ystep/2.0
234        if has_converter == True and output.Q_unit != '1/A':
235            xmin = data_conv_q(xmin, units=output.Q_unit)
236            xmax = data_conv_q(xmax, units=output.Q_unit)
237            ymin = data_conv_q(ymin, units=output.Q_unit)
238            ymax = data_conv_q(ymax, units=output.Q_unit)
239        output.xmin = xmin
240        output.xmax = xmax
241        output.ymin = ymin
242        output.ymax = ymax
243       
244        # Store x and y axis bin centers
245        output.x_bins     = x
246        output.y_bins     = y
247       
248        # Units
249        if data_conv_q is not None:
250            output.xaxis("\\rm{Q}", output.Q_unit)
251            output.yaxis("\\rm{Q}", output.Q_unit)
252        else:
253            output.xaxis("\\rm{Q}", 'A^{-1}')
254            output.yaxis("\\rm{Q}", 'A^{-1}')
255           
256        if data_conv_i is not None:
257            output.zaxis("\\{I(Q)}", output.I_unit)
258        else:
259            output.zaxis("\\rm{I(Q)}","cm^{-1}")
260   
261       
262        return output
263   
264if __name__ == "__main__": 
265    reader = Reader()
266    print reader.read("../test/MAR07232_rest.ASC") 
267   
Note: See TracBrowser for help on using the repository browser.