[bee885e] | 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 | |
---|
| 15 | # Check whether we have a converter available |
---|
| 16 | has_converter = True |
---|
| 17 | try: |
---|
| 18 | from data_util.nxsunit import Converter |
---|
| 19 | except: |
---|
| 20 | has_converter = False |
---|
| 21 | |
---|
| 22 | class Reader(object): |
---|
| 23 | """ |
---|
| 24 | Class to load HFIR 1D 4-column files |
---|
| 25 | """ |
---|
| 26 | ## File type |
---|
| 27 | type = ["HFIR 1D files (*.d1d)|*.d1d"] |
---|
| 28 | ## List of allowed extensions |
---|
| 29 | ext=['.d1d'] |
---|
| 30 | |
---|
| 31 | def read(self, path): |
---|
| 32 | """ |
---|
| 33 | Load data file |
---|
| 34 | |
---|
| 35 | @param path: file path |
---|
| 36 | @return: Data1D object, or None |
---|
| 37 | @raise RuntimeError: when the file can't be opened |
---|
| 38 | @raise ValueError: when the length of the data vectors are inconsistent |
---|
| 39 | """ |
---|
| 40 | if os.path.isfile(path): |
---|
| 41 | basename = os.path.basename(path) |
---|
| 42 | root, extension = os.path.splitext(basename) |
---|
| 43 | if extension.lower() in self.ext: |
---|
| 44 | try: |
---|
| 45 | input_f = open(path,'r') |
---|
| 46 | except : |
---|
| 47 | raise RuntimeError, "hfir1d_reader: cannot open %s" % path |
---|
| 48 | buff = input_f.read() |
---|
| 49 | lines = buff.split('\n') |
---|
| 50 | x = numpy.zeros(0) |
---|
| 51 | y = numpy.zeros(0) |
---|
| 52 | dx = numpy.zeros(0) |
---|
| 53 | dy = numpy.zeros(0) |
---|
| 54 | output = Data1D(x, y, dx=dx, dy=dy) |
---|
| 55 | self.filename = output.filename = basename |
---|
| 56 | |
---|
| 57 | data_conv_q = None |
---|
| 58 | data_conv_i = None |
---|
| 59 | |
---|
[c5f0337] | 60 | if has_converter == True and output.x_unit != 'A^{-1}': |
---|
| 61 | data_conv_q = Converter('A^{-1}') |
---|
[bee885e] | 62 | # Test it |
---|
| 63 | data_conv_q(1.0, output.x_unit) |
---|
| 64 | |
---|
[c5f0337] | 65 | if has_converter == True and output.y_unit != 'cm^{-1}': |
---|
| 66 | data_conv_i = Converter('cm^{-1}') |
---|
[bee885e] | 67 | # Test it |
---|
| 68 | data_conv_i(1.0, output.y_unit) |
---|
| 69 | |
---|
| 70 | for line in lines: |
---|
| 71 | toks = line.split() |
---|
| 72 | try: |
---|
| 73 | _x = float(toks[0]) |
---|
| 74 | _y = float(toks[1]) |
---|
| 75 | _dx = float(toks[3]) |
---|
| 76 | _dy = float(toks[2]) |
---|
| 77 | |
---|
| 78 | if data_conv_q is not None: |
---|
| 79 | _x = data_conv_q(_x, units=output.x_unit) |
---|
| 80 | _dx = data_conv_q(_dx, units=output.x_unit) |
---|
| 81 | |
---|
| 82 | if data_conv_i is not None: |
---|
| 83 | _y = data_conv_i(_y, units=output.y_unit) |
---|
| 84 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | x = numpy.append(x, _x) |
---|
| 88 | y = numpy.append(y, _y) |
---|
| 89 | dx = numpy.append(dx, _dx) |
---|
| 90 | dy = numpy.append(dy, _dy) |
---|
| 91 | except: |
---|
| 92 | # Couldn't parse this line, skip it |
---|
| 93 | pass |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | # Sanity check |
---|
| 97 | if not len(y) == len(dy): |
---|
| 98 | raise RuntimeError, "hfir1d_reader: y and dy have different length" |
---|
| 99 | if not len(x) == len(dx): |
---|
| 100 | raise RuntimeError, "hfir1d_reader: x and dx have different length" |
---|
| 101 | |
---|
| 102 | # If the data length is zero, consider this as |
---|
| 103 | # though we were not able to read the file. |
---|
| 104 | if len(x)==0: |
---|
| 105 | raise RuntimeError, "hfir1d_reader: could not load file" |
---|
| 106 | |
---|
| 107 | output.x = x |
---|
| 108 | output.y = y |
---|
| 109 | output.dy = dy |
---|
| 110 | output.dx = dx |
---|
| 111 | if data_conv_q is not None: |
---|
| 112 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
| 113 | else: |
---|
| 114 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 115 | if data_conv_i is not None: |
---|
| 116 | output.yaxis("\\{I(Q)}", output.y_unit) |
---|
| 117 | else: |
---|
| 118 | output.yaxis("\\rm{I(Q)}","cm^{-1}") |
---|
| 119 | |
---|
| 120 | return output |
---|
| 121 | else: |
---|
| 122 | raise RuntimeError, "%s is not a file" % path |
---|
| 123 | return None |
---|
| 124 | |
---|
| 125 | if __name__ == "__main__": |
---|
| 126 | reader = Reader() |
---|
| 127 | print reader.read("../test/S2-30dq.d1d") |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|