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