####################################
### Reduction Procedure for IRS2 ###
####################################

##### Prepare files #####
### Edit s2o file to make pair-table and mag table ###
!../picktable /data/oXXXXX/FMSA00000000.fits > tmp1
!cut -d"|" -f12 tmp1 > tmp2
!paste tmp2 tmp1 > tmp3
!sort -n tmp3 > tmp4
!../listcomb ../irs2_spine_slit.tbl tmp4 | cut -f2,4 > tmp5
!sort -n tmp5 | sed -e 's/Dummy BS target for/Dummy:/g' > irs2_TARGET_NAME.tbl
cat irs2_TARGET_NAME.tbl
#irs2_TARGET_NAME.tbl
#column 1: IRS2 spectrum No. (1-200 from bottom to top)
#column 2: Corresponding lines of the s2o file

### Create pair table for combine ###
!grep "Dummy:" irs2_TARGET_NAME.tbl | cut -f1 > tmp1a
!grep "Dummy:" irs2_TARGET_NAME.tbl | cut -d"|" -f2 | sed -e 's/Dummy: //g' | cut -c-20 > tmp1b
!paste tmp1b tmp1a > tmp1
!grep -v "Dummy:" irs2_TARGET_NAME.tbl | cut -f1 > tmp2a
!grep -v "Dummy:" irs2_TARGET_NAME.tbl | cut -d"|" -f2 | cut -c-20 > tmp2b
!paste tmp2b tmp2a > tmp2
!../listcomb tmp1 tmp2 | cut -f2,4 > tmp3
!../listcomb tmp1 tmp2 - | cut -f2 > tmp4
!paste tmp4 tmp4 >> tmp3
!mv tmp3 irs2_TARGET_NAME_pair.tbl
cat irs2_TARGET_NAME_pair.tbl
#irs2_TARGET_NAME_pair.tbl
#column 1: IRS2 spectrum No. of the objects in Pos.2.
#column 2: IRS2 spectrum No. of the correspoinding objects in Pos.1.
#Object only in Pos.2 => column 1 = column 2
#Object only in Pos.1 => not listed in this file

### Create redshift table for linepos ###
!grep -v "Dummy:" irs2_TARGET_NAME.tbl | cut -f1 > tmp0
!cut -f2 irs2_TARGET_NAME_pair.tbl > tmp1
!../listcomb tmp1 tmp0 - >> tmp0
!cut -f1 irs2_TARGET_NAME.tbl > tmp2
#!less irs2_TARGET_NAME.tbl
#search keyword "zp=" and "zs=" in the s2o file
#9.999 indicates "No data" in this case
!../readarg irs2_TARGET_NAME.tbl zp= -1 | sed -e 's/9\.999/-1/g' > tmp3
!../readarg irs2_TARGET_NAME.tbl zs= -1 | sed -e 's/9\.999/-1/g' > tmp4
!paste tmp2 tmp3 | grep -v "\-1" | sed -e 's/$/ 0.01/g' > tmp5
!paste tmp2 tmp3 | grep "\-1" >> tmp5
!paste tmp2 tmp4 | grep -v "\-1" > tmp6
!../listcomb tmp5 tmp6 - >> tmp6
!../listcomb tmp0 tmp6 | cut -f2- | sort -n > irs2_TARGET_NAME_fib_z.tbl
cat irs2_TARGET_NAME_fib_z.tbl
#column 1: IRS2 spectrum No.
#column 2: Expected redshifts (-1: No data)
#column 3: Typical error of the estimated redshifts (e.g. nothing for spec-z and 0.01 for phot-z)

### Create magnitude table for mag_fnu plot ###
!grep -v "Dummy:" irs2_TARGET_NAME.tbl | cut -f1 > tmp1
!grep -v "Dummy:" irs2_TARGET_NAME.tbl > tmp2
#!less tmp2
#if column 14 shows Jmag
#!cut -d"|" -f14 tmp2 > tmp3
#else search keyword "J=" in the s2o file
!../readarg tmp2 J= 99.99 > tmp3
!../readarg tmp2 H= 99.99 > tmp4
!../readarg tmp2 K= 99.99 > tmp5
#if they are Vega magnitudes...
#!../readarg tmp2 J= 99.99 | awk '{if($1>90) print $1;else print $1+0.870}' > tmp3
#!../readarg tmp2 H= 99.99 | awk '{if($1>90) print $1;else print $1+1.348}' > tmp4
#!../readarg tmp2 K= 99.99 | awk '{if($1>90) print $1;else print $1+1.858}' > tmp5
!paste -d" " tmp3 tmp4 tmp5 | awk '{if($1<90 && $2>90 && $3<90) print $1,($1+$3)/2;else print $1,$2}' > tmp6
#or
#!paste -d" " tmp3 tmp4 tmp5 | awk '{if($1<90 && $2>90 && $3<90) print $1,(($1-0.870)*0.2+($3-1.858)*0.8)+1.348;else print $1,$2}' > tmp6
!paste tmp1 tmp6 | grep -v "99\.99 99\.99" > irs2_TARGET_NAME_mag.tbl
cat irs2_TARGET_NAME_mag.tbl
#column 1: IRS2 spectrum No. of Pos.1 fibers.
#column 2: J-mag (AB) (No Data => 99.99)
#column 3: H-mag (AB) (No Data => 99.99)

##### Object Image #####
### Make the exposure list ###
!echo "a FMSA00012346 FMSA00012348" >  irs2_TARGET_NAME_MODE.tbl
!echo "b FMSA00012350 FMSA00012352" >> irs2_TARGET_NAME_MODE.tbl
!echo "c FMSA00012354 NONE"         >> irs2_TARGET_NAME_MODE.tbl
!echo "d NONE FMSA00012360"         >> irs2_TARGET_NAME_MODE.tbl
#Delimiter must be " ".
#No ".fits" should be attached.
#Use "NONE" if the correnponding exposure was not taken.
#The middle column should be object frames in the BS mode, and use
#"NONE" for the first image when the first frame is the sky image.
#For quick look during the observation,
# * Prepare this list to the end of the expected exposure.
# * Do "!../mkscr...", "!sed ...", and "cl < mkscr.cl" lines in the
#     next section after every pos.1 of pair exposure is taken.
# * Do the completed part in "redproc_irs2_TARGET_NAME_MODEA" script
#     using cut & paste by mouse.

### Check sky frame (BS mode only) ###
##If you want to recheck all the sky images, delete *sub*.fits files first.
#!../mkscr2 irs2_TARGET_NAME_MODE.tbl MODE
# If you want to increase the speed of subtraction by multi-core CPUs...
#!sed -e 's/subtract /subtract_multi /g' mkscr2.cl > tmp.cl && mv tmp.cl mkscr2.cl
#cl < mkscr2.cl

### Primary reduction ###
#Replace 'CBS' with 'BS' (or 'BS+') for BS (BS for bright object) mode
#If you restart all from here...
#imdel irs2_TARGET_NAME*_MODE.fits
!../mkscr irs2_TARGET_NAME_MODE.tbl CBS > redproc_irs2_TARGET_NAME_MODEB
!sed -e 's/#cl/cl/g' redproc_irs2_TARGET_NAME_MODEB > redproc_irs2_TARGET_NAME_MODEA
# If you want to increase the speed of subtraction by multi-core CPUs...
#!sed -e 's/subtract /subtract_multi /g' mkscr.cl > tmp.cl && mv tmp.cl mkscr.cl
cl < mkscr.cl
cl < redproc_irs2_TARGET_NAME_MODEA
#If you have the object mask, send this line instead of above one.
#(This line is used for the 2ndary reduction...)
cl < redproc_irs2_TARGET_NAME_MODEB

### Check Spine Allocation ###
imdel irs2_TARGET_NAME_MODE_spineerror
#CBS mode
!../spineerror irs2_TARGET_NAME_MODE.tbl irs2_TARGET_NAME_pair.tbl
#BS mode
#!../spineerror irs2_TARGET_NAME_MODE.tbl
displ irs2_TARGET_NAME_MODE_spineerror 1

### Combine ###
!ls irs2_TARGET_NAME?_MODExpb.fits > irs2_TARGET_NAME_MODExpb.tbl
mdisp @irs2_TARGET_NAME_MODExpb.tbl 0
#Make sure the bright object in each subset has the same y-position.
#Remove imcomplete pair from the list
#!emacs irs2_TARGET_NAME_MODExpb.tbl &
!sed -e 's/b\.fits/_bpm\.fits/g' irs2_TARGET_NAME_MODExpb.tbl > irs2_TARGET_NAME_MODEyp_bpm.tbl
imdel irs2_TARGET_NAME_MODEyp_bpm.fits
imcomb @irs2_TARGET_NAME_MODEyp_bpm.tbl irs2_TARGET_NAME_MODEyp_bpm.fits
displ irs2_TARGET_NAME_MODEyp_bpm 2 zs- zr- z1=1 z2=0
imdel irs2_TARGET_NAME_MODExpb.fits
imcomb @irs2_TARGET_NAME_MODExpb.tbl irs2_TARGET_NAME_MODExpb.fits
displ irs2_TARGET_NAME_MODExpb 1

### Secondary Residual Sky Subtraction ###
#For CBS mode or BS mode (faint objects)
!sed -e 's/FMSAfileno/irs2_TARGET_NAME_MODE/g' ../redscr3 | sed -e 's/irs1/irs2/g' | sed -e 's/BAND/MODE/g' > tmpscr
#For BS mode (bright objects)
#!sed -e 's/FMSAfileno/irs2_TARGET_NAME_MODE/g' ../redscr3 | sed -e 's/irs1/irs2/g' | sed -e 's/BAND/MODE/g' | sed -e 's/high_r=2 low_r=2/high_r=1.5 low_r=3/g' > tmpscr
cl < tmpscr
#If you don't want to accept this result,
#imdel irs2_TARGET_NAME_MODEyp
#imcopy irs2_TARGET_NAME_MODExpb irs2_TARGET_NAME_MODEyp.fits

### Final Bad pix Correction ###
!sed -e 's/FMSAfileno/irs2_TARGET_NAME_MODEyp/g' ../redscrb | sed -e 's/\/ 10/\/ 1/g' | sed -e 's/convolve/#convolve/g' > tmpscr
cl < tmpscr
imdel irs2_TARGET_NAME_MODEypb0,irs2_cont_TARGET_NAME_MODEa,irs2_TARGET_NAME_MODEyp_bpm0
imcopy irs2_TARGET_NAME_MODEypb.fits irs2_TARGET_NAME_MODEypb0.fits
imcopy irs2_flat_MODEa.fits irs2_cont_TARGET_NAME_MODEa.fits
imcopy irs2_TARGET_NAME_MODEyp_bpm.fits irs2_TARGET_NAME_MODEyp_bpm0.fits
displ irs2_TARGET_NAME_MODEyp_bpm 4 zs- zr- z1=1 z2=0

### Mask edge correction ###
imdel irs2_flat_TARGET_NAME_MODEa.fits
!../adjflat irs2_TARGET_NAME_MODEypb0.fits irs2_flat_MODEa.fits irs2_flat_TARGET_NAME_MODEa.fits
imrepl irs2_flat_TARGET_NAME_MODEa.fits 0.5 lower=INDEF upper=0.5
imrepl irs2_flat_TARGET_NAME_MODEa.fits 2   upper=INDEF lower=2
imdel irs2_TARGET_NAME_MODEypb
magnify irs2_flat_TARGET_NAME_MODEa.fits irs2_flat_TARGET_NAME_MODEa.fits 1. 9. y1=0.5 y2=200.4999 interpo=nearest boundar=nearest fluxcon-
imarith irs2_TARGET_NAME_MODEypb0.fits / irs2_flat_TARGET_NAME_MODEa.fits irs2_TARGET_NAME_MODEypb.fits

### CBS Combine (CBS mode only) ###
imdel tmp1,tmp2,irs2_cont_TARGET_NAME_MODEa
imcopy irs2_flat_MODEa tmp1
!../matchpair tmp1.fits irs2_TARGET_NAME_pair.tbl tmp2.fits irs2_cont_MODEa.dat
imcomb tmp1,tmp2 irs2_cont_TARGET_NAME_MODEa.fits lthresh=-99990.
imdel irs2_TARGET_NAME_MODEypb1,irs2_TARGET_NAME_MODEypb2,irs2_TARGET_NAME_MODEypb
imarith irs2_TARGET_NAME_MODEypb0.fits / irs2_flat_TARGET_NAME_MODEa.fits irs2_TARGET_NAME_MODEypb1.fits
!../matchpair irs2_TARGET_NAME_MODEypb1.fits irs2_TARGET_NAME_pair.tbl irs2_TARGET_NAME_MODEypb2.fits irs2_cont_MODEa.dat
displ irs2_TARGET_NAME_MODEypb1 1 zs- zr- z1=-20 z2=20
displ irs2_TARGET_NAME_MODEypb2 2 zs- zr- z1=-20 z2=20
imcomb irs2_TARGET_NAME_MODEypb1,irs2_TARGET_NAME_MODEypb2 irs2_TARGET_NAME_MODEypb.fits lthresh=-99990.
##If you have one more image to join...(replace "X" to the name of the extra set)
#imcopy irs2_TARGET_NAME_MODE_mask.fits irs2_TARGET_NAMEX_MODE_mask.fits
#!sed -e 's/FMSAfileno/irs2_TARGET_NAMEX_MODE/g' ../redscr3 | sed -e 's/irs1/irs2/g' | sed -e 's/BAND/MODE/g' > tmpscr
#cl < tmpscr
#!sed -e 's/FMSAfileno/irs2_TARGET_NAMEX_MODEyp/g' ../redscrb | sed -e 's/\/ 10/\/ 1/g' > tmpscr
#cl < tmpscr
#imdel irs2_TARGET_NAMEX_MODEypb1.fits,irs2_TARGET_NAMEX_MODEypb2.fits,irs2_TARGET_NAMEX_MODE_mask.fits
#imarith irs2_TARGET_NAMEX_MODEypb.fits / irs2_flat_TARGET_NAME_MODEa.fits irs2_TARGET_NAMEX_MODEypb1.fits
#!../matchpair irs2_TARGET_NAMEX_MODEypb1.fits irs2_TARGET_NAME_pair.tbl irs2_TARGET_NAMEX_MODEypb2.fits irs2_cont_MODEa.dat
#imdel tmp1
#imrename irs2_TARGET_NAME_MODEypb tmp1
## If you have 3 complete pair...
#!echo -e 3"\n"1 > tmp1
##If you want to add pos.A (MODEypb1)...
#imcomb tmp1,irs2_TARGET_NAMEX_MODEypb1 irs2_TARGET_NAME_MODEypb weight=@tmp1 lthresh=-99990.
displ irs2_TARGET_NAME_MODEypb 3 zs- zr- z1=-20 z2=20
imdel irs2_TARGET_NAME_MODEyp_bpm1,irs2_TARGET_NAME_MODEyp_bpm2,irs2_TARGET_NAME_MODEyp_bpm
imcopy irs2_TARGET_NAME_MODEyp_bpm0 irs2_TARGET_NAME_MODEyp_bpm1
!../matchpair irs2_TARGET_NAME_MODEyp_bpm1.fits irs2_TARGET_NAME_pair.tbl irs2_TARGET_NAME_MODEyp_bpm2.fits irs2_cont_MODEa.dat
imarith irs2_TARGET_NAME_MODEyp_bpm2.fits * -1 irs2_TARGET_NAME_MODEyp_bpm2.fits
imrepl irs2_TARGET_NAME_MODEyp_bpm2.fits -99999. upper=INDEF lower=99990
imcomb irs2_TARGET_NAME_MODEyp_bpm1,irs2_TARGET_NAME_MODEyp_bpm2 irs2_TARGET_NAME_MODEyp_bpm.fits lthresh=-99990.
displ irs2_TARGET_NAME_MODEyp_bpm 4 zs- zr- z1=1 z2=0

### Position of the Expected Emission Lines ###
imdel irs2_TARGET_NAME_linepos_MODE.fits
!../linepos irs2_TARGET_NAME_fib_z.tbl ../linepos.dat irs2_ar_MODEw.fits irs2_TARGET_NAME_linepos_MODE.fits
displ irs2_TARGET_NAME_MODEypb 1 zs- zr- z1=-20 z2=20
displ irs2_TARGET_NAME_linepos_MODE 2 zs- zr- z1=0 z2=10

### Object Mask ###
imdel tmp1,tmp2
magnify irs2_TARGET_NAME_MODE_mask tmp1 1. 9. y1=0.5 y2=200.4999 interpo=nearest boundar=nearest fluxcon-
imarith irs2_TARGET_NAME_MODEypb * tmp1 tmp1
imedit tmp1 tmp2 aperture=square radius=1 value=-99999.
#press "e" where you want to put mask (undo: "u") and press "q" to quit.
#To remove mask, press "e" at the mask position. 
!../maskrepl irs2_TARGET_NAME_MODE_mask.fits tmp2.fits irs2_TARGET_NAME_pair.tbl
#Repeat this section until all the masks are fixed.
#If more than half spectra are automatically masked, reduce the order of fit1d commands in ../redscr2, ../redscr2s, 
#  and ../redscr3 to surpress the background rolling.
#To apply this object mask, make reduction again from "cl < redproc_irs2_TARGET_NAME_MODEB"

### Noise^2 Frame (for faint object ONLY!) ###
#Output file: irs2_TARGET_NAME_MODEypb0_nosq.fits
#CBS mode
!../mknoise irs2_TARGET_NAME_MODEypb0.fits irs2_flat_TARGET_NAME_MODEa.fits irs2_TARGET_NAME_pair.tbl irs2_cont_MODEa.dat
#BS mode
#!../mknoise irs2_TARGET_NAME_MODEypb0.fits irs2_flat_TARGET_NAME_MODEa.fits

### Wavelength and Flux Calibration (Absorption Correction) ###
#Set aperture mask for each spectra
!echo "0.5 1 1 1 1 1 1 1 0.5" > extract.dat
minmax irs2_TARGET_NAME_MODEypb
displ irs2_TARGET_NAME_MODEypb 1 zs- zr+
cat irs2_TARGET_NAME_mag.tbl
#Choose a bright star for the spectrum reference and put the number (int(y/9)+1) for the last argument of the next command (replace "XX").
#You can use an extra argument of the airmass value to improve the accuracy of automatic type-judgement, such as "...ypbw.fits 15 108 1.95".
#Or, if you know the spectral type of this reference star, you can use the argument as the spectral type like "...ypbw.fits 15 108 K2III".
#When you run it with this mode, the parameters for automatic type-judgement are recalibrated and dumped to "conv_w_fnu.dat".
#If you	want to reset the parameters, please delete "conv_w_fnu.dat".
#Note that #33,#35,#56 fibers are unstable, and that the automatic type-judgement for IRS2 is still incomplete.
imdel irs2_TARGET_NAME_MODEypbw,check,irs2_TARGET_NAME_MODEypw_bpm
!../conv_w_fnu irs2_TARGET_NAME_MODEypb.fits irs2_ar_MODEw.fits irs2_cont_TARGET_NAME_MODEa.fits irs2_TARGET_NAME_MODEypbw.fits 15 XX
!../conv_w_fnu irs2_TARGET_NAME_MODEyp_bpm.fits irs2_ar_MODEw.fits irs2_cont_TARGET_NAME_MODEa.fits irs2_TARGET_NAME_MODEypw_bpm.fits
#Reddening correction (XX is the same vlue of above, and MAG is the expected extinction calue.)
#imdel irs2_TARGET_NAME_MODEypbw,check,tmp1
#!../extinc irs2_ar_MODEw.fits XX MAG tmp1.fits
#imarith irs2_TARGET_NAME_MODEypb / tmp1 tmp1
#!../conv_w_fnu tmp1.fits irs2_ar_MODEw.fits irs2_cont_TARGET_NAME_MODEa.fits irs2_TARGET_NAME_MODEypbw.fits 15 XX
##If you want more, compare "check.fits" to the typical thruoughput "../checksum_.fits".
displ irs2_TARGET_NAME_MODEypbw 1
displ irs2_TARGET_NAME_MODEypw_bpm 2 zs- zr- z1=1 z2=0
#0.9um(x=1), 5A/pix, 1count=1uJy (within the fiber aperture) in this image.
imdel irs2_TARGET_NAME_MODEypbwx.fits,irs2_TARGET_NAME_MODEypbwx_no.fits
!../extract2 irs2_TARGET_NAME_MODEypbw.fits irs2_TARGET_NAME_MODEypbwx.fits
!../extract2 irs2_TARGET_NAME_MODEypbw_nosq.fits irs2_TARGET_NAME_MODEypbwx_no.fits
imfunc irs2_TARGET_NAME_MODEypbwx_no.fits irs2_TARGET_NAME_MODEypbwx_no.fits sqrt
displ irs2_TARGET_NAME_MODEypbwx 3
#0.9um(x=1), 5A/pix, 1count=1uJy (within the fiber aperture) in this image.
imdel tmp1
#Mask the data where the relative throughput is less than 5% with +2 pix growing
#!../fillmask irs2_TARGET_NAME_MODEypbwx.fits 0.05 check.fits tmp1.fits 2
#or noise level larger than 2*(typical noise level) with +2 pix growing
!../fillmask irs2_TARGET_NAME_MODEypbwx.fits 2 irs2_TARGET_NAME_MODEypbwx_no.fits tmp1.fits 2
#noao
#onedspec
#splot tmp1
box tmp1.fits tmp1.fits 5 1
!../fits2txt irs2_TARGET_NAME_MODEypbwx_no.fits > plot2.tbl
#Mask the data where the relative throughput is less than 5% with +2 pix growing
#!../fits2txt tmp1.fits 0.05 check.fits 2 > plot.tbl
#or noise level larger than 2*(typical noise level) with +2 pix growing
!../fits2txt tmp1.fits 2 irs2_TARGET_NAME_MODEypbwx_no.fits 2 > plot.tbl
!grep -v "\-1" irs2_TARGET_NAME_fib_z.tbl > plot_z.tbl
!../listcomb irs2_TARGET_NAME_mag.tbl check.txt | cut -f1,2,4 > mag_fnu.tbl
#Make sure the J-H colors of the objects are consistent with the catalog by the following plot.

##### Plot by gnuplot #####
!gnuplot < ../mag_fnu.gnuplot | evince mag_fnu.ps &
!cp mag_fnu.tbl irs2_TARGET_NAME_MODE_mag_fnu.tbl

!grep 59 plot_z.tbl
!sed -e 's/NUM/59/g' ../plot_ind2.gnuplot| sed -e 's/REDSHIFT/0.0/g' > tmp.gnuplot
!gnuplot < tmp.gnuplot
!evince plot_59.ps &
#multi-plots (10 spectrum from specified)
#In this case, 51 -- 60
!sed -e 's/NUM/51/g' ../plot_ind3.gnuplot| sed -e 's/REDSHIFT/0.0/g' > tmp.gnuplot
!gnuplot < tmp.gnuplot
!evince plot_all_51.ps &

##### Plot by SuperMongo #####
!sm < ../mag_fnu.sm
!cp mag_fnu.tbl irs2_TARGET_NAME_MODE_mag_fnu.tbl
!sed -e 's/NUM/59/g' ../plot_ind2.sm | sed -e 's/SCALE/-1/g' > tmp.sm
!sm < tmp.sm
#Plot by object flux (>100uJy)
!sed -e 's/FROM/1/g' ../plot_multi.sm | sed -e 's/TO/200/g' | sed -e 's/THRE/100/g' > tmp.sm
!sm < tmp.sm

##### END #####

