Beispiel #1
0
def make_background_image():
    """Estimate background image.

    See the `IterativeKernelBackgroundEstimator` tutorial and
    documentation how it works, or the SciNeGHe 2014 proceeding
    by Ellis Owen et al.
    """
    radius = Angle(0.2, 'deg')
    r_in = Angle(0.3, 'deg')
    r_out = Angle(0.7, 'deg')
    significance_threshold = 5
    mask_dilation_radius = Angle(0.1, 'deg')
    max_iterations = 3

    hdu = fits.open(COUNTS_IMAGE)['COUNTS']
    binsz = hdu.header['CDELT2']
    images = GammaImages(counts=hdu.data, header=hdu.header)

    # TODO: we should have utility functions to initialise
    # kernels with angles so that we don't have to convert to pix here.
    source_kernel = binary_disk(radius=radius.deg / binsz)
    background_kernel = binary_ring(r_in=r_in.deg / binsz,
                                    r_out=r_out.deg / binsz)

    estimator = IterativeKernelBackgroundEstimator(
        images=images,
        source_kernel=source_kernel,
        background_kernel=background_kernel,
        significance_threshold=significance_threshold,
        mask_dilation_radius=mask_dilation_radius.deg / binsz,
    )
    print('Running background estimation ...')
    estimator.run(max_iterations=max_iterations)

    print('Writing {}'.format(MASK_IMAGE))
    estimator.mask_image_hdu.writeto(MASK_IMAGE, clobber=True)

    print('Writing {}'.format(BACKGROUND_IMAGE))
    estimator.background_image_hdu.writeto(BACKGROUND_IMAGE, clobber=True)
# Background must be provided as an ImageHDU
background_data = np.ones_like(counts_data, dtype=float)
background = fits.ImageHDU(data=background_data[0], header=flux_hdu.header)
images = GammaImages(counts=counts, background=background)

source_kernel = binary_disk(CORRELATION_RADIUS).astype(float)
source_kernel /= source_kernel.sum()

background_kernel = np.ones((5, 100))
background_kernel /= background_kernel.sum()

# *** ITERATOR ***

ibe = IterativeKernelBackgroundEstimator(
    images=images,
    source_kernel=source_kernel,
    background_kernel=background_kernel,
    significance_threshold=SIGNIFICANCE_THRESHOLD,
    mask_dilation_radius=MASK_DILATION_RADIUS)

mask, new_background = ibe.run()

flux_background_data = new_background.data / exposure_spec_cube.data[0]

flux_background = fits.ImageHDU(data=flux_background_data.value,
                                header=flux_hdu.header)

filebase = raw_input('Output file base: ')

flux_background.writeto('{0}_background.fits'.format(filebase), clobber=True)
mask.writeto('{0}_mask.fits'.format(filebase), clobber=True)
background_data=np.ones_like(counts.data, dtype=float)
background = fits.ImageHDU(data=background_data, header=counts.header)
images = GammaImages(counts=counts, background=background)

source_kernel = binary_disk(3).astype(float)
source_kernel /= source_kernel.sum()

background_kernel = np.ones((5, 50))
background_kernel /= background_kernel.sum()

# *** ITERATOR ***

ibe = IterativeKernelBackgroundEstimator(images=images,
                                         source_kernel=source_kernel,
                                         background_kernel=background_kernel,
                                         significance_threshold=4,
                                         mask_dilation_radius=3,
                                         save_intermediate_results=True
                                         )

ibe.run(filebase='TestOutput')
#n_iterations = 3
"""
# *** RUN & PLOT ***
plt.figure(figsize=(4,6))
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

plt.subplot(n_iterations+3, 1, 1)
background_hdu = counts
data = counts.data[:, 700:1400]