
# Pipeline for Target thar10

GAIN := $(shell python -c 'import FITS;f=FITS.FITS("flat.fits");print f["newgain"];f.close()')
all: stage-overscan stage-ydist-copy stage-orders-copy stage-xdist stage-xdist-copy stage-wdist stage-wdist-copy stage-subscat stage-flatten stage-skyaps stage-skyrect stage-skysub stage-objaps stage-multiunrect stage-multispec stage-finalize ../targ_thar10

stage-overscan:
# Overscan Subtraction, Trimming, and Image Reorientation.
	echo Stage:  Overscan Subtraction, Trimming, and Image Reorientation.
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0007.fits thar10_spec0007.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0001.fits thar10_lamp0001.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0008.fits thar10_spec0008.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0009.fits thar10_spec0009.fits
	touch stage-overscan

stage-ydist-copy: stage-overscan
# Copying the Y Distortion.
	echo Stage:  Copying the Y Distortion.
	copyrect -ydist lamp.fits thar10_spec0007.fits
	copyrect -ydist lamp.fits thar10_lamp0001.fits
	copyrect -ydist lamp.fits thar10_spec0008.fits
	copyrect -ydist lamp.fits thar10_spec0009.fits
	touch stage-ydist-copy

stage-orders-copy: stage-ydist-copy
# Copy the Order Edges.
	echo Stage:  Copy the Order Edges.
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits thar10_lamp0001.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits thar10_spec0007.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits thar10_spec0008.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits thar10_spec0009.fits
	touch stage-orders-copy

stage-xdist: stage-orders-copy
# Get X Distortion.
	echo Stage:  Get X Distortion.
	efits thar10_lamp0001.fits '(i1-VTKMedianSmooth(i1,32,8,numret=1))*equal(VTKConvolve(1.*greater(i1,60e3),k=5,numret=1),0)' thar10_lamp0001b.fits
	getrect -xdist -ydist thar10_lamp0001b.fits -nx 8 -dy 3 -x 1 -nsub2 3 -y 1 -b 4
	globalrect thar10_lamp0001b.fits -maxo 2 -go -s 11.000000 -d thar10_lamp0001bglob.ps/vcps
	maprect -xdist thar10_lamp0001b.fits
	touch stage-xdist

stage-xdist-copy: stage-xdist
# Copying X Distortion.
	echo Stage:  Copying X Distortion.
	copyrect -xdist thar10_lamp0001b.fits thar10_spec0007.fits
	copyrect -xdist thar10_lamp0001b.fits thar10_lamp0001.fits
	copyrect -xdist thar10_lamp0001b.fits thar10_spec0008.fits
	copyrect -xdist thar10_lamp0001b.fits thar10_spec0009.fits
	touch stage-xdist-copy

stage-wdist: stage-xdist-copy
# Do the Wavelength Calibration.
	echo Stage:  Do the Wavelength Calibration.
	mageMatchOrders thar10_lamp0001b.fits

	mageFindLines thar10_lamp0001bspecs.fits -fwhm 1.750000 -th 3.000000

	mageMatchLamps thar10_lamp0001bspecs.fits -m lamp.fits
	touch stage-wdist

stage-wdist-copy: stage-wdist
	copyecords thar10_lamp0001bspecs.fits thar10_lamp0001.fits
	copyecords thar10_lamp0001bspecs.fits thar10_lamp0001b.fits

	copyecords thar10_lamp0001bspecs.fits thar10_spec0007.fits
	copyecords thar10_lamp0001bspecs.fits thar10_spec0008.fits
	copyecords thar10_lamp0001bspecs.fits thar10_spec0009.fits

	copyrect -wdist thar10_lamp0001bspecs.fits thar10_spec0007.fits
	copyrect -wdist thar10_lamp0001bspecs.fits thar10_lamp0001.fits
	copyrect -wdist thar10_lamp0001bspecs.fits thar10_spec0008.fits
	copyrect -wdist thar10_lamp0001bspecs.fits thar10_spec0009.fits

	copyrect -edist thar10_lamp0001bspecs.fits thar10_spec0007.fits
	copyrect -edist thar10_lamp0001bspecs.fits thar10_lamp0001.fits
	copyrect -edist thar10_lamp0001bspecs.fits thar10_spec0008.fits
	copyrect -edist thar10_lamp0001bspecs.fits thar10_spec0009.fits
	touch stage-wdist-copy

stage-subscat: stage-wdist-copy
# Subtract the Scattered Light.
	echo Stage:  Subtract the Scattered Light.
	interOrder thar10_spec0007.fits
	interOrder thar10_spec0008.fits
	interOrder thar10_spec0009.fits
	touch stage-subscat

stage-flatten: stage-subscat
# Flattening.
	echo Stage:  Flattening.
	flat2d -low 0.2 flat.fits thar10_spec0007o.fits
	flat2d -low 0.2 flat.fits thar10_lamp0001.fits
	flat2d -low 0.2 flat.fits thar10_spec0008o.fits
	flat2d -low 0.2 flat.fits thar10_spec0009o.fits
	touch stage-flatten

stage-skyaps: stage-flatten
# Generate Sky Apertures.
	echo Stage:  Generate Sky Apertures.
	mkorderaps -med -edge 3 -ydist -nsig 2.000000 -oo 5 thar10_spec0007of.fits thar10_spec0008of.fits thar10_spec0009of.fits
	touch stage-skyaps

stage-skyrect: stage-skyaps
# Determine the Sky Background.
	echo Stage:  Determine the Sky Background.
	skyrect thar10_spec0007of.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 1 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	skyrect thar10_spec0008of.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 1 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	skyrect thar10_spec0009of.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 1 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	touch stage-skyrect

stage-skysub: stage-skyrect
# Subtract the Sky Background.
	echo Stage:  Subtract the Sky Background.
	efits thar10_spec0007of.fits thar10_spec0007ofm.fits '(i1-i2)' thar10_spec0007ofo.fits
	efits thar10_spec0008of.fits thar10_spec0008ofm.fits '(i1-i2)' thar10_spec0008ofo.fits
	efits thar10_spec0009of.fits thar10_spec0009ofm.fits '(i1-i2)' thar10_spec0009ofo.fits
	touch stage-skysub

stage-objaps: stage-skysub
# Generate Object Positions.
	echo Stage:  Generate Object Positions.
	mkorderaps -root thar10- -ydist -bigsky -oo 5 -ex -nsig 1.762500 thar10_spec0007ofo.fits thar10_spec0008ofo.fits thar10_spec0009ofo.fits
	cat thar10_spec0007ofo.aps thar10_spec0008ofo.aps thar10_spec0009ofo.aps > thar10.aps
	touch stage-objaps

stage-multiunrect: stage-objaps
# Run Multiunrect.
	echo Stage:  Run Multiunrect.
	multiunrect thar10.aps -defsample -xdist -ydist -wdist -fwhm 4.000000 -spass 1 -mx 15 -my 1 -ncut 5.000000 -ccut 0.000000 -rn 4.200000 -g ${GAIN} -xsub 1 -ysub 3 -extra thar10_lamp0001f.fits
	mikejoin thar10-???sum.fits -r -o thar10_sum.fits
	mikejoin thar10-???sumsky.fits -r -o thar10_sky.fits
	mikejoin thar10-???sumsig.fits -r -o thar10_sig.fits
	mikejoin thar10-???sumext.fits -r -o thar10_ext.fits
	touch stage-multiunrect

stage-multispec: stage-multiunrect
# Run extractmultispec.
	echo Stage:  Run extractmultispec.
	extractmultispec thar10-???sum.fits -extra lamp -nsig 100.000000 -o thar10_multispec.fits -d thar10_multispec.ps/vcps
	touch stage-multispec

stage-finalize: stage-multispec
# Copy Final Products.
	echo Stage:  Copy Final Products.
	cp thar10_sum.fits thar10_sky.fits thar10_sig.fits thar10_ext.fits thar10_multispec.fits ../Final-Products
	touch stage-finalize

../targ_thar10:
	touch ../targ_thar10

clean:
	rm -f sf1_spec0003.fits sf1_lamp0001.fits sf1_spec0004.fits sf1_spec0003o.fits sf1_spec0004o.fits sf1_spec0003of.fits sf1_lamp0001f.fits sf1_spec0004of.fits sf1_spec0003ofm.fits sf1_spec0004ofm.fits sf1_spec0003ofo.fits sf1_spec0004ofo.fits thar10_spec0007.fits thar10_lamp0001.fits thar10_spec0008.fits thar10_spec0009.fits thar10_spec0007o.fits thar10_spec0008o.fits thar10_spec0009o.fits thar10_spec0007of.fits thar10_lamp0001f.fits thar10_spec0008of.fits thar10_spec0009of.fits thar10_spec0007ofm.fits thar10_spec0008ofm.fits thar10_spec0009ofm.fits thar10_spec0007ofo.fits thar10_spec0008ofo.fits thar10_spec0009ofo.fits

veryclean:
	rm -f stage-overscan stage-ydist-copy stage-orders-copy stage-xdist stage-xdist-copy stage-wdist stage-wdist-copy stage-subscat stage-flatten stage-skyaps stage-skyrect stage-skysub stage-objaps stage-multiunrect stage-multispec stage-finalize

