#! /bin/tcsh -f

if ( -e $list_eml) then
    fkeypar $list_eml+1  NAXIS2 
    set n_eml = `pget fkeypar.par value`
    echo "Catalogue rows: $n_eml"
    echo "fselect infile=$list_eml+1 outfile=$list_eml_dir/ref_cat.fits expr="ID_INST.eq.0" clobber=yes" > $list_eml_dir/fselect.csh
    source $list_eml_dir/fselect.csh
    rm $list_eml_dir/fselect.csh
    fkeypar $list_eml_dir/ref_cat.fits+1  NAXIS2 
    set n_src_eml = `pget fkeypar.par value`
    echo "N. of sources: $n_src_eml"

    set ctrl_cts_new1 = 0
    set ctrl_cts_new2 = 0
    echo "$list_eml_dir/$cat3xmm3" > $list_eml_dir/files1.txt
    echo "$list_eml_dir/$cat3xmm3~" >> $list_eml_dir/files1.txt
    echo "$list_eml_dir/$cat3xmm5" > $list_eml_dir/files2.txt
    echo "$list_eml_dir/$cat3xmm5~" >> $list_eml_dir/files2.txt
    set n = 1
    while($n <= $n_src_eml)       
        ftabpar $list_eml_dir/ref_cat.fits+1 column=BOX_ID_SRC row=$n
        set  box_id = `pget ftabpar.par value`        
        ftabpar $list_eml_dir/ref_cat.fits+1 column=SCTS row=$n
        set  scts_id = `pget ftabpar.par value`        
        ftabpar $list_eml_dir/ref_cat.fits+1 column=ML_ID_SRC row=$n
        set  ml_id = `pget ftabpar.par value`        

        set scts_ref=`echo "$scts_id" | awk '{printf("%i \n",($1))}'`
        if ($scts_ref > $cts_limit) then
          #SRC WP2
          if ($ctrl_cts_new1 == 0) then
           echo "The src "$ml_id" (box id: "$box_id") at the row "$n" has $scts_ref cts (${scts_id} cts)"
           echo "fselect infile=$list_eml+1 outfile=$list_eml_dir/${cat3xmm3} expr="ML_ID_SRC.eq."$ml_id"" clobber=yes" > $list_eml_dir/fselect.csh
           source $list_eml_dir/fselect.csh
           rm $list_eml_dir/fselect.csh
           set ctrl_cts_new1 = `expr $ctrl_cts_new1 + 1`
          else
           echo "The src "$ml_id" (box id: "$box_id") at the row "$n" has $scts_ref cts (${scts_id} cts)"
           echo "fselect infile=$list_eml+1 outfile=$list_eml_dir/${cat3xmm3}~ expr="ML_ID_SRC.eq."$ml_id"" clobber=yes" > $list_eml_dir/fselect.csh
           source $list_eml_dir/fselect.csh
           rm $list_eml_dir/fselect.csh
           if (-e $list_eml_dir/${cat3xmm3}~) then
            echo "fmerge infiles=""$etm""$list_eml_dir"/files1.txt" outfile="${list_eml_dir}/${cat3xmm3}" columns="-" clobber=yes" > $list_eml_dir/fmerge.csh
            source $list_eml_dir/fmerge.csh
            rm $list_eml_dir/fmerge.csh
           endif
          endif
        else
        endif
        #SRC NO WP2
        if ($scts_ref <= $cts_limit) then
          if ($ctrl_cts_new2 == 0) then
           echo "The src "$ml_id" (box id: "$box_id") at the row "$n" has $scts_ref cts (${scts_id} cts) and is filtered out"
           echo "fselect infile=$list_eml+1 outfile=$list_eml_dir/${cat3xmm5} expr="ML_ID_SRC.eq."$ml_id"" clobber=yes" > $list_eml_dir/fselect.csh
           source $list_eml_dir/fselect.csh
           rm $list_eml_dir/fselect.csh
           set ctrl_cts_new2 = `expr $ctrl_cts_new2 + 1`
          else
           echo "The src "$ml_id" (box id: "$box_id") at the row "$n" has $scts_ref cts (${scts_id} cts) and is filtered out"
           echo "fselect infile=$list_eml+1 outfile=$list_eml_dir/${cat3xmm5}~ expr="ML_ID_SRC.eq."$ml_id"" clobber=yes" > $list_eml_dir/fselect.csh
           source $list_eml_dir/fselect.csh
           rm $list_eml_dir/fselect.csh
           if (-e $list_eml_dir/${cat3xmm5}~) then
            echo "fmerge infiles=""$etm""$list_eml_dir"/files2.txt" outfile="${list_eml_dir}/${cat3xmm5}" columns="-" clobber=yes" > $list_eml_dir/fmerge.csh
            source $list_eml_dir/fmerge.csh
            rm $list_eml_dir/fmerge.csh
           endif
          endif
        else
        endif
    set n = `expr $n + 1`
    end
else
  echo "ERROR: there are not the src lists"
endif
