1 | |
---|
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 | ###################################################################### |
---|
9 | |
---|
10 | import numpy |
---|
11 | import os |
---|
12 | from DataLoader.data_info import Data1D |
---|
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 | """ |
---|
23 | Class to load HFIR 1D 4-column files |
---|
24 | """ |
---|
25 | ## File type |
---|
26 | type_name = "HFIR 1D" |
---|
27 | ## Wildcards |
---|
28 | type = ["HFIR 1D files (*.d1d)|*.d1d"] |
---|
29 | ## List of allowed extensions |
---|
30 | ext=['.d1d'] |
---|
31 | |
---|
32 | def read(self, path): |
---|
33 | """ |
---|
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 |
---|
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 | |
---|
63 | if has_converter == True and output.x_unit != '1/A': |
---|
64 | data_conv_q = Converter('1/A') |
---|
65 | # Test it |
---|
66 | data_conv_q(1.0, output.x_unit) |
---|
67 | |
---|
68 | if has_converter == True and output.y_unit != '1/cm': |
---|
69 | data_conv_i = Converter('1/cm') |
---|
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 | |
---|
90 | x = numpy.append(x, _x) |
---|
91 | y = numpy.append(y, _y) |
---|
92 | dx = numpy.append(dx, _dx) |
---|
93 | dy = numpy.append(dy, _dy) |
---|
94 | except: |
---|
95 | # Couldn't parse this line, skip it |
---|
96 | pass |
---|
97 | |
---|
98 | |
---|
99 | # Sanity check |
---|
100 | if not len(y) == len(dy): |
---|
101 | raise RuntimeError, "hfir1d_reader: y and dy have different length" |
---|
102 | if not len(x) == len(dx): |
---|
103 | raise RuntimeError, "hfir1d_reader: x and dx have different length" |
---|
104 | |
---|
105 | # If the data length is zero, consider this as |
---|
106 | # though we were not able to read the file. |
---|
107 | if len(x)==0: |
---|
108 | raise RuntimeError, "hfir1d_reader: could not load file" |
---|
109 | |
---|
110 | output.x = x |
---|
111 | output.y = y |
---|
112 | output.dy = dy |
---|
113 | output.dx = dx |
---|
114 | if data_conv_q is not None: |
---|
115 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
116 | else: |
---|
117 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
118 | if data_conv_i is not None: |
---|
119 | output.yaxis("\\rm{Intensity}", output.y_unit) |
---|
120 | else: |
---|
121 | output.yaxis("\\rm{Intensity}","cm^{-1}") |
---|
122 | |
---|
123 | # Store loading process information |
---|
124 | output.meta_data['loader'] = self.type_name |
---|
125 | return output |
---|
126 | else: |
---|
127 | raise RuntimeError, "%s is not a file" % path |
---|
128 | return None |
---|
129 | |
---|
130 | if __name__ == "__main__": |
---|
131 | reader = Reader() |
---|
132 | print reader.read("../test/S2-30dq.d1d") |
---|
133 | |
---|
134 | |
---|
135 | |
---|