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