#Extract the parts of the spectra using 9 masks
imdel tmp1,tmpe.fits,tmpe_?.fits,FMSAfilenox?.fits,check,bkg,tmpe_bpm.fits,tmpe_?_bpm.fits
!echo "1 0 0 0 0 0 0 0 0" > extract.dat
!echo "0 1 0 0 0 0 0 0 0" >> extract.dat
!echo "0 0 1 0 0 0 0 0 0" >> extract.dat
!echo "0 0 0 1 0 0 0 0 0" >> extract.dat
!echo "0 0 0 0 1 0 0 0 0" >> extract.dat
!echo "0 0 0 0 0 1 0 0 0" >> extract.dat
!echo "0 0 0 0 0 0 1 0 0" >> extract.dat
!echo "0 0 0 0 0 0 0 1 0" >> extract.dat
!echo "0 0 0 0 0 0 0 0 1" >> extract.dat
imcopy FMSAfilenom tmp1.fits
#1#!../quadcopy tmp1.fits -1e10 1
#2#!../quadcopy tmp1.fits -1e10 2
#3#!../quadcopy tmp1.fits -1e10 3
#4#!../quadcopy tmp1.fits -1e10 4
imdel tmp2,tmp2a,tmp2a_?.fits,tmp3a,tmp4a,check,bkg,tmp2_bpm,tmp2a_bpm,tmp2a_?_bpm.fits
imcopy tmp1[1:1024,*] tmp2.fits
imarith FMSAfilenom_bpm[1:1024,*] * 1. tmp2_bpm.fits
!../extract tmp2.fits tmp2a.fits irs1_cont_BANDe1.dat SHIFT
imrename check tmp3a
imrename bkg tmp4a
imdel tmp2,tmp2b,tmp2b_?.fits,tmp3b,tmp4b,tmp2_bpm,tmp2b_bpm,tmp2b_?_bpm.fits
imcopy tmp1[1025:2048,*] tmp2.fits
imarith FMSAfilenom_bpm[1025:2048,*] * 1. tmp2_bpm.fits
!../extract tmp2.fits tmp2b.fits irs1_cont_BANDe2.dat SHIFT
imrename check tmp3b
imrename bkg tmp4b
!echo "0 0" > tmp.offset
!echo "1024 0" >> tmp.offset
imcomb tmp2a,tmp2b tmpe.fits offset=tmp.offset
imcomb tmp2a_1,tmp2b_1 tmpe_1.fits offset=tmp.offset
imcomb tmp2a_2,tmp2b_2 tmpe_2.fits offset=tmp.offset
imcomb tmp2a_3,tmp2b_3 tmpe_3.fits offset=tmp.offset
imcomb tmp2a_4,tmp2b_4 tmpe_4.fits offset=tmp.offset
imcomb tmp2a_5,tmp2b_5 tmpe_5.fits offset=tmp.offset
imcomb tmp2a_6,tmp2b_6 tmpe_6.fits offset=tmp.offset
imcomb tmp2a_7,tmp2b_7 tmpe_7.fits offset=tmp.offset
imcomb tmp2a_8,tmp2b_8 tmpe_8.fits offset=tmp.offset
imcomb tmp2a_bpm,tmp2b_bpm tmpe_bpm.fits offset=tmp.offset
imcomb tmp2a_1_bpm,tmp2b_1_bpm tmpe_1_bpm.fits offset=tmp.offset
imcomb tmp2a_2_bpm,tmp2b_2_bpm tmpe_2_bpm.fits offset=tmp.offset
imcomb tmp2a_3_bpm,tmp2b_3_bpm tmpe_3_bpm.fits offset=tmp.offset
imcomb tmp2a_4_bpm,tmp2b_4_bpm tmpe_4_bpm.fits offset=tmp.offset
imcomb tmp2a_5_bpm,tmp2b_5_bpm tmpe_5_bpm.fits offset=tmp.offset
imcomb tmp2a_6_bpm,tmp2b_6_bpm tmpe_6_bpm.fits offset=tmp.offset
imcomb tmp2a_7_bpm,tmp2b_7_bpm tmpe_7_bpm.fits offset=tmp.offset
imcomb tmp2a_8_bpm,tmp2b_8_bpm tmpe_8_bpm.fits offset=tmp.offset
imcomb tmp3a,tmp3b check.fits offset=tmp.offset
imcomb tmp4a,tmp4b bkg.fits offset=tmp.offset
median bkg bkg 27 9
blkav bkg bkg 1 9
magnify bkg bkg 1. 25. y1=1.01 y2=9.0 interpo=linear boundar=nearest fluxcon-
#Correct the spectral distorsion
imdel tmps.fits,tmps_bpm.fits
imarith tmpe.fits - bkg tmps.fits
imrename tmpe_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox1.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_1.fits - bkg tmps.fits
imrename tmpe_1_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox2.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_2.fits - bkg tmps.fits
imrename tmpe_2_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox3.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_3.fits - bkg tmps.fits
imrename tmpe_3_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox4.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_4.fits - bkg tmps.fits
imrename tmpe_4_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox5.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_5.fits - bkg tmps.fits
imrename tmpe_5_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox6.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_6.fits - bkg tmps.fits
imrename tmpe_6_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox7.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_7.fits - bkg tmps.fits
imrename tmpe_7_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox8.fits irs1_cont_BANDa.dat
imdel tmps.fits,tmps_bpm.fits
imarith tmpe_8.fits - bkg tmps.fits
imrename tmpe_8_bpm.fits tmps_bpm.fits
!../align tmps.fits FMSAfilenox9.fits irs1_cont_BANDa.dat
!echo "0.5 1 1 1 1 1 1 1 0.5" > extract.dat
#Prepare object mask
imdel tmp1,tmp2
imcomb FMSAfilenox4,FMSAfilenox5,FMSAfilenox6 tmp1
imcomb FMSAfilenox1,FMSAfilenox2,FMSAfilenox3,FMSAfilenox7,FMSAfilenox8,FMSAfilenox9 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=100
imrepl tmp1  0. upper=INDEF lower=100
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 FMSAfilenox1 / irs1_cont_BANDa FMSAfilenox1
!../fixbadpix FMSAfilenox1.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox1 - tmp1 FMSAfilenox1
imarith FMSAfilenox1 * irs1_cont_BANDa FMSAfilenox1
imdel tmp1
imarith FMSAfilenox2 / irs1_cont_BANDa FMSAfilenox2
!../fixbadpix FMSAfilenox2.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox2 - tmp1 FMSAfilenox2
imarith FMSAfilenox2 * irs1_cont_BANDa FMSAfilenox2
imdel tmp1
imarith FMSAfilenox3 / irs1_cont_BANDa FMSAfilenox3
!../fixbadpix FMSAfilenox3.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox3 - tmp1 FMSAfilenox3
imarith FMSAfilenox3 * irs1_cont_BANDa FMSAfilenox3
imdel tmp1
imarith FMSAfilenox4 / irs1_cont_BANDa FMSAfilenox4
!../fixbadpix FMSAfilenox4.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox4 - tmp1 FMSAfilenox4
imarith FMSAfilenox4 * irs1_cont_BANDa FMSAfilenox4
imdel tmp1
imarith FMSAfilenox5 / irs1_cont_BANDa FMSAfilenox5
!../fixbadpix FMSAfilenox5.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox5 - tmp1 FMSAfilenox5
imarith FMSAfilenox5 * irs1_cont_BANDa FMSAfilenox5
imdel tmp1
imarith FMSAfilenox6 / irs1_cont_BANDa FMSAfilenox6
!../fixbadpix FMSAfilenox6.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox6 - tmp1 FMSAfilenox6
imarith FMSAfilenox6 * irs1_cont_BANDa FMSAfilenox6
imdel tmp1
imarith FMSAfilenox7 / irs1_cont_BANDa FMSAfilenox7
!../fixbadpix FMSAfilenox7.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox7 - tmp1 FMSAfilenox7
imarith FMSAfilenox7 * irs1_cont_BANDa FMSAfilenox7
imdel tmp1
imarith FMSAfilenox8 / irs1_cont_BANDa FMSAfilenox8
!../fixbadpix FMSAfilenox8.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox8 - tmp1 FMSAfilenox8
imarith FMSAfilenox8 * irs1_cont_BANDa FMSAfilenox8
imdel tmp1
imarith FMSAfilenox9 / irs1_cont_BANDa FMSAfilenox9
!../fixbadpix FMSAfilenox9.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=10 high_r=3 low_r=3 nite=10 grow=0
imarith FMSAfilenox9 - tmp1 FMSAfilenox9
imarith FMSAfilenox9 * irs1_cont_BANDa FMSAfilenox9
#Merge 9 images
!echo "0.5 1 1 1 1 1 1 1 0.5" > extract.dat
imdel FMSAfilenoxp.fits,FMSAfilenoxp_bpm.fits,FMSAfileno_mask.fits
!../pileup FMSAfilenox
imdel FMSAfilenox1,FMSAfilenox2,FMSAfilenox3,FMSAfilenox4,FMSAfilenox5,FMSAfilenox6,FMSAfilenox7,FMSAfilenox8,FMSAfilenox9
#1#!../quadcopy FMSAfilenoxp.fits 0 1
#2#!../quadcopy FMSAfilenoxp.fits 0 2
#3#!../quadcopy FMSAfilenoxp.fits 0 3
#4#!../quadcopy FMSAfilenoxp.fits 0 4
displ FMSAfilenoxp 1
imrename FMSAfilenox1_bpm.fits FMSAfilenox_bpm1.fits
imrename FMSAfilenox2_bpm.fits FMSAfilenox_bpm2.fits
imrename FMSAfilenox3_bpm.fits FMSAfilenox_bpm3.fits
imrename FMSAfilenox4_bpm.fits FMSAfilenox_bpm4.fits
imrename FMSAfilenox5_bpm.fits FMSAfilenox_bpm5.fits
imrename FMSAfilenox6_bpm.fits FMSAfilenox_bpm6.fits
imrename FMSAfilenox7_bpm.fits FMSAfilenox_bpm7.fits
imrename FMSAfilenox8_bpm.fits FMSAfilenox_bpm8.fits
imrename FMSAfilenox9_bpm.fits FMSAfilenox_bpm9.fits
!../pileup FMSAfilenox_bpm
imdel FMSAfilenox_bpm1,FMSAfilenox_bpm2,FMSAfilenox_bpm3,FMSAfilenox_bpm4,FMSAfilenox_bpm5,FMSAfilenox_bpm6,FMSAfilenox_bpm7,FMSAfilenox_bpm8,FMSAfilenox_bpm9
imrename FMSAfilenox_bpmp.fits FMSAfilenoxp_bpm.fits
