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

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 ca10d8e was ca10d8e, checked in by Gervaise Alina <gervyh@…>, 15 years ago

reverse code from version 1237 with modification on output axis unit

  • Property mode set to 100644
File size: 9.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        # 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                theta = (i_x-center_x+1)*0.5 / distance / 100.0
191                qx = 4.0*math.pi/wavelength * math.sin(theta/2.0)
192
193                if has_converter == True and output.Q_unit != '1/A':
194                    qx = data_conv_q(qx, units=output.Q_unit)
195
196                if xmin==None or qx<xmin:
197                    xmin = qx
198                if xmax==None or qx>xmax:
199                    xmax = qx
200               
201                theta = (i_y-center_y+1)*0.5 / distance / 100.0
202                qy = 4.0*math.pi/wavelength * math.sin(theta/2.0)
203
204                if has_converter == True and output.Q_unit != '1/A':
205                    qy = data_conv_q(qy, units=output.Q_unit)
206               
207                if ymin==None or qy<ymin:
208                    ymin = qy
209                if ymax==None or qy>ymax:
210                    ymax = qy
211               
212                if not qx in x:
213                    x.append(qx)
214                if not qy in y:
215                    y.append(qy)
216               
217                itot += 1
218                 
219                 
220        theta = 0.25 / distance / 100.0
221        xstep = 4.0*math.pi/wavelength * math.sin(theta/2.0)
222       
223        theta = 0.25 / distance / 100.0
224        ystep = 4.0*math.pi/wavelength * math.sin(theta/2.0)
225       
226        # Store all data ######################################
227        # Store wavelength
228        if has_converter==True and output.source.wavelength_unit != 'A':
229            conv = Converter('A')
230            wavelength = conv(wavelength, units=output.source.wavelength_unit)
231        output.source.wavelength = wavelength
232
233        # Store distance
234        if has_converter==True and detector.distance_unit != 'm':
235            conv = Converter('m')
236            distance = conv(distance, units=detector.distance_unit)
237        detector.distance = distance
238 
239        # Store transmission
240        output.sample.transmission = transmission
241       
242        # Store pixel size
243        pixel = 5.0
244        if has_converter==True and detector.pixel_size_unit != 'mm':
245            conv = Converter('mm')
246            pixel = conv(pixel, units=detector.pixel_size_unit)
247        detector.pixel_size.x = pixel
248        detector.pixel_size.y = pixel
249 
250        # Store beam center in distance units
251        detector.beam_center.x = center_x*pixel
252        detector.beam_center.y = center_y*pixel
253 
254       
255        # Store limits of the image (2D array)
256        xmin    =xmin-xstep/2.0
257        xmax    =xmax+xstep/2.0
258        ymin    =ymin-ystep/2.0
259        ymax    =ymax+ystep/2.0
260        if has_converter == True and output.Q_unit != '1/A':
261            xmin = data_conv_q(xmin, units=output.Q_unit)
262            xmax = data_conv_q(xmax, units=output.Q_unit)
263            ymin = data_conv_q(ymin, units=output.Q_unit)
264            ymax = data_conv_q(ymax, units=output.Q_unit)
265        output.xmin = xmin
266        output.xmax = xmax
267        output.ymin = ymin
268        output.ymax = ymax
269       
270        # Store x and y axis bin centers
271        output.x_bins     = x
272        output.y_bins     = y
273       
274        # Units
275        if data_conv_q is not None:
276            output.xaxis("\\rm{Q_{x}}", output.Q_unit)
277            output.yaxis("\\rm{Q_{y}}", output.Q_unit)
278        else:
279            output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
280            output.yaxis("\\rm{Q_{y}}", 'A^{-1}')
281           
282        if data_conv_i is not None:
283            output.zaxis("\\{I(Q)}", output.I_unit)
284        else:
285            output.zaxis("\\rm{I(Q)}","cm^{-1}")
286   
287       
288        return output
289   
290if __name__ == "__main__": 
291    reader = Reader()
292    print reader.read("../test/MAR07232_rest.ASC") 
293   
Note: See TracBrowser for help on using the repository browser.