#Extract the parts of the spectra using 9 masks
!echo "1 0 0 0 0 0 0 0 0" > extract.dat
imdel FMSAfilenoy1.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy1.fits
!echo "0 1 0 0 0 0 0 0 0" > extract.dat
imdel FMSAfilenoy2.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy2.fits
!echo "0 0 1 0 0 0 0 0 0" > extract.dat
imdel FMSAfilenoy3.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy3.fits
!echo "0 0 0 1 0 0 0 0 0" > extract.dat
imdel FMSAfilenoy4.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy4.fits
!echo "0 0 0 0 1 0 0 0 0" > extract.dat
imdel FMSAfilenoy5.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy5.fits
!echo "0 0 0 0 0 1 0 0 0" > extract.dat
imdel FMSAfilenoy6.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy6.fits
!echo "0 0 0 0 0 0 1 0 0" > extract.dat
imdel FMSAfilenoy7.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy7.fits
!echo "0 0 0 0 0 0 0 1 0" > extract.dat
imdel FMSAfilenoy8.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy8.fits
!echo "0 0 0 0 0 0 0 0 1" > extract.dat
imdel FMSAfilenoy9.fits
!../extract2 FMSAfilenoxpb.fits FMSAfilenoy9.fits
!echo "0.5 1 1 1 1 1 1 1 0.5" > extract.dat
#Prepare object mask
imdel tmp1,tmp2
imcomb FMSAfilenoy4,FMSAfilenoy5,FMSAfilenoy6 tmp1
imcomb FMSAfilenoy1,FMSAfilenoy2,FMSAfilenoy3,FMSAfilenoy7,FMSAfilenoy8,FMSAfilenoy9 tmp2
imarith tmp1 - tmp2 tmp1
fit1d tmp1 tmp1 fit axis=1 inter- function=legendre order=1
imfunc tmp1 tmp1 abs
imrepl tmp1 -1. lower=INDEF upper=10
imrepl tmp1  0. upper=INDEF lower=10
imarith tmp1 * -1 tmp1
imdel mask,mask2
imcopy tmp1 mask.fits
imarith mask.fits * FMSAfileno_mask.fits mask.fits
#Fit and subtract the background using object mask
imdel tmp1
imarith FMSAfilenoy1 / irs1_cont_BANDa FMSAfilenoy1
!../fixbadpix FMSAfilenoy1.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy1 - tmp1 FMSAfilenoy1
imarith FMSAfilenoy1 * irs1_cont_BANDa FMSAfilenoy1
imdel tmp1
imarith FMSAfilenoy2 / irs1_cont_BANDa FMSAfilenoy2
!../fixbadpix FMSAfilenoy2.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy2 - tmp1 FMSAfilenoy2
imarith FMSAfilenoy2 * irs1_cont_BANDa FMSAfilenoy2
imdel tmp1
imarith FMSAfilenoy3 / irs1_cont_BANDa FMSAfilenoy3
!../fixbadpix FMSAfilenoy3.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy3 - tmp1 FMSAfilenoy3
imarith FMSAfilenoy3 * irs1_cont_BANDa FMSAfilenoy3
imdel tmp1
imarith FMSAfilenoy4 / irs1_cont_BANDa FMSAfilenoy4
!../fixbadpix FMSAfilenoy4.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy4 - tmp1 FMSAfilenoy4
imarith FMSAfilenoy4 * irs1_cont_BANDa FMSAfilenoy4
imdel tmp1
imarith FMSAfilenoy5 / irs1_cont_BANDa FMSAfilenoy5
!../fixbadpix FMSAfilenoy5.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy5 - tmp1 FMSAfilenoy5
imarith FMSAfilenoy5 * irs1_cont_BANDa FMSAfilenoy5
imdel tmp1
imarith FMSAfilenoy6 / irs1_cont_BANDa FMSAfilenoy6
!../fixbadpix FMSAfilenoy6.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy6 - tmp1 FMSAfilenoy6
imarith FMSAfilenoy6 * irs1_cont_BANDa FMSAfilenoy6
imdel tmp1
imarith FMSAfilenoy7 / irs1_cont_BANDa FMSAfilenoy7
!../fixbadpix FMSAfilenoy7.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy7 - tmp1 FMSAfilenoy7
imarith FMSAfilenoy7 * irs1_cont_BANDa FMSAfilenoy7
imdel tmp1
imarith FMSAfilenoy8 / irs1_cont_BANDa FMSAfilenoy8
!../fixbadpix FMSAfilenoy8.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy8 - tmp1 FMSAfilenoy8
imarith FMSAfilenoy8 * irs1_cont_BANDa FMSAfilenoy8
imdel tmp1
imarith FMSAfilenoy9 / irs1_cont_BANDa FMSAfilenoy9
!../fixbadpix FMSAfilenoy9.fits tmp1.fits y
#1#!../quadcopy tmp1.fits 3 1
#2#!../quadcopy tmp1.fits 4 2
#3#!../quadcopy tmp1.fits 1 3
#4#!../quadcopy tmp1.fits 2 4
fit1d tmp1 tmp1 fit axis=2 inter- function=spline3 order=20 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenoy9 - tmp1 FMSAfilenoy9
imarith FMSAfilenoy9 * irs1_cont_BANDa FMSAfilenoy9
#Merge 9 images
imdel FMSAfilenoyp.fits
imrename mask.fits FMSAfileno_mask.fits
!../pileup FMSAfilenoy
imdel FMSAfilenoy1,FMSAfilenoy2,FMSAfilenoy3,FMSAfilenoy4,FMSAfilenoy5,FMSAfilenoy6,FMSAfilenoy7,FMSAfilenoy8,FMSAfilenoy9
#1#!../quadcopy FMSAfilenoyp.fits 0 1
#2#!../quadcopy FMSAfilenoyp.fits 0 2
#3#!../quadcopy FMSAfilenoyp.fits 0 3
#4#!../quadcopy FMSAfilenoyp.fits 0 4
displ FMSAfilenoyp 2
