Lecturer: Christoffer Fremling
Jupyter Notebook Authors: Igor Andreoni, Christoffer Fremling and Cameron Hummels
This is a Jupyter notebook lesson taken from the GROWTH Summer School 2019. For other lessons and their accompanying lectures, please see: http://growth.caltech.edu/growth-school-2019.html
Learn how to perform image subtraction to discover astronomical transients from multiple consecutive images.
See GROWTH school webpage for detailed instructions on how to install these modules and packages. Nominally, you should be able to install the python modules with pip install <module>
. The external astromatic packages are easiest installed using package managers (e.g., rpm
, apt-get
).
# Import the relevant packages
import numpy as np
from astropy.io import fits #FITS files handling
import os #Call commands from outside Python
from astropy.io import ascii #Read/write ascii files
# Useful to smooth the images with a Gaussian kernel before the subtraction
from scipy.signal import convolve as scipy_convolve
from astropy.convolution import convolve
from astropy.convolution import Gaussian2DKernel
from astropy.stats import sigma_clipped_stats
from astropy.coordinates import SkyCoord
from astropy import units as u
# Background subtraction
from astropy.stats import SigmaClip
from photutils import Background2D, MedianBackground
from photutils import make_source_mask
# Plot
import matplotlib.pyplot as plt
# Running external programs
import subprocess
import shutil
In order for this jupyter notebook to function correctly, we must have some external software installed, as described above. The following step assures that these are installed properly before getting to the rest of the content of this lesson.
def test_dependency(dep, alternate_name=None):
"""
Test external dependency by trying to run it as a subprocess
"""
try:
subprocess.check_output(dep, stderr=subprocess.PIPE, shell=True)
print("%s is installed properly as %s. OK" % (dep, dep))
return 1
except subprocess.CalledProcessError:
try:
subprocess.check_output(alternate_name, stderr=subprocess.PIPE, shell=True)
print("%s is installed properly as %s. OK" % (dep, alternate_name))
return 1
except subprocess.CalledProcessError:
print("===%s/%s IS NOT YET INSTALLED PROPERLY===" % (dep, alternate_name))
return 0
dependencies = [('sextractor', 'sex'), ('SWarp', 'swarp'), ('psfex', 'PSFEx'), ('ds9', 'DS9')]
i = 0
for dep_name1, dep_name2 in dependencies:
i += test_dependency(dep_name1, dep_name2)
print("%i out of %i external dependencies installed properly.\n" % (i, len(dependencies)))
if i != len(dependencies):
print("Please correctly install these programs before continuing by following the instructions in README.md.")
else:
print("You are ready to continue.")
# remove temporary fits files in current working directory
[os.remove(f) for f in os.listdir() if f.endswith('.fits')]
# Set directory structure
cwd = os.getcwd()
proc_dir = os.path.join(cwd, 'processed')
data_dir = os.path.join(cwd, 'data')
out_dir = os.path.join(proc_dir, 'out')
if os.path.isdir(proc_dir):
shutil.rmtree(proc_dir)
os.mkdir(proc_dir)
for f in os.listdir(data_dir):
shutil.copy2(os.path.join(data_dir, f), os.path.join(proc_dir, f))
os.chdir(proc_dir)
Define the reference and science images. Open them with ds9 to give them a look.
Also, what is the size of the images in pixel? This information will be useful when we want to align the images using Swarp.
# Reference image
ref_image_name = os.path.join(data_dir, 'refimg_i.fits')
ref_image = fits.open(ref_image_name)
#Plot up the reference image
mean, median, std = sigma_clipped_stats(ref_image[0].data)
plt.figure(figsize=(8,8))
#set the scale of the image based on its statistics
plt.imshow(ref_image[0].data, vmin = median - 2*std, vmax = median + 2*std)
plt.colorbar()
plt.title('Reference image')
plt.show()
#Image size?
print("The dimension of the X axis of the reference image is ")
print(ref_image[0].header["NAXIS1"])
print("The dimension of the Y axis of the reference image is ")
print(ref_image[0].header["NAXIS2"])
The dimension of the X axis of the reference image is 1024 The dimension of the Y axis of the reference image is 2048
Let's do the same for the science image. Can you already spot the Supernova?
#Science image
sci_image_name=os.path.join(data_dir, '20120419094409p.fits')
sci_image = fits.open(sci_image_name)
#Open the images
##os.system('ds9 -zscale '+sci_image_name +' ' + ref_image_name +'&')
#Plot up the science image
mean, median, std = sigma_clipped_stats(sci_image[0].data)
plt.figure(figsize=(8,8))
#set the scale of the image based on its statistics
plt.imshow(sci_image[0].data, vmin = median - 2*std, vmax = median + 2*std)
plt.colorbar()
plt.title('Science image')
plt.show()
#Image size?
print("The dimension of the X axis of the science image is ")
print(sci_image[0].header["NAXIS1"])
print("The dimension of the Y axis of the science image is ")
print(sci_image[0].header["NAXIS2"])