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