This document describes the basics of reducing flats.

Review the pipeline process first.

Before following this thread, you need to have taken flats and darks. A set of flats is needed for every filter used. For every exposure duration of the flats, you need a set of darks.

For this thread, I have the following files:
Dark-1.6s-1.fits
Dark-1.6s-2.fits
Dark-1.6s-3.fits
Dark-10s-1.fits
Dark-10s-2.fits
Dark-10s-3.fits
Dark-120s-1.fits
Dark-120s-2.fits
Dark-120s-3.fits
Flat-B-120s-1.fits
Flat-B-120s-2.fits
Flat-B-120s-3.fits
Flat-R-1.6s-1.fits
Flat-R-1.6s-2.fits
Flat-R-1.6s-3.fits
Flat-V-10s-1.fits
Flat-V-10s-2.fits
Flat-V-10s-3.fits

Here's a quick overview of the process. For each of the filters (B, V, & R in this example), do the following:

  1. Read in the darks frames for the exposure. The dark exposures must be the same as the flats for this filter.
  2. Median the darks to produce a single dark for the given exposure.
  3. Read in each of the flats for this filter.
  4. Subtract the medianed dark produced in step 2 from each of the flats.
  5. Balance the flats for variations due to things like fluctuations in the voltage to the lamps.
  6. Median the balanced flats.
  7. Write the medianed flat to file.

The tricky step is 5. See Step 5 in detail for more details.

The processing can be done in a number of ways. You can use IRAF, ftools, or many popular commercial and free packages.

I suggest using Matlab. It is perhaps not the easiest, but it is the least 'black-boxed'.

Download the Matlab script, ProcessFlats.m.

ProcessFlats.m should be treated as a guide. At the least, you need to edit the first few lines of the file.

Running the script in Matlab will produce the following files:
Flat_B.fits
Flat_R.fits
Flat_V.fits

Step 5 in detail

Slight variations in voltage to the lamps, CCD temperature, or transparency in the dome will lead to offsets in the baselines between the flats (Fig. 1).

Figure 1 (click to examine in detail)
A row of pixels for 3 flats taken with the same exposure. The mean values for each each curve in the plot are 47135, 47062, & 47007 counts. The variation is dominated by true variations in pixel-to-pixel sensitivity. But there is also a baseline shift, plus Poisson noise (plus occasional cosmic rays). For data reduction, the pixel-to-pixel sensitivity is the important signal. The baseline shift is unimportant. The Poisson noise (and cosmic rays) we need to reduce.

The variations are small, on the order of 0.1%, but significant. Consider what the median of three values is; if you have three flats that have natural Poisson variation with sigma=10 counts, but differences in baselines of 50 counts, then taking the median of the frames will simply select all the values from the flat with the intermediate baseline. The Poisson variation of 10 counts will be carried over.

But now consider if we correct for the baseline shift ahead of time. Then for each pixel, the difference between the values for the three flats will be due simply to Poisson noise (or the occasional cosmic ray). Taking the median of those values will reduce the Poisson noise by a factor of 31/2 leading to a Poisson error of about 5 counts (Fig. 2).

Figure 2 (click to examine in detail)
The same as Fig. 1, but normalised to correct for the baseline variation. The white line is the median.

How is the baseline calculated?
For a good first approximation, normalise each flat by its mean value. However, the presence of cosmic rays and other sporadic strong peaks can skew the mean, as does the presence of dead pixels.

To determine the normalisation, I prefer to fit a gaussian to the peak in the distribution of counts in each pixel. Most of the pixels will have about the same number of counts. The local distribution will often be normal. But not always, so this method is sometimes flakey or does no better than simply taking the mean.