TABLE OF CONTENTS
1. OVERVIEW
2. USAGE
3. INPUT
4. NAMELIST
5. ENVINROMENTAL VARIABLES
6. MODULES
7. OUTPUT


1. OVERVIEW.

   Perl script flatfield.pl creates a flatfield image for a set of images
   and applies it to the images in the set.

   Perl script flatfield.pl runs all the modules involved in the image correction.
   The steps in running flatfield.pl are the following
   1. Compute the median images of the input bcd's (optionally)
   2. Detect and mask bright objects in the bcd's (optionally)
   3. Stack up all the bcd's and find the median for each
      array pixel position. (Optionally a trimmed average can be found.)
      The median is normalized to the average of 1 and
      saved as the flatfield image.
   4. Compute the median image of the flatfield image produced in step 3.
   5. Apply the flatfield or the median flatfield to the input bcd's.
   6. Optionally this procedure can be done separately for each scan leg for MIPS data.

   
2. USAGE
   Assuming that mopex has been installed, run
       flatfield.pl -n <namelist_filename> 
   where the namelist <namelist_filename> should be in the subdirectory specified
   by the SIRTF_CDF environmental variable (cdf by default).


3. INPUT
  
  The input for pointing_refine.pl:
Input File	    Default Name      Set in Namelist		Set on Commandline     Required
------------------------------------------------------------------------------------------------
namelist for	    flatfield.nl       N/A			    	-n          	y
flatfield.pl                


List of 	    image_stack.txt    IMAGE_STACK_FILE_NAME	        -I		y
input images

permamently damaged         -          PMASK_FILE_NAME			-M		n
pixels mask image

list of DCE status 	    -          DCE_STATUS_MASK_LIST		-d		n    
mask images (Dmask)

  Quality mask images can be used in the processing.
  Two kinds of mask images can be used: permamently damaged pixels masks (Pmask) 
  and DCE status masks (Dmask). Normally there will be a single Pmask for a set of input images.
  If the Pmask name is not set it will not be used.
  If the Dmask List name is not set it will not be used.
  The fatal bit pattern are now set in the portion of the namelist pertaining the script itself,
  DCE_Status_Fatal_BitPattern is for Dmasks, Pmask_Fatal_BitPattern is for Pmasks.

4. NAMELIST

  The  namelist file contains the namelists for all modules and some 
  input parameters for the script itself. It is expected to be found in the directory
  specified by enironment variable SIRTF_CDF(see below).

  Command line options take precedence over namelist options.

  Here is an excerpt from a namelist file that pertains to script flatfield.pl

	scan_leg = 1
	run_medfilter = 1
	run_detect = 1
	mask_bright = 1
	use_median = 1
	create_flat = 1
	run_medfilter_flat = 1
	use_median_flat = 1
	apply_flat = 1

	delete_intermediate_files = 0

	OUTPUT_DIR = output_flat_detect
	FLAT_DIR = Flat

	IMAGE_STACK_FILE_NAME = ImageList.txt
	PMASK_FILE_NAME = /stage/ssc-pipe/davidm/MIPS/cal/mips24_pmask.fits
	DCE_STATUS_MASK_LIST = MaskList.txt

	DCE_Status_Mask_Fatal_BitPattern = 15107
	PMask_Fatal_BitPattern =  25600


  The script executes only the modules that are set to 1. 
    Module Name        Namelist setting
   ______________________________________________________________________________________________
    medfilter          run_medfilter
	(on the input images)
    medfilter          run_medfilter_flat
	(on the flat image)
    detect             run_detect
	flat               create_flat
    imlistarith        apply_flat


  All the results will be written in OUTPUT_DIR, which can be
  specified as a relative path, as above, or as an absolute path, 
  e.g. /ssc/pipe/davidm/band4.

  Switch  NICE =1 is used to run all the modules called by the script with "nice +19".

  If delete_intermediate_files switch is set all the background images
  produced by medfitler  will be deleted.

5. ENVIRONMENTAL VARIABLES

  The following environment variables are used in the script.
  SIRTF_BIN, SIRTF_CAL, SIRTF_ANC, SIRTF_CDF.
  SIRTF_CDF specifies the location of the namelist file, by default it's ./cdf.
  Also the log files for each module are written into that directory

  SIRTF_CAL specifies the location of the PRF image used by apex_1frame.pl,
  by default it's ./cal.

  SIRTF_BIN specifies the location of the executables, it has no useful 
  for you default, you would have to set it to wherever you copy the executables to.

  SIRTF_ANC specifies the location of the error header files included in the package.
  The default is not useful. 

  Also make sure that the place where you copy the libraries is included in your
  LD_LIBRARY_PATH.

  Make sure PERL_PATH is set to point to the location where your perl is installed.

6. MODULES
   Here is a short description of the modules.
  
   Module medfilter performs estimation of the background of the input images and outputs
   background images and background subtracted images. The background is estimated by computing a 
   median of the pixels in the sliding window with the size Window_X and Window_Y,
   with N_Outliers_Per_Window number of highest pixels being excluded from the window.
   The parameters are set in the namelist:
	&MEDFILTER
	  Window_X = 21,
	  Window_Y = 21,
	  N_Outliers_Per_Window = 100,
	&END
   The same module computes the median image flat_median.fits of the flatfield image flat.fits
   using the same namelist block.

   Module detect detects pixels in the background subtracted images
   above the threshold set in the namelist. The threshold is in terms of 
   the number of standard deviations in the image. The product of this module
   is a set of mask fits files, masking the bright pixels in the input images.
	&DETECT
	  Detection_Max_Area         = 90000,
	  Detection_Min_Area         = 0,
	  Detection_Threshold        = 3,
	&END
   Module detect should be run at least once if mask_bright = 1. In this case
   the pixels masked by detect are excluded from computing the flatfield image by module flat.


   Module flat stacks up all the bcd's and find the median or trimmed average for each
   array pixel position. The two modes of the module flat are set in the namelist:
   Mode =  median(default), or Mode = trim_ave.
   Namelist parameters Top and Bottom are used for the Mode = trim_ave only. 
   They specify the fraction of the pixels to be trimmed above and below the mean,
   correspondingly, to compute the trimmed average.
	&FLAT
	  Mode = median,
	  Top = 0.,
	  Bottom = 0.,
	&END
	The main product of this module is flat.fits. It also produces two axcillary 
	products: the uncertainty image flat_unc.fits and the coverage image flat_cov.fits.

   Module imlistarith divides the input images either by flat.fits, if use_median_flat = 0,
   of flat_median.fits if use_median_flat = 1.

  If scan_leg = 1, then the input list is split according to the 
  fits header keyword SCLEGNUM into a separate list for each 
  scan leg number. For each scan leg a flat is computed and applied.
  The scan leg number # along with the string "scleg" are prepended
  to the names of the flatfield images:
  scleg_#_flat.fits, scleg_#_flat_unc.fits, scleg_#_flat_cov.fist, scleg_#_flat_median.fits.

7. OUTPUT
   Results of intermediate processing steps are written in subdirectories
   of the output directory specified in the namelist (OUTPUT_DIR).
   However, if these directories are given with a full path, they are not treated

   as subdirectories of OUTPUT_DIR.


   Namelist Keyword         Default Value          Module(s)
   ______________________________________________________________________________________________
   MEDFILTER_DIR            Medfilter              medfilter
   DETECT_DIR               Detect                 detect
   FLAT_DIR                 Flat                   flat
   CORRECT_DIR              Correct                imlistarith


   David Makovoz