
# Pipeline for Target xefl-o

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-deslitfn stage-skyaps stage-skyrect stage-skysub stage-objaps stage-multiunrect stage-multispec stage-finalize ../targ_xefl-o

stage-overscan:
# Overscan Subtraction, Trimming, and Image Reorientation.
	echo Stage:  Overscan Subtraction, Trimming, and Image Reorientation.
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0016.fits xefl-o_spec0016.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0001.fits xefl-o_lamp0001.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0017.fits xefl-o_spec0017.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0018.fits xefl-o_spec0018.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0019.fits xefl-o_spec0019.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0020.fits xefl-o_spec0020.fits
	touch stage-overscan

stage-ydist-copy: stage-overscan
# Copying the Y Distortion.
	echo Stage:  Copying the Y Distortion.
	copyrect -ydist lamp.fits xefl-o_spec0016.fits
	copyrect -ydist lamp.fits xefl-o_lamp0001.fits
	copyrect -ydist lamp.fits xefl-o_spec0017.fits
	copyrect -ydist lamp.fits xefl-o_spec0018.fits
	copyrect -ydist lamp.fits xefl-o_spec0019.fits
	copyrect -ydist lamp.fits xefl-o_spec0020.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 xefl-o_lamp0001.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits xefl-o_spec0016.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits xefl-o_spec0017.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits xefl-o_spec0018.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits xefl-o_spec0019.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits xefl-o_spec0020.fits
	touch stage-orders-copy

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

stage-xdist-copy: stage-xdist
# Copying X Distortion.
	echo Stage:  Copying X Distortion.
	copyrect -xdist xefl-o_lamp0001b.fits xefl-o_spec0016.fits
	copyrect -xdist xefl-o_lamp0001b.fits xefl-o_lamp0001.fits
	copyrect -xdist xefl-o_lamp0001b.fits xefl-o_spec0017.fits
	copyrect -xdist xefl-o_lamp0001b.fits xefl-o_spec0018.fits
	copyrect -xdist xefl-o_lamp0001b.fits xefl-o_spec0019.fits
	copyrect -xdist xefl-o_lamp0001b.fits xefl-o_spec0020.fits
	touch stage-xdist-copy

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

	mageFindLines xefl-o_lamp0001bspecs.fits -fwhm 1.750000 -th 3.000000

	mageMatchLamps xefl-o_lamp0001bspecs.fits -m lamp.fits
	touch stage-wdist

stage-wdist-copy: stage-wdist
	copyecords xefl-o_lamp0001bspecs.fits xefl-o_lamp0001.fits
	copyecords xefl-o_lamp0001bspecs.fits xefl-o_lamp0001b.fits

	copyecords xefl-o_lamp0001bspecs.fits xefl-o_spec0016.fits
	copyecords xefl-o_lamp0001bspecs.fits xefl-o_spec0017.fits
	copyecords xefl-o_lamp0001bspecs.fits xefl-o_spec0018.fits
	copyecords xefl-o_lamp0001bspecs.fits xefl-o_spec0019.fits
	copyecords xefl-o_lamp0001bspecs.fits xefl-o_spec0020.fits

	copyrect -wdist xefl-o_lamp0001bspecs.fits xefl-o_spec0016.fits
	copyrect -wdist xefl-o_lamp0001bspecs.fits xefl-o_lamp0001.fits
	copyrect -wdist xefl-o_lamp0001bspecs.fits xefl-o_spec0017.fits
	copyrect -wdist xefl-o_lamp0001bspecs.fits xefl-o_spec0018.fits
	copyrect -wdist xefl-o_lamp0001bspecs.fits xefl-o_spec0019.fits
	copyrect -wdist xefl-o_lamp0001bspecs.fits xefl-o_spec0020.fits

	copyrect -edist xefl-o_lamp0001bspecs.fits xefl-o_spec0016.fits
	copyrect -edist xefl-o_lamp0001bspecs.fits xefl-o_lamp0001.fits
	copyrect -edist xefl-o_lamp0001bspecs.fits xefl-o_spec0017.fits
	copyrect -edist xefl-o_lamp0001bspecs.fits xefl-o_spec0018.fits
	copyrect -edist xefl-o_lamp0001bspecs.fits xefl-o_spec0019.fits
	copyrect -edist xefl-o_lamp0001bspecs.fits xefl-o_spec0020.fits
	touch stage-wdist-copy

stage-subscat: stage-wdist-copy
# Subtract the Scattered Light.
	echo Stage:  Subtract the Scattered Light.
	interOrder xefl-o_spec0016.fits
	interOrder xefl-o_spec0017.fits
	interOrder xefl-o_spec0018.fits
	interOrder xefl-o_spec0019.fits
	interOrder xefl-o_spec0020.fits
	touch stage-subscat

stage-flatten: stage-subscat
# Flattening.
	echo Stage:  Flattening.
	flat2d -low 0.2 flat.fits xefl-o_spec0016o.fits
	flat2d -low 0.2 flat.fits xefl-o_lamp0001.fits
	flat2d -low 0.2 flat.fits xefl-o_spec0017o.fits
	flat2d -low 0.2 flat.fits xefl-o_spec0018o.fits
	flat2d -low 0.2 flat.fits xefl-o_spec0019o.fits
	flat2d -low 0.2 flat.fits xefl-o_spec0020o.fits
	touch stage-flatten

stage-deslitfn: stage-flatten
# Dividing out the 2D Image of the Slit Function.
	echo Stage:  Dividing out the 2D Image of the Slit Function.
	mageflat1d -fft slit.fits xefl-o_spec0016of.fits
	mageflat1d -fft slit.fits xefl-o_spec0017of.fits
	mageflat1d -fft slit.fits xefl-o_spec0018of.fits
	mageflat1d -fft slit.fits xefl-o_spec0019of.fits
	mageflat1d -fft slit.fits xefl-o_spec0020of.fits
	mageflat1d -fft slit.fits xefl-o_lamp0001f.fits
	touch stage-deslitfn

stage-skyaps: stage-deslitfn
# Generate Sky Apertures.
	echo Stage:  Generate Sky Apertures.
	mkorderaps -med -edge 3 -ydist -nsig 2.000000 -oo 5 xefl-o_spec0016ofs.fits xefl-o_spec0017ofs.fits xefl-o_spec0018ofs.fits xefl-o_spec0019ofs.fits xefl-o_spec0020ofs.fits
	touch stage-skyaps

stage-skyrect: stage-skyaps
# Determine the Sky Background.
	echo Stage:  Determine the Sky Background.
	skyrect xefl-o_spec0016ofs.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 3 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	skyrect xefl-o_spec0017ofs.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 3 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	skyrect xefl-o_spec0018ofs.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 3 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	skyrect xefl-o_spec0019ofs.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 3 -dkx 0.800000 -pct 40.000000 -check 5 -lowcut 4.000000 -skycut 4.000000 -spass -5
	skyrect xefl-o_spec0020ofs.fits -xdist -ydist -rn 4.200000 -g ${GAIN} -skyint 2048 -kx 3 -ky 1 -skysuf sky -b 3 -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 xefl-o_spec0016ofs.fits xefl-o_spec0016ofsm.fits '(i1-i2)' xefl-o_spec0016ofso.fits
	efits xefl-o_spec0017ofs.fits xefl-o_spec0017ofsm.fits '(i1-i2)' xefl-o_spec0017ofso.fits
	efits xefl-o_spec0018ofs.fits xefl-o_spec0018ofsm.fits '(i1-i2)' xefl-o_spec0018ofso.fits
	efits xefl-o_spec0019ofs.fits xefl-o_spec0019ofsm.fits '(i1-i2)' xefl-o_spec0019ofso.fits
	efits xefl-o_spec0020ofs.fits xefl-o_spec0020ofsm.fits '(i1-i2)' xefl-o_spec0020ofso.fits
	touch stage-skysub

stage-objaps: stage-skysub
# Generate Object Positions.
	echo Stage:  Generate Object Positions.
	mkorderaps -root xefl-o- -ydist -bigsky -oo 5 -ex -nsig 1.762500 xefl-o_spec0016ofso.fits xefl-o_spec0017ofso.fits xefl-o_spec0018ofso.fits xefl-o_spec0019ofso.fits xefl-o_spec0020ofso.fits
	cat xefl-o_spec0016ofso.aps xefl-o_spec0017ofso.aps xefl-o_spec0018ofso.aps xefl-o_spec0019ofso.aps xefl-o_spec0020ofso.aps > xefl-o.aps
	touch stage-objaps

stage-multiunrect: stage-objaps
# Run Multiunrect.
	echo Stage:  Run Multiunrect.
	multiunrect xefl-o.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 xefl-o_lamp0001fs.fits
	mikejoin xefl-o-???sum.fits -r -o xefl-o_sum.fits
	mikejoin xefl-o-???sumsky.fits -r -o xefl-o_sky.fits
	mikejoin xefl-o-???sumsig.fits -r -o xefl-o_sig.fits
	mikejoin xefl-o-???sumext.fits -r -o xefl-o_ext.fits
	touch stage-multiunrect

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

stage-finalize: stage-multispec
# Copy Final Products.
	echo Stage:  Copy Final Products.
	cp xefl-o_sum.fits xefl-o_sky.fits xefl-o_sig.fits xefl-o_ext.fits xefl-o_multispec.fits ../Final-Products
	touch stage-finalize

../targ_xefl-o:
	touch ../targ_xefl-o

clean:
	rm -f 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_spec0007ofs.fits thar10_lamp0001fs.fits thar10_spec0008ofs.fits thar10_spec0009ofs.fits thar10_spec0007ofsm.fits thar10_spec0008ofsm.fits thar10_spec0009ofsm.fits thar10_spec0007ofso.fits thar10_spec0008ofso.fits thar10_spec0009ofso.fits thar1_spec0007.fits thar1_lamp0001.fits thar1_spec0008.fits thar1_spec0009.fits thar1_spec0010.fits thar1_spec0011.fits thar1_spec0012.fits thar1_spec0007o.fits thar1_spec0008o.fits thar1_spec0009o.fits thar1_spec0010o.fits thar1_spec0011o.fits thar1_spec0012o.fits thar1_spec0007of.fits thar1_lamp0001f.fits thar1_spec0008of.fits thar1_spec0009of.fits thar1_spec0010of.fits thar1_spec0011of.fits thar1_spec0012of.fits thar1_spec0007ofs.fits thar1_lamp0001fs.fits thar1_spec0008ofs.fits thar1_spec0009ofs.fits thar1_spec0010ofs.fits thar1_spec0011ofs.fits thar1_spec0012ofs.fits thar1_spec0007ofsm.fits thar1_spec0008ofsm.fits thar1_spec0009ofsm.fits thar1_spec0010ofsm.fits thar1_spec0011ofsm.fits thar1_spec0012ofsm.fits thar1_spec0007ofso.fits thar1_spec0008ofso.fits thar1_spec0009ofso.fits thar1_spec0010ofso.fits thar1_spec0011ofso.fits thar1_spec0012ofso.fits thar30_spec0013.fits thar30_lamp0001.fits thar30_spec0014.fits thar30_spec0015.fits thar30_spec0013o.fits thar30_spec0014o.fits thar30_spec0015o.fits thar30_spec0013of.fits thar30_lamp0001f.fits thar30_spec0014of.fits thar30_spec0015of.fits thar30_spec0013ofs.fits thar30_lamp0001fs.fits thar30_spec0014ofs.fits thar30_spec0015ofs.fits thar30_spec0013ofsm.fits thar30_spec0014ofsm.fits thar30_spec0015ofsm.fits thar30_spec0013ofso.fits thar30_spec0014ofso.fits thar30_spec0015ofso.fits xefl-o_spec0016.fits xefl-o_lamp0001.fits xefl-o_spec0017.fits xefl-o_spec0018.fits xefl-o_spec0019.fits xefl-o_spec0020.fits xefl-o_spec0016o.fits xefl-o_spec0017o.fits xefl-o_spec0018o.fits xefl-o_spec0019o.fits xefl-o_spec0020o.fits xefl-o_spec0016of.fits xefl-o_lamp0001f.fits xefl-o_spec0017of.fits xefl-o_spec0018of.fits xefl-o_spec0019of.fits xefl-o_spec0020of.fits xefl-o_spec0016ofs.fits xefl-o_lamp0001fs.fits xefl-o_spec0017ofs.fits xefl-o_spec0018ofs.fits xefl-o_spec0019ofs.fits xefl-o_spec0020ofs.fits xefl-o_spec0016ofsm.fits xefl-o_spec0017ofsm.fits xefl-o_spec0018ofsm.fits xefl-o_spec0019ofsm.fits xefl-o_spec0020ofsm.fits xefl-o_spec0016ofso.fits xefl-o_spec0017ofso.fits xefl-o_spec0018ofso.fits xefl-o_spec0019ofso.fits xefl-o_spec0020ofso.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-deslitfn stage-skyaps stage-skyrect stage-skysub stage-objaps stage-multiunrect stage-multispec stage-finalize

