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