The following example uses photutils to find sources in an astronomical image and perform circular aperture photometry on them. This example requires the following packages:
You can install the required package with either conda or pip:
conda -c astropy install photutils
pip install photutils
We start by loading an image from the bundled datasets and selecting a subset of the image. We then subtract a rough estimate of the background, calculated using the image median. In the remainder of this example, we assume that the data is background-subtracted.
import numpy as np
from photutils import datasets
hdu = datasets.load_star_image()
image = hdu.data[300:800, 300:800].astype(float)
image -= np.median(image)
import matplotlib.pylab as plt
%matplotlib inline
plt.rc('figure', figsize=(10, 10))
plt.imshow(image, cmap='gray_r', origin='lower')
Photutils supports several source detection algorithms. For this example, we use photutils.daofind to detect the stars in the image. We set the detection threshold at the 3-sigma noise level, estimated using the median absolution deviation of the image. The parameters of the detected sources are returned as an Astropy Table.
from photutils import daofind
from astropy.stats import mad_std
bkg_sigma = mad_std(image)
sources = daofind(image, fwhm=4., threshold=3.*bkg_sigma)
print(sources)
Using the list of source locations (xcentroid
and ycentroid
),
we can compute the sum of the pixel values within our apertures.
For this example we choose circular apertures with a radius of 4 pixels. The photutils.aperture_photometry
function returns an Astropy Table with the results of
the photometry:
from photutils import aperture_photometry, CircularAperture
positions = (sources['xcentroid'], sources['ycentroid'])
apertures = CircularAperture(positions, r=4.)
phot_table = aperture_photometry(image, apertures)
print(phot_table)
The sum of the pixel values within the apertures are given in the
column aperture_sum
. We now plot the image and the defined
apertures:
import matplotlib.pylab as plt
%matplotlib inline
plt.rc('figure', figsize=(10, 10))
plt.imshow(image, cmap='gray_r', origin='lower')
apertures.plot(color='blue', lw=1.5, alpha=0.5)
For more examples and details please visit the photutils documentation.