source: sasview/DataLoader/readers/ascii_reader.py @ 79ac6f8

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 79ac6f8 was 0997158f, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

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