def med_copy_rois(med, source_mca=0): """ This procedure copies the ROIs defined for one Mca in the Med to all of the other Mcas. Parameters: ----------- * source_mca: The index number of the Mca from which the ROIs are to be copied. This number ranges from 0 to self.n_detectors-1. The default is the first Mca (index=0). Notes: ------ The ROIs are copied by their position in energy rather than in channels. This is very useful when copying ROIs when the calibration parameters for each Mca in the Med are not identical. """ if energy == True: units = "keV" else: units = "channel" rois = med.mca[source_mca].rois off = med.mca[source_mca].offset slope = med.mca[source_mca].slope quad = med.mca[source_mca].quad clip = len(med.mca[source_mca].data) left = [] right = [] label = [] bgr_width = [] for roi in rois: left.append(calib.channel_to_energy(roi.left, offset=off,slope=slope,quad=quad,clip=clip)) right.append(calib.channel_to_energy(roi.right, offset=off,slope=slope,quad=quad,clip=clip)) label.append(roi.label) bgr_width.append(roi.bgr) for j in range(med.n_detectors): off = med.mca[j].offset slope = med.mca[j].slope quad = med.mca[j].quad clip = len(med.mca[j].data) med.mca[j].rois = [] for k in range(len(left)): l = calib.energy_to_channel(left[k],offset=off,slope=slope,quad=quad,clip=clip) r = calib.energy_to_channel(right[k],offset=off,slope=slope,quad=quad,clip=clip) roi = ROI(left=int(l),right=int(r), label=label[k], bgr_width=bgr_width[k])
def find_roi(mca, left, right, energy=False): """ This procedure finds the index number of the ROI with a specified left and right channel number. Parameters: ----------- * mca: mca object * left: Left channel number (or energy) of this ROI * right: Right channel number (or energy) of this ROI * energy: Set this flag to True to indicate that Left and Right are in units of energy rather than channel number. Output: ------- * Returns the index of the specified ROI, -1 if the ROI was not found. Example: -------- >>index = find_roi(mca, 100, 200) """ l = left r = right if (energy == True): off = mca.offset slope = mca.slope quad = mca.quad clip = len(mca.data) l = calib.energy_to_channel(l, offset=off, slope=slope, quad=quad, clip=clip) r = calib.energy_to_channel(r, offset=off, slope=slope, quad=quad, clip=clip) index = 0 for roi in self.rois: if (l == roi.left) and (r == roi.right): return index index = index + 1 return -1
def find_roi(mca, left, right, energy=False): """ This procedure finds the index number of the ROI with a specified left and right channel number. Parameters: ----------- * mca: mca object * left: Left channel number (or energy) of this ROI * right: Right channel number (or energy) of this ROI * energy: Set this flag to True to indicate that Left and Right are in units of energy rather than channel number. Output: ------- * Returns the index of the specified ROI, -1 if the ROI was not found. Example: -------- >>index = find_roi(mca, 100, 200) """ l = left r = right if (energy == True): off = mca.offset slope = mca.slope quad = mca.quad clip = len(mca.data) l = calib.energy_to_channel( l, offset=off,slope=slope,quad=quad,clip=clip) r = calib.energy_to_channel( r, offset=off,slope=slope,quad=quad,clip=clip) index = 0 for roi in self.rois: if (l == roi.left) and (r == roi.right): return index index = index + 1 return -1
def _med2xrf(med, xrf_params={}, lines=None, det_idx=0, emin=-1., emax=-1.): """ med2xrf """ # Xrf just fits one data/energy array if med.total == True: det_idx = 0 if (det_idx < 0) or (det_idx > med.n_detectors - 1): det_idx = 0 # med will always ret data, energy and cal as arrays data = med.get_data()[det_idx] en = med.get_energy()[det_idx] cal = med.get_calib_params()[det_idx] # if energy range specified, truncate if ((emin + emax) > 0.) and (emin != emax): idx = calib.energy_idx(en, emin=emin, emax=emax) data = data[idx] en = en[idx] # Make sure we have energy calib params. # use those passed in if given, otherwise # use those from med. if not xrf_params.has_key('fit'): xrf_params['fit'] = {} keys = xrf_params['fit'].keys() if 'energy_offset' not in keys: xrf_params['fit']['energy_offset'] = cal['offset'] if 'energy_slope' not in keys: xrf_params['fit']['energy_slope'] = cal['slope'] # convert energy back to chans chans = calib.energy_to_channel(en, offset=xrf_params['fit']['energy_offset'], slope=xrf_params['fit']['energy_slope']) # Create an XrfSpectrum object x = Xrf(data=data, chans=chans, params=xrf_params) #Add bgr if xrf_params.has_key('bgr'): x.init_bgr(params=xrf_params['bgr']) else: x.init_bgr() # if lines add lines if lines: x.init_lines(lines) return x
def _med2xrf(med, xrf_params={}, lines=None, det_idx=0, emin=-1.0, emax=-1.0): """ med2xrf """ # Xrf just fits one data/energy array if med.total == True: det_idx = 0 if (det_idx < 0) or (det_idx > med.n_detectors - 1): det_idx = 0 # med will always ret data, energy and cal as arrays data = med.get_data()[det_idx] en = med.get_energy()[det_idx] cal = med.get_calib_params()[det_idx] # if energy range specified, truncate if ((emin + emax) > 0.0) and (emin != emax): idx = calib.energy_idx(en, emin=emin, emax=emax) data = data[idx] en = en[idx] # Make sure we have energy calib params. # use those passed in if given, otherwise # use those from med. if not xrf_params.has_key("fit"): xrf_params["fit"] = {} keys = xrf_params["fit"].keys() if "energy_offset" not in keys: xrf_params["fit"]["energy_offset"] = cal["offset"] if "energy_slope" not in keys: xrf_params["fit"]["energy_slope"] = cal["slope"] # convert energy back to chans chans = calib.energy_to_channel( en, offset=xrf_params["fit"]["energy_offset"], slope=xrf_params["fit"]["energy_slope"] ) # Create an XrfSpectrum object x = Xrf(data=data, chans=chans, params=xrf_params) # Add bgr if xrf_params.has_key("bgr"): x.init_bgr(params=xrf_params["bgr"]) else: x.init_bgr() # if lines add lines if lines: x.init_lines(lines) return x
def _initSinglePeak(self, idx=0, guess=True): """ Init a single peak""" if self.data == None: guess = False peak = self.peaks[idx] # Don't fit peaks outside the energy range of the data chan = calib.energy_to_channel(peak.energy, offset=self.energy_offset, slope=self.energy_slope) if ((chan < self.channels[0]) or (chan > self.channels[self.nchan - 1])): peak.ignore = True # Peak fwhm. # Note fwhm flag: # 0 = Optimize FWHM # 1 = Fix FWHM to global curve # 2 = Fix FWHM to input value if (peak.fwhm_flag == 1): peak.fwhm = (self.fwhm_offset + self.fwhm_slope * num.sqrt(peak.energy)) elif (peak.fwhm_flag == 0) and (guess == True): # use the same approx for guess fwhm? peak.fwhm = (self.fwhm_offset + self.fwhm_slope * num.sqrt(peak.energy)) # Peak ampl # Note peak.ampl_factor # 0. = Optimize amplitude of this peak # >0. = Fix amplitude to this value relative # to amplitude of previous free peak # -1.0 = Fix amplitude at 0.0 if (peak.ignore == True) or (peak.ampl_factor < 0.): peak.ampl = 0. elif (peak.ampl_factor >= 0.) and (guess == True): # find the index closest to the peak energy energy = self.get_energy() del_e = num.abs(energy - peak.energy) idx = num.where(del_e == min(del_e)) idx = idx[0][0] peak.ampl = max(self.data[idx], 0.)
def _initSinglePeak(self,idx=0,guess=True): """ Init a single peak""" if self.data == None: guess = False peak = self.peaks[idx] # Don't fit peaks outside the energy range of the data chan = calib.energy_to_channel(peak.energy, offset=self.energy_offset, slope=self.energy_slope) if ((chan < self.channels[0]) or (chan > self.channels[self.nchan-1])): peak.ignore = True # Peak fwhm. # Note fwhm flag: # 0 = Optimize FWHM # 1 = Fix FWHM to global curve # 2 = Fix FWHM to input value if (peak.fwhm_flag == 1): peak.fwhm = (self.fwhm_offset + self.fwhm_slope*num.sqrt(peak.energy)) elif (peak.fwhm_flag == 0) and (guess==True): # use the same approx for guess fwhm? peak.fwhm = (self.fwhm_offset + self.fwhm_slope*num.sqrt(peak.energy)) # Peak ampl # Note peak.ampl_factor # 0. = Optimize amplitude of this peak # >0. = Fix amplitude to this value relative # to amplitude of previous free peak # -1.0 = Fix amplitude at 0.0 if (peak.ignore == True) or (peak.ampl_factor < 0.): peak.ampl = 0. elif (peak.ampl_factor >= 0.) and (guess == True): # find the index closest to the peak energy energy = self.get_energy() del_e = num.abs(energy - peak.energy) idx = num.where( del_e == min(del_e) ) idx = idx[0][0] peak.ampl = max(self.data[idx], 0.)
def med_copy_rois(med, source_mca=0): """ This procedure copies the ROIs defined for one Mca in the Med to all of the other Mcas. Parameters: ----------- * source_mca: The index number of the Mca from which the ROIs are to be copied. This number ranges from 0 to self.n_detectors-1. The default is the first Mca (index=0). Notes: ------ The ROIs are copied by their position in energy rather than in channels. This is very useful when copying ROIs when the calibration parameters for each Mca in the Med are not identical. """ if energy == True: units = "keV" else: units = "channel" rois = med.mca[source_mca].rois off = med.mca[source_mca].offset slope = med.mca[source_mca].slope quad = med.mca[source_mca].quad clip = len(med.mca[source_mca].data) left = [] right = [] label = [] bgr_width = [] for roi in rois: left.append( calib.channel_to_energy(roi.left, offset=off, slope=slope, quad=quad, clip=clip)) right.append( calib.channel_to_energy(roi.right, offset=off, slope=slope, quad=quad, clip=clip)) label.append(roi.label) bgr_width.append(roi.bgr) for j in range(med.n_detectors): off = med.mca[j].offset slope = med.mca[j].slope quad = med.mca[j].quad clip = len(med.mca[j].data) med.mca[j].rois = [] for k in range(len(left)): l = calib.energy_to_channel(left[k], offset=off, slope=slope, quad=quad, clip=clip) r = calib.energy_to_channel(right[k], offset=off, slope=slope, quad=quad, clip=clip) roi = ROI(left=int(l), right=int(r), label=label[k], bgr_width=bgr_width[k])