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