source: sasview/DataLoader/readers/abs_reader.py @ 0f5fe6b

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 0f5fe6b was 0e2aa40, checked in by Jae Cho <jhjcho@…>, 15 years ago

Made the letter of units in plots consistent for all

  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[d8a5f12]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
[99d1af6]13from DataLoader.data_info import Data1D, Detector
[d8a5f12]14
[99d1af6]15has_converter = True
16try:
17    from data_util.nxsunit import Converter
18except:
19    has_converter = False
20   
[d8a5f12]21class Reader:
22    """
23        Class to load IGOR reduced .ABS files
24    """
[8780e9a]25    ## File type
26    type = ["IGOR 1D files (*.abs)|*.abs"]
[d8a5f12]27    ## List of allowed extensions
[8bd8ea4]28    ext=['.abs', '.ABS'] 
[d8a5f12]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)
[148ad64]52                dx = numpy.zeros(0)
53                output = Data1D(x, y, dy=dy, dx=dx)
[99d1af6]54                detector = Detector()
55                output.detector.append(detector)
[b99ac227]56                output.filename = basename
[d8a5f12]57               
58                is_info = False
59                is_center = False
60                is_data_started = False
61               
[99d1af6]62                data_conv_q = None
63                data_conv_i = None
64               
[ca10d8e]65                if has_converter == True and output.x_unit != '1/A':
66                    data_conv_q = Converter('1/A')
[99d1af6]67                    # Test it
68                    data_conv_q(1.0, output.x_unit)
69                   
[ca10d8e]70                if has_converter == True and output.y_unit != '1/cm':
71                    data_conv_i = Converter('1/cm')
[99d1af6]72                    # Test it
73                    data_conv_i(1.0, output.y_unit)
74               
[d8a5f12]75                for line in lines:
[470bf7e]76                   
[d8a5f12]77                    # Information line 1
78                    if is_info==True:
79                        is_info = False
80                        line_toks = line.split()
81                       
82                        # Wavelength in Angstrom
83                        try:
[99d1af6]84                            value = float(line_toks[1])
85                            if has_converter==True and output.source.wavelength_unit != 'A':
86                                conv = Converter('A')
87                                output.source.wavelength = conv(value, units=output.source.wavelength_unit)
88                            else:
89                                output.source.wavelength = value
[d8a5f12]90                        except:
[470bf7e]91                            #goes to ASC reader
92                            raise  RuntimeError, "abs_reader: cannot open %s" % path
[148ad64]93                            #raise ValueError,"IgorReader: can't read this file, missing wavelength"
[d8a5f12]94                       
95                        # Distance in meters
96                        try:
[99d1af6]97                            value = float(line_toks[3])
98                            if has_converter==True and detector.distance_unit != 'm':
99                                conv = Converter('m')
100                                detector.distance = conv(value, units=detector.distance_unit)
101                            else:
102                                detector.distance = value
[d8a5f12]103                        except:
[470bf7e]104                            #goes to ASC reader
105                            raise  RuntimeError, "abs_reader: cannot open %s" % path
[d8a5f12]106                       
[99d1af6]107                        # Transmission
[d8a5f12]108                        try:
109                            output.sample.transmission = float(line_toks[4])
110                        except:
111                            # Transmission is not a mandatory entry
112                            pass
113                   
[99d1af6]114                        # Thickness in mm
[d8a5f12]115                        try:
[99d1af6]116                            value = float(line_toks[5])
[b99ac227]117                            if has_converter==True and output.sample.thickness_unit != 'cm':
118                                conv = Converter('cm')
[99d1af6]119                                output.sample.thickness = conv(value, units=output.sample.thickness_unit)
120                            else:
121                                output.sample.thickness = value
[d8a5f12]122                        except:
123                            # Thickness is not a mandatory entry
124                            pass
[470bf7e]125                   
126                    #MON CNT   LAMBDA   DET ANG   DET DIST   TRANS   THICK   AVE   STEP
127                    if line.count("LAMBDA")>0:
128                        is_info = True
[d8a5f12]129                       
[470bf7e]130                    # Find center info line
131                    if is_center==True:
132                        is_center = False               
133                        line_toks = line.split()
134                        # Center in bin number
135                        center_x = float(line_toks[0])
136                        center_y = float(line_toks[1])
[99d1af6]137                       
[470bf7e]138                        # Bin size
139                        if has_converter==True and detector.pixel_size_unit != 'mm':
140                            conv = Converter('mm')
141                            detector.pixel_size.x = conv(5.0, units=detector.pixel_size_unit)
142                            detector.pixel_size.y = conv(5.0, units=detector.pixel_size_unit)
143                        else:
144                            detector.pixel_size.x = 5.0
145                            detector.pixel_size.y = 5.0
[99d1af6]146                       
[470bf7e]147                        # Store beam center in distance units
148                        # Det 640 x 640 mm
149                        if has_converter==True and detector.beam_center_unit != 'mm':
150                            conv = Converter('mm')
151                            detector.beam_center.x = conv(center_x*5.0, units=detector.beam_center_unit)
152                            detector.beam_center.y = conv(center_y*5.0, units=detector.beam_center_unit)
153                        else:
154                            detector.beam_center.x = center_x*5.0
155                            detector.beam_center.y = center_y*5.0
[d8a5f12]156                       
[470bf7e]157                        # Detector type
158                        try:
159                            detector.name = line_toks[7]
160                        except:
161                            # Detector name is not a mandatory entry
162                            pass
163                   
164                    #BCENT(X,Y)   A1(mm)   A2(mm)   A1A2DIST(m)   DL/L   BSTOP(mm)   DET_TYP
165                    if line.count("BCENT")>0:
166                        is_center = True
[d8a5f12]167                       
[470bf7e]168                    # Parse the data
169                    if is_data_started==True:
170                        toks = line.split()
171
172                        try: 
173                            _x  = float(toks[0])
174                            _y  = float(toks[1]) 
175                            _dy = float(toks[2])
176                            _dx = float(toks[3])
[99d1af6]177                           
[470bf7e]178                            if data_conv_q is not None:
179                                _x = data_conv_q(_x, units=output.x_unit)
180                                _dx = data_conv_i(_dx, units=output.x_unit)
[148ad64]181                               
[470bf7e]182                            if data_conv_i is not None:
183                                _y = data_conv_i(_y, units=output.y_unit)
184                                _dy = data_conv_i(_dy, units=output.y_unit)
185                           
186                            x  = numpy.append(x,   _x) 
187                            y  = numpy.append(y,   _y)
188                            dy = numpy.append(dy, _dy)
189                            dx  = numpy.append(dx, _dx)
190                           
191                        except:
192                            # Could not read this data line. If we are here
193                            # it is because we are in the data section. Just
194                            # skip it.
195                            pass
[148ad64]196                           
[470bf7e]197                    #The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor|
198                    if line.count("The 6 columns")>0:
199                        is_data_started = True
200           
[d8a5f12]201                # Sanity check
202                if not len(y) == len(dy):
203                    raise ValueError, "abs_reader: y and dy have different length"
[8bd8ea4]204
205                # If the data length is zero, consider this as
206                # though we were not able to read the file.
207                if len(x)==0:
208                    raise ValueError, "ascii_reader: could not load file"
[d8a5f12]209               
210                output.x = x
211                output.y = y
212                output.dy = dy
[148ad64]213                output.dx = dx
[99d1af6]214                if data_conv_q is not None:
215                    output.xaxis("\\rm{Q}", output.x_unit)
216                else:
217                    output.xaxis("\\rm{Q}", 'A^{-1}')
218                if data_conv_i is not None:
[0e2aa40]219                    output.yaxis("\\rm{Intensity}", output.y_unit)
[99d1af6]220                else:
[0e2aa40]221                    output.yaxis("\\rm{Intensity}","cm^{-1}")
[d8a5f12]222                return output
223        else:
224            raise RuntimeError, "%s is not a file" % path
225        return None
226   
227if __name__ == "__main__": 
228    reader = Reader()
[470bf7e]229    print reader.read("../test/jan08002.ABS")
[d8a5f12]230   
231   
232           
Note: See TracBrowser for help on using the repository browser.