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