
# Pipeline for Target ngc524_chil

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_ngc524_chil

stage-overscan:
# Overscan Subtraction, Trimming, and Image Reorientation.
	echo Stage:  Overscan Subtraction, Trimming, and Image Reorientation.
	magebias /Users/yuri/Desktop/NGC524/Reduction/data/mage3101.fits ngc524_chil_spec3101.fits
	magebias /Users/yuri/Desktop/NGC524/Reduction/data/mage0066.fits ngc524_chil_lamp0066.fits
	touch stage-overscan

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

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

stage-xdist-copy: stage-xdist
# Copying X Distortion.
	echo Stage:  Copying X Distortion.
	copyrect -xdist ngc524_chil_lamp0066b.fits ngc524_chil_spec3101.fits
	copyrect -xdist ngc524_chil_lamp0066b.fits ngc524_chil_lamp0066.fits
	touch stage-xdist-copy

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

	mageFindLines ngc524_chil_lamp0066bspecs.fits -fwhm 1.250000 -th 3.000000

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

stage-wdist-copy: stage-wdist
	copyecords ngc524_chil_lamp0066bspecs.fits ngc524_chil_lamp0066.fits
	copyecords ngc524_chil_lamp0066bspecs.fits ngc524_chil_lamp0066b.fits

	copyecords ngc524_chil_lamp0066bspecs.fits ngc524_chil_spec3101.fits

	copyrect -wdist ngc524_chil_lamp0066bspecs.fits ngc524_chil_spec3101.fits
	copyrect -wdist ngc524_chil_lamp0066bspecs.fits ngc524_chil_lamp0066.fits

	copyrect -edist ngc524_chil_lamp0066bspecs.fits ngc524_chil_spec3101.fits
	copyrect -edist ngc524_chil_lamp0066bspecs.fits ngc524_chil_lamp0066.fits
	touch stage-wdist-copy

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

stage-flatten: stage-subscat
# Flattening.
	echo Stage:  Flattening.
	flat2d -low 0.2 flat.fits ngc524_chil_spec3101o.fits
	flat2d -low 0.2 flat.fits ngc524_chil_lamp0066.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 ngc524_chil_spec3101of.fits
	touch stage-skyaps

stage-skyrect: stage-skyaps
# Determine the Sky Background.
	echo Stage:  Determine the Sky Background.
	skyrect ngc524_chil_spec3101of.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 ngc524_chil_spec3101of.fits ngc524_chil_spec3101ofm.fits '(i1-i2)' ngc524_chil_spec3101ofo.fits
	touch stage-skysub

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

stage-multiunrect: stage-objaps
# Run Multiunrect.
	echo Stage:  Run Multiunrect.
	multiunrect ngc524_chil.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 ngc524_chil_lamp0066f.fits -uniform
	mikejoin ngc524_chil-???sum.fits -r -o ngc524_chil_sum.fits
	mikejoin ngc524_chil-???sumsky.fits -r -o ngc524_chil_sky.fits
	mikejoin ngc524_chil-???sumsig.fits -r -o ngc524_chil_sig.fits
	mikejoin ngc524_chil-???sumext.fits -r -o ngc524_chil_ext.fits
	touch stage-multiunrect

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

stage-finalize: stage-multispec
# Copy Final Products.
	echo Stage:  Copy Final Products.
	cp ngc524_chil_sum.fits ngc524_chil_sky.fits ngc524_chil_sig.fits ngc524_chil_ext.fits ngc524_chil_multispec.fits ../Final-Products
	touch stage-finalize

../targ_ngc524_chil:
	touch ../targ_ngc524_chil

clean:
	rm -f ngc524_chil_spec3101.fits ngc524_chil_lamp0066.fits ngc524_chil_spec3101o.fits ngc524_chil_spec3101of.fits ngc524_chil_lamp0066f.fits ngc524_chil_spec3101ofm.fits ngc524_chil_spec3101ofo.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

