[b99ac227] | 1 | |
---|
[0997158f] | 2 | ############################################################################ |
---|
| 3 | #This software was developed by the University of Tennessee as part of the |
---|
| 4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 5 | #project funded by the US National Science Foundation. |
---|
| 6 | #If you use DANSE applications to do scientific research that leads to |
---|
| 7 | #publication, we ask that you acknowledge the use of the software with the |
---|
| 8 | #following sentence: |
---|
| 9 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
| 10 | #copyright 2008, University of Tennessee |
---|
| 11 | ############################################################################# |
---|
[b99ac227] | 12 | |
---|
[0997158f] | 13 | """ |
---|
| 14 | IGOR 2D reduced file reader |
---|
[b99ac227] | 15 | """ |
---|
| 16 | |
---|
[11a0319] | 17 | import os, sys |
---|
[b99ac227] | 18 | import numpy |
---|
| 19 | import math, logging |
---|
| 20 | from DataLoader.data_info import Data2D, Detector |
---|
[eadf1f9] | 21 | from DataLoader.manipulations import reader2D_converter |
---|
[b99ac227] | 22 | |
---|
| 23 | # Look for unit converter |
---|
| 24 | has_converter = True |
---|
| 25 | try: |
---|
| 26 | from data_util.nxsunit import Converter |
---|
| 27 | except: |
---|
| 28 | has_converter = False |
---|
| 29 | |
---|
[bb03739] | 30 | class Reader: |
---|
[11a0319] | 31 | """ Simple data reader for Igor data files """ |
---|
[b99ac227] | 32 | ## File type |
---|
[28caa03] | 33 | type_name = "IGOR 2D" |
---|
| 34 | ## Wildcards |
---|
[b99ac227] | 35 | type = ["IGOR 2D files (*.ASC)|*.ASC"] |
---|
| 36 | ## Extension |
---|
| 37 | ext=['.ASC', '.asc'] |
---|
| 38 | |
---|
[11a0319] | 39 | def read(self,filename=None): |
---|
| 40 | """ Read file """ |
---|
[b99ac227] | 41 | if not os.path.isfile(filename): |
---|
[11a0319] | 42 | raise ValueError, \ |
---|
[b99ac227] | 43 | "Specified file %s is not a regular file" % filename |
---|
[11a0319] | 44 | |
---|
| 45 | # Read file |
---|
[b99ac227] | 46 | f = open(filename,'r') |
---|
[11a0319] | 47 | buf = f.read() |
---|
| 48 | |
---|
[b99ac227] | 49 | # Instantiate data object |
---|
| 50 | output = Data2D() |
---|
| 51 | output.filename = os.path.basename(filename) |
---|
| 52 | detector = Detector() |
---|
| 53 | if len(output.detector)>0: print str(output.detector[0]) |
---|
| 54 | output.detector.append(detector) |
---|
[f55af70] | 55 | |
---|
[11a0319] | 56 | # Get content |
---|
| 57 | dataStarted = False |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | lines = buf.split('\n') |
---|
| 61 | itot = 0 |
---|
[b99ac227] | 62 | x = [] |
---|
| 63 | y = [] |
---|
[11a0319] | 64 | |
---|
| 65 | ncounts = 0 |
---|
| 66 | |
---|
| 67 | xmin = None |
---|
| 68 | xmax = None |
---|
| 69 | ymin = None |
---|
| 70 | ymax = None |
---|
| 71 | |
---|
| 72 | i_x = 0 |
---|
| 73 | i_y = -1 |
---|
[f55af70] | 74 | i_tot_row = 0 |
---|
[11a0319] | 75 | |
---|
| 76 | isInfo = False |
---|
| 77 | isCenter = False |
---|
[f55af70] | 78 | |
---|
| 79 | data_conv_q = None |
---|
| 80 | data_conv_i = None |
---|
| 81 | |
---|
[ca10d8e] | 82 | if has_converter == True and output.Q_unit != '1/A': |
---|
| 83 | data_conv_q = Converter('1/A') |
---|
[f55af70] | 84 | # Test it |
---|
| 85 | data_conv_q(1.0, output.Q_unit) |
---|
| 86 | |
---|
[ca10d8e] | 87 | if has_converter == True and output.I_unit != '1/cm': |
---|
| 88 | data_conv_i = Converter('1/cm') |
---|
[f55af70] | 89 | # Test it |
---|
| 90 | data_conv_i(1.0, output.I_unit) |
---|
| 91 | |
---|
[11a0319] | 92 | for line in lines: |
---|
| 93 | |
---|
| 94 | # Find setup info line |
---|
| 95 | if isInfo: |
---|
| 96 | isInfo = False |
---|
| 97 | line_toks = line.split() |
---|
| 98 | # Wavelength in Angstrom |
---|
| 99 | try: |
---|
| 100 | wavelength = float(line_toks[1]) |
---|
| 101 | except: |
---|
[c125e0c] | 102 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
[f55af70] | 103 | |
---|
| 104 | #Find # of bins in a row assuming the detector is square. |
---|
| 105 | if dataStarted == True: |
---|
| 106 | try: |
---|
| 107 | value = float(line) |
---|
| 108 | except: |
---|
| 109 | # Found a non-float entry, skip it |
---|
| 110 | continue |
---|
| 111 | |
---|
| 112 | # Get total bin number |
---|
| 113 | |
---|
| 114 | i_tot_row += 1 |
---|
| 115 | i_tot_row=math.ceil(math.sqrt(i_tot_row))-1 |
---|
| 116 | #print "i_tot", i_tot_row |
---|
| 117 | size_x = i_tot_row#192#128 |
---|
| 118 | size_y = i_tot_row#192#128 |
---|
| 119 | output.data = numpy.zeros([size_x,size_y]) |
---|
| 120 | output.err_data = numpy.zeros([size_x,size_y]) |
---|
| 121 | |
---|
| 122 | #Read Header and 2D data |
---|
| 123 | for line in lines: |
---|
| 124 | # Find setup info line |
---|
| 125 | if isInfo: |
---|
| 126 | isInfo = False |
---|
| 127 | line_toks = line.split() |
---|
| 128 | # Wavelength in Angstrom |
---|
| 129 | try: |
---|
| 130 | wavelength = float(line_toks[1]) |
---|
| 131 | except: |
---|
| 132 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
[11a0319] | 133 | # Distance in meters |
---|
| 134 | try: |
---|
| 135 | distance = float(line_toks[3]) |
---|
| 136 | except: |
---|
[c125e0c] | 137 | raise ValueError,"IgorReader: can't read this file, missing distance" |
---|
[11a0319] | 138 | |
---|
[b99ac227] | 139 | # Distance in meters |
---|
| 140 | try: |
---|
| 141 | transmission = float(line_toks[4]) |
---|
| 142 | except: |
---|
| 143 | raise ValueError,"IgorReader: can't read this file, missing transmission" |
---|
[f55af70] | 144 | |
---|
[11a0319] | 145 | if line.count("LAMBDA")>0: |
---|
| 146 | isInfo = True |
---|
| 147 | |
---|
| 148 | # Find center info line |
---|
| 149 | if isCenter: |
---|
| 150 | isCenter = False |
---|
| 151 | line_toks = line.split() |
---|
[eadf1f9] | 152 | |
---|
| 153 | # Center in bin number: Must substrate 1 because the index starts from 1 |
---|
| 154 | center_x = float(line_toks[0])-1 |
---|
| 155 | center_y = float(line_toks[1])-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 |
---|
[eadf1f9] | 294 | output = reader2D_converter(output) |
---|
| 295 | |
---|
[16d8e5f] | 296 | return output |
---|
[b99ac227] | 297 | |
---|
| 298 | if __name__ == "__main__": |
---|
| 299 | reader = Reader() |
---|
| 300 | print reader.read("../test/MAR07232_rest.ASC") |
---|
| 301 | |
---|