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

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 b699768 was b699768, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Initial commit of the refactored SasCalc? module.

  • Property mode set to 100644
File size: 15.3 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
[b699768]18from sas.sascalc.dataloader.data_info import Data1D
[8bd8ea4]19
[daa56d0]20# Check whether we have a converter available
[99d1af6]21has_converter = True
22try:
[b699768]23    from sas.sascalc.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()
[ef3445e2]69                lines = buff.splitlines()
[470bf7e]70                 
[8bd8ea4]71                x  = numpy.zeros(0)
72                y  = numpy.zeros(0)
73                dy = numpy.zeros(0)
[de1da34]74                dx = numpy.zeros(0)
75               
[ef3445e2]76                #temp. space to sort data
[de1da34]77                tx  = numpy.zeros(0)
78                ty  = numpy.zeros(0)
79                tdy = numpy.zeros(0)
80                tdx = numpy.zeros(0)
81               
82                output = Data1D(x, y, dy=dy, dx=dx)
[8bd8ea4]83                self.filename = output.filename = basename
[99d1af6]84           
85                data_conv_q = None
86                data_conv_i = None
87               
[ca10d8e]88                if has_converter == True and output.x_unit != '1/A':
89                    data_conv_q = Converter('1/A')
[99d1af6]90                    # Test it
91                    data_conv_q(1.0, output.x_unit)
92                   
[ca10d8e]93                if has_converter == True and output.y_unit != '1/cm':
94                    data_conv_i = Converter('1/cm')
[99d1af6]95                    # Test it
96                    data_conv_i(1.0, output.y_unit)
97           
[8bd8ea4]98                # The first good line of data will define whether
99                # we have 2-column or 3-column ascii
[de1da34]100                has_error_dx = None
101                has_error_dy = None
[8bd8ea4]102               
[892f246]103                #Initialize counters for data lines and header lines.
[7d6351e]104                is_data = False  # Has more than 5 lines
[a7a5886]105                # More than "5" lines of data is considered as actual
106                # data unless that is the only data
[7d6351e]107                mum_data_lines = 5
[a7a5886]108                # To count # of current data candidate lines
[7d6351e]109                i = -1
110                # To count total # of previous data candidate lines
111                i1 = -1
112                # To count # of header lines
[a7a5886]113                j = -1
[7d6351e]114                # Helps to count # of header lines
[a7a5886]115                j1 = -1
[7d6351e]116                #minimum required number of columns of data; ( <= 4).
117                lentoks = 2
[8bd8ea4]118                for line in lines:
[86d13b5]119                    # Initial try for CSV (split on ,)
[ef9d209]120                    toks = line.split(',')
[86d13b5]121                    # Now try SCSV (split on ;)
122                    if len(toks) < 2:
123                        toks = line.split(';')
124                    # Now go for whitespace                   
[ef9d209]125                    if len(toks) < 2:
126                        toks = line.split()
[8bd8ea4]127                    try:
[5f2d3c78]128                        #Make sure that all columns are numbers.
129                        for colnum in range(len(toks)):
130                            float(toks[colnum])
131                           
[8bd8ea4]132                        _x = float(toks[0])
133                        _y = float(toks[1])
134                       
[892f246]135                        #Reset the header line counters
136                        if j == j1:
137                            j = 0
138                            j1 = 0
139                           
140                        if i > 1:
141                            is_data = True
[d508be9]142                       
[99d1af6]143                        if data_conv_q is not None:
144                            _x = data_conv_q(_x, units=output.x_unit)
145                           
146                        if data_conv_i is not None:
[7d6351e]147                            _y = data_conv_i(_y, units=output.y_unit)
[99d1af6]148                       
[8bd8ea4]149                        # If we have an extra token, check
150                        # whether it can be interpreted as a
151                        # third column.
152                        _dy = None
[a7a5886]153                        if len(toks) > 2:
[8bd8ea4]154                            try:
155                                _dy = float(toks[2])
[99d1af6]156                               
157                                if data_conv_i is not None:
158                                    _dy = data_conv_i(_dy, units=output.y_unit)
159                               
[8bd8ea4]160                            except:
161                                # The third column is not a float, skip it.
162                                pass
163                           
164                        # If we haven't set the 3rd column
165                        # flag, set it now.
[de1da34]166                        if has_error_dy == None:
167                            has_error_dy = False if _dy == None else True
168                           
169                        #Check for dx
170                        _dx = None
[a7a5886]171                        if len(toks) > 3:
[de1da34]172                            try:
173                                _dx = float(toks[3])
174                               
175                                if data_conv_i is not None:
176                                    _dx = data_conv_i(_dx, units=output.x_unit)
177                               
178                            except:
179                                # The 4th column is not a float, skip it.
180                                pass
181                           
182                        # If we haven't set the 3rd column
183                        # flag, set it now.
184                        if has_error_dx == None:
185                            has_error_dx = False if _dx == None else True
[892f246]186                       
[7d6351e]187                        #After talked with PB, we decided to take care of only
[a7a5886]188                        # 4 columns of data for now.
[d508be9]189                        #number of columns in the current line
[7d6351e]190                        #To remember the # of columns in the current
[a7a5886]191                        #line of data
[0e5e586]192                        new_lentoks = len(toks)
[d508be9]193                       
[7d6351e]194                        #If the previous columns not equal to the current,
195                        #mark the previous as non-data and reset the dependents.
196                        if lentoks != new_lentoks:
[272b107]197                            if is_data == True:
198                                break
199                            else:
[d508be9]200                                i = -1
201                                i1 = 0
202                                j = -1
203                                j1 = -1
204                           
[a7a5886]205                        #Delete the previously stored lines of data candidates
206                        # if is not data.
207                        if i < 0 and -1 < i1 < mum_data_lines and \
208                            is_data == False:
[892f246]209                            try:
[a7a5886]210                                x = numpy.zeros(0)
211                                y = numpy.zeros(0)
[892f246]212                            except:
213                                pass
214                           
[ef3445e2]215                        x = numpy.append(x, _x)
[7d6351e]216                        y = numpy.append(y, _y)
[892f246]217                       
[de1da34]218                        if has_error_dy == True:
[a7a5886]219                            #Delete the previously stored lines of
220                            # data candidates if is not data.
221                            if i < 0 and -1 < i1 < mum_data_lines and \
222                                is_data == False:
[892f246]223                                try:
[7d6351e]224                                    dy = numpy.zeros(0)
[892f246]225                                except:
[7d6351e]226                                    pass
[8bd8ea4]227                            dy = numpy.append(dy, _dy)
[892f246]228                           
[de1da34]229                        if has_error_dx == True:
[a7a5886]230                            #Delete the previously stored lines of
231                            # data candidates if is not data.
232                            if i < 0 and -1 < i1 < mum_data_lines and \
233                                is_data == False:
[892f246]234                                try:
[7d6351e]235                                    dx = numpy.zeros(0)
[892f246]236                                except:
[7d6351e]237                                    pass
[de1da34]238                            dx = numpy.append(dx, _dx)
239                           
240                        #Same for temp.
[a7a5886]241                        #Delete the previously stored lines of data candidates
242                        # if is not data.
243                        if i < 0 and -1 < i1 < mum_data_lines and\
244                            is_data == False:
[892f246]245                            try:
246                                tx = numpy.zeros(0)
247                                ty = numpy.zeros(0)
248                            except:
[7d6351e]249                                pass
[892f246]250
[7d6351e]251                        tx = numpy.append(tx, _x)
252                        ty = numpy.append(ty, _y)
[892f246]253                       
[de1da34]254                        if has_error_dy == True:
[a7a5886]255                            #Delete the previously stored lines of
256                            # data candidates if is not data.
257                            if i < 0 and -1 < i1 < mum_data_lines and \
258                                is_data == False:
[892f246]259                                try:
260                                    tdy = numpy.zeros(0)
261                                except:
[7d6351e]262                                    pass
[de1da34]263                            tdy = numpy.append(tdy, _dy)
264                        if has_error_dx == True:
[a7a5886]265                            #Delete the previously stored lines of
266                            # data candidates if is not data.
267                            if i < 0 and -1 < i1 < mum_data_lines and \
268                                is_data == False:
[892f246]269                                try:
270                                    tdx = numpy.zeros(0)
271                                except:
[7d6351e]272                                    pass
[de1da34]273                            tdx = numpy.append(tdx, _dx)
[d508be9]274
275                        #reset i1 and flag lentoks for the next
[a7a5886]276                        if lentoks < new_lentoks:
[d508be9]277                            if is_data == False:
[7d6351e]278                                i1 = -1
[a7a5886]279                        #To remember the # of columns on the current line
280                        # for the next line of data
[0e5e586]281                        lentoks = len(toks)
[8bd8ea4]282                       
[7d6351e]283                        #Reset # of header lines and counts #
284                        # of data candidate lines
[a7a5886]285                        if j == 0 and j1 == 0:
[7d6351e]286                            i1 = i + 1
[a7a5886]287                        i += 1
[8bd8ea4]288                    except:
[892f246]289                        # It is data and meet non - number, then stop reading
290                        if is_data == True:
[7d6351e]291                            break
[d508be9]292                        lentoks = 2
[7d6351e]293                        #Counting # of header lines
[a7a5886]294                        j += 1
295                        if j == j1 + 1:
[7d6351e]296                            j1 = j
297                        else:
[892f246]298                            j = -1
299                        #Reset # of lines of data candidates
300                        i = -1
301                       
[7d6351e]302                        # Couldn't parse this line, skip it
[8bd8ea4]303                        pass
[892f246]304                   
[7d6351e]305                input_f.close()
[8bd8ea4]306                # Sanity check
[de1da34]307                if has_error_dy == True and not len(y) == len(dy):
[a7a5886]308                    msg = "ascii_reader: y and dy have different length"
309                    raise RuntimeError, msg
[de1da34]310                if has_error_dx == True and not len(x) == len(dx):
[a7a5886]311                    msg = "ascii_reader: y and dy have different length"
312                    raise RuntimeError, msg
[8bd8ea4]313                # If the data length is zero, consider this as
314                # though we were not able to read the file.
[a7a5886]315                if len(x) == 0:
[daa56d0]316                    raise RuntimeError, "ascii_reader: could not load file"
[de1da34]317               
[a7a5886]318                #Let's re-order the data to make cal.
319                # curve look better some cases
320                ind = numpy.lexsort((ty, tx))
[de1da34]321                for i in ind:
322                    x[i] = tx[ind[i]]
323                    y[i] = ty[ind[i]]
324                    if has_error_dy == True:
325                        dy[i] = tdy[ind[i]]
326                    if has_error_dx == True:
327                        dx[i] = tdx[ind[i]]
[7d6351e]328                # Zeros in dx, dy
[da96629]329                if has_error_dx:
[7d6351e]330                    dx[dx == 0] = _ZERO
[da96629]331                if has_error_dy:
[7d6351e]332                    dy[dy == 0] = _ZERO
333                #Data
334                output.x = x[x != 0]
335                output.y = y[x != 0]
336                output.dy = dy[x != 0] if has_error_dy == True\
337                    else numpy.zeros(len(output.y))
338                output.dx = dx[x != 0] if has_error_dx == True\
339                    else numpy.zeros(len(output.x))
[fca90f82]340                               
[99d1af6]341                if data_conv_q is not None:
342                    output.xaxis("\\rm{Q}", output.x_unit)
343                else:
344                    output.xaxis("\\rm{Q}", 'A^{-1}')
345                if data_conv_i is not None:
[0e2aa40]346                    output.yaxis("\\rm{Intensity}", output.y_unit)
[99d1af6]347                else:
[7d6351e]348                    output.yaxis("\\rm{Intensity}", "cm^{-1}")
[fe78c7b]349                   
350                # Store loading process information
[7d6351e]351                output.meta_data['loader'] = self.type_name
[83b81b8]352                if len(output.x) < 1:
353                    raise RuntimeError, "%s is empty" % path
[8bd8ea4]354                return output
[892f246]355           
[8bd8ea4]356        else:
357            raise RuntimeError, "%s is not a file" % path
358        return None
Note: See TracBrowser for help on using the repository browser.