source: sasview/DataLoader/readers/abs_reader.py @ cfe97ea

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 cfe97ea was 8780e9a, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

First implementation of reader for CanSas? format

  • Property mode set to 100644
File size: 6.2 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
14
15class Reader:
16    """
17        Class to load IGOR reduced .ABS files
18    """
19    ## File type
20    type = ["IGOR 1D files (*.abs)|*.abs"]
21    ## List of allowed extensions
22    ext=['.abs', '.ABS'] 
23   
24    def read(self, path):
25        """
26            Load data file
27           
28            @param path: file path
29            @return: Data1D object, or None
30            @raise RuntimeError: when the file can't be opened
31            @raise ValueError: when the length of the data vectors are inconsistent
32        """
33        if os.path.isfile(path):
34            basename  = os.path.basename(path)
35            root, extension = os.path.splitext(basename)
36            if extension.lower() in self.ext:
37                try:
38                    input_f =  open(path,'r')
39                except :
40                    raise  RuntimeError, "abs_reader: cannot open %s" % path
41                buff = input_f.read()
42                lines = buff.split('\n')
43                x  = numpy.zeros(0)
44                y  = numpy.zeros(0)
45                dy = numpy.zeros(0)
46                output = Data1D(x, y, dy=dy)
47                self.filename = output.filename = basename
48               
49                is_info = False
50                is_center = False
51                is_data_started = False
52               
53                for line in lines:
54                   
55                    # Information line 1
56                    if is_info==True:
57                        is_info = False
58                        line_toks = line.split()
59                       
60                        # Wavelength in Angstrom
61                        try:
62                            output.source.wavelength = float(line_toks[1])
63                        except:
64                            raise ValueError,"IgorReader: can't read this file, missing wavelength"
65                       
66                        # Distance in meters
67                        try:
68                            output.detector.distance = float(line_toks[3])*1000.0
69                        except:
70                            raise ValueError,"IgorReader: can't read this file, missing distance"
71                       
72                        # Transmission
73                        try:
74                            output.sample.transmission = float(line_toks[4])
75                        except:
76                            # Transmission is not a mandatory entry
77                            pass
78                   
79                        # Thickness
80                        try:
81                            output.sample.thickness = float(line_toks[5])
82                        except:
83                            # Thickness is not a mandatory entry
84                            pass
85                   
86                    #MON CNT   LAMBDA   DET ANG   DET DIST   TRANS   THICK   AVE   STEP
87                    if line.count("LAMBDA")>0:
88                        is_info = True
89                       
90                    # Find center info line
91                    if is_center==True:
92                        is_center = False               
93                        line_toks = line.split()
94                        # Center in bin number
95                        center_x = float(line_toks[0])
96                        center_y = float(line_toks[1])
97                        output.detector.beam_center.x = center_x
98                        output.detector.beam_center.y = center_y
99                       
100                        # Detector type
101                        try:
102                            output.detector.name = line_toks[7]
103                        except:
104                            # Detector name is not a mandatory entry
105                            pass
106                   
107                    #BCENT(X,Y)   A1(mm)   A2(mm)   A1A2DIST(m)   DL/L   BSTOP(mm)   DET_TYP
108                    if line.count("BCENT")>0:
109                        is_center = True
110                       
111                    # Parse the data
112                    if is_data_started==True:
113                        toks = line.split()
114                        try: 
115                            _x  = float(toks[0])
116                            _y  = float(toks[1]) 
117                            _dy = float(toks[2])
118                           
119                            x  = numpy.append(x,   _x) 
120                            y  = numpy.append(y,   _y)
121                            dy = numpy.append(dy, _dy)
122                        except:
123                            # Could not read this data line. If we are here
124                            # it is because we are in the data section. Just
125                            # skip it.
126                            pass
127                       
128                    #The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor|
129                    if line.count("The 6 columns")>0:
130                        is_data_started = True
131           
132                # Sanity check
133                if not len(y) == len(dy):
134                    raise ValueError, "abs_reader: y and dy have different length"
135
136                # If the data length is zero, consider this as
137                # though we were not able to read the file.
138                if len(x)==0:
139                    raise ValueError, "ascii_reader: could not load file"
140               
141                output.x = x
142                output.y = y
143                output.dy = dy
144                output.xaxis("\\rm{Q}", 'A^{-1}')
145                output.yaxis("\\rm{I(Q)}","cm^{-1}")
146               
147                return output
148        else:
149            raise RuntimeError, "%s is not a file" % path
150        return None
151   
152if __name__ == "__main__": 
153    reader = Reader()
154    print reader.read("../test/jan08002.ABS")
155   
156   
157           
Note: See TracBrowser for help on using the repository browser.