Example #1
0
 def test_bias_image(self):
     ccd = MaskedCCD(self.image_file)
     overscan = makeAmplifierGeometry(self.image_file)
     ccd_mean = MaskedCCD(self.mean_image_file)
     overscan_mean = makeAmplifierGeometry(self.mean_image_file)
     for amp in ccd:
         for method in ['mean', 'row', 'func', 'spline']:
             if method == 'mean':
                 my_bias_image = imutils.bias_image(
                     ccd_mean[amp],
                     overscan_mean.serial_overscan,
                     bias_method=method,
                     **self.kwargs)
                 fracdiff = ((self.mean_bias_image.getArray() -
                              my_bias_image.getArray()) /
                             self.mean_bias_image.getArray())
                 self.assertTrue(max(np.abs(fracdiff.flat)) < 1.5e-3)
             else:
                 my_bias_image = imutils.bias_image(
                     ccd[amp],
                     overscan.serial_overscan,
                     bias_method=method,
                     **self.kwargs)
                 fracdiff = ((self.bias_image.getArray() -
                              my_bias_image.getArray()) /
                             self.bias_image.getArray())
                 self.assertTrue(max(np.abs(fracdiff.flat)) < 1e-6)
         my_bias_image = imutils.bias_image(ccd[amp],
                                            self.amp_geom.serial_overscan)
         fracdiff = (
             (self.bias_image.getArray() - my_bias_image.getArray()) /
             self.bias_image.getArray())
         self.assertTrue(max(np.abs(fracdiff.flat)) < 1e-6)
Example #2
0
def bias_subtracted_image(image, bias_image, overscan, bias_method='row'):
    # Make deep copies of image and bias image so that we can modify them.
    im_out = image.Factory(image)
    bias_sub = bias_image.Factory(bias_image)
    # Subtract overscans.
    bias_sub -= imutils.bias_image(im=bias_image,
                                   overscan=overscan,
                                   bias_method=bias_method)
    im_out -= imutils.bias_image(im=image,
                                 overscan=overscan,
                                 bias_method=bias_method)
    # Subtract remaining strucutured bias.
    im_out -= bias_sub
    return im_out
    def get_ccd_data(self, ccd, data, **kwargs):
        """Get the bias values and update the data dictionary

        Parameters
        ----------
        ccd : `MaskedCCD`
            The ccd we are getting data from
        data : `dict`
            The data we are updating

        Keywords
        --------
        slot : `str`
            The slot name
        ifile : `int`
            The file index
        nfiles_used : `int`
            Total number of files
        """
        slot = self.config.slot
        bias_type = self.get_bias_algo()
        ifile = kwargs['ifile']
        nfiles = kwargs['nfiles']

        amps = get_amp_list(ccd)
        for i, amp in enumerate(amps):
            regions = get_geom_regions(ccd, amp)
            serial_oscan = regions['serial_overscan']
            img = get_raw_image(ccd, amp)
            bimg = imutil.bias_image(img, serial_oscan, bias_method=bias_type)
            bimg_row_mean = bimg[serial_oscan].getArray().mean(1)
            key_str = "biasval_%s_a%02i" % (slot, i)
            if key_str not in data:
                data[key_str] = np.ndarray((len(bimg_row_mean), nfiles))
            data[key_str][:, ifile] = bimg_row_mean
Example #4
0
def superflat(files,
              bias_frame=None,
              outfile='superflat.fits',
              bitpix=None,
              bias_subtract=True,
              bias_method='row'):
    """
    The superflat is created by bias-offset correcting the input files
    and median-ing them together.
    """
    # Get overscan region.
    overscan = makeAmplifierGeometry(files[0]).serial_overscan
    output_images = dict()
    for amp in imutils.allAmps(files[0]):
        images = []
        for infile in files:
            image = afwImage.ImageF(infile, imutils.dm_hdu(amp))
            if bias_subtract:
                if bias_frame:
                    bias_image = afwImage.ImageF(bias_frame,
                                                 imutils.dm_hdu(amp))
                    image = bias_subtracted_image(image, bias_image, overscan,
                                                  bias_method)
                else:
                    image -= imutils.bias_image(im=image,
                                                overscan=overscan,
                                                bias_method=bias_method)
            images.append(image)
        if lsst.afw.__version__.startswith('12.0'):
            images = afwImage.vectorImageF(images)
        output_images[amp] = afwMath.statisticsStack(images, afwMath.MEDIAN)
    imutils.writeFits(output_images, outfile, files[0])
    return outfile
Example #5
0
    def bias_image_using_overscan(self, amp, overscan=None, **kwargs):
        """
        Generate a bias image containing offset values calculated from
        bias(), bias_row(), bias_func() or bias_spline(). The default bias method
        is set to bias_row() in image_utils.py. Keyword arguments can be passed
        depending on which bias method is used.

        Keyword Arguments:
        fit_order: The order of the polynomial. This only needs to be specified when
            using the 'func' method. The default is: 1.
        k: The degree of the spline fit. This only needs to be specified when using
            the 'spline' method. The default is: 3.
        s: The amount of smoothing to be applied to the fit. This only needs to be
            specified when using the 'spline' method. The default is: 18000.
        t: The number of knots. If None, finds the number of knots to use for a given
            smoothing factor, s. This only needs to be specified when using the 'spline'
            method. The default is: None.
        """
        if overscan is None:
            overscan = self.amp_geom.serial_overscan
        try:
            return imutils.bias_image(self._deep_copy(amp),
                                      overscan=overscan,
                                      **kwargs)
        except pexExcept.LSST_RUNTIME_EXCEPTION as eobj:
            raise MaskedCCDBiasImageException(
                "DM stack error generating bias " +
                "image from overscan region:\n" + str(eobj))
Example #6
0
def superflat(files,
              bias_frame=None,
              outfile='superflat.fits',
              bitpix=-32,
              bias_subtract=True):
    """
    The superflat is created by bias-offset correcting the input files
    and median-ing them together.
    """
    # Get overscan region.
    overscan = makeAmplifierGeometry(files[0]).serial_overscan
    # Use the first file as a template for the fits output.
    output = fits.open(files[0])
    for amp in imutils.allAmps(files[0]):
        images = afwImage.vectorImageF()
        for infile in files:
            image = afwImage.ImageF(infile, imutils.dm_hdu(amp))
            if bias_subtract:
                if bias_frame:
                    bias_image = afwImage.ImageF(bias_frame,
                                                 imutils.dm_hdu(amp))
                    image = bias_subtracted_image(image, bias_image, overscan)
                else:
                    image -= imutils.bias_image(image,
                                                overscan,
                                                statistic=np.median)
            images.push_back(image)
        median_image = afwMath.statisticsStack(images, afwMath.MEDIAN)
        output[amp].data = median_image.getArray()
        if bitpix is not None:
            imutils.set_bitpix(output[amp], bitpix)
    fitsWriteto(output, outfile, clobber=True)
    return outfile
Example #7
0
 def test_bias_image(self):
     ccd = MaskedCCD(self.image_file)
     for amp in ccd:
         my_bias_image = imutils.bias_image(ccd[amp],
                                            self.amp_geom.serial_overscan)
         fracdiff = (
             (self.bias_image.getArray() - my_bias_image.getArray()) /
             self.bias_image.getArray())
         self.assertTrue(max(np.abs(fracdiff.flat)) < 1e-6)
Example #8
0
def bias_subtracted_image(image,
                          bias_image,
                          overscan,
                          fit_order=1,
                          statistic=np.median):
    # Make deep copies of image and bias image so that we can modify them.
    im_out = image.Factory(image)
    bias_sub = bias_image.Factory(bias_image)
    # Subtract overscans.
    bias_sub -= imutils.bias_image(bias_image,
                                   overscan,
                                   fit_order=fit_order,
                                   statistic=statistic)
    im_out -= imutils.bias_image(image,
                                 overscan,
                                 fit_order=fit_order,
                                 statistic=statistic)
    # Subtract remaining strucutured bias.
    im_out -= bias_sub
    return im_out
Example #9
0
 def bias_image_using_overscan(self, amp, overscan=None, fit_order=1):
     if overscan is None:
         overscan = self.amp_geom.serial_overscan
     try:
         return imutils.bias_image(self[amp],
                                   overscan=overscan,
                                   fit_order=fit_order)
     except pexExcept.LSST_RUNTIME_EXCEPTION as eobj:
         raise MaskedCCDBiasImageException(
             "DM stack error generating bias " +
             "image from overscan region:\n" + str(eobj))