
# Pipeline for Target 2131+1042_3

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_2131+1042_3

stage-overscan:
# Overscan Subtraction, Trimming, and Image Reorientation.
	echo Stage:  Overscan Subtraction, Trimming, and Image Reorientation.
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0028.fits 2131+1042_3_spec0028.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0001.fits 2131+1042_3_lamp0001.fits
	touch stage-overscan

stage-ydist-copy: stage-overscan
# Copying the Y Distortion.
	echo Stage:  Copying the Y Distortion.
	copyrect -ydist lamp.fits 2131+1042_3_spec0028.fits
	copyrect -ydist lamp.fits 2131+1042_3_lamp0001.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 2131+1042_3_lamp0001.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits 2131+1042_3_spec0028.fits
	touch stage-orders-copy

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

stage-xdist-copy: stage-xdist
# Copying X Distortion.
	echo Stage:  Copying X Distortion.
	copyrect -xdist 2131+1042_3_lamp0001b.fits 2131+1042_3_spec0028.fits
	copyrect -xdist 2131+1042_3_lamp0001b.fits 2131+1042_3_lamp0001.fits
	touch stage-xdist-copy

stage-wdist: stage-xdist-copy
# Do the Wavelength Calibration.
	echo Stage:  Do the Wavelength Calibration.
	mageMatchOrders 2131+1042_3_lamp0001b.fits

	mageFindLines 2131+1042_3_lamp0001bspecs.fits -fwhm 1.750000 -th 3.000000

	mageMatchLamps 2131+1042_3_lamp0001bspecs.fits -m lamp.fits
	touch stage-wdist

stage-wdist-copy: stage-wdist
	copyecords 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_lamp0001.fits
	copyecords 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_lamp0001b.fits

	copyecords 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_spec0028.fits

	copyrect -wdist 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_spec0028.fits
	copyrect -wdist 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_lamp0001.fits

	copyrect -edist 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_spec0028.fits
	copyrect -edist 2131+1042_3_lamp0001bspecs.fits 2131+1042_3_lamp0001.fits
	touch stage-wdist-copy

stage-subscat: stage-wdist-copy
# Subtract the Scattered Light.
	echo Stage:  Subtract the Scattered Light.
	interOrder 2131+1042_3_spec0028.fits
	touch stage-subscat

stage-flatten: stage-subscat
# Flattening.
	echo Stage:  Flattening.
	flat2d -low 0.2 flat.fits 2131+1042_3_spec0028o.fits
	flat2d -low 0.2 flat.fits 2131+1042_3_lamp0001.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 2131+1042_3_spec0028of.fits
	touch stage-skyaps

stage-skyrect: stage-skyaps
# Determine the Sky Background.
	echo Stage:  Determine the Sky Background.
	skyrect 2131+1042_3_spec0028of.fits -xdist -ydist -rn 3.100000 -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 2131+1042_3_spec0028of.fits 2131+1042_3_spec0028ofm.fits '(i1-i2)' 2131+1042_3_spec0028ofo.fits
	touch stage-skysub

stage-objaps: stage-skysub
# Generate Object Positions.
	echo Stage:  Generate Object Positions.
	mkorderaps -root 2131+1042_3- -ydist -bigsky -oo 5 -ex -nsig 1.762500 2131+1042_3_spec0028ofo.fits
	cat 2131+1042_3_spec0028ofo.aps > 2131+1042_3.aps
	touch stage-objaps

stage-multiunrect: stage-objaps
# Run Multiunrect.
	echo Stage:  Run Multiunrect.
	multiunrect 2131+1042_3.aps -defsample -xdist -ydist -wdist -fwhm 4.000000 -spass 1 -mx 1 -my 1 -ncut 0.000000 -ccut 0.000000 -rn 3.100000 -g ${GAIN} -xsub 1 -ysub 3 -extra 2131+1042_3_lamp0001f.fits -uniform
	mikejoin 2131+1042_3-???sum.fits -r -o 2131+1042_3_sum.fits
	mikejoin 2131+1042_3-???sumsky.fits -r -o 2131+1042_3_sky.fits
	mikejoin 2131+1042_3-???sumsig.fits -r -o 2131+1042_3_sig.fits
	mikejoin 2131+1042_3-???sumext.fits -r -o 2131+1042_3_ext.fits
	touch stage-multiunrect

stage-multispec: stage-multiunrect
# Run extractmultispec.
	echo Stage:  Run extractmultispec.
	extractmultispec 2131+1042_3-???sum.fits -extra lamp -nsig 100.000000 -o 2131+1042_3_multispec.fits -d 2131+1042_3_multispec.ps/vcps
	touch stage-multispec

stage-finalize: stage-multispec
# Copy Final Products.
	echo Stage:  Copy Final Products.
	cp 2131+1042_3_sum.fits 2131+1042_3_sky.fits 2131+1042_3_sig.fits 2131+1042_3_ext.fits 2131+1042_3_multispec.fits ../Final-Products
	touch stage-finalize

../targ_2131+1042_3:
	touch ../targ_2131+1042_3

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 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_spec0007ofm.fits thar1_spec0008ofm.fits thar1_spec0009ofm.fits thar1_spec0010ofm.fits thar1_spec0011ofm.fits thar1_spec0012ofm.fits thar1_spec0007ofo.fits thar1_spec0008ofo.fits thar1_spec0009ofo.fits thar1_spec0010ofo.fits thar1_spec0011ofo.fits thar1_spec0012ofo.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_spec0013ofm.fits thar30_spec0014ofm.fits thar30_spec0015ofm.fits thar30_spec0013ofo.fits thar30_spec0014ofo.fits thar30_spec0015ofo.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_spec0016ofm.fits xefl-o_spec0017ofm.fits xefl-o_spec0018ofm.fits xefl-o_spec0019ofm.fits xefl-o_spec0020ofm.fits xefl-o_spec0016ofo.fits xefl-o_spec0017ofo.fits xefl-o_spec0018ofo.fits xefl-o_spec0019ofo.fits xefl-o_spec0020ofo.fits 1227+0757_sky_spec0021.fits 1227+0757_sky_lamp0001.fits 1227+0757_sky_spec0021o.fits 1227+0757_sky_spec0021of.fits 1227+0757_sky_lamp0001f.fits 1227+0757_sky_spec0021ofm.fits 1227+0757_sky_spec0021ofo.fits 1342+0530_spec0022.fits 1342+0530_lamp0001.fits 1342+0530_spec0023.fits 1342+0530_spec0024.fits 1342+0530_spec0025.fits 1342+0530_spec0022o.fits 1342+0530_spec0023o.fits 1342+0530_spec0024o.fits 1342+0530_spec0025o.fits 1342+0530_spec0022of.fits 1342+0530_lamp0001f.fits 1342+0530_spec0023of.fits 1342+0530_spec0024of.fits 1342+0530_spec0025of.fits 1342+0530_spec0022ofm.fits 1342+0530_spec0023ofm.fits 1342+0530_spec0024ofm.fits 1342+0530_spec0025ofm.fits 1342+0530_spec0022ofo.fits 1342+0530_spec0023ofo.fits 1342+0530_spec0024ofo.fits 1342+0530_spec0025ofo.fits 2131+1042_1_spec0026.fits 2131+1042_1_lamp0001.fits 2131+1042_1_spec0026o.fits 2131+1042_1_spec0026of.fits 2131+1042_1_lamp0001f.fits 2131+1042_1_spec0026ofm.fits 2131+1042_1_spec0026ofo.fits 2131+1042_2_spec0027.fits 2131+1042_2_lamp0001.fits 2131+1042_2_spec0027o.fits 2131+1042_2_spec0027of.fits 2131+1042_2_lamp0001f.fits 2131+1042_2_spec0027ofm.fits 2131+1042_2_spec0027ofo.fits 2131+1042_3_spec0028.fits 2131+1042_3_lamp0001.fits 2131+1042_3_spec0028o.fits 2131+1042_3_spec0028of.fits 2131+1042_3_lamp0001f.fits 2131+1042_3_spec0028ofm.fits 2131+1042_3_spec0028ofo.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

