def convert_image(self, rgb, xmin, xmax, ymin, ymax, zscale): """ Convert image to data2D """ x_len = len(rgb[0]) y_len = len(rgb) x_vals = np.linspace(xmin, xmax, num=x_len) y_vals = np.linspace(ymin, ymax, num=y_len) # Instantiate data object output = Data2D() output.filename = os.path.basename(self.title) output.id = output.filename detector = Detector() detector.pixel_size.x = None detector.pixel_size.y = None # Store the sample to detector distance detector.distance = None output.detector.append(detector) # Initiazed the output data object output.data = zscale * self.rgb2gray(rgb) output.err_data = np.zeros([x_len, y_len]) output.mask = np.ones([x_len, y_len], dtype=bool) output.xbins = x_len output.ybins = y_len output.x_bins = x_vals output.y_bins = y_vals output.qx_data = np.array(x_vals) output.qy_data = np.array(y_vals) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax output.xaxis('\\rm{Q_{x}}', '\AA^{-1}') output.yaxis('\\rm{Q_{y}}', '\AA^{-1}') # Store loading process information output.meta_data['loader'] = self.title.split('.')[-1] + "Reader" output.is_data = True output = reader2D_converter(output) if self.base is not None: data = self.base.create_gui_data(output, self.title) self.base.add_data({data.id:data})
def convert_image(self, rgb, xmin, xmax, ymin, ymax, zscale): """ Convert image to data2D """ x_len = len(rgb[0]) y_len = len(rgb) x_vals = np.linspace(xmin, xmax, num=x_len) y_vals = np.linspace(ymin, ymax, num=y_len) # Instantiate data object output = Data2D() output.filename = os.path.basename(self.title) output.id = output.filename detector = Detector() detector.pixel_size.x = None detector.pixel_size.y = None # Store the sample to detector distance detector.distance = None output.detector.append(detector) # Initiazed the output data object output.data = zscale * self.rgb2gray(rgb) output.err_data = np.zeros([x_len, y_len]) output.mask = np.ones([x_len, y_len], dtype=bool) output.xbins = x_len output.ybins = y_len output.x_bins = x_vals output.y_bins = y_vals output.qx_data = np.array(x_vals) output.qy_data = np.array(y_vals) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax output.xaxis('\\rm{Q_{x}}', '\AA^{-1}') output.yaxis('\\rm{Q_{y}}', '\AA^{-1}') # Store loading process information output.meta_data['loader'] = self.title.split('.')[-1] + "Reader" output.is_data = True output = reader2D_converter(output) if self.base is not None: data = self.base.create_gui_data(output, self.title) self.base.add_data({data.id: data})
def setUp(self): """ Create a flat 2D distribution. All averaging results should return the predefined height of the distribution (1.0). """ x_0 = np.ones([100, 100]) dx_0 = np.ones([100, 100]) self.data = data_info.Data2D(data=x_0, err_data=dx_0) detector = data_info.Detector() detector.distance = 1000.0 # mm detector.pixel_size.x = 1.0 # mm detector.pixel_size.y = 1.0 # mm # center in pixel position = (len(x_0)-1)/2 detector.beam_center.x = (len(x_0) - 1) / 2 # pixel number detector.beam_center.y = (len(x_0) - 1) / 2 # pixel number self.data.detector.append(detector) source = data_info.Source() source.wavelength = 10.0 # A self.data.source = source # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A # respectively. self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) self.qstep = len(x_0) x = np.linspace(start=-1 * self.qmax, stop=self.qmax, num=self.qstep, endpoint=True) y = np.linspace(start=-1 * self.qmax, stop=self.qmax, num=self.qstep, endpoint=True) self.data.x_bins = x self.data.y_bins = y self.data = reader2D_converter(self.data)
def setUp(self): """ Create a flat 2D distribution. All averaging results should return the predefined height of the distribution (1.0). """ x_0 = numpy.ones([100,100]) dx_0 = numpy.ones([100,100]) self.data = data_info.Data2D(data=x_0, err_data=dx_0) detector = data_info.Detector() detector.distance = 1000.0 #mm detector.pixel_size.x = 1.0 #mm detector.pixel_size.y = 1.0 #mm # center in pixel position = (len(x_0)-1)/2 detector.beam_center.x = (len(x_0)-1)/2 #pixel number detector.beam_center.y = (len(x_0)-1)/2 #pixel number self.data.detector.append(detector) source = data_info.Source() source.wavelength = 10.0 #A self.data.source = source # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A respectively. self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) self.qstep = len(x_0) x= numpy.linspace(start= -1*self.qmax, stop= self.qmax, num= self.qstep, endpoint=True ) y = numpy.linspace(start= -1*self.qmax, stop= self.qmax, num= self.qstep, endpoint=True ) self.data.x_bins=x self.data.y_bins=y self.data = reader2D_converter(self.data)
def read(self, filename=None): """ Open and read the data in a file @param file: path of the file """ read_it = False for item in self.ext: if filename.lower().find(item) >= 0: read_it = True if read_it: try: datafile = open(filename, 'r') except: raise RuntimeError, "danse_reader cannot open %s" % (filename) # defaults # wavelength in Angstrom wavelength = 10.0 # Distance in meter distance = 11.0 # Pixel number of center in x center_x = 65 # Pixel number of center in y center_y = 65 # Pixel size [mm] pixel = 5.0 # Size in x, in pixels size_x = 128 # Size in y, in pixels size_y = 128 # Format version fversion = 1.0 output = Data2D() output.filename = os.path.basename(filename) detector = Detector() output.detector.append(detector) output.data = numpy.zeros([size_x, size_y]) output.err_data = numpy.zeros([size_x, size_y]) data_conv_q = None data_conv_i = None if has_converter == True and output.Q_unit != '1/A': data_conv_q = Converter('1/A') # Test it data_conv_q(1.0, output.Q_unit) if has_converter == True and output.I_unit != '1/cm': data_conv_i = Converter('1/cm') # Test it data_conv_i(1.0, output.I_unit) read_on = True while read_on: line = datafile.readline() if line.find("DATA:") >= 0: read_on = False break toks = line.split(':') if toks[0] == "FORMATVERSION": fversion = float(toks[1]) if toks[0] == "WAVELENGTH": wavelength = float(toks[1]) elif toks[0] == "DISTANCE": distance = float(toks[1]) elif toks[0] == "CENTER_X": center_x = float(toks[1]) elif toks[0] == "CENTER_Y": center_y = float(toks[1]) elif toks[0] == "PIXELSIZE": pixel = float(toks[1]) elif toks[0] == "SIZE_X": size_x = int(toks[1]) elif toks[0] == "SIZE_Y": size_y = int(toks[1]) # Read the data data = [] error = [] if fversion == 1.0: data_str = datafile.readline() data = data_str.split(' ') else: read_on = True while read_on: data_str = datafile.readline() if len(data_str) == 0: read_on = False else: toks = data_str.split() try: val = float(toks[0]) err = float(toks[1]) if data_conv_i is not None: val = data_conv_i(val, units=output._yunit) err = data_conv_i(err, units=output._yunit) data.append(val) error.append(err) except: logging.info("Skipping line:%s,%s" % (data_str, sys.exc_value)) # Initialize x_vals = [] y_vals = [] ymin = None ymax = None xmin = None xmax = None # Qx and Qy vectors theta = pixel / distance / 100.0 stepq = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) for i_x in range(size_x): theta = (i_x - center_x + 1) * pixel / distance / 100.0 qx = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) if has_converter == True and output.Q_unit != '1/A': qx = data_conv_q(qx, units=output.Q_unit) x_vals.append(qx) if xmin == None or qx < xmin: xmin = qx if xmax == None or qx > xmax: xmax = qx ymin = None ymax = None for i_y in range(size_y): theta = (i_y - center_y + 1) * pixel / distance / 100.0 qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) if has_converter == True and output.Q_unit != '1/A': qy = data_conv_q(qy, units=output.Q_unit) y_vals.append(qy) if ymin == None or qy < ymin: ymin = qy if ymax == None or qy > ymax: ymax = qy # Store the data in the 2D array i_x = 0 i_y = -1 for i_pt in range(len(data)): try: value = float(data[i_pt]) except: # For version 1.0, the data were still # stored as strings at this point. msg = "Skipping entry (v1.0):%s,%s" % (str( data[i_pt]), sys.exc_value) logging.info(msg) # Get bin number if math.fmod(i_pt, size_x) == 0: i_x = 0 i_y += 1 else: i_x += 1 output.data[i_y][i_x] = value if fversion > 1.0: output.err_data[i_y][i_x] = error[i_pt] # Store all data # Store wavelength if has_converter == True and output.source.wavelength_unit != 'A': conv = Converter('A') wavelength = conv(wavelength, units=output.source.wavelength_unit) output.source.wavelength = wavelength # Store distance if has_converter == True and detector.distance_unit != 'm': conv = Converter('m') distance = conv(distance, units=detector.distance_unit) detector.distance = distance # Store pixel size if has_converter == True and detector.pixel_size_unit != 'mm': conv = Converter('mm') pixel = conv(pixel, units=detector.pixel_size_unit) detector.pixel_size.x = pixel detector.pixel_size.y = pixel # Store beam center in distance units detector.beam_center.x = center_x * pixel detector.beam_center.y = center_y * pixel # Store limits of the image (2D array) xmin = xmin - stepq / 2.0 xmax = xmax + stepq / 2.0 ymin = ymin - stepq / 2.0 ymax = ymax + stepq / 2.0 if has_converter == True and output.Q_unit != '1/A': xmin = data_conv_q(xmin, units=output.Q_unit) xmax = data_conv_q(xmax, units=output.Q_unit) ymin = data_conv_q(ymin, units=output.Q_unit) ymax = data_conv_q(ymax, units=output.Q_unit) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax # Store x and y axis bin centers output.x_bins = x_vals output.y_bins = y_vals # Units if data_conv_q is not None: output.xaxis("\\rm{Q_{x}}", output.Q_unit) output.yaxis("\\rm{Q_{y}}", output.Q_unit) else: output.xaxis("\\rm{Q_{x}}", 'A^{-1}') output.yaxis("\\rm{Q_{y}}", 'A^{-1}') if data_conv_i is not None: output.zaxis("\\rm{Intensity}", output.I_unit) else: output.zaxis("\\rm{Intensity}", "cm^{-1}") if not fversion >= 1.0: msg = "Danse_reader can't read this file %s" % filename raise ValueError, msg else: logging.info("Danse_reader Reading %s \n" % filename) # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output return None
def read(self, filename=None): """ Open and read the data in a file :param file: path of the file """ try: import Image import TiffImagePlugin Image._initialized = 2 except: msg = "tiff_reader: could not load file. Missing Image module." raise RuntimeError(msg) # Instantiate data object output = Data2D() output.filename = os.path.basename(filename) # Read in the image try: im = Image.open(filename) except: raise RuntimeError("cannot open %s" % (filename)) data = im.getdata() # Initiazed the output data object output.data = np.zeros([im.size[0], im.size[1]]) output.err_data = np.zeros([im.size[0], im.size[1]]) output.mask = np.ones([im.size[0], im.size[1]], dtype=bool) # Initialize x_vals = [] y_vals = [] # x and y vectors for i_x in range(im.size[0]): x_vals.append(i_x) itot = 0 for i_y in range(im.size[1]): y_vals.append(i_y) for val in data: try: value = float(val) except: logger.error("tiff_reader: had to skip a non-float point") continue # Get bin number if math.fmod(itot, im.size[0]) == 0: i_x = 0 i_y += 1 else: i_x += 1 output.data[im.size[1] - 1 - i_y][i_x] = value itot += 1 output.xbins = im.size[0] output.ybins = im.size[1] output.x_bins = x_vals output.y_bins = y_vals output.qx_data = np.array(x_vals) output.qy_data = np.array(y_vals) output.xmin = 0 output.xmax = im.size[0] - 1 output.ymin = 0 output.ymax = im.size[0] - 1 # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output
def read(self, filename=None): """ Open and read the data in a file :param file: path of the file """ try: import Image import TiffImagePlugin Image._initialized=2 except: msg = "tiff_reader: could not load file. Missing Image module." raise RuntimeError, msg # Instantiate data object output = Data2D() output.filename = os.path.basename(filename) # Read in the image try: im = Image.open(filename) except: raise RuntimeError, "cannot open %s"%(filename) data = im.getdata() # Initiazed the output data object output.data = numpy.zeros([im.size[0], im.size[1]]) output.err_data = numpy.zeros([im.size[0], im.size[1]]) output.mask = numpy.ones([im.size[0], im.size[1]], dtype=bool) # Initialize x_vals = [] y_vals = [] # x and y vectors for i_x in range(im.size[0]): x_vals.append(i_x) itot = 0 for i_y in range(im.size[1]): y_vals.append(i_y) for val in data: try: value = float(val) except: logging.error("tiff_reader: had to skip a non-float point") continue # Get bin number if math.fmod(itot, im.size[0]) == 0: i_x = 0 i_y += 1 else: i_x += 1 output.data[im.size[1] - 1 - i_y][i_x] = value itot += 1 output.xbins = im.size[0] output.ybins = im.size[1] output.x_bins = x_vals output.y_bins = y_vals output.qx_data = numpy.array(x_vals) output.qy_data = numpy.array(y_vals) output.xmin = 0 output.xmax = im.size[0] - 1 output.ymin = 0 output.ymax = im.size[0] - 1 # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output
def read(self, filename=None): """ Open and read the data in a file @param file: path of the file """ read_it = False for item in self.ext: if filename.lower().find(item) >= 0: read_it = True if read_it: try: datafile = open(filename, 'r') except: raise RuntimeError,"danse_reader cannot open %s" % (filename) # defaults # wavelength in Angstrom wavelength = 10.0 # Distance in meter distance = 11.0 # Pixel number of center in x center_x = 65 # Pixel number of center in y center_y = 65 # Pixel size [mm] pixel = 5.0 # Size in x, in pixels size_x = 128 # Size in y, in pixels size_y = 128 # Format version fversion = 1.0 output = Data2D() output.filename = os.path.basename(filename) detector = Detector() output.detector.append(detector) output.data = numpy.zeros([size_x,size_y]) output.err_data = numpy.zeros([size_x, size_y]) data_conv_q = None data_conv_i = None if has_converter == True and output.Q_unit != '1/A': data_conv_q = Converter('1/A') # Test it data_conv_q(1.0, output.Q_unit) if has_converter == True and output.I_unit != '1/cm': data_conv_i = Converter('1/cm') # Test it data_conv_i(1.0, output.I_unit) read_on = True while read_on: line = datafile.readline() if line.find("DATA:") >= 0: read_on = False break toks = line.split(':') if toks[0] == "FORMATVERSION": fversion = float(toks[1]) if toks[0] == "WAVELENGTH": wavelength = float(toks[1]) elif toks[0] == "DISTANCE": distance = float(toks[1]) elif toks[0] == "CENTER_X": center_x = float(toks[1]) elif toks[0] == "CENTER_Y": center_y = float(toks[1]) elif toks[0] == "PIXELSIZE": pixel = float(toks[1]) elif toks[0] == "SIZE_X": size_x = int(toks[1]) elif toks[0] == "SIZE_Y": size_y = int(toks[1]) # Read the data data = [] error = [] if fversion == 1.0: data_str = datafile.readline() data = data_str.split(' ') else: read_on = True while read_on: data_str = datafile.readline() if len(data_str) == 0: read_on = False else: toks = data_str.split() try: val = float(toks[0]) err = float(toks[1]) if data_conv_i is not None: val = data_conv_i(val, units=output._yunit) err = data_conv_i(err, units=output._yunit) data.append(val) error.append(err) except: logging.info("Skipping line:%s,%s" %(data_str, sys.exc_value)) # Initialize x_vals = [] y_vals = [] ymin = None ymax = None xmin = None xmax = None # Qx and Qy vectors theta = pixel / distance / 100.0 stepq = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) for i_x in range(size_x): theta = (i_x - center_x + 1) * pixel / distance / 100.0 qx = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) if has_converter == True and output.Q_unit != '1/A': qx = data_conv_q(qx, units=output.Q_unit) x_vals.append(qx) if xmin == None or qx < xmin: xmin = qx if xmax == None or qx > xmax: xmax = qx ymin = None ymax = None for i_y in range(size_y): theta = (i_y - center_y + 1) * pixel / distance / 100.0 qy = 4.0 * math.pi / wavelength * math.sin(theta/2.0) if has_converter == True and output.Q_unit != '1/A': qy = data_conv_q(qy, units=output.Q_unit) y_vals.append(qy) if ymin == None or qy < ymin: ymin = qy if ymax == None or qy > ymax: ymax = qy # Store the data in the 2D array i_x = 0 i_y = -1 for i_pt in range(len(data)): try: value = float(data[i_pt]) except: # For version 1.0, the data were still # stored as strings at this point. msg = "Skipping entry (v1.0):%s,%s" % (str(data[i_pt]), sys.exc_value) logging.info(msg) # Get bin number if math.fmod(i_pt, size_x) == 0: i_x = 0 i_y += 1 else: i_x += 1 output.data[i_y][i_x] = value if fversion>1.0: output.err_data[i_y][i_x] = error[i_pt] # Store all data # Store wavelength if has_converter == True and output.source.wavelength_unit != 'A': conv = Converter('A') wavelength = conv(wavelength, units=output.source.wavelength_unit) output.source.wavelength = wavelength # Store distance if has_converter == True and detector.distance_unit != 'm': conv = Converter('m') distance = conv(distance, units=detector.distance_unit) detector.distance = distance # Store pixel size if has_converter == True and detector.pixel_size_unit != 'mm': conv = Converter('mm') pixel = conv(pixel, units=detector.pixel_size_unit) detector.pixel_size.x = pixel detector.pixel_size.y = pixel # Store beam center in distance units detector.beam_center.x = center_x * pixel detector.beam_center.y = center_y * pixel # Store limits of the image (2D array) xmin = xmin - stepq / 2.0 xmax = xmax + stepq / 2.0 ymin = ymin - stepq /2.0 ymax = ymax + stepq / 2.0 if has_converter == True and output.Q_unit != '1/A': xmin = data_conv_q(xmin, units=output.Q_unit) xmax = data_conv_q(xmax, units=output.Q_unit) ymin = data_conv_q(ymin, units=output.Q_unit) ymax = data_conv_q(ymax, units=output.Q_unit) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax # Store x and y axis bin centers output.x_bins = x_vals output.y_bins = y_vals # Units if data_conv_q is not None: output.xaxis("\\rm{Q_{x}}", output.Q_unit) output.yaxis("\\rm{Q_{y}}", output.Q_unit) else: output.xaxis("\\rm{Q_{x}}", 'A^{-1}') output.yaxis("\\rm{Q_{y}}", 'A^{-1}') if data_conv_i is not None: output.zaxis("\\rm{Intensity}", output.I_unit) else: output.zaxis("\\rm{Intensity}", "cm^{-1}") if not fversion >= 1.0: msg = "Danse_reader can't read this file %s" % filename raise ValueError, msg else: logging.info("Danse_reader Reading %s \n" % filename) # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output return None
def read(self, filename=None): """ Read file """ if not os.path.isfile(filename): raise ValueError("Specified file %s is not a regular " "file" % filename) output = Data2D() output.filename = os.path.basename(filename) detector = Detector() if len(output.detector): print(str(output.detector[0])) output.detector.append(detector) data_conv_q = data_conv_i = None if has_converter and output.Q_unit != '1/A': data_conv_q = Converter('1/A') # Test it data_conv_q(1.0, output.Q_unit) if has_converter and output.I_unit != '1/cm': data_conv_i = Converter('1/cm') # Test it data_conv_i(1.0, output.I_unit) data_row = 0 wavelength = distance = center_x = center_y = None dataStarted = isInfo = isCenter = False with open(filename, 'r') as f: for line in f: data_row += 1 # Find setup info line if isInfo: isInfo = False line_toks = line.split() # Wavelength in Angstrom try: wavelength = float(line_toks[1]) except ValueError: msg = "IgorReader: can't read this file, missing wavelength" raise ValueError(msg) # Distance in meters try: distance = float(line_toks[3]) except ValueError: msg = "IgorReader: can't read this file, missing distance" raise ValueError(msg) # Distance in meters try: transmission = float(line_toks[4]) except: msg = "IgorReader: can't read this file, " msg += "missing transmission" raise ValueError(msg) if line.count("LAMBDA"): isInfo = True # Find center info line if isCenter: isCenter = False line_toks = line.split() # Center in bin number: Must subtract 1 because # the index starts from 1 center_x = float(line_toks[0]) - 1 center_y = float(line_toks[1]) - 1 if line.count("BCENT"): isCenter = True # Find data start if line.count("***"): # now have to continue to blank line dataStarted = True # Check that we have all the info if (wavelength is None or distance is None or center_x is None or center_y is None): msg = "IgorReader:Missing information in data file" raise ValueError(msg) if dataStarted: if len(line.rstrip()): continue else: break # The data is loaded in row major order (last index changing most # rapidly). However, the original data is in column major order (first # index changing most rapidly). The swap to column major order is done # in reader2D_converter at the end of this method. data = np.loadtxt(filename, skiprows=data_row) size_x = size_y = int(np.rint(np.sqrt(data.size))) output.data = np.reshape(data, (size_x, size_y)) output.err_data = np.zeros_like(output.data) # Det 640 x 640 mm # Q = 4 * pi/lambda * sin(theta/2) # Bin size is 0.5 cm # Removed +1 from theta = (i_x - center_x + 1)*0.5 / distance # / 100.0 and # Removed +1 from theta = (i_y - center_y + 1)*0.5 / # distance / 100.0 # ToDo: Need complete check if the following # convert process is consistent with fitting.py. # calculate qx, qy bin centers of each pixel in the image theta = (np.arange(size_x) - center_x) * 0.5 / distance / 100. qx = 4 * np.pi / wavelength * np.sin(theta / 2) theta = (np.arange(size_y) - center_y) * 0.5 / distance / 100. qy = 4 * np.pi / wavelength * np.sin(theta / 2) if has_converter and output.Q_unit != '1/A': qx = data_conv_q(qx, units=output.Q_unit) qy = data_conv_q(qx, units=output.Q_unit) xmax = np.max(qx) xmin = np.min(qx) ymax = np.max(qy) ymin = np.min(qy) # calculate edge offset in q. theta = 0.25 / distance / 100.0 xstep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0) theta = 0.25 / distance / 100.0 ystep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0) # Store all data ###################################### # Store wavelength if has_converter and output.source.wavelength_unit != 'A': conv = Converter('A') wavelength = conv(wavelength, units=output.source.wavelength_unit) output.source.wavelength = wavelength # Store distance if has_converter and detector.distance_unit != 'm': conv = Converter('m') distance = conv(distance, units=detector.distance_unit) detector.distance = distance # Store transmission output.sample.transmission = transmission # Store pixel size (mm) pixel = 5.0 if has_converter and detector.pixel_size_unit != 'mm': conv = Converter('mm') pixel = conv(pixel, units=detector.pixel_size_unit) detector.pixel_size.x = pixel detector.pixel_size.y = pixel # Store beam center in distance units detector.beam_center.x = center_x * pixel detector.beam_center.y = center_y * pixel # Store limits of the image (2D array) xmin -= xstep / 2.0 xmax += xstep / 2.0 ymin -= ystep / 2.0 ymax += ystep / 2.0 if has_converter and output.Q_unit != '1/A': xmin = data_conv_q(xmin, units=output.Q_unit) xmax = data_conv_q(xmax, units=output.Q_unit) ymin = data_conv_q(ymin, units=output.Q_unit) ymax = data_conv_q(ymax, units=output.Q_unit) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax # Store x and y axis bin centers output.x_bins = qx.tolist() output.y_bins = qy.tolist() # Units if data_conv_q is not None: output.xaxis("\\rm{Q_{x}}", output.Q_unit) output.yaxis("\\rm{Q_{y}}", output.Q_unit) else: output.xaxis("\\rm{Q_{x}}", 'A^{-1}') output.yaxis("\\rm{Q_{y}}", 'A^{-1}') if data_conv_i is not None: output.zaxis("\\rm{Intensity}", output.I_unit) else: output.zaxis("\\rm{Intensity}", "cm^{-1}") # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output
def read(self, filename=None): """ Read file """ if not os.path.isfile(filename): raise ValueError("Specified file %s is not a regular " "file" % filename) output = Data2D() output.filename = os.path.basename(filename) detector = Detector() if len(output.detector): print(str(output.detector[0])) output.detector.append(detector) data_conv_q = data_conv_i = None if has_converter and output.Q_unit != '1/A': data_conv_q = Converter('1/A') # Test it data_conv_q(1.0, output.Q_unit) if has_converter and output.I_unit != '1/cm': data_conv_i = Converter('1/cm') # Test it data_conv_i(1.0, output.I_unit) data_row = 0 wavelength = distance = center_x = center_y = None dataStarted = isInfo = isCenter = False with open(filename, 'r') as f: for line in f: data_row += 1 # Find setup info line if isInfo: isInfo = False line_toks = line.split() # Wavelength in Angstrom try: wavelength = float(line_toks[1]) except ValueError: msg = "IgorReader: can't read this file, missing wavelength" raise ValueError(msg) # Distance in meters try: distance = float(line_toks[3]) except ValueError: msg = "IgorReader: can't read this file, missing distance" raise ValueError(msg) # Distance in meters try: transmission = float(line_toks[4]) except: msg = "IgorReader: can't read this file, " msg += "missing transmission" raise ValueError(msg) if line.count("LAMBDA"): isInfo = True # Find center info line if isCenter: isCenter = False line_toks = line.split() # Center in bin number: Must subtract 1 because # the index starts from 1 center_x = float(line_toks[0]) - 1 center_y = float(line_toks[1]) - 1 if line.count("BCENT"): isCenter = True # Find data start if line.count("***"): # now have to continue to blank line dataStarted = True # Check that we have all the info if (wavelength is None or distance is None or center_x is None or center_y is None): msg = "IgorReader:Missing information in data file" raise ValueError(msg) if dataStarted: if len(line.rstrip()): continue else: break # The data is loaded in row major order (last index changing most # rapidly). However, the original data is in column major order (first # index changing most rapidly). The swap to column major order is done # in reader2D_converter at the end of this method. data = np.loadtxt(filename, skiprows=data_row) size_x = size_y = int(np.rint(np.sqrt(data.size))) output.data = np.reshape(data, (size_x, size_y)) output.err_data = np.zeros_like(output.data) # Det 640 x 640 mm # Q = 4 * pi/lambda * sin(theta/2) # Bin size is 0.5 cm # Removed +1 from theta = (i_x - center_x + 1)*0.5 / distance # / 100.0 and # Removed +1 from theta = (i_y - center_y + 1)*0.5 / # distance / 100.0 # ToDo: Need complete check if the following # convert process is consistent with fitting.py. # calculate qx, qy bin centers of each pixel in the image theta = (np.arange(size_x) - center_x) * 0.5 / distance / 100. qx = 4 * np.pi / wavelength * np.sin(theta/2) theta = (np.arange(size_y) - center_y) * 0.5 / distance / 100. qy = 4 * np.pi / wavelength * np.sin(theta/2) if has_converter and output.Q_unit != '1/A': qx = data_conv_q(qx, units=output.Q_unit) qy = data_conv_q(qx, units=output.Q_unit) xmax = np.max(qx) xmin = np.min(qx) ymax = np.max(qy) ymin = np.min(qy) # calculate edge offset in q. theta = 0.25 / distance / 100.0 xstep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0) theta = 0.25 / distance / 100.0 ystep = 4.0 * np.pi/ wavelength * np.sin(theta / 2.0) # Store all data ###################################### # Store wavelength if has_converter and output.source.wavelength_unit != 'A': conv = Converter('A') wavelength = conv(wavelength, units=output.source.wavelength_unit) output.source.wavelength = wavelength # Store distance if has_converter and detector.distance_unit != 'm': conv = Converter('m') distance = conv(distance, units=detector.distance_unit) detector.distance = distance # Store transmission output.sample.transmission = transmission # Store pixel size (mm) pixel = 5.0 if has_converter and detector.pixel_size_unit != 'mm': conv = Converter('mm') pixel = conv(pixel, units=detector.pixel_size_unit) detector.pixel_size.x = pixel detector.pixel_size.y = pixel # Store beam center in distance units detector.beam_center.x = center_x * pixel detector.beam_center.y = center_y * pixel # Store limits of the image (2D array) xmin -= xstep / 2.0 xmax += xstep / 2.0 ymin -= ystep / 2.0 ymax += ystep / 2.0 if has_converter and output.Q_unit != '1/A': xmin = data_conv_q(xmin, units=output.Q_unit) xmax = data_conv_q(xmax, units=output.Q_unit) ymin = data_conv_q(ymin, units=output.Q_unit) ymax = data_conv_q(ymax, units=output.Q_unit) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax # Store x and y axis bin centers output.x_bins = qx.tolist() output.y_bins = qy.tolist() # Units if data_conv_q is not None: output.xaxis("\\rm{Q_{x}}", output.Q_unit) output.yaxis("\\rm{Q_{y}}", output.Q_unit) else: output.xaxis("\\rm{Q_{x}}", 'A^{-1}') output.yaxis("\\rm{Q_{y}}", 'A^{-1}') if data_conv_i is not None: output.zaxis("\\rm{Intensity}", output.I_unit) else: output.zaxis("\\rm{Intensity}", "cm^{-1}") # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output
def read(self, filename=None): """ Read file """ if not os.path.isfile(filename): raise ValueError, \ "Specified file %s is not a regular file" % filename # Read file f = open(filename, 'r') buf = f.read() # Instantiate data object output = Data2D() output.filename = os.path.basename(filename) detector = Detector() if len(output.detector) > 0: print str(output.detector[0]) output.detector.append(detector) # Get content dataStarted = False lines = buf.split('\n') itot = 0 x = [] y = [] ncounts = 0 xmin = None xmax = None ymin = None ymax = None i_x = 0 i_y = -1 i_tot_row = 0 isInfo = False isCenter = False data_conv_q = None data_conv_i = None if has_converter == True and output.Q_unit != '1/A': data_conv_q = Converter('1/A') # Test it data_conv_q(1.0, output.Q_unit) if has_converter == True and output.I_unit != '1/cm': data_conv_i = Converter('1/cm') # Test it data_conv_i(1.0, output.I_unit) for line in lines: # Find setup info line if isInfo: isInfo = False line_toks = line.split() # Wavelength in Angstrom try: wavelength = float(line_toks[1]) except: msg = "IgorReader: can't read this file, missing wavelength" raise ValueError, msg #Find # of bins in a row assuming the detector is square. if dataStarted == True: try: value = float(line) except: # Found a non-float entry, skip it continue # Get total bin number i_tot_row += 1 i_tot_row = math.ceil(math.sqrt(i_tot_row)) - 1 #print "i_tot", i_tot_row size_x = i_tot_row # 192#128 size_y = i_tot_row # 192#128 output.data = numpy.zeros([size_x, size_y]) output.err_data = numpy.zeros([size_x, size_y]) #Read Header and 2D data for line in lines: # Find setup info line if isInfo: isInfo = False line_toks = line.split() # Wavelength in Angstrom try: wavelength = float(line_toks[1]) except: msg = "IgorReader: can't read this file, missing wavelength" raise ValueError, msg # Distance in meters try: distance = float(line_toks[3]) except: msg = "IgorReader: can't read this file, missing distance" raise ValueError, msg # Distance in meters try: transmission = float(line_toks[4]) except: msg = "IgorReader: can't read this file, " msg += "missing transmission" raise ValueError, msg if line.count("LAMBDA") > 0: isInfo = True # Find center info line if isCenter: isCenter = False line_toks = line.split() # Center in bin number: Must substrate 1 because #the index starts from 1 center_x = float(line_toks[0]) - 1 center_y = float(line_toks[1]) - 1 if line.count("BCENT") > 0: isCenter = True # Find data start if line.count("***")>0: dataStarted = True # Check that we have all the info if wavelength == None \ or distance == None \ or center_x == None \ or center_y == None: msg = "IgorReader:Missing information in data file" raise ValueError, msg if dataStarted == True: try: value = float(line) except: # Found a non-float entry, skip it continue # Get bin number if math.fmod(itot, i_tot_row) == 0: i_x = 0 i_y += 1 else: i_x += 1 output.data[i_y][i_x] = value ncounts += 1 # Det 640 x 640 mm # Q = 4pi/lambda sin(theta/2) # Bin size is 0.5 cm #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance # / 100.0 and #REmoved +1 from theta = (i_y-center_y+1)*0.5 / # distance / 100.0 #ToDo: Need complete check if the following # covert process is consistent with fitting.py. theta = (i_x - center_x) * 0.5 / distance / 100.0 qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0) if has_converter == True and output.Q_unit != '1/A': qx = data_conv_q(qx, units=output.Q_unit) if xmin == None or qx < xmin: xmin = qx if xmax == None or qx > xmax: xmax = qx theta = (i_y - center_y) * 0.5 / distance / 100.0 qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) if has_converter == True and output.Q_unit != '1/A': qy = data_conv_q(qy, units=output.Q_unit) if ymin == None or qy < ymin: ymin = qy if ymax == None or qy > ymax: ymax = qy if not qx in x: x.append(qx) if not qy in y: y.append(qy) itot += 1 theta = 0.25 / distance / 100.0 xstep = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) theta = 0.25 / distance / 100.0 ystep = 4.0 * math.pi/ wavelength * math.sin(theta / 2.0) # Store all data ###################################### # Store wavelength if has_converter == True and output.source.wavelength_unit != 'A': conv = Converter('A') wavelength = conv(wavelength, units=output.source.wavelength_unit) output.source.wavelength = wavelength # Store distance if has_converter == True and detector.distance_unit != 'm': conv = Converter('m') distance = conv(distance, units=detector.distance_unit) detector.distance = distance # Store transmission output.sample.transmission = transmission # Store pixel size pixel = 5.0 if has_converter == True and detector.pixel_size_unit != 'mm': conv = Converter('mm') pixel = conv(pixel, units=detector.pixel_size_unit) detector.pixel_size.x = pixel detector.pixel_size.y = pixel # Store beam center in distance units detector.beam_center.x = center_x * pixel detector.beam_center.y = center_y * pixel # Store limits of the image (2D array) xmin = xmin - xstep / 2.0 xmax = xmax + xstep / 2.0 ymin = ymin - ystep / 2.0 ymax = ymax + ystep / 2.0 if has_converter == True and output.Q_unit != '1/A': xmin = data_conv_q(xmin, units=output.Q_unit) xmax = data_conv_q(xmax, units=output.Q_unit) ymin = data_conv_q(ymin, units=output.Q_unit) ymax = data_conv_q(ymax, units=output.Q_unit) output.xmin = xmin output.xmax = xmax output.ymin = ymin output.ymax = ymax # Store x and y axis bin centers output.x_bins = x output.y_bins = y # Units if data_conv_q is not None: output.xaxis("\\rm{Q_{x}}", output.Q_unit) output.yaxis("\\rm{Q_{y}}", output.Q_unit) else: output.xaxis("\\rm{Q_{x}}", 'A^{-1}') output.yaxis("\\rm{Q_{y}}", 'A^{-1}') if data_conv_i is not None: output.zaxis("\\rm{Intensity}", output.I_unit) else: output.zaxis("\\rm{Intensity}", "cm^{-1}") # Store loading process information output.meta_data['loader'] = self.type_name output = reader2D_converter(output) return output