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

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

working pylint

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