NAME: 
  mkdark
 PURPOSE: 
  Collect and combine CCD dark frames into a superdark frame
 DESCRIPTION:

  The files are assumed to be named as 'root'NNN where 'root' is the
  root of the file name that you supply and NNN is a three digit number.
  If your file name has an imbedded '.' then add it to the root.

  The specified range of files are all read in from FITS files.  Then each
  image has the overscan mean subtracted (if desired), cropped (as indicated),
  and then the input bias image is subtracted.  These images are then
  normalized to counts per second and then averaged.  The averaging
  is done with AVGCLIP.PRO which does a robust average of the image
  stack so that cosmic rays or other transient image defects are eliminated.

  When done, the resulting dark image is returned to the caller and the image
  is saved to a FITS file with the specified output filename.

 CATEGORY:
  CCD data processing
 CALLING SEQUENCE:
  mkdark,root,outsuf,start,nframes,bias,dark
 INPUTS:
  root    - Root of the file name (you must include . in the name).
  outsuf  - The suffix of the final output file.
  start   - First frame number to read (integer or long).
               Start can also be a vector of explicit frame numbers to load.
               In this case, nframes need not be specified and in fact will
               be ignored.
               Additionally, start can also be a string array containing the
               file names of all files to be read.  In this case, set nframes
               to 0 or someother innocuous integer.  Exclude is treated
               differently.  In this case, exclude is a vector of the same
               length as start and is 0 if the file is to be used, 1 if not.
  nframes - Number of frames to average.
  bias    - Name of bias frame image to subtract from each raw dark.
 OPTIONAL INPUT PARAMETERS:
 KEYWORD INPUT PARAMETERS:

   CROP     = region of original image to save, default=no cropping.
                 [x1,x2,y1,y2]

  DIGITS   - Optional input to indicate how many digits are in the suffix
                of the file name.  The default for this input is 0.  In this
                case it uses the ROBOCCD scheme which is three digits up
                to 999.  After that, it gets complicated, see numtoflist
                for more information.  If you were to give it a value of
                three you would get the same behavior except you won't get
                the ROBOCDD extension.  This is really designed for when
                you have four or more digits.

   DDIR    - Path to the raw data, default = ''

   EXCLUDE - Optional vector of image numbers that should be excluded from
                average.  Default is to include all frames.

   EXPKEY = String - FITS keyword to read to get exposure time, default = EXPTIME

   OVERSCAN = column overscan region to use for frame bias level,
                 default=no overscan subtraction.

   RDNOISE  - Read noise of CCD, e-/DN, default=10
   MINGOOD - Minimum number of frames required to be considered a good
                bias.  (Default=15)
   MAXBAD  - Maximum number of bad rows allowed per image (see gradebias)
                Default is inherited from gradebias

 OUTPUTS:
  dark - Final robust averaged dark image scaled to counts per second.
  good  - Flag, true if this collection of dark images is considered to be
            good.
 KEYWORD OUTPUT PARAMETERS:
  NGOOD - Number of frames considered to be good at the end of operation.
            Note that this will usually be >0 even if the set is marked
            bad since all process stops once the number drops below MINGOOD.
          If NOGRADE is set the value returned is -1
  IDXGOOD - Indicies into the list of files for those that were considered
              to be good
 COMMON BLOCKS:
 SIDE EFFECTS:
 RESTRICTIONS:
 PROCEDURE:
 MODIFICATION HISTORY:
  95/03/09 - Initial crude version written, Marc W. Buie, Lowell Observatory
  95/08/30, MWB, added OVERSCAN and CROP keywords
  95/11/22, MWB, add EXCLUDE keyword
  99/06/10, MWB, added EXPTIME keyword and added documentation.
  2001/02/23, MWB, added option to provide input file list.  Also, added
                     functions to make it consistent with mkbais,mkflat.
  2005/03/10, MWB, added DDIR keyword, converted bias input to string
  2006/07/14, MWB, added RDNOISE keyword
  2018/07/24, MWB, added DIGITS, MINGOOD, MAXBAD, good return and auto grading
  2018/12/17, MWB, capture failure of running avgclip
  2018/12/21, MWB, added NGOOD output keyword
  2019/05/16, MWB, added IDXGOOD output keyword
  2019/12/02, MWB, added a check to make sure there are at least mingood
                      frames at the start, otherwise, return right away
                      and mark it all as bad.
  2019/12/03, MWB, added a trap for a special failure case