The following is taken from two message to Brian Warner in the CIS CCD User's Forum from Stan Moore, one of the leading CCD imagers in the US.
Part One: Specific Instructions
Here is a VERY brief course in flat fields.
Using CCDOPS, presuming that the scope/CCD is near focus (from previous session):
Part Two: Additional Insights
Flat field frames are used to correct for illumination variations in the optical system (esp vignetting), anomalies in the optical path (specs of dust on various surfaces), and sensitivity variances between pixels in the CCD. A flat field frame is basically an image of a uniformly illuminated field. The flat captures detector variations and, when normalized and divided into an image, will correct the image for such variations.
There are several ways of creating flat fields. The three most common methods used by amateurs are flat target, light box, and twilight. The flat target relies on imaging a near flat white sheet which is uniformly illuminated. The light box is a contraption with light bulbs and diffusers which you place over the aperture. Twilight flats are done by imaging a blank spot in the sky around twilight. Each of these methods has advantages and problems. The biggest problem with the target and light box is the difficulty in obtaining a truly uniform illumination. My own experience is that twilight flats are the most uniform and easiest approach.
The following pointers are based on my experience in creating and applying twilight flats. I use an SBIG ST-7 CCD with CCDOPS and MIRA software, but these instructions can be implemented for any CCD under any software with minimal translation.
Do not remove the CCD or reorient it (relative to the scope) between the time of flats and imaging. I have experimented with various alignment schemes, but none of them work very well (you can judge for yourself: take a series of flats, remove & replace the CCD, take another series - use one set to "flat field" the other or do a normalized subtraction to see the difference).
Examine and remedy any light leaks in your optical system. Look for leaks by shinning a flashlight at various parts of your scope during an exposure. I need to wrap a cloth around my JMI elect focuser because of a small leak.
The CCD should be very near proper focus. This can be a problem if you are starting the evening with a new setup. A 2 hole aperture mask can be used to focus on a planet in early twilight, but it is usually necessary to forget about taking flats that evening - keep the camera on the scope after your night's work & take your flats at the next opportunity.
You need to be cognizant of where in the sky you are imaging your flats. I quickly discovered that horizontal gradations of the sky are present in the field. The best place in the sky to avoid gradients is near zenith, slightly away from the Sun. The worst place is towards the bright sky above the Sun, which is a tempting target because of the lingering light. (see the Fall 1996 issue of CCD-Astronomy).
Twilight flats are tricky due to the short period when the sky is dim enough to not saturate the CCD yet bright enough to not show stars (although, stars can be tolerated if you are using a median combine of a sufficient number of images). I've discovered that by placing a smooth, translucent membrane over the aperture, one can start imaging the flats earlier and continue later without stars intruding. Obtain large sheets of milky plastic at art supply stores. I've had very good results using this sky/diffuser setup on my refractor, SCT, and Newt.
Exposure levels - each flat should have an avg e-count of about 60-70% full-well capacity (I aim for 10,000 on the ST-7 ABG & 30,000 on the non-ABG). Lesser exposures are noisy. Greater exposures may be affected by bleed and ABG non-linearity. Avoid extremely short exposures because the shutter action may not be completely uniform.
Take many exposures in order to reduce the noise level of the finished flat. Because the sky brightness is changing rapidly, take a batch of 3 auto-grab exposures (plus an associated dark), then examine the final image for avg value & set the exposure for the next batch accordingly. I usually take about 30 flats. Later I examine each one & reject any that have improper e-counts or obvious flaws (e.g. cosmic ray strikes). I'm usually left with about 20 good flats.
Pay attention to the dark subtract for each flat. Best to take a new dark for every 3 raw flats.
Creating the finished flat : If you only use CCDOPS, then create the finished flat by computing the MEAN of all good flats. If you use more sophisticated software, or write your own, then normalize the raw flats and compute the MEDIAN, convert the image to real number and normalize so that the avg pixel value = 1.
References: CCDA Winter 1996, M Newberry