FORWARD_FUNCTION DIcal_geom, getTiePoints
;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Used in the DI Calibration Pipeline.
;;	Performs a rubber-sheet geometric distortion in any spacial dimensions.
;;
;;	!!!! Warning !!! - In the primary mission, the analysis was not 
;;		completed for this module so it was not used.
;;
;; CALLING SEQUENCE:
;;	x = DIcal_geom(in, fitsHdr, FN=fn)
;;
;; REQUIRED INPUTS:
;;	in - The image to which this pipeline element is going to be applied
;;	fitsHdr - The FITS header for the image
;;
;; OUTPUTS:
;;	RETURN - the image after going through this calibration step
;;
;; OPTIONAL INPUT KEYWORDS:
;;	FN - Filename of the file containing the tie points
;;
;; EXAMPLE:
;;      IDL> imgOut = DIcal_geom(imgIn, fitsHdr)
;;
;; PROCEDURES USED (i.e. called directly!):
;;	getTiePoints - determines the tie points to use
;;
;; MODIFICATION HISTORY:
;;   2004-05-24  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Retrieves the tie points for the image used to perform a rubber sheet
;;	distortion or geometric calibration
;;
;; CALLING SEQUENCE:
;;	ties = getTiePoints(img, FN=fn)
;;
;; REQUIRED INPUTS:
;;	img - the image to get the tie points for
;;	fitsHdr - the FITS header for the image
;;
;; OUTPUTS:
;;	RETURN - a Nx4 array containing the tie points. The first row is x 
;;		coordinate in the output image. The second row is the y
;;		coordinate in the output image. The thrid row is the x 
;;		coordinate in the input image. The last row is the y coordinate
;;		in the input image. Each column defines a tie point.
;;
;; OPTIONAL INPUT KEYWORDS:
;;	FN - Filename of the file containing the tie points. The file should
;;		be an IDL save file containing a variable called "ties"
;;
;; EXAMPLE:
;;      IDL> ties = getTiePoints(img)
;;
;; PROCEDURES USED (i.e. called directly!):
;;
;; MODIFICATION HISTORY:
;;   2004-07-21  M. Desnoyer    Created
;;   2005-04-12  M. Desnoyer    Reordered functions
;;
;;-----------------------------------------------------------------------------
FUNCTION getTiePoints, img, fitsHdr, FN=fn

ON_ERROR, 2

;; Get the instrument
inst = SXPAR(fitsHdr, 'INSTRUME', COUNT=c1)

;; Get the date
date = SXPAR(fitsHdr, 'OBSDATE', COUNT=c2)

;; Get the Mode
mode = SXPAR(fitsHdr, 'IMGH030', COUNT=c3)

;; Make sure header contained all the data we need
IF (c1 EQ 0) OR (c2 EQ 0) OR (c3 EQ 0) THEN $
	message, 'Invalid FITS Header'

;; Get the filename
IF not keyword_set(fn) THEN BEGIN
	serv = getSQLserver()
	db = 'di_calib'
	tbl = 'GEOM'
	sel = ['Filepath']
	cond = 'Instrument = "'+strtrim(inst,2)+'" AND Date <= "'+date+$
		'" AND Mode = '+strtrim(mode, 2)+' order by Date desc, Version desc limit 1'

	webfn = di_sqlquery(serv, db, tbl, sel, cond, dim=dim)

	IF min(dim) EQ 0 THEN message, 'Could not find calibration file', /NONAME

	gfn = getLocFn(webFn, subdir='GEOM')
ENDIF ELSE gfn=fn

;; Open the file
ties = readfits(gfn[0])

IF n_elements(ties) LT 4 THEN message, 'Invalid tie point file'

;; Update the FITS header
fxaddpar, fitsHdr, 'GEOMFILE', $
	strupcase(strmid(gfn[0], strpos(gfn[0], path_sep(), /reverse_search)+1))

RETURN, ties
END

;;-----------------------------------------------------------------------------
;; Main function
;;-----------------------------------------------------------------------------
FUNCTION DIcal_geom, in, fitsHdr, FN=fn

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

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

out = double(in)

;; Get the tie points TODO
ties = getTiePoints(out, fitsHdr, FN=fn)

;; Perform the geometric transformation
out = warp_tri(ties[*,0],ties[*,1],ties[*,2],ties[*,3],out)

;; Update the FITS header
fxaddpar, fitsHdr, 'GEOMCAL', 'T'

RETURN, out

END