PRO DIcal_calcDarkFit, STARTDT=startDT, ENDDT=endDT, HRIVIS=hrivis, $
	HRIIR=hriir, MRI=mri, ITS=its

;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Calculates the dark frame fitting function for all specified
;;	instruments and detectors. Then, saves the fitting parameters
;;	in a web accessible location and inserts a reference to that file
;;	into the SDC SQL database in the CAL_DARK table. 
;;	This procedure can only be run on a UNIX based machine.
;;
;;	!!!Warning!!! - This procedure can currently only run on the SDC
;;		machine because the web accessible location is hardcoded.
;;
;;	!!!Warning2!!! - For each selected instrument/detector combo, this
;;		procedure can take a LONG LONG time (over 12hrs)
;;
;; CALLING SEQUENCE:
;;	DIcal_calcDarkFit, STARTDT=startDT, ENDDT=endDT, /HRIVIS, /HRIIR,
;;		/MRI, /ITS
;;
;; REQUIRED INPUTS:
;;	none
;;
;; OUTPUTS:
;;	none
;;
;; OPTIONAL INPUT KEYWORDS:
;;	STARTDT - The earliest date in ISO month/day format of dark frames to 
;;		use when generating the fitting parameters. DEFAULT=2002-06-27
;;	ENDDT - The latest date in ISO month/day format of dark frames to use
;;		when generating the fitting parameters. DEFAULT=Today
;;	HRIVIS - If specified, the fitting parameters will be calculated for
;;		the HRI_VIS unit
;;	HRIIR - If specified, the fitting parameters will be calculated for
;;		the HRI_IR unit
;;	MRI - If specified, the fitting parameters will be calculated for the
;;		MRI unit
;;	ITS - If specified, the fitting parameters will be calculated for the 
;;		ITS unit
;;
;; EXAMPLE:
;;      IDL> DIcal_calcDarkFit, STARTDT='2003-05-23', /HRIVIS
;;
;; PROCEDURES USED (i.e. called directly!):
;;	getSQLServer - Determines which SQL server to use
;;	DI_sqlInsert - Inserts pointer to file into the SQL database
;;	DI_fitDarkFunc - Performs the dark frame fitting
;;	cp - UNIX command to move the file
;;
;; MODIFICATION HISTORY:
;;   2004-06-11  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

relDir = 'DeepImpact/Data/Cal/DarkFunc/'
locDir = '/home/sdcNN/sdc/www/html/'+relDir
webAddr = 'http://sdc.../'+relDir

;; Count up the number of parameter cubes we need to make
toMkCnt = 0
IF keyword_set(HRIVIS) THEN toMkCnt=toMkCnt+1
IF keyword_set(HRIIR) THEN toMkCnt=toMkCnt+1
IF keyword_set(MRI) THEN toMkCnt=toMkCnt+1
IF keyword_set(ITS) THEN toMkCnt=toMkCnt+1
IF toMkCnt EQ 0 THEN $
	message, 'Must specify at least one instrument to perform calculations for'

;; Set defaults
IF NOT keyword_set(STARTDT) THEN startDT = '2002-06-27'
IF NOT keyword_set(ENDDT) THEN BEGIN
	caldat, systime(/JULIAN,/UTC),M,D,Y,H,min
	endDT = string(Y)+'-'
	IF M LT 10 THEN endDT = endDT+'0'+string(M)+'-' $
	ELSE endDT = endDT+string(D)+'-'
	IF D LT 10 THEN endDT = endDT+'0'+string(D)+'T' $
	ELSE endDT = endDT+string(D)+'T'
	IF H LT 10 THEN endDT = endDT+'0'+string(H)+':' $
	ELSE endDT = endDT+string(H)+':'
	IF min LT 10 THEN endDT = endDT+'0'+string(min) $
	ELSE endDT = endDT+string(min)
	endDT = strcompress(endDT,/REMOVE_ALL)
END

;; Get the SQL Server info
serv = getSQLServer()
db = 'di_calib'
tblNm = 'DARK'

;; Create the structure of info to write to the SQL database
toDB = replicate({inst:'',det:0,mode:1,startT:startDT,endT:endDT,path:''}, $
	toMkCnt)

;; Fill in the instrument and detectors for each fitting to run
i = 0
IF keyword_set(HRIVIS) THEN BEGIN
	toDB[i].inst = 'HRI'
	toDB[i].det = 0
	i = i+1
ENDIF
IF keyword_set(HRIIR) THEN BEGIN
	toDB[i].inst = 'HRI'
	toDB[i].det = 1
	i = i+1
ENDIF
IF keyword_set(MRI) THEN BEGIN
	toDB[i].inst = 'MRI'
	toDB[i].det = 0
	i = i+1
ENDIF
IF keyword_set(ITS) THEN BEGIN
	toDB[i].inst = 'ITS'
	toDB[i].det = 0
	i = i+1
ENDIF

;; Do all of the dark fittings
FOR i=0, toMkCnt-1 DO BEGIN
	fn = DI_fitDarkFunc(toDB[i].inst,toDB[i].det,ERROR=err,START=startDT, $
		ENDDT=endDT)
	IF err NE 0 THEN message, !ERROR_STATE.MSG
	
	;; Move the parameter file to where it's acessible from the web
	spawn, 'cp '+fn+' '+locDir+fn, EXIT_STATUS=err
	IF err NE 0 THEN message, 'Could not move file to web accessible area'
	FILE_DELETE, fn
	
	toDB[i].path = webAddr+fn
END

;; Update the database
DI_sqlInsert, serv, db, tblNm, toDB

END