
# Pipeline for Target ngc524_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-mkmask stage-extractall stage-finalize stage-reporting ../targ_ngc524_1

stage-overscan:
# Overscan Subtraction, Trimming, and Image Reorientation.
	echo Stage:  Overscan Subtraction, Trimming, and Image Reorientation.
	magebias /Users/yuri/Desktop/NGC524/Reduction/data/mage1001.fits ngc524_1_spec1001.fits
	magebias /Users/yuri/Desktop/NGC524/Reduction/data/mage0066.fits ngc524_1_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_1_spec1001.fits
	copyrect -ydist lamp.fits ngc524_1_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_1_lamp0066.fits
	copyslit -fwhm 20.000000 -ydist -fft lamp.fits ngc524_1_spec1001.fits
	touch stage-orders-copy

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

stage-xdist-copy: stage-xdist
# Copying X Distortion.
	echo Stage:  Copying X Distortion.
	copyrect -xdist ngc524_1_lamp0066b.fits ngc524_1_spec1001.fits
	copyrect -xdist ngc524_1_lamp0066b.fits ngc524_1_lamp0066.fits
	touch stage-xdist-copy

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

	mageFindLines ngc524_1_lamp0066bspecs.fits -fwhm 1.250000 -th 3.000000

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

stage-wdist-copy: stage-wdist
	copyecords ngc524_1_lamp0066bspecs.fits ngc524_1_lamp0066.fits
	copyecords ngc524_1_lamp0066bspecs.fits ngc524_1_lamp0066b.fits

	copyecords ngc524_1_lamp0066bspecs.fits ngc524_1_spec1001.fits

	copyrect -wdist ngc524_1_lamp0066bspecs.fits ngc524_1_spec1001.fits
	copyrect -wdist ngc524_1_lamp0066bspecs.fits ngc524_1_lamp0066.fits

	copyrect -edist ngc524_1_lamp0066bspecs.fits ngc524_1_spec1001.fits
	copyrect -edist ngc524_1_lamp0066bspecs.fits ngc524_1_lamp0066.fits
	touch stage-wdist-copy

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

stage-flatten: stage-subscat
# Flattening.
	echo Stage:  Flattening.
	flat2d -low 0.2 flat.fits ngc524_1_spec1001o.fits
	flat2d -low 0.2 flat.fits ngc524_1_lamp0066.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 ngc524_1_spec1001of.fits
	mageflat1d -fft slit.fits ngc524_1_lamp0066f.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 ngc524_1_spec1001ofs.fits
	touch stage-skyaps

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

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

stage-mkmask: stage-objaps
# Make a quick mask image of bad pixels
	echo Stage:  Make a quick mask image of bad pixels
	efits flat.fits "1.0*greater(i1,0.25)" mask.fits
	touch stage-mkmask

stage-extractall: stage-mkmask
# Gauss-Hermite-Legendre-BSpline Extractions
	echo Stage:  Gauss-Hermite-Legendre-BSpline Extractions
	time GHLBExtract -p -npass 1 -nfactor 0 -rn 3.100000 -g ${GAIN} -kx 5 -ky 0 -yx 4 -ya 4 -sx 2 -sa 1 -hy 4 -hx 2 -ha 1 -lamp ngc524_1_lamp0066fs.fits -mask mask.fits -o ngc524_1_multi.fits -header ngc524_1_spec1001.fits ngc524_1_spec1001ofso.aps
	touch stage-extractall

stage-finalize: stage-extractall
# Copy Final Products.
	echo Stage:  Copy Final Products.
	cp ngc524_1_multi.fits ../Final-Products
	touch stage-finalize

stage-reporting: stage-finalize
# Generate html and pdf reports.
	echo Stage:  Generate html and pdf reports.
	touch stage-reporting

../targ_ngc524_1:
	touch ../targ_ngc524_1

clean:
	rm -f hd18751_spec0078.fits hd18751_lamp0081.fits hd18751_spec0079.fits hd18751_spec0080.fits hd18751_spec0078o.fits hd18751_spec0079o.fits hd18751_spec0080o.fits hd18751_spec0078of.fits hd18751_lamp0081f.fits hd18751_spec0079of.fits hd18751_spec0080of.fits hd18751_spec0078ofs.fits hd18751_lamp0081fs.fits hd18751_spec0079ofs.fits hd18751_spec0080ofs.fits hd18751_spec0078ofsm.fits hd18751_spec0079ofsm.fits hd18751_spec0080ofsm.fits hd18751_spec0078ofso.fits hd18751_spec0079ofso.fits hd18751_spec0080ofso.fits ltt1020_spec0087.fits ltt1020_lamp0088.fits ltt1020_spec0087o.fits ltt1020_spec0087of.fits ltt1020_lamp0088f.fits ltt1020_spec0087ofs.fits ltt1020_lamp0088fs.fits ltt1020_spec0087ofsm.fits ltt1020_spec0087ofso.fits ngc524_1_spec1001.fits ngc524_1_lamp0066.fits ngc524_1_spec1001o.fits ngc524_1_spec1001of.fits ngc524_1_lamp0066f.fits ngc524_1_spec1001ofs.fits ngc524_1_lamp0066fs.fits ngc524_1_spec1001ofsm.fits ngc524_1_spec1001ofso.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-mkmask stage-extractall stage-finalize stage-reporting

