source: sasview/DataLoader/readers/IgorReader.py @ c35e8492

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 c35e8492 was 4cc8e14b, checked in by Jae Cho <jhjcho@…>, 15 years ago

fixed bug

  • Property mode set to 100644
File size: 9.9 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        # Get content
56        dataStarted = False
57       
58       
59        lines = buf.split('\n')
60        itot = 0
61        x = []
62        y = []
63       
64        ncounts = 0
65       
66        xmin = None
67        xmax = None
68        ymin = None
69        ymax = None
70       
71        i_x = 0
72        i_y = -1
73        i_tot_row = 0
74       
75        isInfo = False
76        isCenter = False
77       
78        data_conv_q = None
79        data_conv_i = None
80       
81        if has_converter == True and output.Q_unit != '1/A':
82            data_conv_q = Converter('1/A')
83            # Test it
84            data_conv_q(1.0, output.Q_unit)
85           
86        if has_converter == True and output.I_unit != '1/cm':
87            data_conv_i = Converter('1/cm')
88            # Test it
89            data_conv_i(1.0, output.I_unit)           
90         
91        for line in lines:
92           
93            # Find setup info line
94            if isInfo:
95                isInfo = False
96                line_toks = line.split()
97                # Wavelength in Angstrom
98                try:
99                    wavelength = float(line_toks[1])
100                except:
101                    raise ValueError,"IgorReader: can't read this file, missing wavelength"
102               
103            #Find # of bins in a row assuming the detector is square.
104            if dataStarted == True:
105                try:
106                    value = float(line)
107                except:
108                    # Found a non-float entry, skip it
109                    continue
110               
111                # Get total bin number
112               
113            i_tot_row += 1
114        i_tot_row=math.ceil(math.sqrt(i_tot_row))-1 
115        #print "i_tot", i_tot_row
116        size_x = i_tot_row#192#128
117        size_y = i_tot_row#192#128
118        output.data = numpy.zeros([size_x,size_y])
119        output.err_data = numpy.zeros([size_x,size_y])
120     
121        #Read Header and 2D data
122        for line in lines:
123            # Find setup info line
124            if isInfo:
125                isInfo = False
126                line_toks = line.split()
127                # Wavelength in Angstrom
128                try:
129                    wavelength = float(line_toks[1])
130                except:
131                    raise ValueError,"IgorReader: can't read this file, missing wavelength"
132                # Distance in meters
133                try:
134                    distance = float(line_toks[3])
135                except:
136                    raise ValueError,"IgorReader: can't read this file, missing distance"
137               
138                # Distance in meters
139                try:
140                    transmission = float(line_toks[4])
141                except:
142                    raise ValueError,"IgorReader: can't read this file, missing transmission"
143                                           
144            if line.count("LAMBDA")>0:
145                isInfo = True
146               
147            # Find center info line
148            if isCenter:
149                isCenter = False               
150                line_toks = line.split()
151                # Center in bin number
152                center_x = float(line_toks[0])
153                center_y = float(line_toks[1])
154
155            if line.count("BCENT")>0:
156                isCenter = True
157               
158       
159            # Find data start
160            if line.count("***")>0:
161                dataStarted = True
162               
163                # Check that we have all the info
164                if wavelength == None \
165                    or distance == None \
166                    or center_x == None \
167                    or center_y == None:
168                    raise ValueError, "IgorReader:Missing information in data file"
169               
170            if dataStarted == True:
171                try:
172                    value = float(line)
173                except:
174                    # Found a non-float entry, skip it
175                    continue
176               
177                # Get bin number
178                if math.fmod(itot, i_tot_row)==0:
179                    i_x = 0
180                    i_y += 1
181                else:
182                    i_x += 1
183                   
184                output.data[i_y][i_x] = value
185                ncounts += 1 
186               
187                # Det 640 x 640 mm
188                # Q = 4pi/lambda sin(theta/2)
189                # Bin size is 0.5 cm
190                #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance / 100.0 and
191                #REmoved +1 from theta = (i_y-center_y+1)*0.5 / distance / 100.0
192                #ToDo: Need  complete check if the following covert process is consistent with fitting.py.
193                theta = (i_x-center_x)*0.5 / distance / 100.0
194                qx = 4.0*math.pi/wavelength * math.sin(theta/2.0)
195
196                if has_converter == True and output.Q_unit != '1/A':
197                    qx = data_conv_q(qx, units=output.Q_unit)
198
199                if xmin==None or qx<xmin:
200                    xmin = qx
201                if xmax==None or qx>xmax:
202                    xmax = qx
203               
204                theta = (i_y-center_y)*0.5 / distance / 100.0
205                qy = 4.0*math.pi/wavelength * math.sin(theta/2.0)
206
207                if has_converter == True and output.Q_unit != '1/A':
208                    qy = data_conv_q(qy, units=output.Q_unit)
209               
210                if ymin==None or qy<ymin:
211                    ymin = qy
212                if ymax==None or qy>ymax:
213                    ymax = qy
214               
215                if not qx in x:
216                    x.append(qx)
217                if not qy in y:
218                    y.append(qy)
219               
220                itot += 1
221                 
222                 
223        theta = 0.25 / distance / 100.0
224        xstep = 4.0*math.pi/wavelength * math.sin(theta/2.0)
225       
226        theta = 0.25 / distance / 100.0
227        ystep = 4.0*math.pi/wavelength * math.sin(theta/2.0)
228       
229        # Store all data ######################################
230        # Store wavelength
231        if has_converter==True and output.source.wavelength_unit != 'A':
232            conv = Converter('A')
233            wavelength = conv(wavelength, units=output.source.wavelength_unit)
234        output.source.wavelength = wavelength
235
236        # Store distance
237        if has_converter==True and detector.distance_unit != 'm':
238            conv = Converter('m')
239            distance = conv(distance, units=detector.distance_unit)
240        detector.distance = distance
241 
242        # Store transmission
243        output.sample.transmission = transmission
244       
245        # Store pixel size
246        pixel = 5.0
247        if has_converter==True and detector.pixel_size_unit != 'mm':
248            conv = Converter('mm')
249            pixel = conv(pixel, units=detector.pixel_size_unit)
250        detector.pixel_size.x = pixel
251        detector.pixel_size.y = pixel
252 
253        # Store beam center in distance units
254        detector.beam_center.x = center_x*pixel
255        detector.beam_center.y = center_y*pixel
256 
257       
258        # Store limits of the image (2D array)
259        xmin    =xmin-xstep/2.0
260        xmax    =xmax+xstep/2.0
261        ymin    =ymin-ystep/2.0
262        ymax    =ymax+ystep/2.0
263        if has_converter == True and output.Q_unit != '1/A':
264            xmin = data_conv_q(xmin, units=output.Q_unit)
265            xmax = data_conv_q(xmax, units=output.Q_unit)
266            ymin = data_conv_q(ymin, units=output.Q_unit)
267            ymax = data_conv_q(ymax, units=output.Q_unit)
268        output.xmin = xmin
269        output.xmax = xmax
270        output.ymin = ymin
271        output.ymax = ymax
272       
273        # Store x and y axis bin centers
274        output.x_bins     = x
275        output.y_bins     = y
276       
277        # Units
278        if data_conv_q is not None:
279            output.xaxis("\\rm{Q_{x}}", output.Q_unit)
280            output.yaxis("\\rm{Q_{y}}", output.Q_unit)
281        else:
282            output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
283            output.yaxis("\\rm{Q_{y}}", 'A^{-1}')
284           
285        if data_conv_i is not None:
286            output.zaxis("\\{I(Q)}", output.I_unit)
287        else:
288            output.zaxis("\\rm{I(Q)}","cm^{-1}")
289   
290       
291        return output
292   
293if __name__ == "__main__": 
294    reader = Reader()
295    print reader.read("../test/MAR07232_rest.ASC") 
296   
Note: See TracBrowser for help on using the repository browser.