FUNCTION DIcal_fillGaps, in, fitsHdr, maxGap, flags, imgCols, imgRows, BADPIX=badpixs, $
	MISSING=missing

;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Used in the DI Calibration Pipeline.
;;	Interpolates over areas in the image. The interpolated pixels can either
;;	be missing data or bad pixels
;;
;; CALLING SEQUENCE:
;;	out = DIcal_fillGaps(in, maxGap, imgCols, imgRows, /BADPIXS, /MISSING)
;;
;; REQUIRED INPUTS:
;;	in - The image to which this pipeline element is going to be applied.
;;		Image should be in floating point format
;;	fitsHdr - The FITS header of the image
;;	maxGap - The maximum dimension of a gap to fill in with interpolated
;;		 data
;;	flags - flag array to identify pixels
;;
;; OUTPUTS:
;;	RETURN - the image after going through this calibration step
;;
;; OPTIONAL INPUT KEYWORDS:
;;	imgCols - two element array where the first element is the column
;;		where the image starts and the second element is the comumn 
;;		where the image ends 
;;	imgRows - two element array where the first element is the row
;;		where the image starts and the second element is the row where
;;		the image ends 
;;	BADPIXS - Interpolate over bad pixels
;;	MISSING - Interpolate over missing data
;;
;; EXAMPLE:
;;      IDL> imgOut = DIcal_fillGaps(imgIn, maxGap,imgCols,imgRows, /BADPIXS)
;;
;; PROCEDURES USED (i.e. called directly!):
;;	dical_interp - performs interpolation
;;    	SXPAR - reads the FITS header
;;
;; MODIFICATION HISTORY:
;;   2004-05-24  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

;; Do error handling 
;CATCH, error
error=0

IF error NE 0 THEN BEGIN
	CATCH, /CANCEL
	message, 'Fill Gaps - ' + !ERROR_STATE.MSG, /NONAME
ENDIF

;; check inputs
IF max(size(in,/dimensions) NE size(flags,/dimensions)) THEN $
	message, 'Invalid inputs'

;; Default the parameters
IF n_params() LT 6 THEN BEGIN
	tmp = size(in, /dimensions)
	imgRows = [0,tmp[1]-1]
	imgCols = [0,tmp[0]-1]
ENDIF
doBad = keyword_set(BADPIXS)
doMiss = keyword_set(MISSING)

IF NOT(doBad OR doMiss) THEN return, in

;; Get the detector
det = SXPAR(fitsHdr, 'IMGH021', count=c1)

;; Make sure the header is valid
IF c1 EQ 0 THEN message, 'Invalid FITS header', /noname

;; Window the image and the flags
img = in[imgCols[0]:imgCols[1],imgRows[0]:imgRows[1]]
fl = flags[imgCols[0]:imgCols[1],imgRows[0]:imgRows[1]]

;; Identify where to interpolate
@dical_flags
mask = 0
IF doBad THEN mask = mask OR FLAG_BAD
IF doMiss THEN mask = mask OR FLAG_GAP
gaps = where((fl AND mask) NE 0,gapCnt)

;; Perform the interpolation
IF gapCnt GT 0 THEN $
	dical_interp, img, gaps, vert=(det EQ 1), maxSize=maxGap, flags=fl $
		    , Fail2DBlobs=fail2db

if fail2db then begin    ;;; find_2Dblobs.pro failed, no interpolation performed

  ;; Update the FITS header
  fxaddpar, fitsHdr, 'CLEAN', 'F'
  fxaddpar, fitsHdr, 'CLEANV', -999999L
  fxaddpar, fitsHdr, 'CLNBAD', 'F'
  fxaddpar, fitsHdr, 'CLNMISS', 'F'

endif else begin

  ;; Update the FITS header
  fxaddpar, fitsHdr, 'CLEAN', 'T'
  fxaddpar, fitsHdr, 'CLEANV', maxGap
  fxaddpar, fitsHdr, 'CLNBAD', doBad?'T':'F'
  fxaddpar, fitsHdr, 'CLNMISS', doMiss?'T':'F'

  in[imgCols[0],imgRows[0]] = img
  flags[imgCols[0],imgRows[0]] = fl

endelse

RETURN, in
END