#Flat-fielding and badpix rejection
imdel tmp0,tmp1,mask,mask2,FMSAfilenom.fits,FMSAfilenom_bpm.fits
imcopy FMSAfileno tmp0.fits
#Cross talk subtraction
rmcross_ tmp0.fits tmp0.fits
###Stripe pattern subtraction
#rmstripe1 tmp0.fits tmp0.fits
#rmstripe2 tmp0.fits tmp0.fits
###Quadrant level adjustment
quadlev tmp0.fits tmp0.fits
###
imarith tmp0.fits / ../flat_ tmp0.fits
displ tmp0 1 zs+
median tmp0 tmp1 3 3
imarith tmp0 - tmp1 tmp1
imarith tmp1 * ../badpixr_ tmp1
imfunc tmp1 tmp1 abs
imrepl tmp1 65000. upper=INDEF lower=65000.
!rm tmp.tbl tmp1.tbl
imhist tmp1 z1=-50 z2=65050 binwidt=100 nbins=651 hist_type=cumulative listout+ > tmp.tbl
###Number of rejected bad pixels with high count.
###(If you want to apply this script to a raw image, use upper line.)
#!../thresh tmp.tbl 1300 tmp1 > tmp1.tbl
!../thresh tmp.tbl 600 tmp1 > tmp1.tbl
cl < tmp1.tbl
imstati tmp1 lower=INDEF upper=0.5
imarith ../badpixr_ * tmp1 mask.fits
convolve mask.fits mask.fits "0. 0.2 0.; 0.2 0.2 0.2; 0. 0.2 0."
imrepl mask 0. lower=INDEF upper=0.9
displ mask 2 zs- zr- z1=1 z2=0
imcopy mask FMSAfilenom_bpm.fits
imstati mask lower=INDEF upper=0.5
imcopy ../badpix2r mask2.fits
!../fixbadpix tmp0.fits FMSAfilenom.fits
###Stripe pattern subtraction
#rmstripe3 FMSAfilenom.fits FMSAfilenom.fits
###Dark pattern subtraction
#!../fit0lev FMSAfilenom.fits FMSAfilenom.fits
###The center gap should be at the center of the image.
#imshift FMSAfilenom FMSAfilenom 313 0 boundar=constant constan=0.
#imshift FMSAfilenom FMSAfilenom 131 0 boundar=constant constan=0.
#imshift FMSAfilenom FMSAfilenom 217 0 boundar=constant constan=0.
displ FMSAfilenom 3 zs+

#Deformation and extract spectra
imdel tmp1,FMSAfilenoe.fits,FMSAfilenos.fits,FMSAfilenoa.fits,FMSAfilenox.fits,check,bkg
!echo "0.5 1 1 1 1 1 1 1 0.5" > 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,tmp3a,tmp4a,tmp2_bpm,tmp2a_bpm
imcopy tmp1[1:1024,*] tmp2.fits
imcopy FMSAfilenom_bpm[1:1024,*] tmp2_bpm.fits
!../extract tmp2.fits tmp2a.fits irs2_cont_BANDe1.dat SHIFT
imrename check tmp3a
imrename bkg tmp4a
imdel tmp2,tmp2b,tmp3b,tmp4b,tmp2_bpm,tmp2b_bpm
imcopy tmp1[1025:2048,*] tmp2.fits
imcopy FMSAfilenom_bpm[1025:2048,*] tmp2_bpm.fits
!../extract tmp2.fits tmp2b.fits irs2_cont_BANDe2.dat SHIFT
imrename check tmp3b
imrename bkg tmp4b
!echo "0 0" > tmp.offset
!echo "1024 0" >> tmp.offset
imcomb tmp2a,tmp2b FMSAfilenoe.fits offset=tmp.offset
imcomb tmp2a_bpm,tmp2b_bpm FMSAfilenoe_bpm.fits offset=tmp.offset
imcomb tmp3a,tmp3b check.fits offset=tmp.offset
imcomb tmp4a,tmp4b bkg.fits offset=tmp.offset
displ check 4 zs+
displ FMSAfilenoe 5 zs+
median bkg bkg 27 9
!../extract2 bkg.fits bkg.fits
magnify bkg bkg 1. 25. y1=1.01 y2=9.0 interpo=linear boundar=nearest fluxcon-
displ bkg 6 zs+
imarith FMSAfilenoe.fits - bkg FMSAfilenos.fits
displ FMSAfilenos 7 zs+
imrename FMSAfilenoe_bpm.fits FMSAfilenos_bpm.fits
!../align FMSAfilenos.fits FMSAfilenoa.fits irs2_cont_BANDa.dat
#!../align FMSAfilenoe.fits FMSAfilenoa.fits irs2_cont_BANDa.dat
displ FMSAfilenoa 8 zs+
imdel tmp1
imarith FMSAfilenoa / irs2_cont_BANDa tmp1
#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 difference axis=2 inter- function=spline3 order=10 high_reject=3 low_reject=3 niterate=10 grow=0
#1#!../quadcopy tmp1.fits 0 1
#2#!../quadcopy tmp1.fits 0 2
#3#!../quadcopy tmp1.fits 0 3
#4#!../quadcopy tmp1.fits 0 4
imarith tmp1 * irs2_cont_BANDa FMSAfilenox.fits
### If the cont image is not good, use this line instead of above three lines.
#fit1d FMSAfilenoa tmp1 difference axis=2 inter- function=spline3 order=10 high_reject=3 low_reject=3 niterate=10 grow=0
displ FMSAfilenox 9 zs+
