#! /bin/tcsh -f

set infile=`ls *_tcat.fits`
set outroot=`echo ${infile}|cut -d'.' -f1`
set outfile=`echo ${outroot}_3xmm.fits`

cp ${infile} infile.fits

fcopy infile.fits'[ID_INST == 0][col OBS_ID; T_ID; N_TIME_BIN; SPclean_FLAG; TRIGGER_FLAG; T_START; T_STOP; EXT; EXT_ERR; EXT_ML; ID_INST; RA; DEC; RADEC_ERR; LII; BII;  EP_0_SCTS == SCTS; EP_0_SCTS_ERR == SCTS_ERR; EP_0_DET_ML == DET_ML; EP_0_BG_MAP == BG_MAP; EP_0_FLUX == FLUX; EP_0_FLUX_ERR == FLUX_ERR; EP_0_RATE == RATE; EP_0_RATE_ERR == RATE_ERR]' tmp_ep0.fits 
fdelcol tmp_ep0.fits+1 ID_INST N Y

fcopy infile.fits'[ID_INST == 1 && ID_BAND == 0][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; ONTIME == ONTIME]' tmp_pn0.fits
fdelcol tmp_pn0.fits+1 ID_INST N Y
fdelcol tmp_pn0.fits+1 ID_BAND N Y

ftjoin leftfile=tmp_ep0.fits rightfile=tmp_pn0.fits outfile=tmp0.fits expr="(T_ID == PN_0_T_ID) && (OBS_ID == PN_0_OBS_ID)" leftnameprefix=none rightnameprefix="PN_0_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp0.fits+1 PN_0_T_ID N Y
fdelcol tmp0.fits+1 PN_0_OBS_ID N Y
rm tmp_ep0.fits
rm tmp_pn0.fits

fcopy infile.fits'[ID_INST == 1 && ID_BAND == 1][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_pn1.fits
fdelcol tmp_pn1.fits+1 ID_INST N Y
fdelcol tmp_pn1.fits+1 ID_BAND N Y

ftjoin leftfile=tmp0.fits rightfile=tmp_pn1.fits outfile=tmp11.fits expr="(T_ID == PN_1_T_ID) && (OBS_ID == PN_1_OBS_ID)" leftnameprefix=none rightnameprefix="PN_1_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp11.fits+1 PN_1_T_ID N Y
fdelcol tmp11.fits+1 PN_1_OBS_ID N Y
rm tmp0.fits
rm tmp_pn1.fits

fcopy infile.fits'[ID_INST == 1 && ID_BAND == 2][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_pn2.fits
fdelcol tmp_pn2.fits+1 ID_INST N Y
fdelcol tmp_pn2.fits+1 ID_BAND N Y

ftjoin leftfile=tmp11.fits rightfile=tmp_pn2.fits outfile=tmp12.fits expr="(T_ID == PN_2_T_ID) && (OBS_ID == PN_2_OBS_ID)" leftnameprefix=none rightnameprefix="PN_2_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp12.fits+1 PN_2_T_ID N Y
fdelcol tmp12.fits+1 PN_2_OBS_ID N Y
rm tmp11.fits
rm tmp_pn2.fits

fcopy infile.fits'[ID_INST == 1 && ID_BAND == 3][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_pn3.fits
fdelcol tmp_pn3.fits+1 ID_INST N Y
fdelcol tmp_pn3.fits+1 ID_BAND N Y

ftjoin leftfile=tmp12.fits rightfile=tmp_pn3.fits outfile=tmp13.fits expr="(T_ID == PN_3_T_ID) && (OBS_ID == PN_3_OBS_ID)" leftnameprefix=none rightnameprefix="PN_3_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp13.fits+1 PN_3_T_ID N Y
fdelcol tmp13.fits+1 PN_3_OBS_ID N Y
rm tmp12.fits
rm tmp_pn3.fits

fcopy infile.fits'[ID_INST == 1 && ID_BAND == 4][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_pn4.fits
fdelcol tmp_pn4.fits+1 ID_INST N Y
fdelcol tmp_pn4.fits+1 ID_BAND N Y

ftjoin leftfile=tmp13.fits rightfile=tmp_pn4.fits outfile=tmp14.fits expr="(T_ID == PN_4_T_ID) && (OBS_ID == PN_4_OBS_ID)" leftnameprefix=none rightnameprefix="PN_4_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp14.fits+1 PN_4_T_ID N Y
fdelcol tmp14.fits+1 PN_4_OBS_ID N Y
rm tmp13.fits
rm tmp_pn4.fits

fcopy infile.fits'[ID_INST == 1 && ID_BAND == 5][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_pn5.fits
fdelcol tmp_pn5.fits+1 ID_INST N Y
fdelcol tmp_pn5.fits+1 ID_BAND N Y

ftjoin leftfile=tmp14.fits rightfile=tmp_pn5.fits outfile=tmp15.fits expr="(T_ID == PN_5_T_ID) && (OBS_ID == PN_5_OBS_ID)" leftnameprefix=none rightnameprefix="PN_5_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp15.fits+1 PN_5_T_ID N Y
fdelcol tmp15.fits+1 PN_5_OBS_ID N Y
rm tmp14.fits
rm tmp_pn5.fits


fcopy infile.fits'[ID_INST == 2 && ID_BAND == 0][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; ONTIME == ONTIME]' tmp_m10.fits
fdelcol tmp_m10.fits+1 ID_INST N Y
fdelcol tmp_m10.fits+1 ID_BAND N Y

ftjoin leftfile=tmp15.fits rightfile=tmp_m10.fits outfile=tmp20.fits expr="(T_ID == M1_0_T_ID) && (OBS_ID == M1_0_OBS_ID)" leftnameprefix=none rightnameprefix="M1_0_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp20.fits+1 M1_0_T_ID N Y
fdelcol tmp20.fits+1 M1_0_OBS_ID N Y
rm tmp15.fits
rm tmp_m10.fits

fcopy infile.fits'[ID_INST == 2 && ID_BAND == 1][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m11.fits
fdelcol tmp_m11.fits+1 ID_INST N Y
fdelcol tmp_m11.fits+1 ID_BAND N Y

ftjoin leftfile=tmp20.fits rightfile=tmp_m11.fits outfile=tmp21.fits expr="(T_ID == M1_1_T_ID) && (OBS_ID == M1_1_OBS_ID)" leftnameprefix=none rightnameprefix="M1_1_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp21.fits+1 M1_1_T_ID N Y
fdelcol tmp21.fits+1 M1_1_OBS_ID N Y
rm tmp20.fits
rm tmp_m11.fits

fcopy infile.fits'[ID_INST == 2 && ID_BAND == 2][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m12.fits
fdelcol tmp_m12.fits+1 ID_INST N Y
fdelcol tmp_m12.fits+1 ID_BAND N Y

ftjoin leftfile=tmp21.fits rightfile=tmp_m12.fits outfile=tmp22.fits expr="(T_ID == M1_2_T_ID) && (OBS_ID == M1_2_OBS_ID)" leftnameprefix=none rightnameprefix="M1_2_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp22.fits+1 M1_2_T_ID N Y
fdelcol tmp22.fits+1 M1_2_OBS_ID N Y
rm tmp21.fits
rm tmp_m12.fits

fcopy infile.fits'[ID_INST == 2 && ID_BAND == 3][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m13.fits
fdelcol tmp_m13.fits+1 ID_INST N Y
fdelcol tmp_m13.fits+1 ID_BAND N Y

ftjoin leftfile=tmp22.fits rightfile=tmp_m13.fits outfile=tmp23.fits expr="(T_ID == M1_3_T_ID) && (OBS_ID == M1_3_OBS_ID)" leftnameprefix=none rightnameprefix="M1_3_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp23.fits+1 M1_3_T_ID N Y
fdelcol tmp23.fits+1 M1_3_OBS_ID N Y
rm tmp22.fits
rm tmp_m13.fits

fcopy infile.fits'[ID_INST == 2 && ID_BAND == 4][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m14.fits
fdelcol tmp_m14.fits+1 ID_INST N Y
fdelcol tmp_m14.fits+1 ID_BAND N Y

ftjoin leftfile=tmp23.fits rightfile=tmp_m14.fits outfile=tmp24.fits expr="(T_ID == M1_4_T_ID) && (OBS_ID == M1_4_OBS_ID)" leftnameprefix=none rightnameprefix="M1_4_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp24.fits+1 M1_4_T_ID N Y
fdelcol tmp24.fits+1 M1_4_OBS_ID N Y
rm tmp23.fits
rm tmp_m14.fits

fcopy infile.fits'[ID_INST == 2 && ID_BAND == 5][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m15.fits
fdelcol tmp_m15.fits+1 ID_INST N Y
fdelcol tmp_m15.fits+1 ID_BAND N Y

ftjoin leftfile=tmp24.fits rightfile=tmp_m15.fits outfile=tmp25.fits expr="(T_ID == M1_5_T_ID) && (OBS_ID == M1_5_OBS_ID)" leftnameprefix=none rightnameprefix="M1_5_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp25.fits+1 M1_5_T_ID N Y
fdelcol tmp25.fits+1 M1_5_OBS_ID N Y
rm tmp24.fits
rm tmp_m15.fits


fcopy infile.fits'[ID_INST == 3 && ID_BAND == 0][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; ONTIME == ONTIME]' tmp_m20.fits
fdelcol tmp_m20.fits+1 ID_INST N Y
fdelcol tmp_m20.fits+1 ID_BAND N Y

ftjoin leftfile=tmp25.fits rightfile=tmp_m20.fits outfile=tmp30.fits expr="(T_ID == M2_0_T_ID) && (OBS_ID == M2_0_OBS_ID)" leftnameprefix=none rightnameprefix="M2_0_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp30.fits+1 M2_0_T_ID N Y
fdelcol tmp30.fits+1 M2_0_OBS_ID N Y
rm tmp25.fits
rm tmp_m20.fits

fcopy infile.fits'[ID_INST == 3 && ID_BAND == 1][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m21.fits
fdelcol tmp_m21.fits+1 ID_INST N Y
fdelcol tmp_m21.fits+1 ID_BAND N Y

ftjoin leftfile=tmp30.fits rightfile=tmp_m21.fits outfile=tmp31.fits expr="(T_ID == M2_1_T_ID) && (OBS_ID == M2_1_OBS_ID)" leftnameprefix=none rightnameprefix="M2_1_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp31.fits+1 M2_1_T_ID N Y
fdelcol tmp31.fits+1 M2_1_OBS_ID N Y
rm tmp30.fits
rm tmp_m21.fits

fcopy infile.fits'[ID_INST == 3 && ID_BAND == 2][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m22.fits
fdelcol tmp_m22.fits+1 ID_INST N Y
fdelcol tmp_m22.fits+1 ID_BAND N Y

ftjoin leftfile=tmp31.fits rightfile=tmp_m22.fits outfile=tmp32.fits expr="(T_ID == M2_2_T_ID) && (OBS_ID == M2_2_OBS_ID)" leftnameprefix=none rightnameprefix="M2_2_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp32.fits+1 M2_2_T_ID N Y
fdelcol tmp32.fits+1 M2_2_OBS_ID N Y
rm tmp31.fits
rm tmp_m22.fits

fcopy infile.fits'[ID_INST == 3 && ID_BAND == 3][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m23.fits
fdelcol tmp_m23.fits+1 ID_INST N Y
fdelcol tmp_m23.fits+1 ID_BAND N Y

ftjoin leftfile=tmp32.fits rightfile=tmp_m23.fits outfile=tmp33.fits expr="(T_ID == M2_3_T_ID) && (OBS_ID == M2_3_OBS_ID)" leftnameprefix=none rightnameprefix="M2_3_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp33.fits+1 M2_3_T_ID N Y
fdelcol tmp33.fits+1 M2_3_OBS_ID N Y
rm tmp32.fits
rm tmp_m23.fits

fcopy infile.fits'[ID_INST == 3 && ID_BAND == 4][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m24.fits
fdelcol tmp_m24.fits+1 ID_INST N Y
fdelcol tmp_m24.fits+1 ID_BAND N Y

ftjoin leftfile=tmp33.fits rightfile=tmp_m24.fits outfile=tmp34.fits expr="(T_ID == M2_4_T_ID) && (OBS_ID == M2_4_OBS_ID)" leftnameprefix=none rightnameprefix="M2_4_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp34.fits+1 M2_4_T_ID N Y
fdelcol tmp34.fits+1 M2_4_OBS_ID N Y
rm tmp33.fits
rm tmp_m24.fits

fcopy infile.fits'[ID_INST == 3 && ID_BAND == 5][col OBS_ID; T_ID; ID_INST; ID_BAND; SCTS; SCTS_ERR; DET_ML; BG_MAP; FLUX; FLUX_ERR; RATE; RATE_ERR; EXP_MAP]' tmp_m25.fits
fdelcol tmp_m25.fits+1 ID_INST N Y
fdelcol tmp_m25.fits+1 ID_BAND N Y

ftjoin leftfile=tmp34.fits rightfile=tmp_m25.fits outfile=tmp35.fits expr="(T_ID == M2_5_T_ID) && (OBS_ID == M2_5_OBS_ID)" leftnameprefix=none rightnameprefix="M2_5_" clobber=yes jointype="LEFT OUTER"
fdelcol tmp35.fits+1 M2_5_T_ID N Y
fdelcol tmp35.fits+1 M2_5_OBS_ID N Y
rm tmp34.fits
rm tmp_m25.fits

rm infile.fits
mv tmp35.fits ${outfile}

