source: sasview/src/sas/dataloader/readers/ascii_reader.py @ 79492222

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 79492222 was 79492222, checked in by krzywon, 9 years ago

Changed the file and folder names to remove all SANS references.

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