source: sasview/DataLoader/readers/abs_reader.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.5 KB
Line 
1"""
2This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2008, University of Tennessee
9"""
10
11import numpy
12import os
13from DataLoader.data_info import Data1D, Detector
14
15has_converter = True
16try:
17    from data_util.nxsunit import Converter
18except:
19    has_converter = False
20   
21class Reader:
22    """
23        Class to load IGOR reduced .ABS files
24    """
25    ## File type
26    type = ["IGOR 1D files (*.abs)|*.abs"]
27    ## List of allowed extensions
28    ext=['.abs', '.ABS'] 
29   
30    def read(self, path):
31        """
32            Load data file
33           
34            @param path: file path
35            @return: Data1D object, or None
36            @raise RuntimeError: when the file can't be opened
37            @raise ValueError: when the length of the data vectors are inconsistent
38        """
39        if os.path.isfile(path):
40            basename  = os.path.basename(path)
41            root, extension = os.path.splitext(basename)
42            if extension.lower() in self.ext:
43                try:
44                    input_f =  open(path,'r')
45                except :
46                    raise  RuntimeError, "abs_reader: cannot open %s" % path
47                buff = input_f.read()
48                lines = buff.split('\n')
49                x  = numpy.zeros(0)
50                y  = numpy.zeros(0)
51                dy = numpy.zeros(0)
52                output = Data1D(x, y, dy=dy)
53                detector = Detector()
54                output.detector.append(detector)
55                output.filename = basename
56               
57                is_info = False
58                is_center = False
59                is_data_started = False
60               
61                data_conv_q = None
62                data_conv_i = None
63               
64                if has_converter == True and output.x_unit != '1/A':
65                    data_conv_q = Converter('1/A')
66                    # Test it
67                    data_conv_q(1.0, output.x_unit)
68                   
69                if has_converter == True and output.y_unit != '1/cm':
70                    data_conv_i = Converter('1/cm')
71                    # Test it
72                    data_conv_i(1.0, output.y_unit)
73               
74                for line in lines:
75                   
76                    # Information line 1
77                    if is_info==True:
78                        is_info = False
79                        line_toks = line.split()
80                       
81                        # Wavelength in Angstrom
82                        try:
83                            value = float(line_toks[1])
84                            if has_converter==True and output.source.wavelength_unit != 'A':
85                                conv = Converter('A')
86                                output.source.wavelength = conv(value, units=output.source.wavelength_unit)
87                            else:
88                                output.source.wavelength = value
89                        except:
90                            raise ValueError,"IgorReader: can't read this file, missing wavelength"
91                       
92                        # Distance in meters
93                        try:
94                            value = float(line_toks[3])
95                            if has_converter==True and detector.distance_unit != 'm':
96                                conv = Converter('m')
97                                detector.distance = conv(value, units=detector.distance_unit)
98                            else:
99                                detector.distance = value
100                        except:
101                            raise ValueError,"IgorReader: can't read this file, missing distance"
102                       
103                        # Transmission
104                        try:
105                            output.sample.transmission = float(line_toks[4])
106                        except:
107                            # Transmission is not a mandatory entry
108                            pass
109                   
110                        # Thickness in mm
111                        try:
112                            value = float(line_toks[5])
113                            if has_converter==True and output.sample.thickness_unit != 'cm':
114                                conv = Converter('cm')
115                                output.sample.thickness = conv(value, units=output.sample.thickness_unit)
116                            else:
117                                output.sample.thickness = value
118                        except:
119                            # Thickness is not a mandatory entry
120                            pass
121                   
122                    #MON CNT   LAMBDA   DET ANG   DET DIST   TRANS   THICK   AVE   STEP
123                    if line.count("LAMBDA")>0:
124                        is_info = True
125                       
126                    # Find center info line
127                    if is_center==True:
128                        is_center = False               
129                        line_toks = line.split()
130                        # Center in bin number
131                        center_x = float(line_toks[0])
132                        center_y = float(line_toks[1])
133                       
134                        # Bin size
135                        if has_converter==True and detector.pixel_size_unit != 'mm':
136                            conv = Converter('mm')
137                            detector.pixel_size.x = conv(5.0, units=detector.pixel_size_unit)
138                            detector.pixel_size.y = conv(5.0, units=detector.pixel_size_unit)
139                        else:
140                            detector.pixel_size.x = 5.0
141                            detector.pixel_size.y = 5.0
142                       
143                        # Store beam center in distance units
144                        # Det 640 x 640 mm
145                        if has_converter==True and detector.beam_center_unit != 'mm':
146                            conv = Converter('mm')
147                            detector.beam_center.x = conv(center_x*5.0, units=detector.beam_center_unit)
148                            detector.beam_center.y = conv(center_y*5.0, units=detector.beam_center_unit)
149                        else:
150                            detector.beam_center.x = center_x*5.0
151                            detector.beam_center.y = center_y*5.0
152                       
153                        # Detector type
154                        try:
155                            detector.name = line_toks[7]
156                        except:
157                            # Detector name is not a mandatory entry
158                            pass
159                   
160                    #BCENT(X,Y)   A1(mm)   A2(mm)   A1A2DIST(m)   DL/L   BSTOP(mm)   DET_TYP
161                    if line.count("BCENT")>0:
162                        is_center = True
163                       
164                    # Parse the data
165                    if is_data_started==True:
166                        toks = line.split()
167                        try: 
168                            _x  = float(toks[0])
169                            _y  = float(toks[1]) 
170                            _dy = float(toks[2])
171                           
172                            if data_conv_q is not None:
173                                _x = data_conv_q(_x, units=output.x_unit)
174                               
175                            if data_conv_i is not None:
176                                _y = data_conv_i(_y, units=output.y_unit)
177                                _dy = data_conv_i(_dy, units=output.y_unit)
178                           
179                            x  = numpy.append(x,   _x) 
180                            y  = numpy.append(y,   _y)
181                            dy = numpy.append(dy, _dy)
182                        except:
183                            # Could not read this data line. If we are here
184                            # it is because we are in the data section. Just
185                            # skip it.
186                            pass
187                       
188                    #The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor|
189                    if line.count("The 6 columns")>0:
190                        is_data_started = True
191           
192                # Sanity check
193                if not len(y) == len(dy):
194                    raise ValueError, "abs_reader: y and dy have different length"
195
196                # If the data length is zero, consider this as
197                # though we were not able to read the file.
198                if len(x)==0:
199                    raise ValueError, "ascii_reader: could not load file"
200               
201                output.x = x
202                output.y = y
203                output.dy = dy
204                if data_conv_q is not None:
205                    output.xaxis("\\rm{Q}", output.x_unit)
206                else:
207                    output.xaxis("\\rm{Q}", 'A^{-1}')
208                if data_conv_i is not None:
209                    output.yaxis("\\{I(Q)}", output.y_unit)
210                else:
211                    output.yaxis("\\rm{I(Q)}","cm^{-1}")
212               
213                return output
214        else:
215            raise RuntimeError, "%s is not a file" % path
216        return None
217   
218if __name__ == "__main__": 
219    reader = Reader()
220    print reader.read("../test/jan08002.ABS")
221   
222   
223           
Note: See TracBrowser for help on using the repository browser.