Viewing contents of file '../idllib/ghrs/pro/calhrs_abs.pro'
;+
;*NAME:
;			calhrs_abs
;
;*PURPOSE:
; Routine to convert GHRS data to absolute flux units.
;
;*PARAMETERS:
; CALLING SEQUENCE:
;	calhrs_abs,fnames,ih,wave,flux,err,log
;
; INPUTS:
;	fnames - string array of two elements
;		fnames(0) = sensitivity reference file name
;		fnames(1) = wavelengths for fnames(0)
;	ih - header vectors (128 x nreads)
;	wave - wavelength array npoints x nreads
;
; INPUTS/OUTPUTS:
;	flux - flux array npoints x nreads
;	err - propagated statistical error array
;		if not supplied or set to a scalar, statistical
;		errors are not computed
;	log - (optional) processing log (string array)
;
; HISTORY:
;	version 1  D. Lindler   Apr 89
;       Mar 18 1991      JKF/ACC    - moved to GHRS DAF (IDL Version 2)
;-
;--------------------------------------------------------------------------
pro calhrs_abs,fnames,ih,wave,flux,err,log
 
	VERSION=1.0
	MINABS=1.0
;
; determine aperture  binid=1 ssa  binid=2 lsa
;
	binid = ih(53)
	if (binid lt 1) or (binid gt 2) then return
;
; open abs. sensitivity file and read correct group
;
	sxopen,8,strtrim(fnames(0),2),hh
	sens=sxread(8,0,par)
	apname = strtrim(sxgpar(hh,par,'APERTURE'))
	if (binid eq 1) and ((apname eq 'LSA') or (apname eq 'Z2')) $
						then sens = sxread(8,1)
	if (binid eq 2) and ((apname eq 'SSA') or (apname eq 'Z1')) $
						then sens = sxread(8,1)
	sxopen,8,strtrim(fnames(1),2),hh
	wsens=sxread(8,0,par)
	apname = strtrim(sxgpar(hh,par,'APERTURE'))
	if (binid eq 1) and ((apname eq 'LSA') or (apname eq 'Z2')) $
						then wsens = sxread(8,1)
	if (binid eq 2) and ((apname eq 'SSA') or (apname eq 'Z1')) $
						then wsens = sxread(8,1)
	close,8
	if n_elements(sens) ne n_elements(wsens) then begin
		print,'CALHRS_ABS-- sensitivity vector not same size as' + $
			' wavelength vector'
		retall
	endif
	minw=min(wsens)
	maxw=max(wsens)
;
; History processing
;
	hist=strarr(3)
	hist(0)='CALHRS_ABS version '+string(version,'(f5.2)')+ $
		': Conversion to absolute flux units'
	hist(1)='    Sensitivity file = '+strtrim(fnames(0),2)
	hist(2)='    Wavelengths file = '+strtrim(fnames(1),2)
	if !dump gt 0 then printf,!textunit,hist
	if n_elements(log) gt 0 then sxaddhist,hist,log
;
; loop on readouts   
;                                       
	s=size(flux) & ns=s(1) & nreads = n_elements(flux)/ns
	for i=0,nreads-1 do begin
		ih(66,i)=ih(66,i) or 256	;flag as done
		w=wave(*,i)
		f=flux(*,i)
		quadterp,wsens,sens,w,s
		good=where((w ge minw) and (w le maxw) and (s gt minabs),found)
		if found gt 0 then begin
			fout=fltarr(ns)
			fout(good)=f(good)/s(good)
			flux(0,i)=fout
			if n_elements(err) gt 1 then begin
				e=err(*,i)
				eout=fltarr(ns)
				eout(good)=e(good)/s(good)
				err(0,i)=eout
			endif
		end else begin
		  ;
		  ; Calibration sensitivity curve does not cover specified
		  ; 	spectral region.
		  ;   
		  message,'Invalid range for computing ABSOLUTE SENSITIVITY',/cont
		  flux(0,i)= f*0
		  err(0,i) = err(*,i)*0
		endelse
	endfor
	return
	end