[b99ac227] | 1 | """ |
---|
| 2 | IGOR 2D reduced file reader |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | """ |
---|
| 6 | This software was developed by the University of Tennessee as part of the |
---|
| 7 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 8 | project funded by the US National Science Foundation. |
---|
| 9 | |
---|
| 10 | If you use DANSE applications to do scientific research that leads to |
---|
| 11 | publication, we ask that you acknowledge the use of the software with the |
---|
| 12 | following sentence: |
---|
| 13 | |
---|
| 14 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
| 15 | |
---|
| 16 | copyright 2008, University of Tennessee |
---|
| 17 | """ |
---|
| 18 | |
---|
[11a0319] | 19 | import os, sys |
---|
[b99ac227] | 20 | import numpy |
---|
| 21 | import math, logging |
---|
| 22 | from DataLoader.data_info import Data2D, Detector |
---|
[eadf1f9] | 23 | from DataLoader.manipulations import reader2D_converter |
---|
[b99ac227] | 24 | |
---|
| 25 | # Look for unit converter |
---|
| 26 | has_converter = True |
---|
| 27 | try: |
---|
| 28 | from data_util.nxsunit import Converter |
---|
| 29 | except: |
---|
| 30 | has_converter = False |
---|
| 31 | |
---|
[bb03739] | 32 | class Reader: |
---|
[11a0319] | 33 | """ Simple data reader for Igor data files """ |
---|
[b99ac227] | 34 | ## File type |
---|
[28caa03] | 35 | type_name = "IGOR 2D" |
---|
| 36 | ## Wildcards |
---|
[b99ac227] | 37 | type = ["IGOR 2D files (*.ASC)|*.ASC"] |
---|
| 38 | ## Extension |
---|
| 39 | ext=['.ASC', '.asc'] |
---|
| 40 | |
---|
[11a0319] | 41 | def read(self,filename=None): |
---|
| 42 | """ Read file """ |
---|
[b99ac227] | 43 | if not os.path.isfile(filename): |
---|
[11a0319] | 44 | raise ValueError, \ |
---|
[b99ac227] | 45 | "Specified file %s is not a regular file" % filename |
---|
[11a0319] | 46 | |
---|
| 47 | # Read file |
---|
[b99ac227] | 48 | f = open(filename,'r') |
---|
[11a0319] | 49 | buf = f.read() |
---|
| 50 | |
---|
[b99ac227] | 51 | # Instantiate data object |
---|
| 52 | output = Data2D() |
---|
| 53 | output.filename = os.path.basename(filename) |
---|
| 54 | detector = Detector() |
---|
| 55 | if len(output.detector)>0: print str(output.detector[0]) |
---|
| 56 | output.detector.append(detector) |
---|
[f55af70] | 57 | |
---|
[11a0319] | 58 | # Get content |
---|
| 59 | dataStarted = False |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | lines = buf.split('\n') |
---|
| 63 | itot = 0 |
---|
[b99ac227] | 64 | x = [] |
---|
| 65 | y = [] |
---|
[11a0319] | 66 | |
---|
| 67 | ncounts = 0 |
---|
| 68 | |
---|
| 69 | xmin = None |
---|
| 70 | xmax = None |
---|
| 71 | ymin = None |
---|
| 72 | ymax = None |
---|
| 73 | |
---|
| 74 | i_x = 0 |
---|
| 75 | i_y = -1 |
---|
[f55af70] | 76 | i_tot_row = 0 |
---|
[11a0319] | 77 | |
---|
| 78 | isInfo = False |
---|
| 79 | isCenter = False |
---|
[f55af70] | 80 | |
---|
| 81 | data_conv_q = None |
---|
| 82 | data_conv_i = None |
---|
| 83 | |
---|
[ca10d8e] | 84 | if has_converter == True and output.Q_unit != '1/A': |
---|
| 85 | data_conv_q = Converter('1/A') |
---|
[f55af70] | 86 | # Test it |
---|
| 87 | data_conv_q(1.0, output.Q_unit) |
---|
| 88 | |
---|
[ca10d8e] | 89 | if has_converter == True and output.I_unit != '1/cm': |
---|
| 90 | data_conv_i = Converter('1/cm') |
---|
[f55af70] | 91 | # Test it |
---|
| 92 | data_conv_i(1.0, output.I_unit) |
---|
| 93 | |
---|
[11a0319] | 94 | for line in lines: |
---|
| 95 | |
---|
| 96 | # Find setup info line |
---|
| 97 | if isInfo: |
---|
| 98 | isInfo = False |
---|
| 99 | line_toks = line.split() |
---|
| 100 | # Wavelength in Angstrom |
---|
| 101 | try: |
---|
| 102 | wavelength = float(line_toks[1]) |
---|
| 103 | except: |
---|
[c125e0c] | 104 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
[f55af70] | 105 | |
---|
| 106 | #Find # of bins in a row assuming the detector is square. |
---|
| 107 | if dataStarted == True: |
---|
| 108 | try: |
---|
| 109 | value = float(line) |
---|
| 110 | except: |
---|
| 111 | # Found a non-float entry, skip it |
---|
| 112 | continue |
---|
| 113 | |
---|
| 114 | # Get total bin number |
---|
| 115 | |
---|
| 116 | i_tot_row += 1 |
---|
| 117 | i_tot_row=math.ceil(math.sqrt(i_tot_row))-1 |
---|
| 118 | #print "i_tot", i_tot_row |
---|
| 119 | size_x = i_tot_row#192#128 |
---|
| 120 | size_y = i_tot_row#192#128 |
---|
| 121 | output.data = numpy.zeros([size_x,size_y]) |
---|
| 122 | output.err_data = numpy.zeros([size_x,size_y]) |
---|
| 123 | |
---|
| 124 | #Read Header and 2D data |
---|
| 125 | for line in lines: |
---|
| 126 | # Find setup info line |
---|
| 127 | if isInfo: |
---|
| 128 | isInfo = False |
---|
| 129 | line_toks = line.split() |
---|
| 130 | # Wavelength in Angstrom |
---|
| 131 | try: |
---|
| 132 | wavelength = float(line_toks[1]) |
---|
| 133 | except: |
---|
| 134 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
[11a0319] | 135 | # Distance in meters |
---|
| 136 | try: |
---|
| 137 | distance = float(line_toks[3]) |
---|
| 138 | except: |
---|
[c125e0c] | 139 | raise ValueError,"IgorReader: can't read this file, missing distance" |
---|
[11a0319] | 140 | |
---|
[b99ac227] | 141 | # Distance in meters |
---|
| 142 | try: |
---|
| 143 | transmission = float(line_toks[4]) |
---|
| 144 | except: |
---|
| 145 | raise ValueError,"IgorReader: can't read this file, missing transmission" |
---|
[f55af70] | 146 | |
---|
[11a0319] | 147 | if line.count("LAMBDA")>0: |
---|
| 148 | isInfo = True |
---|
| 149 | |
---|
| 150 | # Find center info line |
---|
| 151 | if isCenter: |
---|
| 152 | isCenter = False |
---|
| 153 | line_toks = line.split() |
---|
[eadf1f9] | 154 | |
---|
| 155 | # Center in bin number: Must substrate 1 because the index starts from 1 |
---|
| 156 | center_x = float(line_toks[0])-1 |
---|
| 157 | center_y = float(line_toks[1])-1 |
---|
[11a0319] | 158 | |
---|
| 159 | if line.count("BCENT")>0: |
---|
| 160 | isCenter = True |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | # Find data start |
---|
| 164 | if line.count("***")>0: |
---|
| 165 | dataStarted = True |
---|
| 166 | |
---|
| 167 | # Check that we have all the info |
---|
| 168 | if wavelength == None \ |
---|
| 169 | or distance == None \ |
---|
| 170 | or center_x == None \ |
---|
| 171 | or center_y == None: |
---|
[c125e0c] | 172 | raise ValueError, "IgorReader:Missing information in data file" |
---|
[11a0319] | 173 | |
---|
| 174 | if dataStarted == True: |
---|
| 175 | try: |
---|
| 176 | value = float(line) |
---|
| 177 | except: |
---|
[b99ac227] | 178 | # Found a non-float entry, skip it |
---|
[11a0319] | 179 | continue |
---|
| 180 | |
---|
| 181 | # Get bin number |
---|
[f55af70] | 182 | if math.fmod(itot, i_tot_row)==0: |
---|
[11a0319] | 183 | i_x = 0 |
---|
| 184 | i_y += 1 |
---|
| 185 | else: |
---|
| 186 | i_x += 1 |
---|
| 187 | |
---|
[b99ac227] | 188 | output.data[i_y][i_x] = value |
---|
[11a0319] | 189 | ncounts += 1 |
---|
| 190 | |
---|
| 191 | # Det 640 x 640 mm |
---|
| 192 | # Q = 4pi/lambda sin(theta/2) |
---|
[4cc8e14b] | 193 | # Bin size is 0.5 cm |
---|
| 194 | #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance / 100.0 and |
---|
| 195 | #REmoved +1 from theta = (i_y-center_y+1)*0.5 / distance / 100.0 |
---|
| 196 | #ToDo: Need complete check if the following covert process is consistent with fitting.py. |
---|
| 197 | theta = (i_x-center_x)*0.5 / distance / 100.0 |
---|
[11a0319] | 198 | qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
[b99ac227] | 199 | |
---|
[ca10d8e] | 200 | if has_converter == True and output.Q_unit != '1/A': |
---|
[b99ac227] | 201 | qx = data_conv_q(qx, units=output.Q_unit) |
---|
| 202 | |
---|
[11a0319] | 203 | if xmin==None or qx<xmin: |
---|
| 204 | xmin = qx |
---|
| 205 | if xmax==None or qx>xmax: |
---|
| 206 | xmax = qx |
---|
| 207 | |
---|
[4cc8e14b] | 208 | theta = (i_y-center_y)*0.5 / distance / 100.0 |
---|
[11a0319] | 209 | qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
[b99ac227] | 210 | |
---|
[ca10d8e] | 211 | if has_converter == True and output.Q_unit != '1/A': |
---|
[b99ac227] | 212 | qy = data_conv_q(qy, units=output.Q_unit) |
---|
| 213 | |
---|
[11a0319] | 214 | if ymin==None or qy<ymin: |
---|
| 215 | ymin = qy |
---|
| 216 | if ymax==None or qy>ymax: |
---|
| 217 | ymax = qy |
---|
| 218 | |
---|
[b99ac227] | 219 | if not qx in x: |
---|
| 220 | x.append(qx) |
---|
| 221 | if not qy in y: |
---|
| 222 | y.append(qy) |
---|
[11a0319] | 223 | |
---|
| 224 | itot += 1 |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | theta = 0.25 / distance / 100.0 |
---|
| 228 | xstep = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
| 229 | |
---|
| 230 | theta = 0.25 / distance / 100.0 |
---|
| 231 | ystep = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
| 232 | |
---|
[b99ac227] | 233 | # Store all data ###################################### |
---|
| 234 | # Store wavelength |
---|
| 235 | if has_converter==True and output.source.wavelength_unit != 'A': |
---|
| 236 | conv = Converter('A') |
---|
| 237 | wavelength = conv(wavelength, units=output.source.wavelength_unit) |
---|
| 238 | output.source.wavelength = wavelength |
---|
| 239 | |
---|
| 240 | # Store distance |
---|
| 241 | if has_converter==True and detector.distance_unit != 'm': |
---|
| 242 | conv = Converter('m') |
---|
| 243 | distance = conv(distance, units=detector.distance_unit) |
---|
| 244 | detector.distance = distance |
---|
| 245 | |
---|
| 246 | # Store transmission |
---|
| 247 | output.sample.transmission = transmission |
---|
[11a0319] | 248 | |
---|
[b99ac227] | 249 | # Store pixel size |
---|
| 250 | pixel = 5.0 |
---|
| 251 | if has_converter==True and detector.pixel_size_unit != 'mm': |
---|
| 252 | conv = Converter('mm') |
---|
| 253 | pixel = conv(pixel, units=detector.pixel_size_unit) |
---|
| 254 | detector.pixel_size.x = pixel |
---|
| 255 | detector.pixel_size.y = pixel |
---|
[11a0319] | 256 | |
---|
[b99ac227] | 257 | # Store beam center in distance units |
---|
| 258 | detector.beam_center.x = center_x*pixel |
---|
| 259 | detector.beam_center.y = center_y*pixel |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | # Store limits of the image (2D array) |
---|
| 263 | xmin =xmin-xstep/2.0 |
---|
| 264 | xmax =xmax+xstep/2.0 |
---|
| 265 | ymin =ymin-ystep/2.0 |
---|
| 266 | ymax =ymax+ystep/2.0 |
---|
[ca10d8e] | 267 | if has_converter == True and output.Q_unit != '1/A': |
---|
[b99ac227] | 268 | xmin = data_conv_q(xmin, units=output.Q_unit) |
---|
| 269 | xmax = data_conv_q(xmax, units=output.Q_unit) |
---|
| 270 | ymin = data_conv_q(ymin, units=output.Q_unit) |
---|
| 271 | ymax = data_conv_q(ymax, units=output.Q_unit) |
---|
| 272 | output.xmin = xmin |
---|
| 273 | output.xmax = xmax |
---|
| 274 | output.ymin = ymin |
---|
| 275 | output.ymax = ymax |
---|
| 276 | |
---|
| 277 | # Store x and y axis bin centers |
---|
| 278 | output.x_bins = x |
---|
| 279 | output.y_bins = y |
---|
| 280 | |
---|
| 281 | # Units |
---|
| 282 | if data_conv_q is not None: |
---|
[b568ec1b] | 283 | output.xaxis("\\rm{Q_{x}}", output.Q_unit) |
---|
| 284 | output.yaxis("\\rm{Q_{y}}", output.Q_unit) |
---|
[b99ac227] | 285 | else: |
---|
[b568ec1b] | 286 | output.xaxis("\\rm{Q_{x}}", 'A^{-1}') |
---|
| 287 | output.yaxis("\\rm{Q_{y}}", 'A^{-1}') |
---|
[b99ac227] | 288 | |
---|
| 289 | if data_conv_i is not None: |
---|
[0e2aa40] | 290 | output.zaxis("\\rm{Intensity}", output.I_unit) |
---|
[b99ac227] | 291 | else: |
---|
[0e2aa40] | 292 | output.zaxis("\\rm{Intensity}","cm^{-1}") |
---|
[b99ac227] | 293 | |
---|
[fe78c7b] | 294 | # Store loading process information |
---|
| 295 | output.meta_data['loader'] = self.type_name |
---|
[eadf1f9] | 296 | output = reader2D_converter(output) |
---|
| 297 | |
---|
[16d8e5f] | 298 | return output |
---|
[b99ac227] | 299 | |
---|
| 300 | if __name__ == "__main__": |
---|
| 301 | reader = Reader() |
---|
| 302 | print reader.read("../test/MAR07232_rest.ASC") |
---|
| 303 | |
---|