#######################################
### Preparatory Processing for IRS1 ###
#######################################

### Dome Flat ###
!../headerinfo Data_directory/ 1
imdel tmp1,tmp2,tmp3
imcomb Flat_OFF1,Flat_OFF2,Flat_OFF3 tmp1
imcomb CAL_ON1,CAL_ON2 tmp2
imcomb Flat_ON1,Flat_ON2,Flat_ON3,Flat_ON4,Flat_ON5,Flat_ON6 tmp3
imarith tmp2 - tmp1 irs1_ar_MODE.fits
imarith tmp3 - tmp1 irs1_flat_MODE.fits
imcopy irs1_flat_MODE.fits irs1_cont_MODE.fits
#"irs1_cont_MODE.fits" is the fixed name.
!sed -e 's/BAND/MODE/g' ../redscr0s > tmpscr
# If you want to increase the speed of fitting by multi-core CPUs...
#!sed -e 's/extract /extract_multi /g' tmpscr > tmp0 && mv tmp0 tmpscr
cl < tmpscr
!sed -e 's/FMSAfileno/irs1_flat_MODE/g' ../redscr1s | sed -e 's/BAND/MODE/g' | sed -e 's/SHIFT/0/g' > tmpscr
cl < tmpscr

### CAL Lamp ###
!sed -e 's/FMSAfileno/irs1_ar_MODE/g' ../redscr1s | sed -e 's/BAND/MODE/g' | sed -e 's/SHIFT/0/g' > tmpscr
cl < tmpscr
imdel tmp1
median irs1_ar_MODEa tmp1.fits 1 21 boundar=reflect
imdel check,irs1_ar_MODEw.fits
!../wlmap tmp1.fits irs1_ar_MODEw.fits ../ThAr.dat 0.001 1.371 0.470 -0.028 0.004
#If fluorescent lamps are used intead of ThAr lamp...
#!../wlmap tmp1.fits irs1_ar_MODEw.fits ../FL_lamp.dat 0.001 1.371 0.470 -0.028 0.004
#Wavelength[um]=wcen+dw*x+dw2*x^2+dw3*x^3 (x=-1 to 1 from left to right)
#    step  wcen  dw    dw2    dw3
#js: 0.001 1.011 0.121 -0.001 0.000
#jm: 0.001 1.125 0.120 -0.001 0.000
#jl: 0.001 1.238 0.118 -0.001 0.000
#hs: 0.001 1.502 0.111 -0.002 0.000
#hm: 0.001 1.607 0.108 -0.002 0.000
#hl: 0.001 1.713 0.105 -0.002 -0.001
#lr: 0.001 1.371 0.470 -0.028 0.004
displ tmp1 1 zs+
displ check 2 zs- zr+
#Compair these two images (read data / model) to make sure the conversion is not failed.
#Reduce "step" parameter to restrict the initial search range to a narrower one.
#Change "wcen" and "dw" parameters to shift and enlarge the model image, respectively.
displ irs1_ar_MODEw 3 zs- zr+
#The image gives the wavelength at each pixel.

#######################################
### Preparatory Processing for IRS2 ###
#######################################

### Dome Flat ###
!../headerinfo Data_directory/ 2
imdel tmp1,tmp2,tmp3
imcomb Flat_OFF1,Flat_OFF2,Flat_OFF3 tmp1
imcomb CAL_ON1,CAL_ON2 tmp2
imcomb Flat_ON1,Flat_ON2,Flat_ON3,Flat_ON4,Flat_ON5,Flat_ON6 tmp3
imarith tmp2 - tmp1 irs2_ar_MODE.fits
imarith tmp3 - tmp1 irs2_flat_MODE.fits
imcopy irs2_flat_MODE.fits irs2_cont_MODE.fits
#In the case of Js band of IRS2...
#imarith irs2_flat_js.fits + irs2_ar_js.fits irs2_cont_js.fits
#"irs2_cont_MODE.fits" is the fixed name.
!sed -e 's/BAND/MODE/g' ../redscr0s_ | sed -e 's/#rmstripe3/rmstripe3/g' > tmpscr
# If you want to increase the speed of fitting by multi-core CPUs...
#!sed -e 's/extract /extract_multi /g' tmpscr > tmp0 && mv tmp0 tmpscr
cl < tmpscr
!sed -e 's/FMSAfileno/irs2_flat_MODE/g' ../redscr1s_ | sed -e 's/BAND/MODE/g' | sed -e 's/SHIFT/0/g' | sed -e 's/#rmstripe3/rmstripe3/g' > tmpscr
cl < tmpscr

### CAL Lamp ###
!sed -e 's/FMSAfileno/irs2_ar_MODE/g' ../redscr1s_ | sed -e 's/BAND/MODE/g' | sed -e 's/SHIFT/0/g' | sed -e 's/#rmstripe3/rmstripe3/g' > tmpscr
cl < tmpscr
imdel tmp1
median irs2_ar_MODEa tmp1.fits 1 21 boundar=reflect
imdel check,irs2_ar_MODEw.fits
!../wlmap tmp1.fits irs2_ar_MODEw.fits ../ThAr.dat 0.001 1.371 0.485 -0.033 0.005
#If fluorescent lamps are used intead of ThAr lamp...
#!../wlmap tmp1.fits irs2_ar_MODEw.fits ../FL_lamp.dat 0.001 1.371 0.485 -0.033 0.005
#Wavelength[um]=wcen+dw*x+dw2*x^2+dw3*x^3 (x=-1 to 1 from left to right)
#    step  wcen  dw    dw2    dw3
#js: 0.001 1.010 0.127 -0.001 0.000
#jm: 0.001 1.203 0.124 -0.001 0.000
#jl: 0.001 1.235 0.124 -0.001 0.000
#hs: 0.001 1.517 0.116 -0.002 0.001
#hm: 0.001 1.626 0.113 -0.002 0.000
#hl: 0.001 1.705 0.109 -0.002 0.002
#lr: 0.001 1.371 0.485 -0.033 0.005
displ tmp1 1 zs+
displ check 2 zs- zr+
#Compair these two images (read data / model) to make sure the conversion is not failed.
#Reduce "step" parameter to restrict the initial search range to a narrower one.
#Change "wcen" and "dw" parameters to shift and enlarge the model image, respectively.
displ irs2_ar_MODEw 3 zs- zr+
#The image gives the wavelength at each pixel.
