Viewing contents of file '../idllib/uit/pro/calfix.pro'
pro calfix,image,cmpr=cmpr
;+
; NAME:
;    CALFIX
; PURPOSE:
;    Updates calibration keywords in a UIT FITS header to use the latest 
;    values.    Exposure time is read from the NEWFRAMES data base.   
;    Absolute calibration is set to the Landsman 10-Dec In-Flight calibration.
;    These are the values used in REV 2.
; CALLING SEQUENCE:
;    CALFIX,'image_name' ,[cmpr= ]
;            or
;    CALFIX,header, [cmpr= ]
; INPUT-OUTPUT PARAMETERS:
;    image_name = name of .HHH header file.   The file will be updated 
;                 with the proper calibration.
;           or
;    header  - FITS header, string array
; KEYWORD PARAMETERS:
;    cmpr = compression factor, e. g. 4 for images boxaveraged by
;       a factor 4.   If not supplied, then CALFIX checks the PIXELSIZ
;       keyword to determine the compression.   Warning - HREBIN did not
;       reset the PIXELSIZ keyword prior to 10-Dec-91.
;       
; OUTPUT PARAMETERS:
;    none
; SIDE EFFECTS:
;    Writes changed copy of header to disk.
; PROCEDURE:
;    Header read, parameters retrieved, manipulated, stored.
; MODIFICATION HISTORY:
;    Written by R. S. Hill, ST Sys. Corp., 4 Feb 1991
;    Correction for PIXELSIZ parameter ne 20, added by J. K. Hill, STX, 2/5/91 
;    Also fixes character-string formatting problem.  RSH, STX, 9 April 1991.
;    Updated to emulate REV 2 calibration   WBL   10 Dec 1991
;    Exp time is read from NEWFRAMES database. N. Collins, HSTX, 24 Feb. 1994
;-
On_error,2

if N_params() LT 1 then begin
     print,"Calling Sequence - CALFIX,'image_name',[cmpr=]"
     return
endif

filters = ['A1','A2','A3','A4','A5','B1','B2','B3','B4','B5','B6']

band_factors = $
   [ 1.0, 13.10, 78.58, 33.07, 6.67, 1.00, 101.70, 7.80, 44.14, 1.82, 0.94 ]

sz_image = size(image)
if sz_image(sz_image(0)+1) NE 7 then $            ;First param must be a string
              message,'First parameter must be image header or file name'

filename = sz_image(0) EQ 0                      ;If scalar, then a file name
if filename then sxhread,image,h else h = image

img = sxpar(h,'IMAGE')
exp1 = sxpar(h,'EXPTIME')
dbopen,'newframes'
nf = strmid(img,0,3)
IF nf EQ 'NUV' THEN field = 'AFRAME='
IF nf EQ 'FUV' THEN field = 'BFRAME='
list = dbfind(field+strmid(img,3,4), /SILENT)
IF list(0) EQ -1 THEN BEGIN
      print,'Error:  image ',img,' not in frames database.'
      print,'RETALL -- Header will not be changed.'
      RETALL
ENDIF ELSE BEGIN
      dbext,list,'EXP_TIM',expo
      expo = expo(0)
      if expo ne exp1 then sxaddpar,h,'EXPTIME',expo
ENDELSE
dbclose

fil = sxpar(h,'FILTER')
fil = strtrim(strupcase(fil),2)
w = where(filters EQ fil,c)
IF c NE 1 THEN BEGIN
   print,'FILTER invalid in header.  RETALL -- nothing changed.'
   RETALL
ENDIF

IF strmid(fil,0,1) EQ 'A' THEN calibcon = 9e-17*1.14
IF strmid(fil,0,1) EQ 'B' THEN calibcon = 5e-16*0.95

filtfac = band_factors(w(0))
message,fil + ' filter scale factor = ' + strtrim(filtfac,2),/INF
if keyword_set(cmpr) then pxsf=cmpr^2 else $
        pxsf = (sxpar( h, 'PIXELSIZ' )/20.0)^2
bsca = calibcon * filtfac * pxsf / expo
message,'UIT Calibration of 9-Dec-91',/INF 
message, 'Input BSCALE =' + strtrim(sxpar(h,'BSCALE'),2), /INF
message, 'Output BSCALE = ' + strtrim(bsca,2), /INF

sxaddpar,h,'BSCALE',bsca
sxaddpar,h,'CAMSCALE',calibcon,'CAMERA CALIBRATION'
sxaddpar,h,'BUNIT','ERGS/CM**2/S/ANGSTRM'
sxaddpar,h,'FILTFAC',filtfac,'Relative Filter Sensitivity','HISTORY'
sxaddpar,h,'PICSCALE',bsca,'= CALIBCON*FILTFAC/EXPTIME','HISTORY'
sxaddpar,h,'CALIBVER','10-DEC-91 LANDSMAN USING FLIGHT DATA'
sxaddhist,'CALFIX:  BSCALE corrected '+!STIME,h
sxaddhist,'CALFIX: compression scale factor used = '+string(pxsf),h

blanks = string( replicate(32b,80) )
nhed = N_elements(h)
FOR i=0,nhed-1 DO BEGIN
   hbuf = h(i) + blanks
   IF (strmid(hbuf,8,1) eq '=') and (strmid(hbuf,9,1) ne ' ') THEN BEGIN
      part1 = strmid(hbuf,0,9)
      part2 = strmid(hbuf,9,70)
      h(i) = part1 + " " + part2
   ENDIF
ENDFOR
if filename then sxhwrite,image,h else image = h
RETURN
END