Viewing contents of file '../idllib/ghrs/pro/calfos.pro'
pro calfos,name,params,wave,flux,eps,err,h,gpar,rate,gross,sky,back,net
;
;+
;			CALFOS
;
; Calibrates FOS GEIS files
;
; CALLING SEQUENCE:
;   calfos,name,params,wave,flux,eps,err,h,gpar,rate,gross,sky,back,net
;
; INPUTS:
;	name - rootname of input file (without extension)
;	params - keyword parameters
;		0 - use all defaults
;		1 - interactive specification
;		'filename' - from text file with form
;			keyword=value
;			keyword=value
;
;		   or
;		'keyword=value,keyword=value'
;
; OUTPUTS:
;	wave - wavelength array  
;	flux - flux array
;	eps - data quality array
;	err - propagated stat array
;	h - FITS header array
;	gpar - group parameter array
;	rate - counts/sec (not deadtime corrected)
;	gross - gross spectrum (background subtracted and flat fielded)
;	sky - unsmoothed sky
;	back - background spectra
;	net - gross minus smoothed sky
;
;	   Output data arrays are three dimensional:
;
;		dimension 1 - (nchnls+ overscan - 1)*nxsteps
;		dimension 2 - nysteps (number of ysteps relevant to the
;			data type)
;		dimension 3 - number of readouts x number of slices
;	  
;	   If some processing steps are omitted, some output arrays
;	   may not defined.
;
; KEYWORD PARAMETERS:
;
;	The following keyword parameters can be specified in the
;	PARAMS input.
;
;			   FILTER WIDTHS
;		
;		bck_md		background median filter width
;		bck_mn		background mean filter width
;		sky_md		sky median filter width
;		sky_mn		sky mean filter width
;
;			   PROCESSING STEPS
;
;		dqi_corr	data quality     	PERFORM or OMIT or
;		err_corr	propagate errors 	LAST (stop after step)
;		cnt_corr	count rates      	Blank use header
;		off_corr	compute/apply GIMP offsets
;		ppc_corr	paired pulse
;		wav_corr	wavelengths
;		bac_corr	background subtraction
;		gmf_corr	background geomagnetic field correction
;		flt_corr	flat field
;		sky_corr	sky subtraction
;		flx_corr	flux calibration
;
;			   SPECIAL SWITCHES
;
;		ddt_over	override DEFDDTBL keyword in header to use
;				dead diode table specified in ddthfile (see
;				below)
;
;			   REFERENCE FILES
;
;		bachfile	background header file
;		fl1hfile	flat-field file 1
;		fl2hfile	flat-field file 2
;		iv1hfile	inverse sensitivity file 1
;		iv2hfile	inverse sensitivity file 2
;		ddthfile	disabled diode table file
;		dq1hfile	data quality init. file 1
;		dq2hfile	data quality init. file 2
;		ccg2		paired pulse coef. table
;		ccs0		aperture area table
;		ccs1		aperture position table
;		ccs2		emission line table
;		ccs3		filter width table
;		ccs5		background shift table
;		ccs6		dispersion coef. table
;		offs_tab	GIMP offset table (if supplied then
;				the offsets will not be computed but will
;				instead be taken from this table)
;		gmftab		Background correction table due to geomagnetic
;				field (if supplied then the corrections will not
;				be computed but will be taken from this table)
;		ccs7		gimp sensitivity table
;		ccs8		Background correction geomagnetic rate
;				table (used if no corrections table supplied)
;
; 	All input parameters have a blank default.  A blank value indicates
;	that the value will be taken from the input header except for the
;	filter widths which will be taken from reference table CCS3 if not
;	supplied.
;
;	Processing steps may have values PERFORM, OMIT, or LAST.
;	The steps are performed in the order specified above.
;	If a step is set to LAST, all subsequent steps will be omitted.
;
;	Reference file names may have a full VMS file specification with
;	one exception.  The first $ (dollar sign) will be converted to
;	a colon.  This allows you to use the iraf logicals which may already
;	be in the headers as a VMS logical.  You will not be able to use the
;	dollar sign in disk names.  You must set a logical to the disk which
;	does not have the dollar sign.
;
; OPERATIONAL NOTES:
;
;	If !dump>0 you will get text history (which is also added to the
;	header) printed to the screen.
;
; EXAMPLES:
;
;	Run CALFOS on observation Y00VK106R using all defaults.
;
;		CALFOS,'y0vk106r',0,wave,flux,eps,err,header
;
;	Process file with rootname myjunk, stop after sky subtraction:
;	Use a sky median filter width of 11 and mean width of 5.
;
;		CALFOS,'myjunk','SKY_CORR=LAST,SKY_MD=11,SKY_MN=5',wave,flux
;
; HISTORY:
;	version 1.0  D. Lindler  Jan. 1990
;	version 1.1  G. Hartig   Aug. 1990 - modified to handle new data
;		quality files.
;	version 1.2  D. Neill	 Nov. 1990 - added kludge to skip opening
;		*.q0h/*.q0d files if data quality corr not enabled.
;	version 1.2 D. Lindler  Feb 1991 - added GIMP offset application
;	version 1.3 D. Lindler  Mar 1991 - added gimp offset computation
;	version 1.4 D. Neill	Jul 1991 - added Backgrnd GeoMag Field corr
;	version 2.0 Lindler/Neill Jul 1991 - added gimp sensitivity table.
;				Modifications to work with new header/
;				group parameter keywords.
;	version 2.1 D. Neill    Oct 1991 - added GMF rates reference table.
;		    D. Neill	Jan 1992 - Changed BGF to GMF to comply with
;						SDAS
;	version 2.2 D. Neill	Mar 1992 - changed structure to make reading
;				data files more sensible
;	version 2.3 D. Neill	Sep 1992 - removed lower limit of 1 in
;				calculating error array
;	version 2.4 D. Lindler  April 1992 - corrected round off of zero
;				count rate and zero error in GIMP routine to
;				non-zero values.
;-
;-----------------------------------------------------------------------------
	common calfos,rootname,hcom,gparcom
	VERSION = 2.4
	if n_params(0) lt 1 then begin
		print,'CALLING SEQUENCE:'
		print,' calfos,name,params,wave,flux,eps,err,h,gpar,rate,gross,sky,back,net'
		retall
	endif
;
; get keyword parameter values
;
	getpar,params,'calfos',par
	reference = par(30:48)		;reference file names
	reference(14) = par(37)		;workaround for added DQI file
	reference(7) = par(38:44)	;
	switch = par(10:20)		;calibration switches
	ddt_over = par(21)		;dead diode table override switch
	fwidths = par(0:3)		;filter widths
;
; read input data
;
	calfos_rd,name,h,data,gpar,eps_data,pattern,config
;
; start history
;
	hist = 'CALFOS version '+string(version,format='(f5.2)')+'   '+!stime+ $
		' '+name
	sxaddhist,hist,h
	if !dump gt 0 then print,hist
;
; get unsupplied calibration switches and reference files from header
;
	calfos_ref,h,pattern,config,reference,switch,ddt_over
;
; read aperture position table if paired aperture and fill in
; group parameters
;
	calfos_ccs1,reference(9),config,pattern,h,gpar,ypos
;
; perform data quality initialization
;
	if switch(0) then begin 
		calfos_rddqi,reference(6),config,pattern,h,dqi
		calfos_rddqi,reference(14),config,pattern,h,dqi
		calfos_dqi,name,ypos,config,pattern,dqi,eps_data,data
		sxaddpar,h,'DQI_CORR','COMPLETE'
	endif
;
; create error array
;
	if switch(1) then begin
		errd = sqrt(data)
		bad = where(eps_data gt 200) & nbad = !err
		if nbad gt 0 then begin
			errd(bad) = 0.0
			data(bad) = 0.0
		endif
		sxaddpar,h,'ERR_CORR','COMPLETE'
	endif else begin
		errd = 0
	endelse
;
; convert to count rates
;
	if switch(2) then begin
		calfos_rdddt,name,reference(5),config,pattern,h,ddt
		calfos_exp,name,pattern,ddt,h,gpar,data,eps_data,errd,expomax
		sxaddpar,h,'CNT_CORR','COMPLETE'
	endif
;
; compute/apply GIMP offsets
;
	if switch(3) then begin
		calfos_off,name,reference(15),reference(17), $
			pattern,expomax,h,data,errd,eps_data,gpar
		sxaddpar,h,'OFF_CORR','COMPLETE'
	endif
	if n_params(0) gt 8 then rate=data
;
; deadtime correction
;
	if switch(4) then begin
		calfos_ccg2,reference(7),config,h,ppcoef
		calfos_ppc,ppcoef,h,data,errd,eps_data
		sxaddpar,h,'PPC_CORR','COMPLETE'
	end
;
; wavelengths
;
	if switch(5) then begin
		calfos_ccs6,reference(13),config,h,disp_coef
		calfos_wav,disp_coef,ypos,config,pattern,h,wave
		sxaddpar,h,'WAV_CORR','COMPLETE'
	endif
;
; background subtraction
;
	calfos_ccs3,reference(11),config,h,fwidths	;read filter widths
	if switch(6) then begin
		calfos_rdbac,reference(0),config,pattern,h,bac
		if switch(7) then begin
			calfos_bac,bac,fwidths(0:1),config,pattern,h,data,$
				eps_data,back,name,reference(16),reference(18)
			sxaddpar,h,'GMF_CORR','COMPLETE'
		endif else $
			calfos_bac,bac,fwidths(0:1),config,pattern,h,data,$
				eps_data,back
		sxaddpar,h,'BAC_CORR','COMPLETE'
	endif
;
; flat fielding
;
	if switch(8) then begin
		calfos_rdflt,reference(1),config,pattern,h,flat
		calfos_rdflt,reference(2),config,pattern,h,flat
		calfos_flt,flat,ypos,config,pattern,h,data,errd
		sxaddpar,h,'FLT_CORR','COMPLETE'
	endif
;
; separate gross and sky
;
	calfos_ext,config,pattern,data,errd,eps_data,gross,err,eps, $
				sky,eps_sky
;
; sky subtraction
;
	if switch(9) then begin
		calfos_ccs0,reference(8),config,h,areas
		calfos_ccs2,reference(10),config,pattern,h,line_beg,line_end
		calfos_ccs5,reference(12),config,pattern,h,nshift
		calfos_sky,areas,line_beg,line_end,nshift,ypos,fwidths(2:3), $
			config,pattern,gross,sky,eps_sky,h,eps,net
		sxaddpar,h,'SKY_CORR','COMPLETE'
	    end else begin
		net = gross
	end
;
; inverse sensitivity
;
	if switch(10) then begin
		calfos_rdivs,reference(3),config,pattern,h,ivs
		calfos_rdivs,reference(4),config,pattern,h,ivs
		calfos_ivs,ivs,ypos,config,pattern,net,h,err,flux
		sxaddpar,h,'FLX_CORR','COMPLETE'
	    end else begin
		flux = net
	end
close,1,2
;
; update the common block
;
	fdecomp,name,disk,dir,rootname
	hcom = h
	gparcom = gpar
return
end