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)
Beispiel #2
0
def mean_overscans(imagefile, nexp=7, plots=None, verbose=False):
    if nexp is None:
        nexp = 7
    result = [
    ]  # decay constant, amplitude, flux of image, residue of first overscan, sensor number, segment, run number
    for segment in range(1, 16 + 1):
        header = getheader(imagefile)
        info = [
            header['LSST_NUM'], 'Segment {}'.format(segment),
            header['RUNNUM'].split()[0]
        ]
        image_untrimmed = ImageF(imagefile, segment)
        amp = makeAmplifierGeometry(imagefile)

        flat_mean = np.mean(
            imutils.trim(image_untrimmed, imaging=amp.imaging).array[:, -1])
        # mean value of the last column of the image

        image = imutils.trim(image_untrimmed, imaging=amp.serial_overscan)
        overscans_mean = [
            np.mean([image.array[i, j] for i in np.arange(len(image.array))])
            for j in range(len(image.array[0]))
        ]
        bias = np.mean(overscans_mean[5:-2])
        over_mean_subfit = overscans_mean[:nexp]
        params, cov = curve_fit(exp_fit,
                                np.arange(nexp),
                                over_mean_subfit,
                                p0=(10, 10, 20000),
                                bounds=([.1, 0, 0], [20, 300, 50000]))
        residue = params[1] / (flat_mean - bias)

        result.append([params[0], params[1], flat_mean, residue, *info])

        if verbose:
            print('Segment {seg}:\n  Decay    : {p[0]:<10.03g} pixels\n  Amplitude: {p[1]:<10.03g} '\
                    'ADU\n  Offset   : {p[2]:<10.03g} ADU'.format(seg=segment, p=params))
        if plots is None:
            continue
        fig = plt.figure(figsize=(10, 10))
        plt.plot(over_mean_subfit, ls='none', marker='.')
        xfit = np.linspace(0, nexp - 1, 50)
        plt.plot(xfit, [exp_fit(x, *params) for x in xfit])
        plt.title(
            'Superflat Mean Serial Overscan in {0} {1} Run {2}'.format(*info))
        plt.figtext(0.5,0.5,('Decay constant: {p[0]:.03g} pixels \nAmplitude: {p[1]:.03g}'\
                    ' ADU\nImage flux: {0:.00f} ADU\nResidue in first overscan pixel: {1:.03%}').format(flat_mean,residue,p=params))
        plt.ylabel('ADU')
        plt.xlabel('Pixel')
        plt.legend(['Data', 'Fit'])
        fig.patch.set_facecolor('white')
        plt.savefig(('{0}/{1}_{2}_run{3}.png'.format(plots,
                                                     *info)).replace(" ", ""))
        plt.close(fig)

    return result
Beispiel #3
0
def flats(imagefile, verbose=False):
    result = {}
    ccd = makeAmplifierGeometry(imagefile)
    for segment in range(1, 16 + 1):
        image_untrimmed = ImageF(imagefile, segment)  #imutils.dm_hdu(segment))
        amp = ccd[segment]
        flatmean = imutils.mean(imutils.unbias_and_trim \
                (image_untrimmed, overscan=ccd.serial_overscan, imaging=ccd.imaging))
        result[segment] = flatmean
    return result
 def test_bias_row(self):
     ccd = MaskedCCD(self.image_file)
     overscan = makeAmplifierGeometry(self.image_file)
     for amp in ccd:
         # Unmasked image
         br_i = imutils.bias_row(ccd[amp].getImage(),
                                 overscan.serial_overscan)
         # Masked image
         br_m = imutils.bias_row(ccd[amp], overscan.serial_overscan)
         for ii in range(2022):
             self.assertEqual(br_i(ii), br_m(ii))
 def test_bias_spline(self):
     ccd = MaskedCCD(self.image_file)
     overscan = makeAmplifierGeometry(self.image_file)
     for amp in ccd:
         # Unmasked image
         bs_i = imutils.bias_spline(ccd[amp].getImage(),
                                    overscan.serial_overscan)
         # Masked image
         bs_m = imutils.bias_spline(ccd[amp], overscan.serial_overscan)
         ny = len(bs_i)
         for ii in range(ny):
             self.assertEqual(interpolate.splev(ii, bs_i),
                              interpolate.splev(ii, bs_m))
 def test_stack(self):
     ccd = MaskedCCD(self.image_file)
     overscan = makeAmplifierGeometry(self.image_file)
     for method in ['mean', 'row', 'func']:
         corrected = []
         for image in ccd.values():
             corrected.append(
                 imutils.unbias_and_trim(image,
                                         overscan.serial_overscan,
                                         bias_method=method,
                                         **self.kwargs).getImage())
         stacked = imutils.stack(corrected)
         imarr = stacked.getArray()
         if method == 'mean':
             self.assertTrue(max(np.abs(imarr.flat)) < 2)
         else:
             self.assertTrue(max(np.abs(imarr.flat)) < 1e-6)
 def test_unbias_and_trim(self):
     ccd = MaskedCCD(self.image_file)
     overscan = makeAmplifierGeometry(self.image_file)
     for amp in ccd:
         for method in ['mean', 'row', 'func', 'spline']:
             image = imutils.unbias_and_trim(ccd[amp],
                                             overscan.serial_overscan,
                                             bias_method=method,
                                             **self.kwargs)
             imarr = image.getImage().getArray()
             if method == 'mean':
                 self.assertTrue(max(np.abs(imarr.flat)) < 2)
             else:
                 self.assertTrue(max(np.abs(imarr.flat)) < 1e-6)
                 #
                 # Test of corresponding MaskedCCD method.
                 #
                 image = ccd.unbiased_and_trimmed_image(
                     amp, overscan.serial_overscan, **self.kwargs)
                 imarr = image.getImage().getArray()
                 self.assertTrue(max(np.abs(imarr.flat)) < 1e-6)
    def stack(self,
              sensor_id,
              infiles,
              mask_files,
              gains,
              binsize=1,
              bias_frame=None,
              exps=['0.27', '0.54', '1.60', '2.40']):
        for exp in exps:

            all_amps = imutils.allAmps(infiles[0])
            #ptc_stats = dict([amp, ([],[])) for amp in all_amps])
            exposure = []
            bitpix = None
            overscan = makeAmplifierGeometry(infiles[0]).serial_overscan

            mean_stack = fits.open(infiles[0])
            var_stack = fits.open(infiles[0])
            sum_stack = fits.open(infiles[0])
            median_stack = fits.open(infiles[0])
            for amp in imutils.allAmps(infiles[0]):
                print 'on amp number', amp
                images = afwImage.vectorImageF()
                for idx, infile in enumerate(infiles):
                    print infile
                    ccd = MaskedCCD(infile,
                                    mask_files=(),
                                    bias_frame=bias_frame)
                    image = ccd.unbiased_and_trimmed_image(amp)

                    if idx == 0:
                        fratio_im = afwImage.MaskedImageF(image)
                        fratio_im = fratio_im.getImage().getArray()
                    image_array = image.getImage().getArray()
                    fratio = np.mean(
                        fratio_im[50:fratio_im.shape[0] - 50,
                                  50:fratio_im.shape[1] - 50]) / np.mean(
                                      image_array[50:image_array.shape[0] - 50,
                                                  50:image_array.shape[1] -
                                                  50])
                    image = afwImage.MaskedImageF(image).getImage()
                    image *= fratio
                    images.push_back(image)

                    meanstack_image = afwMath.statisticsStack(
                        images, afwMath.MEAN)
                    varstack_image = afwMath.statisticsStack(
                        images, afwMath.VARIANCE)
                    sumstack_image = afwMath.statisticsStack(
                        images, afwMath.SUM)
                    medianstack_image = afwMath.statisticsStack(
                        images, afwMath.MEDIAN)

                    mean_stack[amp].data = meanstack_image.getArray()
                    var_stack[amp].data = varstack_image.getArray()

                    sum_stack[amp].data = sumstack_image.getArray()
                    median_stack[amp].data = medianstack_image.getArray()

                    if bitpix is not None:
                        imutils.set_bitpix(output[amp], bitpix)

            fitsWriteto(mean_stack,
                        'mean_image_' + exp + '.fits',
                        clobber=True)
            fitsWriteto(var_stack, 'var_image_' + exp + '.fits', clobber=True)
            fitsWriteto(sum_stack, 'sum_image_' + exp + '.fits', clobber=True)
            fitsWriteto(median_stack,
                        'median_image_' + exp + '.fits',
                        clobber=True)