source: sasview/DataLoader/readers/ascii_reader.py @ 272b107

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

improved…

  • Property mode set to 100644
File size: 14.5 KB
RevLine 
[8bd8ea4]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
[daa56d0]15# Check whether we have a converter available
[99d1af6]16has_converter = True
17try:
18    from data_util.nxsunit import Converter
19except:
20    has_converter = False
21
[8bd8ea4]22class Reader:
23    """
24        Class to load ascii files (2 or 3 columns)
25    """
[8780e9a]26    ## File type
[28caa03]27    type_name = "ASCII"
28   
29    ## Wildcards
[8780e9a]30    type = ["ASCII files (*.txt)|*.txt",
[470bf7e]31            "ASCII files (*.dat)|*.dat",
32            "ASCII files (*.abs)|*.abs"]
[8bd8ea4]33    ## List of allowed extensions
[470bf7e]34    ext=['.txt', '.TXT', '.dat', '.DAT', '.abs', '.ABS'] 
[8bd8ea4]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')
[470bf7e]55               
[892f246]56                #Jae could not find python universal line spliter: keep the below for now
[470bf7e]57                # some ascii data has \r line separator, try it when it has only one line
58                if len(lines) < 2 : 
59                    lines = buff.split('\r')
60                 
[8bd8ea4]61                x  = numpy.zeros(0)
62                y  = numpy.zeros(0)
63                dy = numpy.zeros(0)
[de1da34]64                dx = numpy.zeros(0)
65               
66               #temp. space to sort data
67                tx  = numpy.zeros(0)
68                ty  = numpy.zeros(0)
69                tdy = numpy.zeros(0)
70                tdx = numpy.zeros(0)
71               
72                output = Data1D(x, y, dy=dy, dx=dx)
[8bd8ea4]73                self.filename = output.filename = basename
[99d1af6]74           
75                data_conv_q = None
76                data_conv_i = None
77               
[ca10d8e]78                if has_converter == True and output.x_unit != '1/A':
79                    data_conv_q = Converter('1/A')
[99d1af6]80                    # Test it
81                    data_conv_q(1.0, output.x_unit)
82                   
[ca10d8e]83                if has_converter == True and output.y_unit != '1/cm':
84                    data_conv_i = Converter('1/cm')
[99d1af6]85                    # Test it
86                    data_conv_i(1.0, output.y_unit)
87           
[8bd8ea4]88               
89                # The first good line of data will define whether
90                # we have 2-column or 3-column ascii
[de1da34]91                has_error_dx = None
92                has_error_dy = None
[8bd8ea4]93               
[892f246]94                #Initialize counters for data lines and header lines.
95                is_data = False #Has more than 3 lines
[d508be9]96                mum_data_lines = 3 # More than "3" lines of data is considered as actual data unless that is the only data
[892f246]97
[d508be9]98                i=-1            # To count # of current data candidate lines
99                i1=-1           # To count total # of previous data candidate lines
100                j=-1            # To count # of header lines
101                j1=-1           # Helps to count # of header lines
102                lentoks = 2     # minimum required number of columns of data; ( <= 4).
[892f246]103               
[8bd8ea4]104                for line in lines:
105                    toks = line.split()
[892f246]106                   
[8bd8ea4]107                    try:
[892f246]108                       
[8bd8ea4]109                        _x = float(toks[0])
110                        _y = float(toks[1])
111                       
[d508be9]112                        #To reject the line when reader meets less columns of data
[272b107]113                        #if lentoks == 3:
114                        #    _dy = float(toks[2])
115                        #elif lentoks == 4:
116                        #    _dx = float(toks[3])
[d508be9]117                       
[892f246]118                        #Reset the header line counters
119                        if j == j1:
120                            j = 0
121                            j1 = 0
122                           
123                        if i > 1:
124                            is_data = True
[d508be9]125                       
[99d1af6]126                        if data_conv_q is not None:
127                            _x = data_conv_q(_x, units=output.x_unit)
128                           
129                        if data_conv_i is not None:
130                            _y = data_conv_i(_y, units=output.y_unit)                       
131                       
[8bd8ea4]132                        # If we have an extra token, check
133                        # whether it can be interpreted as a
134                        # third column.
135                        _dy = None
136                        if len(toks)>2:
137                            try:
138                                _dy = float(toks[2])
[99d1af6]139                               
140                                if data_conv_i is not None:
141                                    _dy = data_conv_i(_dy, units=output.y_unit)
142                               
[8bd8ea4]143                            except:
144                                # The third column is not a float, skip it.
145                                pass
146                           
147                        # If we haven't set the 3rd column
148                        # flag, set it now.
[de1da34]149                        if has_error_dy == None:
150                            has_error_dy = False if _dy == None else True
151                           
152                        #Check for dx
153                        _dx = None
154                        if len(toks)>3:
155                            try:
156                                _dx = float(toks[3])
157                               
158                                if data_conv_i is not None:
159                                    _dx = data_conv_i(_dx, units=output.x_unit)
160                               
161                            except:
162                                # The 4th column is not a float, skip it.
163                                pass
164                           
165                        # If we haven't set the 3rd column
166                        # flag, set it now.
167                        if has_error_dx == None:
168                            has_error_dx = False if _dx == None else True
[892f246]169                       
[d508be9]170                        #After talked with PB, we decided to take care of only 4 columns of data for now.
171                        #number of columns in the current line
172                        if len(toks)>= 4:
173                            new_lentoks = 4
174                        else:
175                            new_lentoks = len(toks)
176                       
177                        #If the previous columns are less than the current, mark the previous as non-data and reset the dependents. 
[272b107]178                        if lentoks != new_lentoks :
179                            if is_data == True:
180                                break
181                            else:
[d508be9]182                                i = -1
183                                i1 = 0
184                                j = -1
185                                j1 = -1
186                           
[272b107]187                           
[892f246]188                        #Delete the previously stored lines of data candidates if is not data.
189                        if i < 0 and -1< i1 < mum_data_lines and is_data == False:
190                            try:
191                                x= numpy.zeros(0)
192                                y= numpy.zeros(0)
193                               
194                            except:
195                                pass
196                           
[8bd8ea4]197                        x  = numpy.append(x,   _x) 
198                        y  = numpy.append(y,   _y)
[892f246]199                       
[de1da34]200                        if has_error_dy == True:
[892f246]201                            #Delete the previously stored lines of data candidates if is not data.
202                            if i < 0 and -1< i1 < mum_data_lines and is_data== False:
203                                try:
204                                    dy = numpy.zeros(0) 
205                                except:
206                                    pass                                                               
[8bd8ea4]207                            dy = numpy.append(dy, _dy)
[892f246]208                           
[de1da34]209                        if has_error_dx == True:
[892f246]210                            #Delete the previously stored lines of data candidates if is not data.
211                            if i < 0 and -1< i1 < mum_data_lines and is_data== False:
212                                try:
213                                    dx = numpy.zeros(0)                           
214                                except:
215                                    pass                                                               
[de1da34]216                            dx = numpy.append(dx, _dx)
217                           
218                        #Same for temp.
[892f246]219                        #Delete the previously stored lines of data candidates if is not data.
220                        if i < 0 and -1< i1 < mum_data_lines and is_data== False:
221                            try:
222                                tx = numpy.zeros(0)
223                                ty = numpy.zeros(0)
224                            except:
225                                pass                                                               
226
[de1da34]227                        tx  = numpy.append(tx,   _x) 
228                        ty  = numpy.append(ty,   _y)
[892f246]229                       
[de1da34]230                        if has_error_dy == True:
[892f246]231                            #Delete the previously stored lines of data candidates if is not data.
232                            if i < 0 and -1<i1 < mum_data_lines and is_data== False:
233                                try:
234                                    tdy = numpy.zeros(0)
235                                except:
236                                    pass                                                                                                               
[de1da34]237                            tdy = numpy.append(tdy, _dy)
238                        if has_error_dx == True:
[892f246]239                            #Delete the previously stored lines of data candidates if is not data.
240                            if i < 0 and -1< i1 < mum_data_lines and is_data== False:
241                                try:
242                                    tdx = numpy.zeros(0)
243                                except:
244                                    pass                                                                                                             
[de1da34]245                            tdx = numpy.append(tdx, _dx)
[d508be9]246
247                        #reset i1 and flag lentoks for the next
248                        if lentoks < new_lentoks :
249                            if is_data == False:
250                                i1 = -1                           
251                        if len(toks)>= 4:
252                            lentoks = 4
253                        else:
254                            lentoks = len(toks)
[8bd8ea4]255                       
[892f246]256                        #Reset # of header lines and counts # of data candidate lines   
257                        if j == 0 and j1 ==0:
258                            i1 = i + 1                           
259                        i+=1
260                       
[8bd8ea4]261                    except:
[892f246]262
263                        # It is data and meet non - number, then stop reading
264                        if is_data == True:
265                            break   
[d508be9]266                        lentoks = 2
[892f246]267                        #Counting # of header lines                   
268                        j+=1
269                        if j == j1+1:
270                            j1 = j                           
271                        else:                           
272                            j = -1
273                        #Reset # of lines of data candidates
274                        i = -1
275                       
[8bd8ea4]276                        # Couldn't parse this line, skip it
277                        pass
[892f246]278                   
279   
[8bd8ea4]280                     
281                # Sanity check
[de1da34]282                if has_error_dy == True and not len(y) == len(dy):
283                    raise RuntimeError, "ascii_reader: y and dy have different length"
284                if has_error_dx == True and not len(x) == len(dx):
[daa56d0]285                    raise RuntimeError, "ascii_reader: y and dy have different length"
[8bd8ea4]286
287                # If the data length is zero, consider this as
288                # though we were not able to read the file.
289                if len(x)==0:
[daa56d0]290                    raise RuntimeError, "ascii_reader: could not load file"
[de1da34]291               
[470bf7e]292                #Let's re-order the data to make cal. curve look better some cases
[de1da34]293                ind = numpy.lexsort((ty,tx))
294                for i in ind:
295                    x[i] = tx[ind[i]]
296                    y[i] = ty[ind[i]]
297                    if has_error_dy == True:
298                        dy[i] = tdy[ind[i]]
299                    if has_error_dx == True:
300                        dx[i] = tdx[ind[i]]
[892f246]301               
[d508be9]302               
[892f246]303                #Data   
[8bd8ea4]304                output.x = x
305                output.y = y
[de1da34]306                output.dy = dy if has_error_dy == True else None
307                output.dx = dx if has_error_dx == True else None
308               
[99d1af6]309                if data_conv_q is not None:
310                    output.xaxis("\\rm{Q}", output.x_unit)
311                else:
312                    output.xaxis("\\rm{Q}", 'A^{-1}')
313                if data_conv_i is not None:
[0e2aa40]314                    output.yaxis("\\rm{Intensity}", output.y_unit)
[99d1af6]315                else:
[0e2aa40]316                    output.yaxis("\\rm{Intensity}","cm^{-1}")
[8bd8ea4]317                return output
[892f246]318           
[8bd8ea4]319        else:
320            raise RuntimeError, "%s is not a file" % path
321        return None
322   
323if __name__ == "__main__": 
324    reader = Reader()
325    #print reader.read("../test/test_3_columns.txt")
326    print reader.read("../test/empty.txt")
327   
328   
329                       
Note: See TracBrowser for help on using the repository browser.