Changeset f68d503 in sasview for src/sas/sascalc/dataloader/readers/IgorReader.py
- Timestamp:
- Apr 4, 2017 10:16:11 AM (8 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 4551598, 7b15990
- Parents:
- b2b3009d (diff), 05a9d29 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - git-author:
- Andrew Jackson <andrew.jackson@…> (04/04/17 10:16:11)
- git-committer:
- GitHub <noreply@…> (04/04/17 10:16:11)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/IgorReader.py
rb699768 red2276f 13 13 ############################################################################# 14 14 import os 15 import numpy 15 16 import numpy as np 16 17 import math 17 18 #import logging 19 18 20 from sas.sascalc.dataloader.data_info import Data2D 19 21 from sas.sascalc.dataloader.data_info import Detector … … 40 42 """ Read file """ 41 43 if not os.path.isfile(filename): 42 raise ValueError, \ 43 "Specified file %s is not a regular file" % filename 44 45 # Read file 46 f = open(filename, 'r') 47 buf = f.read() 48 49 # Instantiate data object 44 raise ValueError("Specified file %s is not a regular " 45 "file" % filename) 46 50 47 output = Data2D() 48 51 49 output.filename = os.path.basename(filename) 52 50 detector = Detector() 53 if len(output.detector) > 0:54 print str(output.detector[0])51 if len(output.detector): 52 print(str(output.detector[0])) 55 53 output.detector.append(detector) 56 57 # Get content 58 dataStarted = False 59 60 lines = buf.split('\n') 61 itot = 0 62 x = [] 63 y = [] 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 74 i_tot_row = 0 75 76 isInfo = False 77 isCenter = False 78 79 data_conv_q = None 80 data_conv_i = None 81 82 if has_converter == True and output.Q_unit != '1/A': 54 55 data_conv_q = data_conv_i = None 56 57 if has_converter and output.Q_unit != '1/A': 83 58 data_conv_q = Converter('1/A') 84 59 # Test it 85 60 data_conv_q(1.0, output.Q_unit) 86 61 87 if has_converter == Trueand output.I_unit != '1/cm':62 if has_converter and output.I_unit != '1/cm': 88 63 data_conv_i = Converter('1/cm') 89 64 # Test it 90 65 data_conv_i(1.0, output.I_unit) 91 66 92 67 for line in lines: 93 68 … … 118 93 size_x = i_tot_row # 192#128 119 94 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 msg = "IgorReader: can't read this file, missing wavelength" 134 raise ValueError, msg 135 # Distance in meters 136 try: 137 distance = float(line_toks[3]) 138 except: 139 msg = "IgorReader: can't read this file, missing distance" 140 raise ValueError, msg 141 142 # Distance in meters 143 try: 144 transmission = float(line_toks[4]) 145 except: 146 msg = "IgorReader: can't read this file, " 147 msg += "missing transmission" 148 raise ValueError, msg 149 150 if line.count("LAMBDA") > 0: 151 isInfo = True 152 153 # Find center info line 154 if isCenter: 155 isCenter = False 156 line_toks = line.split() 157 158 # Center in bin number: Must substrate 1 because 159 #the index starts from 1 160 center_x = float(line_toks[0]) - 1 161 center_y = float(line_toks[1]) - 1 162 163 if line.count("BCENT") > 0: 164 isCenter = True 165 166 # Find data start 167 if line.count("***")>0: 168 dataStarted = True 169 170 # Check that we have all the info 171 if wavelength == None \ 172 or distance == None \ 173 or center_x == None \ 174 or center_y == None: 175 msg = "IgorReader:Missing information in data file" 176 raise ValueError, msg 177 178 if dataStarted == True: 179 try: 180 value = float(line) 181 except: 182 # Found a non-float entry, skip it 183 continue 184 185 # Get bin number 186 if math.fmod(itot, i_tot_row) == 0: 187 i_x = 0 188 i_y += 1 189 else: 190 i_x += 1 191 192 output.data[i_y][i_x] = value 193 ncounts += 1 194 195 # Det 640 x 640 mm 196 # Q = 4pi/lambda sin(theta/2) 197 # Bin size is 0.5 cm 198 #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance 199 # / 100.0 and 200 #REmoved +1 from theta = (i_y-center_y+1)*0.5 / 201 # distance / 100.0 202 #ToDo: Need complete check if the following 203 # covert process is consistent with fitting.py. 204 theta = (i_x - center_x) * 0.5 / distance / 100.0 205 qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0) 206 207 if has_converter == True and output.Q_unit != '1/A': 208 qx = data_conv_q(qx, units=output.Q_unit) 209 210 if xmin == None or qx < xmin: 211 xmin = qx 212 if xmax == None or qx > xmax: 213 xmax = qx 214 215 theta = (i_y - center_y) * 0.5 / distance / 100.0 216 qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) 217 218 if has_converter == True and output.Q_unit != '1/A': 219 qy = data_conv_q(qy, units=output.Q_unit) 220 221 if ymin == None or qy < ymin: 222 ymin = qy 223 if ymax == None or qy > ymax: 224 ymax = qy 225 226 if not qx in x: 227 x.append(qx) 228 if not qy in y: 229 y.append(qy) 230 231 itot += 1 232 233 95 output.data = np.zeros([size_x, size_y]) 96 output.err_data = np.zeros([size_x, size_y]) 97 98 data_row = 0 99 wavelength = distance = center_x = center_y = None 100 dataStarted = isInfo = isCenter = False 101 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) 121 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) 129 130 if line.count("LAMBDA"): 131 isInfo = True 132 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) 190 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) 194 195 xmax = np.max(qx) 196 xmin = np.min(qx) 197 ymax = np.max(qy) 198 ymin = np.min(qy) 199 200 # calculate edge offset in q. 234 201 theta = 0.25 / distance / 100.0 235 xstep = 4.0 * math.pi / wavelength * math.sin(theta / 2.0)202 xstep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0) 236 203 237 204 theta = 0.25 / distance / 100.0 238 ystep = 4.0 * math.pi/ wavelength * math.sin(theta / 2.0)205 ystep = 4.0 * np.pi/ wavelength * np.sin(theta / 2.0) 239 206 240 207 # Store all data ###################################### 241 208 # Store wavelength 242 if has_converter == Trueand output.source.wavelength_unit != 'A':209 if has_converter and output.source.wavelength_unit != 'A': 243 210 conv = Converter('A') 244 211 wavelength = conv(wavelength, units=output.source.wavelength_unit) … … 246 213 247 214 # Store distance 248 if has_converter == Trueand detector.distance_unit != 'm':215 if has_converter and detector.distance_unit != 'm': 249 216 conv = Converter('m') 250 217 distance = conv(distance, units=detector.distance_unit) … … 254 221 output.sample.transmission = transmission 255 222 256 # Store pixel size 223 # Store pixel size (mm) 257 224 pixel = 5.0 258 if has_converter == Trueand detector.pixel_size_unit != 'mm':225 if has_converter and detector.pixel_size_unit != 'mm': 259 226 conv = Converter('mm') 260 227 pixel = conv(pixel, units=detector.pixel_size_unit) … … 267 234 268 235 # Store limits of the image (2D array) 269 xmin = xmin -xstep / 2.0270 xmax = xmax +xstep / 2.0271 ymin = ymin -ystep / 2.0272 ymax = ymax +ystep / 2.0273 if has_converter == Trueand output.Q_unit != '1/A':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': 274 241 xmin = data_conv_q(xmin, units=output.Q_unit) 275 242 xmax = data_conv_q(xmax, units=output.Q_unit) … … 282 249 283 250 # Store x and y axis bin centers 284 output.x_bins = x285 output.y_bins = y251 output.x_bins = qx.tolist() 252 output.y_bins = qy.tolist() 286 253 287 254 # Units
Note: See TracChangeset
for help on using the changeset viewer.