[d8a5f12] | 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 |
---|
[99d1af6] | 13 | from DataLoader.data_info import Data1D, Detector |
---|
[d8a5f12] | 14 | |
---|
[99d1af6] | 15 | has_converter = True |
---|
| 16 | try: |
---|
| 17 | from data_util.nxsunit import Converter |
---|
| 18 | except: |
---|
| 19 | has_converter = False |
---|
| 20 | |
---|
[d8a5f12] | 21 | class Reader: |
---|
| 22 | """ |
---|
| 23 | Class to load IGOR reduced .ABS files |
---|
| 24 | """ |
---|
[8780e9a] | 25 | ## File type |
---|
| 26 | type = ["IGOR 1D files (*.abs)|*.abs"] |
---|
[d8a5f12] | 27 | ## List of allowed extensions |
---|
[8bd8ea4] | 28 | ext=['.abs', '.ABS'] |
---|
[d8a5f12] | 29 | |
---|
| 30 | def read(self, path): |
---|
| 31 | """ |
---|
| 32 | Load data file |
---|
| 33 | |
---|
| 34 | @param path: file path |
---|
| 35 | @return: Data1D object, or None |
---|
| 36 | @raise RuntimeError: when the file can't be opened |
---|
| 37 | @raise ValueError: when the length of the data vectors are inconsistent |
---|
| 38 | """ |
---|
| 39 | if os.path.isfile(path): |
---|
| 40 | basename = os.path.basename(path) |
---|
| 41 | root, extension = os.path.splitext(basename) |
---|
| 42 | if extension.lower() in self.ext: |
---|
| 43 | try: |
---|
| 44 | input_f = open(path,'r') |
---|
| 45 | except : |
---|
| 46 | raise RuntimeError, "abs_reader: cannot open %s" % path |
---|
| 47 | buff = input_f.read() |
---|
| 48 | lines = buff.split('\n') |
---|
| 49 | x = numpy.zeros(0) |
---|
| 50 | y = numpy.zeros(0) |
---|
| 51 | dy = numpy.zeros(0) |
---|
| 52 | output = Data1D(x, y, dy=dy) |
---|
[99d1af6] | 53 | detector = Detector() |
---|
| 54 | output.detector.append(detector) |
---|
[b99ac227] | 55 | output.filename = basename |
---|
[d8a5f12] | 56 | |
---|
| 57 | is_info = False |
---|
| 58 | is_center = False |
---|
| 59 | is_data_started = False |
---|
| 60 | |
---|
[99d1af6] | 61 | data_conv_q = None |
---|
| 62 | data_conv_i = None |
---|
| 63 | |
---|
| 64 | if has_converter == True and output.x_unit != '1/A': |
---|
| 65 | data_conv_q = Converter('1/A') |
---|
| 66 | # Test it |
---|
| 67 | data_conv_q(1.0, output.x_unit) |
---|
| 68 | |
---|
| 69 | if has_converter == True and output.y_unit != '1/cm': |
---|
| 70 | data_conv_i = Converter('1/cm') |
---|
| 71 | # Test it |
---|
| 72 | data_conv_i(1.0, output.y_unit) |
---|
| 73 | |
---|
[d8a5f12] | 74 | for line in lines: |
---|
| 75 | |
---|
| 76 | # Information line 1 |
---|
| 77 | if is_info==True: |
---|
| 78 | is_info = False |
---|
| 79 | line_toks = line.split() |
---|
| 80 | |
---|
| 81 | # Wavelength in Angstrom |
---|
| 82 | try: |
---|
[99d1af6] | 83 | value = float(line_toks[1]) |
---|
| 84 | if has_converter==True and output.source.wavelength_unit != 'A': |
---|
| 85 | conv = Converter('A') |
---|
| 86 | output.source.wavelength = conv(value, units=output.source.wavelength_unit) |
---|
| 87 | else: |
---|
| 88 | output.source.wavelength = value |
---|
[d8a5f12] | 89 | except: |
---|
| 90 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
| 91 | |
---|
| 92 | # Distance in meters |
---|
| 93 | try: |
---|
[99d1af6] | 94 | value = float(line_toks[3]) |
---|
| 95 | if has_converter==True and detector.distance_unit != 'm': |
---|
| 96 | conv = Converter('m') |
---|
| 97 | detector.distance = conv(value, units=detector.distance_unit) |
---|
| 98 | else: |
---|
| 99 | detector.distance = value |
---|
[d8a5f12] | 100 | except: |
---|
| 101 | raise ValueError,"IgorReader: can't read this file, missing distance" |
---|
| 102 | |
---|
[99d1af6] | 103 | # Transmission |
---|
[d8a5f12] | 104 | try: |
---|
| 105 | output.sample.transmission = float(line_toks[4]) |
---|
| 106 | except: |
---|
| 107 | # Transmission is not a mandatory entry |
---|
| 108 | pass |
---|
| 109 | |
---|
[99d1af6] | 110 | # Thickness in mm |
---|
[d8a5f12] | 111 | try: |
---|
[99d1af6] | 112 | value = float(line_toks[5]) |
---|
[b99ac227] | 113 | if has_converter==True and output.sample.thickness_unit != 'cm': |
---|
| 114 | conv = Converter('cm') |
---|
[99d1af6] | 115 | output.sample.thickness = conv(value, units=output.sample.thickness_unit) |
---|
| 116 | else: |
---|
| 117 | output.sample.thickness = value |
---|
[d8a5f12] | 118 | except: |
---|
| 119 | # Thickness is not a mandatory entry |
---|
| 120 | pass |
---|
| 121 | |
---|
| 122 | #MON CNT LAMBDA DET ANG DET DIST TRANS THICK AVE STEP |
---|
| 123 | if line.count("LAMBDA")>0: |
---|
| 124 | is_info = True |
---|
| 125 | |
---|
| 126 | # Find center info line |
---|
| 127 | if is_center==True: |
---|
| 128 | is_center = False |
---|
| 129 | line_toks = line.split() |
---|
| 130 | # Center in bin number |
---|
| 131 | center_x = float(line_toks[0]) |
---|
| 132 | center_y = float(line_toks[1]) |
---|
[99d1af6] | 133 | |
---|
| 134 | # Bin size |
---|
| 135 | if has_converter==True and detector.pixel_size_unit != 'mm': |
---|
| 136 | conv = Converter('mm') |
---|
| 137 | detector.pixel_size.x = conv(5.0, units=detector.pixel_size_unit) |
---|
| 138 | detector.pixel_size.y = conv(5.0, units=detector.pixel_size_unit) |
---|
| 139 | else: |
---|
| 140 | detector.pixel_size.x = 5.0 |
---|
| 141 | detector.pixel_size.y = 5.0 |
---|
| 142 | |
---|
| 143 | # Store beam center in distance units |
---|
| 144 | # Det 640 x 640 mm |
---|
| 145 | if has_converter==True and detector.beam_center_unit != 'mm': |
---|
| 146 | conv = Converter('mm') |
---|
| 147 | detector.beam_center.x = conv(center_x*5.0, units=detector.beam_center_unit) |
---|
| 148 | detector.beam_center.y = conv(center_y*5.0, units=detector.beam_center_unit) |
---|
| 149 | else: |
---|
| 150 | detector.beam_center.x = center_x*5.0 |
---|
| 151 | detector.beam_center.y = center_y*5.0 |
---|
[d8a5f12] | 152 | |
---|
| 153 | # Detector type |
---|
| 154 | try: |
---|
[99d1af6] | 155 | detector.name = line_toks[7] |
---|
[d8a5f12] | 156 | except: |
---|
| 157 | # Detector name is not a mandatory entry |
---|
| 158 | pass |
---|
| 159 | |
---|
| 160 | #BCENT(X,Y) A1(mm) A2(mm) A1A2DIST(m) DL/L BSTOP(mm) DET_TYP |
---|
| 161 | if line.count("BCENT")>0: |
---|
| 162 | is_center = True |
---|
| 163 | |
---|
| 164 | # Parse the data |
---|
| 165 | if is_data_started==True: |
---|
| 166 | toks = line.split() |
---|
| 167 | try: |
---|
| 168 | _x = float(toks[0]) |
---|
| 169 | _y = float(toks[1]) |
---|
| 170 | _dy = float(toks[2]) |
---|
[99d1af6] | 171 | |
---|
| 172 | if data_conv_q is not None: |
---|
| 173 | _x = data_conv_q(_x, units=output.x_unit) |
---|
| 174 | |
---|
| 175 | if data_conv_i is not None: |
---|
| 176 | _y = data_conv_i(_y, units=output.y_unit) |
---|
| 177 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
[d8a5f12] | 178 | |
---|
| 179 | x = numpy.append(x, _x) |
---|
| 180 | y = numpy.append(y, _y) |
---|
| 181 | dy = numpy.append(dy, _dy) |
---|
| 182 | except: |
---|
| 183 | # Could not read this data line. If we are here |
---|
| 184 | # it is because we are in the data section. Just |
---|
| 185 | # skip it. |
---|
| 186 | pass |
---|
| 187 | |
---|
| 188 | #The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor| |
---|
| 189 | if line.count("The 6 columns")>0: |
---|
| 190 | is_data_started = True |
---|
| 191 | |
---|
| 192 | # Sanity check |
---|
| 193 | if not len(y) == len(dy): |
---|
| 194 | raise ValueError, "abs_reader: y and dy have different length" |
---|
[8bd8ea4] | 195 | |
---|
| 196 | # If the data length is zero, consider this as |
---|
| 197 | # though we were not able to read the file. |
---|
| 198 | if len(x)==0: |
---|
| 199 | raise ValueError, "ascii_reader: could not load file" |
---|
[d8a5f12] | 200 | |
---|
| 201 | output.x = x |
---|
| 202 | output.y = y |
---|
| 203 | output.dy = dy |
---|
[99d1af6] | 204 | if data_conv_q is not None: |
---|
| 205 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
| 206 | else: |
---|
| 207 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 208 | if data_conv_i is not None: |
---|
| 209 | output.yaxis("\\{I(Q)}", output.y_unit) |
---|
| 210 | else: |
---|
| 211 | output.yaxis("\\rm{I(Q)}","cm^{-1}") |
---|
[d8a5f12] | 212 | |
---|
| 213 | return output |
---|
| 214 | else: |
---|
| 215 | raise RuntimeError, "%s is not a file" % path |
---|
| 216 | return None |
---|
| 217 | |
---|
| 218 | if __name__ == "__main__": |
---|
| 219 | reader = Reader() |
---|
| 220 | print reader.read("../test/jan08002.ABS") |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|