FUNCTION DIcal_tempvolt, fitsHdr

;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Used in the DI Calibration Pipeline.
;;	Converts the raw temperature and voltage values in the header to
;;	physical values, which are stored in different keywords in the header.
;;	Accesses the database to get the calibration coefficients
;;
;;
;; CALLING SEQUENCE:
;;	out = DIcal_tempvolt(fitsHdr)
;;
;; REQUIRED INPUTS:
;;	fitsHdr - The FITS header for the image
;;
;; OUTPUTS:
;;	RETURN -  the updated FITS header
;;
;; OPTIONAL INPUT KEYWORDS:
;;
;; EXAMPLE:
;;      IDL> out = DIcal_tempvolt(fitsHdr)
;;
;; PROCEDURES USED (i.e. called directly!):
;;	SXPAR - Read FITS header
;;	FXADDPAR - Modifies the FITS header
;;	di_dn2tempvolt - Converts the raw temperatures and voltages to si units
;;
;; MODIFICATION HISTORY:
;;   2004-08-25  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

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

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

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

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

;; Make sure we got everything from the header
IF c1 EQ 0 OR c2 EQ 0 THEN message, 'Invalid FITS header', /noname

;; Create the mapping of keywords from physical to raw
;;	Physical - Raw
map = [['IC590T', 'IMGH039'], $
	['CCD590T', 'IMGH041'], $
	['IR590T', 'IMGH043'], $
	['LVPS590T', 'IMGH045'], $
	['CCDPRET', 'IMGH047'], $
	['CCDT', 'IMGH049'], $
	['OPTBENT', 'IMGH051'], $
	['PRIMIRT', 'IMGH053'], $
	['SECMIRT', 'IMGH055'], $
	['COVERT', 'IMGH057'], $
	['IRFPAT', 'IMGH089'], $
	['CCDOFSAV', 'IMGH059'], $
	['CCDOFSBV', 'IMGH061'], $
	['CCDOFSCV', 'IMGH063'], $
	['CCDOFSDV', 'IMGH065'], $
	['CCDOUTAV', 'IMGH067'], $
	['CCDOUTBV', 'IMGH069'], $
	['CCDOUTCV', 'IMGH071'], $
	['CCDOUTDV', 'IMGH073'], $
	['CCDSERPV', 'IMGH075'], $
	['CCDSERNV', 'IMGH077'], $
	['CCDPARPV', 'IMGH079'], $
	['CCDPARNV', 'IMGH081'], $
	['IRRESETV', 'IMGH083'], $
	['IRSUBSTV', 'IMGH085'], $
	['IRBIASGV', 'IMGH087'], $
	['CALLAMPV', 'IMGH091']]

;; Make the changes to the FITS header
out = fitsHdr
n = (size(map, /dimensions))[1]
FOR i=0, n-1 DO BEGIN
	raw = sxpar(out, map[1,i])
	fxaddpar, out, map[0,i], di_dn2tempvolt(date, inst, map[1,i], raw, $
		VALDATE=valdate), format='(F10.3)'
ENDFOR

;; Modify calibration portion of the FITS header
fxaddpar, out, 'TMPVLTUP', 'T'
fxaddpar, out, 'TMPVLTV', valdate

RETURN, out

END