[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 |
---|
[28caa03] | 27 | type_name = "HFIR 1D" |
---|
| 28 | ## Wildcards |
---|
[bee885e] | 29 | type = ["HFIR 1D files (*.d1d)|*.d1d"] |
---|
| 30 | ## List of allowed extensions |
---|
| 31 | ext=['.d1d'] |
---|
| 32 | |
---|
| 33 | def read(self, path): |
---|
| 34 | """ |
---|
| 35 | Load data file |
---|
| 36 | |
---|
| 37 | @param path: file path |
---|
| 38 | @return: Data1D object, or None |
---|
| 39 | @raise RuntimeError: when the file can't be opened |
---|
| 40 | @raise ValueError: when the length of the data vectors are inconsistent |
---|
| 41 | """ |
---|
| 42 | if os.path.isfile(path): |
---|
| 43 | basename = os.path.basename(path) |
---|
| 44 | root, extension = os.path.splitext(basename) |
---|
| 45 | if extension.lower() in self.ext: |
---|
| 46 | try: |
---|
| 47 | input_f = open(path,'r') |
---|
| 48 | except : |
---|
| 49 | raise RuntimeError, "hfir1d_reader: cannot open %s" % path |
---|
| 50 | buff = input_f.read() |
---|
| 51 | lines = buff.split('\n') |
---|
| 52 | x = numpy.zeros(0) |
---|
| 53 | y = numpy.zeros(0) |
---|
| 54 | dx = numpy.zeros(0) |
---|
| 55 | dy = numpy.zeros(0) |
---|
| 56 | output = Data1D(x, y, dx=dx, dy=dy) |
---|
| 57 | self.filename = output.filename = basename |
---|
| 58 | |
---|
| 59 | data_conv_q = None |
---|
| 60 | data_conv_i = None |
---|
| 61 | |
---|
[ca10d8e] | 62 | if has_converter == True and output.x_unit != '1/A': |
---|
| 63 | data_conv_q = Converter('1/A') |
---|
[bee885e] | 64 | # Test it |
---|
| 65 | data_conv_q(1.0, output.x_unit) |
---|
| 66 | |
---|
[ca10d8e] | 67 | if has_converter == True and output.y_unit != '1/cm': |
---|
| 68 | data_conv_i = Converter('1/cm') |
---|
[bee885e] | 69 | # Test it |
---|
| 70 | data_conv_i(1.0, output.y_unit) |
---|
| 71 | |
---|
| 72 | for line in lines: |
---|
| 73 | toks = line.split() |
---|
| 74 | try: |
---|
| 75 | _x = float(toks[0]) |
---|
| 76 | _y = float(toks[1]) |
---|
| 77 | _dx = float(toks[3]) |
---|
| 78 | _dy = float(toks[2]) |
---|
| 79 | |
---|
| 80 | if data_conv_q is not None: |
---|
| 81 | _x = data_conv_q(_x, units=output.x_unit) |
---|
| 82 | _dx = data_conv_q(_dx, units=output.x_unit) |
---|
| 83 | |
---|
| 84 | if data_conv_i is not None: |
---|
| 85 | _y = data_conv_i(_y, units=output.y_unit) |
---|
| 86 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
| 87 | |
---|
| 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): |
---|
| 100 | raise RuntimeError, "hfir1d_reader: y and dy have different length" |
---|
| 101 | if not len(x) == len(dx): |
---|
| 102 | raise RuntimeError, "hfir1d_reader: x and dx have different length" |
---|
| 103 | |
---|
| 104 | # If the data length is zero, consider this as |
---|
| 105 | # though we were not able to read the file. |
---|
| 106 | if len(x)==0: |
---|
| 107 | raise RuntimeError, "hfir1d_reader: could not load file" |
---|
| 108 | |
---|
| 109 | output.x = x |
---|
| 110 | output.y = y |
---|
| 111 | output.dy = dy |
---|
| 112 | output.dx = dx |
---|
| 113 | if data_conv_q is not None: |
---|
| 114 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
| 115 | else: |
---|
| 116 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 117 | if data_conv_i is not None: |
---|
[0e2aa40] | 118 | output.yaxis("\\rm{Intensity}", output.y_unit) |
---|
[bee885e] | 119 | else: |
---|
[0e2aa40] | 120 | output.yaxis("\\rm{Intensity}","cm^{-1}") |
---|
[bee885e] | 121 | |
---|
[fe78c7b] | 122 | # Store loading process information |
---|
| 123 | output.meta_data['loader'] = self.type_name |
---|
[bee885e] | 124 | return output |
---|
| 125 | else: |
---|
| 126 | raise RuntimeError, "%s is not a file" % path |
---|
| 127 | return None |
---|
| 128 | |
---|
| 129 | if __name__ == "__main__": |
---|
| 130 | reader = Reader() |
---|
| 131 | print reader.read("../test/S2-30dq.d1d") |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|