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