source: sasview/DataLoader/readers/IgorReader.py @ 3cd95c8

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 3cd95c8 was eadf1f9, checked in by Jae Cho <jhjcho@…>, 14 years ago

added calling qmap converter

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