[bee885e] | 1 | |
---|
[0997158f] | 2 | ##################################################################### |
---|
| 3 | #This software was developed by the University of Tennessee as part of the |
---|
| 4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 5 | #project funded by the US National Science Foundation. |
---|
| 6 | #See the license text in license.txt |
---|
| 7 | #copyright 2008, University of Tennessee |
---|
| 8 | ###################################################################### |
---|
[bee885e] | 9 | |
---|
| 10 | import numpy |
---|
| 11 | import os |
---|
[ad8034f] | 12 | from sans.dataloader.data_info import Data1D |
---|
[bee885e] | 13 | |
---|
| 14 | # Check whether we have a converter available |
---|
| 15 | has_converter = True |
---|
| 16 | try: |
---|
| 17 | from data_util.nxsunit import Converter |
---|
| 18 | except: |
---|
| 19 | has_converter = False |
---|
| 20 | |
---|
| 21 | class Reader(object): |
---|
| 22 | """ |
---|
[0997158f] | 23 | Class to load HFIR 1D 4-column files |
---|
[bee885e] | 24 | """ |
---|
| 25 | ## File type |
---|
[28caa03] | 26 | type_name = "HFIR 1D" |
---|
| 27 | ## Wildcards |
---|
[bee885e] | 28 | type = ["HFIR 1D files (*.d1d)|*.d1d"] |
---|
| 29 | ## List of allowed extensions |
---|
[a7a5886] | 30 | ext = ['.d1d'] |
---|
[bee885e] | 31 | |
---|
| 32 | def read(self, path): |
---|
| 33 | """ |
---|
[0997158f] | 34 | Load data file |
---|
| 35 | |
---|
| 36 | :param path: file path |
---|
| 37 | |
---|
| 38 | :return: Data1D object, or None |
---|
| 39 | |
---|
| 40 | :raise RuntimeError: when the file can't be opened |
---|
| 41 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
[bee885e] | 42 | """ |
---|
| 43 | if os.path.isfile(path): |
---|
| 44 | basename = os.path.basename(path) |
---|
| 45 | root, extension = os.path.splitext(basename) |
---|
| 46 | if extension.lower() in self.ext: |
---|
| 47 | try: |
---|
| 48 | input_f = open(path,'r') |
---|
| 49 | except : |
---|
| 50 | raise RuntimeError, "hfir1d_reader: cannot open %s" % path |
---|
| 51 | buff = input_f.read() |
---|
| 52 | lines = buff.split('\n') |
---|
| 53 | x = numpy.zeros(0) |
---|
| 54 | y = numpy.zeros(0) |
---|
| 55 | dx = numpy.zeros(0) |
---|
| 56 | dy = numpy.zeros(0) |
---|
| 57 | output = Data1D(x, y, dx=dx, dy=dy) |
---|
| 58 | self.filename = output.filename = basename |
---|
| 59 | |
---|
| 60 | data_conv_q = None |
---|
| 61 | data_conv_i = None |
---|
| 62 | |
---|
[ca10d8e] | 63 | if has_converter == True and output.x_unit != '1/A': |
---|
| 64 | data_conv_q = Converter('1/A') |
---|
[bee885e] | 65 | # Test it |
---|
| 66 | data_conv_q(1.0, output.x_unit) |
---|
| 67 | |
---|
[ca10d8e] | 68 | if has_converter == True and output.y_unit != '1/cm': |
---|
| 69 | data_conv_i = Converter('1/cm') |
---|
[bee885e] | 70 | # Test it |
---|
| 71 | data_conv_i(1.0, output.y_unit) |
---|
| 72 | |
---|
| 73 | for line in lines: |
---|
| 74 | toks = line.split() |
---|
| 75 | try: |
---|
| 76 | _x = float(toks[0]) |
---|
| 77 | _y = float(toks[1]) |
---|
| 78 | _dx = float(toks[3]) |
---|
| 79 | _dy = float(toks[2]) |
---|
| 80 | |
---|
| 81 | if data_conv_q is not None: |
---|
| 82 | _x = data_conv_q(_x, units=output.x_unit) |
---|
| 83 | _dx = data_conv_q(_dx, units=output.x_unit) |
---|
| 84 | |
---|
| 85 | if data_conv_i is not None: |
---|
| 86 | _y = data_conv_i(_y, units=output.y_unit) |
---|
| 87 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
| 88 | |
---|
| 89 | x = numpy.append(x, _x) |
---|
| 90 | y = numpy.append(y, _y) |
---|
| 91 | dx = numpy.append(dx, _dx) |
---|
| 92 | dy = numpy.append(dy, _dy) |
---|
| 93 | except: |
---|
| 94 | # Couldn't parse this line, skip it |
---|
| 95 | pass |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | # Sanity check |
---|
| 99 | if not len(y) == len(dy): |
---|
[a7a5886] | 100 | msg = "hfir1d_reader: y and dy have different length" |
---|
| 101 | raise RuntimeError, msg |
---|
[bee885e] | 102 | if not len(x) == len(dx): |
---|
[a7a5886] | 103 | msg = "hfir1d_reader: x and dx have different length" |
---|
| 104 | raise RuntimeError, msg |
---|
[bee885e] | 105 | |
---|
| 106 | # If the data length is zero, consider this as |
---|
| 107 | # though we were not able to read the file. |
---|
[a7a5886] | 108 | if len(x) == 0: |
---|
[bee885e] | 109 | raise RuntimeError, "hfir1d_reader: could not load file" |
---|
| 110 | |
---|
| 111 | output.x = x |
---|
| 112 | output.y = y |
---|
| 113 | output.dy = dy |
---|
| 114 | output.dx = dx |
---|
| 115 | if data_conv_q is not None: |
---|
| 116 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
| 117 | else: |
---|
| 118 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 119 | if data_conv_i is not None: |
---|
[0e2aa40] | 120 | output.yaxis("\\rm{Intensity}", output.y_unit) |
---|
[bee885e] | 121 | else: |
---|
[0e2aa40] | 122 | output.yaxis("\\rm{Intensity}","cm^{-1}") |
---|
[bee885e] | 123 | |
---|
[fe78c7b] | 124 | # Store loading process information |
---|
| 125 | output.meta_data['loader'] = self.type_name |
---|
[bee885e] | 126 | return output |
---|
| 127 | else: |
---|
| 128 | raise RuntimeError, "%s is not a file" % path |
---|
| 129 | return None |
---|
| 130 | |
---|
| 131 | if __name__ == "__main__": |
---|
| 132 | reader = Reader() |
---|
| 133 | print reader.read("../test/S2-30dq.d1d") |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|