Viewing contents of file '../idllib/ghrs/pro/calfos_ref.pro'
pro calfos_ref,h,pattern,config,reference,switches,ddt_over
;
;+
;			calfos_ref
;
; Get reference file names and switches from input header
;
; CALLING SEQUENCE:
;	calfos_ref,h,pattern,config,reference,switches,ddt_over
;
; INPUTS:
;	h - fits header of .d0h file (from CALFOS_RD)
;	pattern - pattern vector (from CALFOS_RD)
;	config - configuration vector (from CALFOS_RD)
;	ddt_over - switch to override the defddtbl header keyword
;
; INPUT/OUTPUTS:
;	reference - reference file names
;	switches - calibration switches (on output it will be changed
;		to a logical vector 1-perform 2-omit)
;
; OPERATIONAL NOTES:
;
; Only blank reference files and calibration switches are filled in
; with values from the input header.
;
; HISTORY:
;	version 1.0 D. Lindler  Jan. 1990
;	added osfcnvrt to handle logical name conversion (between sunos and vms)
;		    D. Neill    May. 1990
;	prints error message if filename n/a used  D. Neill  Jan 1991
;	Feb. 1991  added GIMP offset application
;	Jul. 1991  added Background Geomagnetic Field correction application
;	Jul. 1991  added gimp sensitivity file and OFFS_TAB header table
;	Oct. 1991  added BGF rates reference table (CCS8)
;	Oct. 1991  changed error messages and actions associated with
;		   GIMP and BGF corrections
;	Jan. 1992  changed BGF to GMF to comply with STSDAS
;	Mar. 1992  Changed order of DQI and ERR corrs, added special switch
;		   to override DEFDDTBL
;-
;---------------------------------------------------------------------
;
; get switches
;
	names = ['DQI','ERR','CNT','OFF','PPC','WAV','BAC','GMF','FLT',$
		 'SKY','FLX']
	nsw = n_elements(names)
	for i = 0,nsw-1 do begin
	  if strtrim(switches(i)) eq '' then $
		switches(i)=sxpar(h,names(i)+'_CORR')
	  switches(i) = strupcase(switches(i))
	endfor
;
; if switch is LAST change it to PERFORM and change all subsequent switches
; to OMIT
;
	for i=0,nsw-1 do begin
	    if strupcase(strmid(switches(i),0,1)) eq 'L' then begin
		switches(i) = 'PERFORM'
		if i lt nsw-1 then for j=i+1,nsw-1 do switches(j) = 'OMIT'
	    endif
	endfor
;
; If no sky spectra, change SKY_CORR to omit (also determine if BCK)
; 
	ysteps = pattern(6)
	if ysteps gt 3 then begin
		switches(9) = 'OMIT'
		bck = 0
		no_sky = 1
	    end else begin
		no_sky = 1
		bck = 0

		for i=4,3+ysteps do begin
			if strtrim(config(i)) eq 'SKY' then no_sky = 0
			if strtrim(config(i)) eq 'BCK' then bck = 1
		endfor

		if no_sky then switches(9) = 'OMIT'
	end
;
; if CNT_CORR not performed and PPC_CORR requested, print warning
;
	if (strupcase(strmid(switches(2),0,1)) ne 'P') and $
	   (strupcase(strmid(switches(4),0,1)) eq 'P') then $
	   print,'WARNING - deadtime correction requested without '+ $
		 'conversion to count rates'
;
; convert to logical array
;
	s = bytarr(nsw)
	for i=0,nsw-1 do $
		if strupcase(strmid(switches(i),0,1)) eq 'P' then s(i)=1
	switches = s
	if strupcase(strmid(ddt_over,0,1)) eq 'P' then ddt_over=1 $
						  else ddt_over=0
;
; get reference files from headers when not supplied
;
	names = ['BACHFILE','FL1HFILE','FL2HFILE','IV1HFILE','IV2HFILE', $
		 'DDTHFILE','DQ1HFILE','CCG2    ','CCS0    ','CCS1    ', $
		 'CCS2    ','CCS3    ','CCS5    ','CCS6    ','DQ2HFILE', $
		 'OFFS_TAB','GMFTAB  ','CCS7    ','CCS8    ']
	nr=n_elements(names)
	for i=0,nr-1 do begin
	    if strtrim(reference(i)) eq '' then begin
		reference(i) = sxpar(h,strtrim(names(i)))
		if !err lt 0 then reference(i) = ' '
	     end else begin
		sxaddpar,h,strtrim(names(i)),strtrim(reference(i),2)
	    end
	endfor
;
; set reference files to blank for files not needed
;
	if not s(0) then begin
		reference(6) = ' '				; DQ1HFILE
		reference(14) = ' '				; DQ2HFILE
	endif
	if (not s(2)) or (sxpar(h,'DEFDDTBL') and not ddt_over) then $
			reference(5) = ' ' 			; DDTHFILE
	if not s(4) then reference(7) = ' '			; CCG2
	if not s(3) then begin			; GIMP corrections
		reference(15) = ' '				; OFFTAB
		reference(17) = ' '				; CCS7
	endif
	if (not s(6)) or (not s(7)) then begin	; Backgrnd. corr. due to GMF
		reference(16) = ' '				; GMFTAB
		reference(18) = ' '				; CCS8
	endif
	if (not s(6)) or bck then reference(0) = ' '		; BACHFILE
	if (not s(6)) then reference(12) = ' '			; CCS5
	if (not s(6)) and (not s(9)) then reference(11) = ' '	; CCS3
	if not s(9) then begin
		reference(8) = ' '				; CCS0
		reference(10) = ' '				; CCS2
	endif
	if not s(8) then reference(1:2) = ' '			; FL*HFILE
	if not s(5) then reference(13) = ' '			; CCS6
	if not s(10) then reference(3:4) = ' '			; IV*HFILE
	if (strtrim(config(7)) ne 'PAIR') or $			
	   ((not s(0)) and (not s(5)) and (not s(8)) and (not s(9))) then $
					reference(9) = ' '	; CCS1
	if (strtrim(config(7)) ne 'PAIR') and $
	   (strtrim(config(2)) eq 'C') then begin	
		reference(2) = ' '				; FL2HFILE
		reference(4) = ' '				; IV2HFILE
		reference(14) = ' '
	endif
;
; check if files needed for corrections are present
;
	if s(3) then begin		; OFF_CORR selected
	  if (strtrim(reference(15)) eq '') and $
	     (strtrim(reference(17)) eq '') then begin
	    print,'OFF_CORR selected and no GIMP corrections (OFFTAB) or',$
		  '         GIMP sensitivity (CCS7) reference file specified',$
		  '         Aborting CALFOS'
	    retall
	  endif
	endif
	if (s(6) and s(7)) then begin	; BAC_CORR and GMF_CORR selected
	  if (strtrim(reference(16)) eq '') and $
	     (strtrim(reference(18)) eq '') then begin
	    print,'GMF_CORR selected and no GMF corrections (GMFTAB) or',$
		  '         GMF rates (CCS8) reference file specified       ',$
		  '         Aborting CALFOS'
	    retall
	  endif
	endif
;
; check for n/a filenames
;
	if strupcase(strtrim(reference(15))) eq 'N/A' then reference(15) = ' '
	for i=0,nr-1 do $
	  if (strpos(strupcase(reference(i)),'N/A') ne -1) then $
	   print,'CALFOS_REF: Warning: invalid ref. filename for ',names(i)
;
; change $ signs to : in reference file names
;
	for i=0,nr-1 do begin
	   pos = strpos(reference(i),'$')
	   if pos ge 0 then begin
		temp = reference(i)
		strput,temp,':',pos
		reference(i) = temp
	   end
;
; convert logical syntax to comply with host
;
	   reference(i)=osfcnvrt(reference(i))
	end
return
end