PRO gui_calibrate, top

;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Calibrated the image
;;
;; CALLING SEQUENCE:
;;	gui_calibrate, top
;;
;; REQUIRED INPUTS:
;;	top - Widget ID fot he top window
;;
;; OUTPUTS:
;;
;; OPTIONAL INPUT KEYWORDS:
;;
;; EXAMPLE:
;;      IDL> gui_calibrate, top
;;
;; PROCEDURES USED (i.e. called directly!):
;;
;; MODIFICATION HISTORY:
;;   2004-09-06  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

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

IF error NE 0 THEN BEGIN
	trash = dialog_message(!ERROR_STATE.MSG, /ERROR, $
		title='Calibration Error',  dialog_parent=top)
	RETURN
ENDIF
		
;; Get the options for calibration
widget_control, top, get_uvalue=opts

IF opts.infn EQ '' THEN $
	message, 'Image must be open to calibrate it', /NONAME

;; Open a dialog to ask for the output types
tmp = gui_outputDialog(top, OPTIONS=opts.outputs)
IF (size(tmp, /dimensions))[0] EQ 0 THEN return ELSE opts.outputs = tmp
	
widget_control, /hourglass
	
;; Figure out which manual filenames were specified
IF opts.darkAlg EQ 2 THEN darkfn = opts.darkfn
IF opts.compressEna THEN compressfn = opts.compressfn
IF opts.adcEna THEN adcfn = opts.adcfn
IF opts.lindnEna THEN lindnfn = opts.lindnfn
IF opts.gainEna THEN gainfn = opts.gainfn
IF opts.badpixsEna THEN badpixsfn = opts.badpixsfn
IF opts.geomEna THEN geomfn = opts.geomfn
IF opts.mtfEna THEN mtffn = opts.mtffn
IF opts.visconstEna THEN visconstfn = opts.visconstfn
IF opts.specEna THEN specfn = opts.specfn
IF opts.constmapEna THEN constmapfn = opts.constmapfn
IF opts.hlambEna THEN hlambfn = opts.hlambfn
IF opts.flatEna THEN flatfn = opts.flatfn

;; Get the list of files to calibrate
IF opts.doBatch THEN BEGIN
	@batchtemp

	;; Read the ASCII file
	data = read_ascii(opts.infn, template=batchtemp)

	;; Get the input filenames
	inFN=data.fn

	;; Generate the output filenames
	n = (size(inFN, /dimensions))[0]
	pos = strpos(inFn, path_sep(), /reverse_search)+1
	outDir = strarr(n)
	outFn = strarr(n)
	FOR i=0, n-1 DO BEGIN
		outDir[i] = strmid(inFn[i], 0, pos[i])
		outFn[i] = strmid(inFn[i], pos[i])
	END

	;; DN reversible
	IF opts.outputs.dnEna THEN BEGIN
		file_mkdir, outDir+'CALDN'
		dnFN = outDir+'CALDN'+path_sep()+outFN
	ENDIF ELSE $
		dnFN=replicate('',n)

	;; Rad reversible
	IF opts.outputs.radRevEna THEN BEGIN
		file_mkdir, outDir+'CALRADREV'
		radrevFN = outDir+'CALRADREV'+path_sep()+outFN
	ENDIF ELSE $
		radrevFN=replicate('',n)

	;; I/F reversible
	IF opts.outputs.ifRevEna THEN BEGIN
		file_mkdir, outDir+'CALIFREV'
		ifrevFN = outDir+'CALIFREV'+path_sep()+outFN 
	ENDIF ELSE $
		ifrevFN=replicate('',n)

	;; Rad unreversible
	IF opts.outputs.radEna THEN BEGIN
		file_mkdir, outDir+'CALRAD'
		radFN = outDir+'CALRAD'+path_sep()+outFN
	ENDIF ELSE $
		radFN=replicate('',n)

	;; I/F unreversible
	IF opts.outputs.ifEna THEN BEGIN
		file_mkdir, outDir+'CALIF'
		ifFN = outDir+'CALIF'+path_sep()+outFN 
	ENDIF ELSE $
		ifFN=replicate('',n)
ENDIF ELSE BEGIN
	;; A single file to calibrate
	n=1
	inFN=opts.inFN
	dnFN=(opts.outputs.dnEna)?opts.outputs.dnFN:''
	radRevFN=(opts.outputs.radrevEna)?opts.outputs.radrevFN:''
	ifRevFN=(opts.outputs.ifrevEna)?opts.outputs.ifrevFN:''
	radFN=(opts.outputs.radEna)?opts.outputs.radFN:''
	ifFN=(opts.outputs.ifEna)?opts.outputs.ifFN:'' 
ENDELSE

;; Save the options in case it fails
widget_control, top, set_uvalue=opts

;; Do the actual calibration
FOR i=0,n-1 DO BEGIN
	DIcal, inFN[i], dnFN=dnFn[i], radFN=radFN[i], revRadFn=radRevFn[i], $
		ifFN=ifFN[i], revIfFn=ifRevFn[i], $
		modules=opts.mods, desmearalg=opts.desmearalg, $
		darkfn=darkfn, flatfn=flatfn, $
		maxGapSize=opts.maxGapSize, $
		usedarkmodel=opts.darkalg EQ 0, /SILENT, $
		compressfn=compressfn, adcfn=adcfn, lindnfn=lindnfn,$
		badpixsfn=badpixsfn, geomfn=geomfn,mtffn=mtffn,$
		visconstfn=visconstfn, specfn=specfn, $
		constmapfn=constmapfn, gainfn=gainfn, $
		spikesig=opts.spikethresh, spikeIter=opts.spikeIter, $
		spikeBox=opts.spikeBox, spikeMed=opts.spikeMed, spikeAlg=opts.spikeAlg,$
		badpixsInterp=opts.badPixsInterp, missingInterp=opts.missingInterp, $
		hlutfn=hlambfn, compressmeth=opts.compressMeth, mtfAlg=opts.mtfAlg, $
		mtfParam = opts.mtfParam
ENDFOR
widget_control, hourglass=0
trash = dialog_message('Calibration Successful', dialog_parent=top,$
	/information, title='Success')

IF NOT opts.doBatch THEN BEGIN
	;; Figure out which files to display as calibrated
	fn = ''
	title = ''
	IF opts.outputs.dnEna THEN BEGIN
		fn = [fn, opts.outputs.dnFN]
		title = [title, 'Reversible DN Image']
	ENDIF
	IF opts.outputs.radrevEna THEN BEGIN
		fn = [fn, opts.outputs.radrevFN]
		title = [title, 'Reversible Radiance Image']
	ENDIF
	IF opts.outputs.ifrevEna THEN BEGIN
		fn = [fn,opts.outputs.ifrevFN]
		title = [title, 'Reversible I/F Image']
	ENDIF
	IF opts.outputs.radEna THEN BEGIN
		fn = [fn, opts.outputs.radFN]
		title = [title, 'Unreversible Radiance Image']
	ENDIF
	IF opts.outputs.ifEna THEN BEGIN
		fn = [fn,opts.outputs.ifFN]
		title = [title, 'Unreversible I/F Image']
	ENDIF
	w = where(fn, n)
	fn = fn[w]
	title = title[w]

	;; Display the actual calibrated images
	IF opts.det EQ 1 THEN BEGIN
		xsize=300
		ysize=150
	ENDIF
	FOR i=0, n-1 DO BEGIN
		message, /reset
		img = readFITS(fn[i], hdr, /silent)
		IF !ERROR_STATE.CODE LT 0 THEN message, !ERROR_STATE.MSG

		tmp = getBiasCols(hdr, COMPLEMENT=imgCols)
		tmp = getPOCRows(hdr, COMPLEMENT=imgRows)
		img = img[imgCols[0]+1:imgCols[1]-1, imgRows[0]+1:imgRows[1]-1]
		wnd = cwimg_preview(group_leader=top, title=title[i],retain=2,$
			value=img,kill_notify='gui_preview_close_event',$
			uvalue=top, xsize=xsize,ysize=ysize)
		opts.calWnd[i] = wnd
	ENDFOR
ENDIF
widget_control, top, set_uvalue=opts, /no_copy
END