source: sasview/DataLoader/readers/ascii_reader.py @ 5973b566

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 5973b566 was 28caa03, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

Improved hierarchical reader structure, put back reader plugin, minor fixes.

  • Property mode set to 100644
File size: 8.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
15# Check whether we have a converter available
16has_converter = True
17try:
18    from data_util.nxsunit import Converter
19except:
20    has_converter = False
21
22class Reader:
23    """
24        Class to load ascii files (2 or 3 columns)
25    """
26    ## File type
27    type_name = "ASCII"
28   
29    ## Wildcards
30    type = ["ASCII files (*.txt)|*.txt",
31            "ASCII files (*.dat)|*.dat",
32            "ASCII files (*.abs)|*.abs"]
33    ## List of allowed extensions
34    ext=['.txt', '.TXT', '.dat', '.DAT', '.abs', '.ABS'] 
35   
36    def read(self, path):
37        """
38            Load data file
39           
40            @param path: file path
41            @return: Data1D object, or None
42            @raise RuntimeError: when the file can't be opened
43            @raise ValueError: when the length of the data vectors are inconsistent
44        """
45        if os.path.isfile(path):
46            basename  = os.path.basename(path)
47            root, extension = os.path.splitext(basename)
48            if extension.lower() in self.ext:
49                try:
50                    input_f =  open(path,'r')
51                except :
52                    raise  RuntimeError, "ascii_reader: cannot open %s" % path
53                buff = input_f.read()
54                lines = buff.split('\n')
55               
56                # some ascii data has \r line separator, try it when it has only one line
57                if len(lines) < 2 : 
58                    lines = buff.split('\r')
59                 
60                x  = numpy.zeros(0)
61                y  = numpy.zeros(0)
62                dy = numpy.zeros(0)
63                dx = numpy.zeros(0)
64               
65               #temp. space to sort data
66                tx  = numpy.zeros(0)
67                ty  = numpy.zeros(0)
68                tdy = numpy.zeros(0)
69                tdx = numpy.zeros(0)
70               
71                output = Data1D(x, y, dy=dy, dx=dx)
72                self.filename = output.filename = basename
73           
74                data_conv_q = None
75                data_conv_i = None
76               
77                if has_converter == True and output.x_unit != '1/A':
78                    data_conv_q = Converter('1/A')
79                    # Test it
80                    data_conv_q(1.0, output.x_unit)
81                   
82                if has_converter == True and output.y_unit != '1/cm':
83                    data_conv_i = Converter('1/cm')
84                    # Test it
85                    data_conv_i(1.0, output.y_unit)
86           
87               
88                # The first good line of data will define whether
89                # we have 2-column or 3-column ascii
90                has_error_dx = None
91                has_error_dy = None
92               
93                for line in lines:
94                    toks = line.split()
95                    try:
96                        _x = float(toks[0])
97                        _y = float(toks[1])
98                       
99                        if data_conv_q is not None:
100                            _x = data_conv_q(_x, units=output.x_unit)
101                           
102                        if data_conv_i is not None:
103                            _y = data_conv_i(_y, units=output.y_unit)                       
104                       
105                        # If we have an extra token, check
106                        # whether it can be interpreted as a
107                        # third column.
108                        _dy = None
109                        if len(toks)>2:
110                            try:
111                                _dy = float(toks[2])
112                               
113                                if data_conv_i is not None:
114                                    _dy = data_conv_i(_dy, units=output.y_unit)
115                               
116                            except:
117                                # The third column is not a float, skip it.
118                                pass
119                           
120                        # If we haven't set the 3rd column
121                        # flag, set it now.
122                        if has_error_dy == None:
123                            has_error_dy = False if _dy == None else True
124                           
125                        #Check for dx
126                        _dx = None
127                        if len(toks)>3:
128                            try:
129                                _dx = float(toks[3])
130                               
131                                if data_conv_i is not None:
132                                    _dx = data_conv_i(_dx, units=output.x_unit)
133                               
134                            except:
135                                # The 4th column is not a float, skip it.
136                                pass
137                           
138                        # If we haven't set the 3rd column
139                        # flag, set it now.
140                        if has_error_dx == None:
141                            has_error_dx = False if _dx == None else True
142
143                        x  = numpy.append(x,   _x) 
144                        y  = numpy.append(y,   _y)
145                        if has_error_dy == True:
146                            dy = numpy.append(dy, _dy)
147                        if has_error_dx == True:
148                            dx = numpy.append(dx, _dx)
149                           
150                        #Same for temp.
151                        tx  = numpy.append(tx,   _x) 
152                        ty  = numpy.append(ty,   _y)
153                        if has_error_dy == True:
154                            tdy = numpy.append(tdy, _dy)
155                        if has_error_dx == True:
156                            tdx = numpy.append(tdx, _dx)
157                       
158                    except:
159                        # Couldn't parse this line, skip it
160                        pass
161                         
162                     
163                # Sanity check
164                if has_error_dy == True and not len(y) == len(dy):
165                    raise RuntimeError, "ascii_reader: y and dy have different length"
166                if has_error_dx == True and not len(x) == len(dx):
167                    raise RuntimeError, "ascii_reader: y and dy have different length"
168
169                # If the data length is zero, consider this as
170                # though we were not able to read the file.
171                if len(x)==0:
172                    raise RuntimeError, "ascii_reader: could not load file"
173               
174                #Let's re-order the data to make cal. curve look better some cases
175                ind = numpy.lexsort((ty,tx))
176                for i in ind:
177                    x[i] = tx[ind[i]]
178                    y[i] = ty[ind[i]]
179                    if has_error_dy == True:
180                        dy[i] = tdy[ind[i]]
181                    if has_error_dx == True:
182                        dx[i] = tdx[ind[i]]
183                   
184                output.x = x
185                output.y = y
186                output.dy = dy if has_error_dy == True else None
187                output.dx = dx if has_error_dx == True else None
188               
189                if data_conv_q is not None:
190                    output.xaxis("\\rm{Q}", output.x_unit)
191                else:
192                    output.xaxis("\\rm{Q}", 'A^{-1}')
193                if data_conv_i is not None:
194                    output.yaxis("\\rm{Intensity}", output.y_unit)
195                else:
196                    output.yaxis("\\rm{Intensity}","cm^{-1}")
197
198                return output
199        else:
200            raise RuntimeError, "%s is not a file" % path
201        return None
202   
203if __name__ == "__main__": 
204    reader = Reader()
205    #print reader.read("../test/test_3_columns.txt")
206    print reader.read("../test/empty.txt")
207   
208   
209                       
Note: See TracBrowser for help on using the repository browser.