;;;+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;-----------------------------------------------------------------------------
;;; PURPOSE:
;;;      Wrapper for DICAL.PRO with default values
;;;
;;; CALLING SEQUENCE:
;;;	DIsdc_calpipe, inFn $
;;;                   , radfn $
;;;                   , iffn $
;;;                   , REVRADFN=revradfn $
;;;                   , REVIFFN=reviffn $
;;;		      , DNFN=dnfn $
;;;                   , CalibDir=argCalibDir $
;;;                   , CalibSqlHosts=CalibSqlHosts $
;;;                   , CalibSqlUsers=CalibSqlUsers $
;;;                   , CalibSqlPasss=CalibSqlPasss $
;;;                   , OVERRIDEDARKMODEL=OVERRIDEDARKMODEL $
;;;                   , /VERBOSE $
;;;                   , /DEBUG
;;;
;;; REQUIRED INPUTS:
;;;	inFn - The filename of the RAW image to calibrate
;;;
;;; OUTPUTS:
;;;	radFn - OPTIONAL - The filename of a FITS output image, RADIANCE units
;;;
;;;      iffn - Filename of FITS output image, I/F units
;;;
;;; DEFAULTS:
;;;
;;;   - SEE "modules.xyz = ..." BELOW
;;;
;;;   - /USEDARKMODEL is passed to DIcal
;;;
;;;     - override with /OVERRIDEDARKMODEL
;;;
;;;   - Uses ./dical.config for "di_calib" database access
;;;     - dical.config is an IDL save file containing
;;;         info as to 
;;;     - Builds dical.config if none exists before calling DIcal
;;;       - Uses following defaults for CalibDir & CalibSql* arguments 
;;;         if they ar not otherwise specified:
;;;
;;;         CalibDir      = '/home/sdcNN/sdc/www/html/'
;;;         CalibSqlHosts = [ 'localhost', 'sdc...', 'disdc...']
;;;         CalibSqlUsers = 'sdc... ' (for all three hosts)
;;;         CalibSqlPasss = ''        (for all three hosts)
;;;
;;; OPTIONAL INPUT KEYWORDS:
;;;
;;;   REVRADFN=revradfn 
;;;     - Filename of FITS output image, reversible RADIANCE
;;;
;;;   REVIFFN=reviffn
;;;     - Filename of FITS output image, reversible I/F
;;;
;;;   CalibDir=CalibDir 
;;;     - Local top of local WWW server DocumentRoot
;;;     - N.B. Does not override existing dical.config
;;;
;;;   CalibSqlHosts=CalibSqlHosts 
;;;     - Host(s) to query for calib files & values
;;;     - N.B. Does not override existing dical.config
;;;
;;;   CalibSqlUsers=CalibSqlUsers 
;;;     - User(s) to connect to Host(s)
;;;     - N.B. Does not override existing dical.config
;;;
;;;   CalibSqlPasss=CalibSqlPasss 
;;;     - Password(s) to connect to Host(s)
;;;     - N.B. Does not override existing dical.config
;;;
;;;   /OVERRIDEDARKMODEL 
;;;     - Do not pass /USEDARKMODEL to DIcal
;;;
;;;   /VERBOSE 
;;;     - Pass /VERBOSE to DIcal
;;;
;;; EXAMPLE:
;;;      IDL> DIcal,'in.fit',radFn='rad.fit',/VERBOSE
;;;
;;;      IDL> disdc_calpipe $
;;;           , '/.../mv_1_0160046821_083_v01.fit' $
;;;           , '/.../mv_1_0160046821_083_v01_cal.fit' $
;;;           , verbose=verbose
;;;
;;; PROCEDURES USED (i.e. called directly!):
;;;	DIcal - Calibrate a Deep Impact image
;;;
;;; MODIFICATION HISTORY:
;;;   2005-04-05  B. Carcich     Created
;;;   2005-05-28  M. Desnoyer    Added reversible I/F output and set up to use
;;;                              Despiking and MTF/Deconvolution
;;;   2005-06-16  M. Desnoyer    Added gap filling and changed despiking sigma value
;;;   2005-09-29  M. Desnoyer    Added DN output
;;;
;;;-----------------------------------------------------------------------------
;;;-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro DIsdc_calpipe, inFn $
                 , radfn $
                 , iffn $
                 , REVRADFN=revradfn $
                 , REVIFFN=reviffn $
		 , DNFN=dnfn $
                 , CalibDir=argCalibDir $
                 , CalibSqlHosts=CalibSqlHosts $
                 , CalibSqlUsers=CalibSqlUsers $
                 , CalibSqlPasss=CalibSqlPasss $
                 , OVERRIDEDARKMODEL=OVERRIDEDARKMODEL $
                 , VERBOSE=verbose $
                 , debug=debug

  doverbose = keyword_set(verbose)
  dodebug = keyword_set(debug)
  ;;; Do error handling 
  calerror=0L
  if not dodebug then CATCH, calerror

  IF calerror NE 0L THEN BEGIN
    ;; Throw an error from here if an error causes things to stop & catch here
    CATCH, /CANCEL
    if size(inFn,/type) eq size('',/type) then begin
      s = string(inFn) 
    endif else begin
      s = 'UNKNOWN INPUT FILE'
    endelse
    if doverbose then begin
      help,/trace
      help,/st,!error_state
    endif
    message, /continue, s+': '+!ERROR_STATE.MSG + '; returning ...'
    message, /reset
    return
  ENDIF

  if doverbose then print, 'Starting calibration of ' + inFn

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; Defaults

  USEDARKMODEL = 0	;; Force the theoretical dark model to be used
  SPIKEMED = 1		;; Force despiking to use median for rejection
  SPIKESIG = 3.0	;; Standard devation used in despiking

  modules = {activeModules}
  
  modules.tempVolt = 0B	;;; DO NOT (re)-calibrate temps and voltages
  modules.flagSat = 1B	;;; DO flag saturated pixels
  modules.bitWeight = 0B ;;; DO NOT correct for uneven bit weighting
  modules.linDN = 1B	;;; DO Linearize DN values
  modules.xTalk = 1B	;;; DO Remove electrical crosstalk
  modules.dark = 1B	;;; DO Subtract dark frame
  modules.gain = 0B	;;; DO NOT Normalize quadrant gain
  modules.flat = 1B	;;; DO Normalize by flat field
  modules.desmear = 1B	;;; DO Remove frame transfer smear
  modules.badPixs = 1B	;;; DO Flag bad pixels
  modules.fillGaps = 1B	;;; DO Fill in small data gaps
  modules.despike = 1B	;;; DO Remove spikes caused by cosmic ray hits
  modules.denoise = 0B	;;; DO NOT Remove random gaussian noise
  modules.geom = 0B	;;; DO NOT Perform rubbet sheet geometric calibration
  modules.mtf = 1B	;;; DO Adjust for point spread by applying a convolution 

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; Default overrides based on input arguments

  if keyword_set(OVERRIDEDARKMODEL) then USEDARKMODEL=0

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; Calibration file configuration file dical.config:
  ;;;
  ;;; - If it does not already exist

  config = getdicalconfig()
  if config.calibdir eq '' and not ptr_valid(config.sqlservers) then begin

    if keyword_set(verbose) then begin
      message,/continue,'Generating new file dical.config ...'
      message,/reset
    endif

    ;;; Get hosts, users and passwords from command line

    if n_elements(argCalibDir) gt 0 then calibdir = [argCalibDir] $
    else calibdir = '/home/sdcNN/sdc/www/html/'

    if n_elements(CalibSqlHosts) gt 0 then hosts = [CalibSqlHosts] $
    else begin
      ;;; - defaults
      hosts = [ 'localhost' $
              , 'disdc...' $
              , 'sdc...' $
              ]
    endelse

    if n_elements(CalibSqlUsers) gt 0 then usrs = [CalibSqlUsers] $
         ;;; - defaults
    else usrs = ['sdc...']

    if n_elements(CalibSqlPasss) gt 0 then passs = [CalibSqlPasss] $
         ;;; - defaults
    else passs = ['']

    ;;; - find the count of each

    nh = n_elements(hosts)
    nu = n_elements(usrs)
    np = n_elements(passs)

    ncount = max([ nh, nu, np])

    ;;; Create the structures last to first

    ptr = ptr_new()
    sqls = {sqlserver}

    for i=ncount-1L,0L,-1L do begin
      sqls.host = hosts[i lt nh ? i : (nh-1)]
      sqls.usr = usrs[i lt nu ? i : (nu-1)]
      sqls.pass = passs[i lt np ? i : (np-1)]
      sqls.next = ptr
      ptr = ptr_new( sqls)
      if keyword_set(verbose) then help,/st,sqls
    endfor

    setDICalConfig, { calibdir: CalibDir $
                    , sqlservers: ptr $
                    }
  endif

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; Call DIcal
  ;;; - /SILENT:  Pass error back to here
  ;;; - MODULES:  Use modules as determined above
  ;;; - USEDARKMODEL:  Default 1 unless overridden above (/OVERRIDEDARKMODEL)
  ;;; - SPIKEMED: Force despiking to use the median

  DIcal, inFn $
    , RADFN=radfn $
    , IFFN=iffn $
    , REVRADFN=revradfn $
    , REVIFFN=reviffn $
    , DNFN=dnfn $
    , VERBOSE=verbose $
    , /SILENT $
    , MODULES=modules $
    , USEDARKMODEL=USEDARKMODEL $
    , SPIKEMED=SPIKEMED $
    , SPIKESIG=SPIKESIG

  return

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; DIcal arguments from 2005-05-26 version
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ;;;   CHOSEN_ONLY=chosen_only, BADPIXS=badpixs, DESPIKE=despike, $
  ;;;	FILLGAPS=fillgaps, DENOISE=denoise, GEOM=geom, MTF=mtf, $
  ;;;	DESMEARALG=desmearalg, DARKFN=darkfn, FLATFN=flatfn, $
  ;;;	MAXGAPSIZE=maxgapsize, USEDARKMODEL=usedarkmodel, $
  ;;;	COMPRESSFN=compressfn, ADCFN=adcfn, BADPIXSFN=badpixsfn, $
  ;;;	GEOMFN=geomfn, MTFFN=mtffn, VISCONSTFN=visconstfn, LINDNFN=lindnfn, $
  ;;;	SPECFN=specfn, CONSTMAPFN=constmapfn, GAINFN=gainfn, SPIKESIG=spikesig,$
  ;;;	SPIKEITER=spikeIter, SPIKEBOX=spikebox, SPIKEMED=spikemed, $
  ;;;	BADPIXSINTERP=badpixsinterp, MISSINGINTERP=missinginterp, $
  ;;;	HLUTFN=hlutfn, COMPRESSMETH=compressmeth, MTFALG=mtfalg, MTFPARAM=mtfparam
  ;;;
  ;;; OPTIONAL INPUT KEYWORDS:
  ;;;	DNFN - Filename of the calibrate file in DN
  ;;;	RADFN - Filename of the calibrated file in radience units to output
  ;;;	REVRADFN - Filename of the reversibly calibrated file in radiance units
  ;;;		to output
  ;;;	IFFN - Filename of the calibrated file in I/F units to output. RADFN
  ;;;		and/or IFFN must be set
  ;;;	REVIFFN - Filename of the reversibly calibrated file in I/F units to
  ;;;		output
  ;;;	VERBOSE - If applied then status messages will be printed
  ;;;	SILENT - If flagged, then error message won't be displayed
  ;;;	FORCE - If flagged then an error in a calibration step won't abort
  ;;;		the calibration
  ;;;	MODULES - A activeModules structure specifying which modules to use.
  ;;;		This allows complete control of what modules are applied 
  ;;;		(Any module whose structure element is flagged is applied)
  ;;;	CHOSEN_ONLY - Only apply those modules that are explicity chosen are
  ;;;		applied. This can be used to apply badPixs, despike, fillGaps,
  ;;;		denoise, geom, mtf, desmear, dark frame and flat field
  ;;;		operations only.
  ;;;	BADPIXS - Applies the module to interpolate over bad pixels.  
  ;;;	DESPIKE - Applies the module to despike the image.
  ;;;	FILLGAPS - Causes gaps in data whose size is less than MAXGAPSIZE 
  ;;;		(default=9999) to be interpolated over.
  ;;;	DENOISE - Applies the module that tries to remove random gaussian noise
  ;;;	GEOM - Applies a geometric rubber sheet correction
  ;;;	MTF - Applies a convolution filter to compensate for MTF and scattered
  ;;;		light
  ;;;	DESMEARALG - Determines which desmearing algorithm to use (VIS only)
  ;;;		0 - No desmearing occurs
  ;;;		1 - Remove smear using the POC values (default for modes 1-6,9)
  ;;;		2 - Remove smear using column averaging (default for modes 7&8)
  ;;;	DARKFN -  Filename of the dark FITS file to sutract from the image.
  ;;;		If this is not set, then a dark frame is found based on the 
  ;;;		USEDARKMODEL keyword. Setting this keyword is equivilant to 
  ;;;		explicity choosing the the dark subtraction operation.
  ;;;	FLATFN - Filename of the flat FITS file to divide the image by. If
  ;;;		this is not set, the default flat field is used. Setting this
  ;;;		keyword is equivilant to explicity choosing the flat field
  ;;;		compensation module
  ;;;	MAXGAPSIZE - The maximum size of a data gap to fill if the Fill gap
  ;;;		module is run.
  ;;;	USEDARKMODEL - If flagged then the dark frame to subtract is found
  ;;;		by using a pre-calculated dark model at every pixel. 
  ;;;		Explicity setting this keyword to any value is equivilant of
  ;;;		choosing the dark subtraction module.
  ;;;	COMPRESSFN - filename of the decompression LUT to use
  ;;;	ADCFN - filename of the bit weighting correction LUT to use
  ;;;	BADPIXSFN - filename of the bad pixel map to use
  ;;;	GEOMFN - filename of the geometric tie points to use
  ;;;	LINDNFN - filename of the linearization polynomial to use
  ;;;	MTFFN - filename of the convolution filter to use
  ;;;	SPECFN - filename of the spectral map to use
  ;;;	CONSTMAPFN - filename of the ST calibration constant map to use
  ;;;	VISCONSTFN - filename of the calibration constant to use
  ;;;	GAINFN - filename of the gain maps to use
  ;;;	SPIKESIG - threshold for the despiking algorithm in distance from mean
  ;;;		in standard deviations
  ;;;	SPIKEITER - maximum number of iterations for the despking routine
  ;;;	SPIKEBOX - How big a box to use to calculate statistics for despiking
  ;;;	SPIKEMED - Should despiking use Median? Otherwise it will use mean
  ;;;	BADPIXSINTERP - Should the bad pixels be interpolated over.
  ;;;	MISSINGINTERP - Should missing data be interpolated over.
  ;;;	HLUTFN - The filename of the H lambda lookup table for IR images
  ;;;	COMPRESSMETH - The method used to distribute DN values in decompression
  ;;;		1 - average value
  ;;;		2 - gaussian distribution
  ;;;		3 - random uniform distribution
  ;;;	MTFALG - Algorithm used for deconvolution 
  ;;;		1 - constrained least squares
  ;;;		2 - Richardson-Lucy with wavelet noise suppression
  ;;;	MTFPARAM - Algorithm specific parameter for the deconvolution algorithm
  ;;; [...]
  ;;; MODIFICATION HISTORY:
  ;;;   2004-05-25  M. Desnoyer    Created
  ;;;   2005-04-05  B. Carcich     Modified to do check_FITS,...,/SILENT
  ;;;   2005-04-12  M. Desnoyer    Removed call to compile routines
  ;;;   2005-05-26  M. Desnoyer    Added new options to despiking on deconvolution

end