[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 |
---|
[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 | |
---|
| 56 | # some ascii data has \r line separator, try it when it has only one line |
---|
| 57 | if len(lines) < 2 : |
---|
| 58 | lines = buff.split('\r') |
---|
| 59 | |
---|
[8bd8ea4] | 60 | x = numpy.zeros(0) |
---|
| 61 | y = numpy.zeros(0) |
---|
| 62 | dy = numpy.zeros(0) |
---|
[de1da34] | 63 | dx = numpy.zeros(0) |
---|
| 64 | |
---|
| 65 | #temp. space to sort data |
---|
| 66 | tx = numpy.zeros(0) |
---|
| 67 | ty = numpy.zeros(0) |
---|
| 68 | tdy = numpy.zeros(0) |
---|
| 69 | tdx = numpy.zeros(0) |
---|
| 70 | |
---|
| 71 | output = Data1D(x, y, dy=dy, dx=dx) |
---|
[8bd8ea4] | 72 | self.filename = output.filename = basename |
---|
[99d1af6] | 73 | |
---|
| 74 | data_conv_q = None |
---|
| 75 | data_conv_i = None |
---|
| 76 | |
---|
[ca10d8e] | 77 | if has_converter == True and output.x_unit != '1/A': |
---|
| 78 | data_conv_q = Converter('1/A') |
---|
[99d1af6] | 79 | # Test it |
---|
| 80 | data_conv_q(1.0, output.x_unit) |
---|
| 81 | |
---|
[ca10d8e] | 82 | if has_converter == True and output.y_unit != '1/cm': |
---|
| 83 | data_conv_i = Converter('1/cm') |
---|
[99d1af6] | 84 | # Test it |
---|
| 85 | data_conv_i(1.0, output.y_unit) |
---|
| 86 | |
---|
[8bd8ea4] | 87 | |
---|
| 88 | # The first good line of data will define whether |
---|
| 89 | # we have 2-column or 3-column ascii |
---|
[de1da34] | 90 | has_error_dx = None |
---|
| 91 | has_error_dy = None |
---|
[8bd8ea4] | 92 | |
---|
| 93 | for line in lines: |
---|
| 94 | toks = line.split() |
---|
| 95 | try: |
---|
| 96 | _x = float(toks[0]) |
---|
| 97 | _y = float(toks[1]) |
---|
| 98 | |
---|
[99d1af6] | 99 | if data_conv_q is not None: |
---|
| 100 | _x = data_conv_q(_x, units=output.x_unit) |
---|
| 101 | |
---|
| 102 | if data_conv_i is not None: |
---|
| 103 | _y = data_conv_i(_y, units=output.y_unit) |
---|
| 104 | |
---|
[8bd8ea4] | 105 | # If we have an extra token, check |
---|
| 106 | # whether it can be interpreted as a |
---|
| 107 | # third column. |
---|
| 108 | _dy = None |
---|
| 109 | if len(toks)>2: |
---|
| 110 | try: |
---|
| 111 | _dy = float(toks[2]) |
---|
[99d1af6] | 112 | |
---|
| 113 | if data_conv_i is not None: |
---|
| 114 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
| 115 | |
---|
[8bd8ea4] | 116 | except: |
---|
| 117 | # The third column is not a float, skip it. |
---|
| 118 | pass |
---|
| 119 | |
---|
| 120 | # If we haven't set the 3rd column |
---|
| 121 | # flag, set it now. |
---|
[de1da34] | 122 | if has_error_dy == None: |
---|
| 123 | has_error_dy = False if _dy == None else True |
---|
| 124 | |
---|
| 125 | #Check for dx |
---|
| 126 | _dx = None |
---|
| 127 | if len(toks)>3: |
---|
| 128 | try: |
---|
| 129 | _dx = float(toks[3]) |
---|
| 130 | |
---|
| 131 | if data_conv_i is not None: |
---|
| 132 | _dx = data_conv_i(_dx, units=output.x_unit) |
---|
| 133 | |
---|
| 134 | except: |
---|
| 135 | # The 4th column is not a float, skip it. |
---|
| 136 | pass |
---|
| 137 | |
---|
| 138 | # If we haven't set the 3rd column |
---|
| 139 | # flag, set it now. |
---|
| 140 | if has_error_dx == None: |
---|
| 141 | has_error_dx = False if _dx == None else True |
---|
[8bd8ea4] | 142 | |
---|
| 143 | x = numpy.append(x, _x) |
---|
| 144 | y = numpy.append(y, _y) |
---|
[de1da34] | 145 | if has_error_dy == True: |
---|
[8bd8ea4] | 146 | dy = numpy.append(dy, _dy) |
---|
[de1da34] | 147 | if has_error_dx == True: |
---|
| 148 | dx = numpy.append(dx, _dx) |
---|
| 149 | |
---|
| 150 | #Same for temp. |
---|
| 151 | tx = numpy.append(tx, _x) |
---|
| 152 | ty = numpy.append(ty, _y) |
---|
| 153 | if has_error_dy == True: |
---|
| 154 | tdy = numpy.append(tdy, _dy) |
---|
| 155 | if has_error_dx == True: |
---|
| 156 | tdx = numpy.append(tdx, _dx) |
---|
[8bd8ea4] | 157 | |
---|
| 158 | except: |
---|
| 159 | # Couldn't parse this line, skip it |
---|
| 160 | pass |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | # Sanity check |
---|
[de1da34] | 164 | if has_error_dy == True and not len(y) == len(dy): |
---|
| 165 | raise RuntimeError, "ascii_reader: y and dy have different length" |
---|
| 166 | if has_error_dx == True and not len(x) == len(dx): |
---|
[daa56d0] | 167 | raise RuntimeError, "ascii_reader: y and dy have different length" |
---|
[8bd8ea4] | 168 | |
---|
| 169 | # If the data length is zero, consider this as |
---|
| 170 | # though we were not able to read the file. |
---|
| 171 | if len(x)==0: |
---|
[daa56d0] | 172 | raise RuntimeError, "ascii_reader: could not load file" |
---|
[de1da34] | 173 | |
---|
[470bf7e] | 174 | #Let's re-order the data to make cal. curve look better some cases |
---|
[de1da34] | 175 | ind = numpy.lexsort((ty,tx)) |
---|
| 176 | for i in ind: |
---|
| 177 | x[i] = tx[ind[i]] |
---|
| 178 | y[i] = ty[ind[i]] |
---|
| 179 | if has_error_dy == True: |
---|
| 180 | dy[i] = tdy[ind[i]] |
---|
| 181 | if has_error_dx == True: |
---|
| 182 | dx[i] = tdx[ind[i]] |
---|
| 183 | |
---|
[8bd8ea4] | 184 | output.x = x |
---|
| 185 | output.y = y |
---|
[de1da34] | 186 | output.dy = dy if has_error_dy == True else None |
---|
| 187 | output.dx = dx if has_error_dx == True else None |
---|
| 188 | |
---|
[99d1af6] | 189 | if data_conv_q is not None: |
---|
| 190 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
| 191 | else: |
---|
| 192 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 193 | if data_conv_i is not None: |
---|
[0e2aa40] | 194 | output.yaxis("\\rm{Intensity}", output.y_unit) |
---|
[99d1af6] | 195 | else: |
---|
[0e2aa40] | 196 | output.yaxis("\\rm{Intensity}","cm^{-1}") |
---|
[470bf7e] | 197 | |
---|
[8bd8ea4] | 198 | return output |
---|
| 199 | else: |
---|
| 200 | raise RuntimeError, "%s is not a file" % path |
---|
| 201 | return None |
---|
| 202 | |
---|
| 203 | if __name__ == "__main__": |
---|
| 204 | reader = Reader() |
---|
| 205 | #print reader.read("../test/test_3_columns.txt") |
---|
| 206 | print reader.read("../test/empty.txt") |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|