
# Pipeline for Target 2131+1042_1

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

stage-overscan:
# Overscan Subtraction, Trimming, and Image Reorientation.
	echo Stage:  Overscan Subtraction, Trimming, and Image Reorientation.
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0026.fits 2131+1042_1_spec0026.fits
	magebias /data1/Data/MagE/processed_2d/../ut170530_31/mage0001.fits 2131+1042_1_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_1_spec0026.fits
	copyrect -ydist lamp.fits 2131+1042_1_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_1_lamp0001.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits 2131+1042_1_spec0026.fits
	touch stage-orders-copy

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

stage-xdist-copy: stage-xdist
# Copying X Distortion.
	echo Stage:  Copying X Distortion.
	copyrect -xdist 2131+1042_1_lamp0001b.fits 2131+1042_1_spec0026.fits
	copyrect -xdist 2131+1042_1_lamp0001b.fits 2131+1042_1_lamp0001.fits
	touch stage-xdist-copy

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

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

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

stage-wdist-copy: stage-wdist
	copyecords 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_lamp0001.fits
	copyecords 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_lamp0001b.fits

	copyecords 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_spec0026.fits

	copyrect -wdist 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_spec0026.fits
	copyrect -wdist 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_lamp0001.fits

	copyrect -edist 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_spec0026.fits
	copyrect -edist 2131+1042_1_lamp0001bspecs.fits 2131+1042_1_lamp0001.fits
	touch stage-wdist-copy

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

stage-flatten: stage-subscat
# Flattening.
	echo Stage:  Flattening.
	flat2d -low 0.2 flat.fits 2131+1042_1_spec0026o.fits
	flat2d -low 0.2 flat.fits 2131+1042_1_lamp0001.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 2131+1042_1_spec0026of.fits
	mageflat1d -fft slit.fits 2131+1042_1_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 2131+1042_1_spec0026ofs.fits
	touch stage-skyaps

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

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

stage-multiunrect: stage-objaps
# Run Multiunrect.
	echo Stage:  Run Multiunrect.
	multiunrect 2131+1042_1.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_1_lamp0001fs.fits -uniform
	mikejoin 2131+1042_1-???sum.fits -r -o 2131+1042_1_sum.fits
	mikejoin 2131+1042_1-???sumsky.fits -r -o 2131+1042_1_sky.fits
	mikejoin 2131+1042_1-???sumsig.fits -r -o 2131+1042_1_sig.fits
	mikejoin 2131+1042_1-???sumext.fits -r -o 2131+1042_1_ext.fits
	touch stage-multiunrect

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

stage-finalize: stage-multispec
# Copy Final Products.
	echo Stage:  Copy Final Products.
	cp 2131+1042_1_sum.fits 2131+1042_1_sky.fits 2131+1042_1_sig.fits 2131+1042_1_ext.fits 2131+1042_1_multispec.fits ../Final-Products
	touch stage-finalize

../targ_2131+1042_1:
	touch ../targ_2131+1042_1

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 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_spec0021ofs.fits 1227+0757_sky_lamp0001fs.fits 1227+0757_sky_spec0021ofsm.fits 1227+0757_sky_spec0021ofso.fits 1342+0530_1_spec0022.fits 1342+0530_1_lamp0001.fits 1342+0530_1_spec0022o.fits 1342+0530_1_spec0022of.fits 1342+0530_1_lamp0001f.fits 1342+0530_1_spec0022ofs.fits 1342+0530_1_lamp0001fs.fits 1342+0530_1_spec0022ofsm.fits 1342+0530_1_spec0022ofso.fits 1342+0530_2_spec0023.fits 1342+0530_2_lamp0001.fits 1342+0530_2_spec0023o.fits 1342+0530_2_spec0023of.fits 1342+0530_2_lamp0001f.fits 1342+0530_2_spec0023ofs.fits 1342+0530_2_lamp0001fs.fits 1342+0530_2_spec0023ofsm.fits 1342+0530_2_spec0023ofso.fits 1342+0530_3_spec0024.fits 1342+0530_3_lamp0001.fits 1342+0530_3_spec0024o.fits 1342+0530_3_spec0024of.fits 1342+0530_3_lamp0001f.fits 1342+0530_3_spec0024ofs.fits 1342+0530_3_lamp0001fs.fits 1342+0530_3_spec0024ofsm.fits 1342+0530_3_spec0024ofso.fits 1342+0530_4_spec0025.fits 1342+0530_4_lamp0001.fits 1342+0530_4_spec0025o.fits 1342+0530_4_spec0025of.fits 1342+0530_4_lamp0001f.fits 1342+0530_4_spec0025ofs.fits 1342+0530_4_lamp0001fs.fits 1342+0530_4_spec0025ofsm.fits 1342+0530_4_spec0025ofso.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_spec0026ofs.fits 2131+1042_1_lamp0001fs.fits 2131+1042_1_spec0026ofsm.fits 2131+1042_1_spec0026ofso.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

