def apply_gain(self, trim=True): """ Apply gain (instead of ampsec scale) Parameters ---------- Returns ------- self.mspixelflat -- Modified internally """ # TODO: This is overkill when self.datasec is loaded, and this # call is made for a few of the steps. Can we be more # efficient? datasec_img = self.spectrograph.get_datasec_img(self.files[0], det=self.det) if trim: datasec_img = procimg.trim_frame(datasec_img, datasec_img < 1) if self.stack.shape != datasec_img.shape: raise ValueError('Shape mismatch: {0} {1}'.format( self.stack.shape, datasec_img.shape)) gain = self.spectrograph.detector[self.det - 1]['gain'] if self.spectrograph.detector[self.det-1]['numamplifiers'] == 1 \ and not isinstance(gain, list): gain = [gain] self.stack *= procimg.gain_frame( datasec_img, self.spectrograph.detector[self.det - 1]['numamplifiers'], gain) # Step self.steps.append(inspect.stack()[0][3]) # Return return self.stack
def apply_gain(self, force=False): """ Apply the Gain values to self.image Args: force (bool, optional): Returns: np.ndarray: copy of self.image """ step = inspect.stack()[0][3] # Check if already trimmed if self.steps[step] and (not force): msgs.warn("Gain was already applied. Returning") return self.image.copy() gain = np.atleast_1d(self.spectrograph.detector[self.det - 1]['gain']).tolist() # Apply self.image *= procimg.gain_frame(self.datasec_img, gain) #, trim=self.steps['trim']) self.steps[step] = True # Return return self.image.copy()
def nonlinear_counts(self, datasec_img=None, apply_gain=True): """ Return the ADU/DN or counts at which the detector response becomes non-linear. Args: datasec_img (`numpy.ndarray`_, optional): An image identifying the amplifier used to read each pixel in the detector data section. If provided, the returned object is an image giving the non-linear counts for each pixel. apply_gain (:obj:`bool`, optional): Apply gain in the calculation. I.e., convert the value to counts. If only a float is returned, (i.e. ``datasec_img`` is not provided), the mean of the gains for all amplifiers is used. Returns: :obj:`float`, `numpy.ndarray`_: Counts at which the detector response becomes nonlinear. If ``datasec_img`` is provided, an image of the same shape is returned with the pixel-specific nonlinear-count threshold. """ if apply_gain: gain = np.mean(self.gain) if datasec_img is None \ else procimg.gain_frame(datasec_img, self.gain) else: gain = 1. return self.saturation * self.nonlinear * gain
def nonlinear_counts(self, detector_par, datasec_img=None, apply_gain=True): """ Return the counts at which the detector response becomes non-linear. Default is to apply the gain, i.e. return this is counts not ADU Args: detector_par (:class:`pypeit.par.pypeitpar.DetectorPar`): datasec_img (np.ndarray, optional): If provided, nonlinear_counts is returned as an image. DO NOT USE THIS OPTION; IT IS NOT YET IMPLEMENTED DOWNSTREAM. apply_gain (bool, optional): Apply gain in the calculation, i.e. convert to counts If only a float is returned, (i.e. no datasec_img is provided) then the mean of the gains for all amplifiers is adopted Returns: float, np.ndarray: Counts at which detector response becomes nonlinear. If datasec_img is provided, an image with the same shape is returned """ # Deal with gain gain = np.atleast_1d(detector_par['gain']).tolist() if not apply_gain: # Set to 1 if gain is not to be applied gain = [1. for item in gain] # Calculation without gain nonlinear_counts = detector_par['saturation'] * detector_par[ 'nonlinear'] # Finish if datasec_img is not None: # 2D image nonlinear_counts = nonlinear_counts * procimg.gain_frame( datasec_img, gain) else: # float nonlinear_counts = nonlinear_counts * np.mean(gain) # Return return nonlinear_counts
def nonlinear_counts(self, detector_par, datasec_img=None, apply_gain=True): """ Return the counts at which the detector response becomes non-linear. Default is to apply the gain, i.e. return this is counts not ADU Args: detector_par (:class:`~pypeit.images.detector_container.DetectorContainer`): Detector-specific metadata. datasec_img (`numpy.ndarray`_, optional): If provided, nonlinear_counts is returned as an image. **Do not use this option**; it is not yet implemented downstream. apply_gain (:obj:`bool`, optional): Apply gain in the calculation. I.e., convert the value to counts. If only a float is returned, (i.e. ``datasec_img`` is not provided), the mean of the gains for all amplifiers is used. Returns: :obj:`float`, `numpy.ndarray`_: Counts at which the detector response becomes nonlinear. If ``datasec_img`` is provided, an image of the same shape is returned with the pixel-specific nonlinear-count threshold. """ if datasec_img is not None: raise NotImplementedError( 'Cannot accommodate pixel-specific definition of ' 'nonlinear counts.') gain = np.atleast_1d(detector_par['gain']) if apply_gain \ else np.ones(len(detector_par['gain']), dtype=float) return detector_par['saturation'] * detector_par['nonlinear'] \ * (np.mean(gain) if datasec_img is None else procimg.gain_frame(datasec_img, gain.tolist()))