Viewing contents of file '../idllib/iuedac/iuelib/pro/calib.pro'
;******************************************************************************
;+
;*NAME:
;
;    	CALIB        (RDAF General Production Library)         JUNE 8, 1981
;
;*CLASS:
;
;    	Spectral Calibration
;
;*CATEGORY:
;
;	IUESIPS
;
;*PURPOSE:  
;
;    	To apply an absolute calibration to IUE low and high dispersion
;    	spectral data and the LWR sensitivity degradation correction
;    	to IUE low dispersion data only.
;
;*CALLING SEQUENCE:    
;
;    	CALIB,H,WAVE,FNET,EXPS,FABS
; 
;*PARAMETERS:
;
;        H     	(REQ) (I) (1) (L)
;        	Spectral header record.
;
;        WAVE   (REQ) (I) (1) (R)
;        	Wavelength vector.
;
;        FNET   (REQ) (I) (1) (R)
;        	Net flux in IUE flux units.
;
;        EXPS   (REQ) (I) (1) (R)
;        	Exposure time in seconds.
;          	If EXPS <= 0 then the total flux in absolute units is 
;          	returned in the output FABS (i.e. FABS=FNET*SINV).
;         
;        FABS 	(REQ) (O) (1) (R)
;        	Flux in erg/sec/cm2/A.
;         
;*EXAMPLES:
;
;*SYSTEM VARIABLES USED:
;
;	!iuer (.dat and .inf)
;
;*INTERACTIVE INPUT:
;
;      	None.
;
;*SUBROUTINES CALLED:
;
;    	PARCHECK
;	IFITSRD
;    	QUADTERP 
;    	SENSCOR
;
;*FILES USED:
;
;    	!iuer.DAT IUECAL.FIT   (I)
;    	Data set containing the absolute flux calibration tables.
;	See !iuer.INF IUECAL.INF for more information.
;
;    	!iuer.DAT IUECAL2.FIT  (I)   (no longer used)
;    	File as above containing LWP data appropriate for LWP ITF file #2.
;	See !iuer.INF IUECAL2.INF for more information.
;
;	!iuer.DAT IUECAL3.FIT  (I)
;	File as above containing the new high dispersion C values as
;	implemented in IUESIPS on 12/22/90 (replaces IUECAL2).
;	See !iuer.INF IUECAL3.INF for more information.
;
;    	!iuer.DAT DEGRAD.FIT   (I)
;    	Data set containing the sensitivity degradation tables (currently 
;	only LWR).  See !iuer.INF DEGRAD.INF for more information.
;         
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;	This procedure should only be used with IUESIPS data.
;
;*NOTES:
;
;       Tables containing the inverse sensitivity function are available
;	for LWP, LWR and SWP low dispersion and high dispersion.  The values
;	stored for both SWR dispersions are set to 1.0.  The actual values
;	used and the references are listed in the CALIB.INF file stored in
;	!iuer.INF.  Information on the sensitivity degradation tables is
;	listed in !iuer.INF DEGRAD.INF.  Currently only LWR low dispersion
;	images are corrected for sensitivity degradation.
;
;	Header record element 90 is used to flag absolute calibration.  
;	Uncalibrated data has h(90) = 0.  For low dispersion calibrated data
;	h(90) = 1.  High dispersion calibrated data is either 10, for data
;	calibrated with prelimimary C values, or 11, for data calibrated
;	using C values implemented in IUESIPS on 12/22/90.
;
;*PROCEDURE:
;
;	The inverse sensitivity table is read from IUECAL3.FIT (or
;	IUECAL.FIT) for the camera specified in the header record, and,
;	if the header record specifies a low dispersion LWR image , the 
;    	sensitivity degradation table is read from !iuer.DAT DEGRAD.FIT.
;    	The two arrays are multiplied together and the corrected fluxes 
;    	for each wavelength are computed using quadratic interpolation in
;	the log of the convolved function.  The absolute flux in
;	erg/sec/cm2/A is computed by:
;                
;           fabs = (inverse sens. * sens. degrad.) * fnet/(exposure time)
;
;I_HELP nn
;
;*MODIFICATION HISTORY:
;
;    	VERSION 1 BY SALLY HEAP      25 FEB 1981
;    	VERSION 2 BY SALLY HEAP      8 JUNE 1981
;    	    INCORPORATES HIGH DISPERSION CALIBRATION
;    	    OF CASTELLA ET AL.  PUTS EXPOSURE TIME INTO HEADER
;    	VERSION 3 20-Oct-1981 by F.H. Schiffer 3rd
;    	    Eliminate extra points from WTAB and STAB before
;    	    interpolating
;    	    Round exposure time to nearest 1/2 Millisecond
;    	    Return total flux for EXPS <= 0.
;    	VERSION 4 15-Dec-1981 by F.H. Schiffer 3rd
;    	    Correct for station id in camera number.
;    	VERSION 5 12-12-84 RWT leave H(90) = 0 until LWP high dispersion
;    	    calibration is adopted.
;    	VERSION 6 4-19-85 RWT use new IUECAL.DAT file and use bytes
;    	    per record to determine old or new processing
;    	    11-1-85 RWT modify for DIDL (i.e. use # & remove REORDER)
;    	    11-26-85 RWT add LWP high dispersion inverse sensitivity
;    	    function based on Cassatella (private communication, 1985).
;    	VERSION 7 5-14-86 RWT add subroutine SENSCOR for sensitivity
;    	    degradation correction.
;    	4-13-87 RWT VAX mods: add PARCHECK
;    	8-11-87 RWT use GET_LUN & FREE_LUN, and make SENSCOR a separate routine
;    	    and move IUECAL to !iuer.PRODROOT:[DAT]
;    	1-6-88 RWT use IUECAL2.DAT file for new LWP ITF and add 
;    	           procedure call listing
;    	5-10-88 HAA add RDAF Prolog
;       7-17-89 RWT mod. for SUN IDL
;	3-04-91 PJL use IUECAL3.DAT file (replaces IUECAL2.DAT); updated
;		prolog; modified to lowercase
;       4-10-91 KBC modify filename structure based on operating system type
;                   for compatibility on SUN/DEC/VAX
;      23July91 LLT new logicals; tested on VAX
;      23July91 PJL cleaned up; tested on SUN; updated prolog
;	 5 Nov 93  PJL	changed IUECAL.DAT, IUECAL2.DAT, and IUECAL3.DAT to
;			fits format - IUECAL.FIT, IUECAL2.FIT, and IUECAL3.FIT;
;			added IFITSRD
;        2 Sep 94  LLT  replace environment variables with !iuer structure
;-
;******************************************************************************
 pro calib,h,wave,fnet,exps,fabs
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'CALIB,H,WAVE,FNET,EXPS,FABS'
    retall 
 endif ; napr
 parcheck,npar,5,'CALIB'
;
;  check header characteristics
;
 temp = size(h)
 if (temp(temp(0)+1) eq 7) then begin
    print,'H vector supplied is a string.'
    print,'ACTION:  retall'
    retall
 endif  ; temp(temp(0)+1) eq 7
;
;  get characteristics of spectrum-- camera and dispersion
;
 ncam = h(3) mod 10   ; correct for station id (?)
 nord = h(2)
 ngrp = h(5)
 if (ncam gt 4) then begin    ; bad camera number
    print,'Camera number bad = ',ncam
    print,'ACTION: Returning'
    retall 
 endif  ; ncam
;
;  if low dispersion LWR, check for degradation correction
;
 if ( (ncam eq 2) and (nord eq 1) and (h(69) lt 0) ) then begin
    senscor,ncam,h,scor
 endif else begin
    print,'Degradation correction not applied, continuing execution'
    scor = 1.0
 endelse  ; (ncam eq 2) and (nord eq 1) and (h(69) lt 0)
;
;  fetch appropriate sensitivity file based on camera and itf file used
;
 if ( (ncam eq 1) and (h(580) gt 1) ) then   $
    file = !iuer.dat + 'iuecal3.fit' else   $
    file = !iuer.dat+'iuecal.fit'
 ifitsrd,file,ncam,main,extn,camname,wtab,/silent
 if ( (nord gt 1) and (ngrp ne 3) ) then begin   ; hi dispersion
    ifitsrd,file,ncam,main,extn,stab,efld=4+(h(0) gt 2000),/silent
    h(90) = 11
 endif else begin  ; low dispersion
    ifitsrd,file,ncam,main,extn,stab,efld=3,/silent
    h(90) = 1
 endelse  ; nord
;
 print,' '
 print,'Applying absolute calibration'
;
;  truncate data to valid points (& correct for sens. degrad.)
;
 i = where(wtab gt 0)
 wtab = wtab(i)
 stab = stab(i)
 stab = stab * scor      ; degradation correction, if requested
;
;  make quadratic interpolation in the tables to get inverse
;  sensitivity for each element of wave
;
 logstab = alog10(stab>1.e-16)
 quadterp,wtab,logstab,wave,logsinv
 sinv = 10.^logsinv
;
;  apply calibration
;
 fabs = sinv * fnet
 if (exps gt 0) then fabs = fabs/exps
;
;  update header
;
 tim = exps + 0.0005   ; round time to nearest msec
 mn = fix(tim/60.)
 sec = tim mod 60
 sc = fix(sec)
 ms = fix(1000. * (sec-sc))
 h([39,40,41]) = [mn,sc,ms]
;
 return
 end  ; calib