# Optimal Subtraction Tutorial

## Description

This is a python implementation of the Zackay, Ofek, Gal-Yam image subtraction technique. It can be used as a command line tool or imported into Python directly. Given two images and their PSFs, the code can generate the proper subtraction image (D) and its zero point, the score image (S) and its zero point, the corrected score image (Scorr), and a list of possible transients (Find) appearing in the corrected score image. PSFs will be fit if none are provided, but require the user to input the FWHM and standard deviation of the background. The images will be scaled to each other using a linear fit unless the user provides the scaling parameters. The images do not have to be background subtracted but the scaling is improved if they are. The images must be aligned and have enough sources to fit a high quality PSF.

## Command Line Usage

### Required Arguments

 -N New image -R Reference image -o Output image

### Optional Arguments

 –NewPSF PSF for new image –RefPSF PSF for reference image –Beta Beta; slope of linear scaling –Gamma Gamma; y-intercept of linear scaling –NewBackground New image before background subtraction –RefBackground Reference image before background subtraction –Mode Subtraction mode [D, S, Scorr, Find] –Interactive Fit scaling parameters interactively –Verbose print IRAF output when fitting PSFs –NewNoise Standard deviation of new image background –RefNoise Standard deviation of reference image background –Flatten Fix spatial variations in background noise –Normalize normalize to image, template, or none

## Python usage

OptimalSub.py defines the class OptimalSubtraction(NewImage, RefImage, ArgsDict). The useful methods of this class are:

 D(normalize = ””) Calculate the proper subtraction image S() Calculate the score image Scorr() Calculate the corrected score image FindTransients(Threshold=3., filename=”transients.txt”) Save a list of possible transients SaveD(filename, normalize=””) Save the proper subtraction image to a file SaveS(filename) Save the score image to a file SaveScorr(filename) Save the corrected score image to a file

All of these except FindTransients store their outputs in class attributes to avoid unnecessary computation. D() is stored under D_, S() is stored under S_, etc. OptimalSubtraction takes arguments in a dictionary with the same keywords as the command line arguments.

## How to get a good subtraction

 1. Get good PSFs. This does not work at all without high quality PSFs. 2. Make sure there is no space around the edges of the images. This will confuse the scale matching 3. Align the images as well as possible 4. Coadd many frames from the same time to minimize uncertainties.