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