NAME:
   addnccal
 PURPOSE:  
   Add calibration frames for entries in the nasacam calib data base
 DESCRIPTION:
   Searches the database nasacam.calib table for entries with 
   status=UNKNOWN and attempts to generate appropriate "sub-supers"  which
   are superbias, dark, or flat frames for the given range of images. The
   constraints on nasacam flat taking (in particular) are such that these
   super frames will usually need to be combined with others nearby in time,
   thus the term "sub-supers." After generating (without interaction) what it
   can, addnccal will update the calname for the rows processed. Ordinarily
   biases are set to status "GOOD" or "PENDING" based on automatic criteria
   and flats are set PENDING prior to inspection via the tool chknccal.
 CATEGORY:
    CCD data processing
 CALLING SEQUENCE:
   addnccal
 INPUTS:

 OPTIONAL INPUT PARAMETERS:

 KEYWORD INPUT PARAMETERS:
 ABSMOMENT - A 2 element vector giving the maximum value for absolute mean and
             standard deviation of any superbias generated. This is the
             baseline test for sub super biases. Current default is
             [ 5.0, 5.0] based on a cropping region.
  CALROOT  - string giving the path to locate the calibration images referenced
             in the data base entries, and created by addnccal. 
             The current default is
             '/kolvir/data1/buie/rawfits/nasacamcal' (on kolvir).
  COLDTEMPS- An vector of 4 elements giving, respectively, the expected
             CCD detector temperature, the expected cold head temperature,
             and the maximum absolute deviations from those levels for
             individual bias or flat frames to be considered good. The NASACAM
             default is [-100.0, -160.0, 20.0, 50.0].
             This keyword is not implemented and is currently a no-op.
  CROP     - region of original image to save, [x1,x2,y1,y2]
             default is 2006 NASACAM default of [34, 2064, 2, 2046].
  DATABASE - Name of MYSQL database for calibration entries.
             The default is 'nasacam'.
  EARLIEST - string in MYSQL date format giving the earliest row in the 
             data base to process. The default is the beginning of time.
  IMGROOT  - string giving the path to locate the image files referenced
             in the data base entries. The default is 
             '/kolvir/data1/buie/rawfits' (on kolvir).
  LATEST   - string in MYSQL date format giving the latest row in the 
             data base to process. The default is the end of time.
  MASTBIAS - Absolute path to a fits file which contains a superbias
             considered good. Superbiases generated by addnccal will be
             subtracted from  this, and the mean and standard deviation
             of the resulting array is compared with MASTMOMENT. If not
             specified, this comparison is not made.
             This keyword is not implemented and is currently a no-op.
 MASTMOMENT- A 2 element vector giving the maximum absolute mean and standard
             deviation of the difference array (against MASTBIAS) for a 
             generated sub-super-bias to be considered good. MASTMOMENT is
             only meaningful if MASTBIAS is specified. The default is
             [ 300.0, 30.0]
             This keyword is not implemented and is currently a no-op.
 NOMEDIANFLAT- Flag, if set, the JUSTMEDIAN flag is not set to MKFLAT 
             when making flats. The default is to set JUSTMEDIAN,
             which is passed to mkflat and precludes noise clipping
             and averaging, when making flats.
  OLEVEL   - a 4 element vector giving the expected overscan level and
             the overscan standard deviation, and
             the maximum permitted absolute deviations from those levels for
             individual bias or flat frames to be considered good. The NASACAM
             default is [2800.0, 10.0, 300.0, 30.0].
             This keyword is not implemented and is currently a no-op.
  OVERSCAN - column overscan region to use for frame bias level,
             default is 2006 NASACAM default of [2074, 2103].
  RDNOISE  - Read noise of CCD, DN/pixel, default is 2006 NASACAM default of 
             9.0
  SCALE    - For flats, 4 element vector which, if provide, defines the 
             array dimensions that are used to scale the mean
             of the arrays before combining.  If combined in this
             manner, the arrays are combined weighted by the means.
                 [x1,x2,y1,y2]
             These coordinates apply to the pixel locations AFTER cropping.
             The default is to use the center 50% of the image but not any
             bigger than 200x200 subsection at the center.
             This keyword is passed transparently to mkflat.
  TABLENAME- Name of table in MYSQL database for calibration entries.
             The default is 'calib'. 
  TEST     - Flag, if set, addnccal prints the mkbias, mkflat, and db 
             update commands without running them. It makes the cal
             subdirectories only.
  
 OUTPUTS:
 No outputs for this procedure.

 KEYWORD OUTPUT PARAMETERS:
 COMMON BLOCKS:
 SIDE EFFECTS:
    Data base entries are updated and calibration superframes are placed in
    CALROOT. New subdirectories will generally be created in CALROOT.
    The calibration superframe outputs will follow the naming convention
    YYYYMMDD.suf
    where suf is  n.b1    for superbiases
              and n.f1.f2 for superflats
    where     n represents the ordinal of the entry for that night among
              raw calibration images of that class, and f1 and f2 are 
              positions on the NASACAM filter wheel. 
              FOR EXAMPLE, if 10 biases and 5 flats each in B,V and methane
              were taken at each twilght for 20060715, then the cal files
              would be called
              20060715.1.b1  20060715.1.11.8  20060715.1.12.2  20060715.1.12.3
              20060715.2.b1  20060715.2.11.8  20060715.2.12.2  20060715.2.12.3

 RESTRICTIONS:
   Assumes images are located in subdirectories named YYYYMMDD for each night.
   Places calibration products in paths named YYYY/MM/DD for each night.
   It currently processes data ONLY for the NASACAM CCD on the 31", and there
   is no support for dark images.
   Image frame numbers must be in the range 1-999 inclusive.
 
 PROCEDURE:
 MODIFICATION HISTORY:
   2006/7/15, Written by Peter L. Collins, Lowell Observatory
   2006/7/24, PLC, modified after adding new column startime to calib table,
                   adding "PENDING" as a value for status,
                   changes to order of bias/flat evaluation- in
                   particular, designed to make good/bad choice
                   on sub-super-bias whenever possible automatically.
   2006/07/26, PLC,added support from empty returns from mysqlquery.
   2006/07/27, PLC,general cleanup and mark unimplemented keywords in header.