#! /bin/tcsh -f

if ($DET == 1 || $DET == 2) then
 set inst = 0
 echo "Filtering $det/emllist.fits for energy band "$b_tot" and for all instruments: $inst"
endif
if ($DET == 01) then
 set inst = 2
 echo "Filtering $det/emllist.fits for energy band "$b_tot" and for MOS1 instrument: $inst"
endif
if ($DET == 02) then
 set inst = 3
 echo "Filtering $det/emllist.fits for energy band "$b_tot" and for MOS2 instrument: $inst"
endif
if ($DET == 03) then
 set inst = 1
 echo "Filtering $det/emllist.fits for energy band "$b_tot" and for pn instrument: $inst"
endif

if ( -e $det/emllist.fits ) then
 cp $det/emllist.fits $prod_dir_final/P${dir0}EPX000OMSRLI0000.fits
 gzip $prod_dir_final/P${dir0}EPX000OMSRLI0000.fits
 cp $prod_dir_final/P${dir0}EPX000OMSRLI0000.fits.gz $prod_dir_final/P${dir0}EPX000OMSRLI0000.FTZ
 rm $prod_dir_final/P${dir0}EPX000OMSRLI0000.fits.gz
 set Nband=`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
 set b = 0
#for each instrument in the total energy band
 set c = ` expr $b + 1`
 fselect emllist.fits+1 emllist_singsrc_"$c".fits ID_INST.eq."$inst".and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
 set ctrl_max_dist = 0
 if ($target_src == "yes") then
   set cat3xmm2="emllist_singsrc_"$c".fits"
   set list_eml_dir = $det
   set list_eml = $list_eml_dir/$cat3xmm2
   if ( -e "$cat3xmm2" ) then
    echo "Catalogue $cat3xmm2 : starting to filter the target $target_name with coordinates RA: $target_RA Dec: $target_DEC" 
    set cat3xmm3=`echo ""$dir0"_FULL_catalogue_"$target_name".fits"`
    source $progr_dir/check_src_position.csh 
   endif
   if ( -e $cat3xmm3 ) then
    fkeypar $cat3xmm3+1  NAXIS2
    set orows = `pget fkeypar.par value`
    echo "Number of src in $cat3xmm3 in the energy band "$c": "$orows""
    if ( $orows == 0 ) then
     echo "WARNING $dir0 $cat3xmm3 is empty after energy filtering"
     echo "WARNING $dir0 $cat3xmm3 is empty after energy filtering FULLDET" >> $log_dir/messages.log
    endif
    cp $det/$cat3xmm3 $prod_dir
    fdump $det/$cat3xmm3 $det/"$dir0"_FULL_catalogue_"$target_name".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
    cat $det/"$dir0"_FULL_catalogue_"$target_name".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/"$dir0"_3XMM_catalogue_"$target_name".txt
    rm $det/"$dir0"_FULL_catalogue_"$target_name".txt~
    echo "Coping the $det/"$dir0"_3XMM_catalogue_"$target_name".txt in $det/emllist_singsrc_"$c".txt"
    cp $det/"$dir0"_FULL_catalogue_"$target_name".txt $det/emllist_singsrc_"$c".txt
    cp $det/"$dir0"_FULL_catalogue_"$target_name".txt $prod_dir
   else
    echo "ERROR $dir0 $cat3xmm3 does not exist"
    echo "ERROR $dir0 $cat3xmm3 does not exist FULLDET" >> $log_dir/messages.log
    if ( -e emllist_singsrc_"$c".fits ) then
     fkeypar emllist_singsrc_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
      echo "WARNING $dir0 emllist_singsrc_"$c".fits is empty after energy filtering"
      echo "WARNING $dir0 emllist_singsrc_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     cp $det/emllist_singsrc_"$c".fits $prod_dir
     fdump $det/emllist_singsrc_"$c".fits $det/emllist_singsrc_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$c".txt
     rm $det/emllist_singsrc_"$c".txt~
     cp $det/emllist_singsrc_"$c".txt $prod_dir
    else
     echo "ERROR $dir0 emllist_singsrc_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
   endif
 endif
 if ($target_src == "no") then
  if ( -e emllist_singsrc_"$c".fits ) then
   fkeypar emllist_singsrc_"$c".fits+1  NAXIS2
   set orows = `pget fkeypar.par value`
   echo "Number of src in emllist_singsrc_"$c".fits in the energy band "$c": "$orows""
   if ( $orows == 0 ) then
    echo "WARNING $dir0 emllist_singsrc_"$c".fits is empty after energy filtering"
    echo "WARNING $dir0 emllist_singsrc_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
   endif
   cp $det/emllist_singsrc_"$c".fits $prod_dir
   fdump $det/emllist_singsrc_"$c".fits $det/emllist_singsrc_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
   cat $det/emllist_singsrc_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$c".txt
   rm $det/emllist_singsrc_"$c".txt~
   cp $det/emllist_singsrc_"$c".txt $prod_dir
  else
   echo "ERROR $dir0 emllist_singsrc_"$c".fits does not exist"
   echo "ERROR $dir0 emllist_singsrc_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
  endif
 endif
if ($full_list == "yes") then
 if ($DET == 1 ) then
####M1 total energy band
   set epi = "M1"
   set c = ` expr $b + 1`
   fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.2.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
   if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
    fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
    set orows = `pget fkeypar.par value`
    echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
    if ( $orows == 0 ) then
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
    endif
    fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
    cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
    rm $det/emllist_singsrc_"$epi"_"$c".txt~
   else
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
   endif
####M2 total energy band
   set epi = "M2"
   fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.3.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
   if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
    fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
    set orows = `pget fkeypar.par value`
    echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
    if ( $orows == 0 ) then
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
    endif
    fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
    cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
    rm $det/emllist_singsrc_"$epi"_"$c".txt~
   else
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
   endif
####M1 sub energy band
   set b = 1
   foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set c = ` expr $b + 1`
    set epi = "M1"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.2.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
      echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
      echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
####M2 sub energy band
    set epi = "M2"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.3.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
      echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
      echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
  set b = ` expr $b + 1`
  end
 endif

 if ($DET == 01) then
####M1 total energy band
  set epi = "M1"
  set c = ` expr $b + 1`
  fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.2.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
  if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
   fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
   set orows = `pget fkeypar.par value`
   echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
   if ( $orows == 0 ) then
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
   endif
   fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
   cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
   rm $det/emllist_singsrc_"$epi"_"$c".txt~
  else
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
  endif
####M1 sub energy band
  set b = 1
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set c = ` expr $b + 1`
    set epi = "M1"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.2.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
  set b = ` expr $b + 1`
  end
 endif

 if ($DET == 02) then
####M2 total energy band
  set epi = "M2"
  set c = ` expr $b + 1`
  fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.3.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
  if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
   fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
   set orows = `pget fkeypar.par value`
   echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
   if ( $orows == 0 ) then
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
   endif
   fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
   cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
   rm $det/emllist_singsrc_"$epi"_"$c".txt~
  else
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
  endif
####M2 sub energy band
  set b = 1
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set c = ` expr $b + 1`
    set epi = "M2"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.3.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
  set b = ` expr $b + 1`
  end
 endif

 if ($DET == 03) then
####PN total energy band
  set epi = "PN"
  set c = ` expr $b + 1`
  fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.1.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
  if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
   fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
   set orows = `pget fkeypar.par value`
   echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
   if ( $orows == 0 ) then
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
   endif
   fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
   cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
   rm $det/emllist_singsrc_"$epi"_"$c".txt~
  else
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
  endif
####PN sub energy band
  set b = 1
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set c = ` expr $b + 1`
    set epi = "PN"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.1.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
  set b = ` expr $b + 1`
  end
 endif

if ($DET == 2) then
####M1 total energy band
  set epi = "M1"
  set c = ` expr $b + 1`
  fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.2.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
  if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
   fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
   set orows = `pget fkeypar.par value`
   echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
   if ( $orows == 0 ) then
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
     echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
   endif
   fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
   cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
   rm $det/emllist_singsrc_"$epi"_"$c".txt~
  else
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
     echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
  endif
####M2 total energy band
    set epi = "M2"
    set c = ` expr $b + 1`
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.3.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
####PN total energy band
    set epi = "PN"
    set c = ` expr $b + 1`
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.1.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
####M1 sub energy band
  set b = 1
  foreach band (`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`)
    set c = ` expr $b + 1`
    set epi = "M1"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.2.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
####M2 sub energy band
    set epi = "M2"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.3.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
####PN sub energy band
    set epi = "PN"
    fselect emllist.fits+1 emllist_singsrc_"$epi"_"$c".fits ID_INST.eq.1.and.ID_BAND.eq."$b".and.RATE.gt.0.and.DET_ML.gt."$det_ml" CLOBBER=yes
    if ( -e emllist_singsrc_"$epi"_"$c".fits ) then
     fkeypar emllist_singsrc_"$epi"_"$c".fits+1  NAXIS2
     set orows = `pget fkeypar.par value`
     echo "Number of src in emllist_singsrc_"$epi"_"$c".fits in the energy band "$c": "$orows""
     if ( $orows == 0 ) then
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering"
       echo "WARNING $dir0 emllist_singsrc_"$epi"_"$c".fits is empty after energy filtering FULLDET" >> $log_dir/messages.log
     endif
     fdump $det/emllist_singsrc_"$epi"_"$c".fits $det/emllist_singsrc_"$epi"_"$c".txt~  columns='RATE,RATE_ERR,RA,DEC,RADEC_ERR,SCTS,SCTS_ERR,EXT,EXT_ERR,DET_ML,BG_MAP,ML_ID_SRC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     cat $det/emllist_singsrc_"$epi"_"$c".txt~ | sed -e "s/INDEF/0/g" | sed -e "s/NULL/0/g" > $det/emllist_singsrc_"$epi"_"$c".txt
     rm $det/emllist_singsrc_"$epi"_"$c".txt~
    else
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist"
       echo "ERROR $dir0 emllist_singsrc_"$epi"_"$c".fits does not exist FULLDET" >> $log_dir/messages.log
    endif
  set b = ` expr $b + 1`
  end
 endif
endif
else
   echo "ERROR emldetect aborts: emllist.fits does not exist"
   echo "ERROR emldetect aborts: emllist.fits does not exist FULLDET" >> $log_dir/messages.log
endif
