#Extract the parts of the spectra using 9 masks
imdel 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
!../extract FMSAfilenom.fits tmpe.fits irs1_cont_BANDe.dat SHIFT
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
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
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
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
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
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
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
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
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
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
imdel FMSAfilenoxp.fits,FMSAfilenoxp_bpm.fits,FMSAfileno_mask.fits
!../pileup FMSAfilenox
imdel FMSAfilenox1,FMSAfilenox2,FMSAfilenox3,FMSAfilenox4,FMSAfilenox5,FMSAfilenox6,FMSAfilenox7,FMSAfilenox8,FMSAfilenox9
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
