# names of permanent files:
rawfile = rootname // "_raw.fits"	# input uncalibrated science image
wavfile = rootname // "_wav.fits"	# input uncalibrated wavecal
crjfile = rootname // "_crj.fits"	# cosmic-ray rejected, flat fielded
fltfile = rootname // "_flt.fits"	# flat fielded science image
sflfile = rootname // "_sfl.fits"	# summed, flat fielded 2-D image
x1dfile = rootname // "_x1d.fits"	# extracted 1-D spectrum
x2dfile = rootname // "_x2d.fits"	# rectified 2-D spectrum or image
sx1file = rootname // "_sx1.fits"	# summed, extracted 1-D spectrum
sx2file = rootname // "_sx2.fits"	# summed, rectified 2-D spectrum

# names of temporary files:
blv_tmp = rootname // "_blv_tmp.fits"	# blevcorr; then CR flagged
crj_tmp = rootname // "_crj_tmp.fits"	# CR rejected, summed
fwv_tmp = rootname // "_fwv_tmp.fits"	# flat fielded wavecal
cwv_tmp = rootname // "_cwv_tmp.fits"	# flat fielded, source-sub wavecal
w2d_tmp = rootname // "_w2d_tmp.fits"	# 2-D extracted wavecal


main:

	get name of input file
	CALL initialize

	if no switch is PERFORM then exit

	if CRCORR == PERFORM then

	    if ATODCORR, DQICORR, or BLEVCORR is PERFORM then
		calstis1a rawfile blv_tmp	# subtract bias from overscan
	    else
		copy name of rawfile to blv_tmp variable
	    end if

	    if no calstis1 switch is set to PERFORM then
		copy name of crjfile to crj_tmp variable
	    end if

	    calstis2 blv_tmp crj_tmp	# reject CRs; flag in blv_tmp

	    if any calstis1 switch is set to PERFORM then
		calstis1 crj_tmp crjfile	# flat field summed science data
		calstis1 blv_tmp fltfile	# flat field non-summed data
	    end if

	    delete crj_tmp
	    delete blv_tmp

	else if any calstis1 switch is set to PERFORM then

	    calstis1 rawfile fltfile		# flat field the raw data

	else

	    copy name of rawfile to fltfile variable

	end if

	if OBSTYPE is SPECTROSCOPIC then

	    if WAVECORR is PERFORM then

		if no calstis1 switch is set to PERFORM then
		    copy wavfile to fwv_tmp
		else
		    calstis1 wavfile fwv_tmp	# flat field the wavecal
		end if

		set X2DCORR to PERFORM in fwv_tmp

		if SCLAMP is HITM then
		    # subtract science from wavecal, then rectify
		    if CRCORR is PERFORM then
			calstis11 fwv_tmp crjfile cwv_tmp
		    else
			calstis11 fwv_tmp fltfile cwv_tmp
		    end if
		    calstis7 cwv_tmp w2d_tmp
		    delete cwv_tmp
		else
		    calstis7 fwv_tmp w2d_tmp	# rectify wavecal
		end if
		delete fwv_tmp

		calstis4 w2d_tmp		# update shift keywords
		calstis12 w2d_tmp fltfile	# update header of fltfile
		if CRCORR is PERFORM then
		    calstis12 w2d_tmp crjfile	# update header of crjfile
		end if
		delete w2d_tmp
	    end if

	    if X2DCORR is PERFORM then		# 2-D rectification
		if CRCORR is PERFORM then
		    calstis7 crjfile sx2file
		else
		    calstis7 fltfile x2dfile
		    if RPTCORR is PERFORM then
			calstis8 x2dfile sx2file
		    end if
		end if
	    end if

	    if X1DCORR is PERFORM then		# 1-D spectral extraction
		# not implemented yet
		if CRCORR is PERFORM then
		    calstis6 crjfile sx1file
		else
		    calstis6 fltfile x1dfile
		    if RPTCORR is PERFORM then
			calstis13 x1dfile sx1file
		    end if
		end if
	    end if

	else if OBSTYPE is IMAGING then

	    if GEOCORR is PERFORM then		# 2-D image rectification
		if CRCORR is PERFORM then
		    calstis7 crjfile sx2file
		else
		    calstis7 fltfile x2dfile
		    if RPTCORR is PERFORM then
			calstis8 x2dfile sx2file
		    end if
		end if
	    end if

	end if
