#! /bin/tcsh -f

cd ${filter3xmm}
set N_filtered = 1
foreach ctrl_filtered (`ls -1 filtered*_res.txt | awk '{print $1}'`)
  set N_filtered_txt=`cat $ctrl_filtered | wc -l | awk '{print $1}'`
  if ($N_filtered_txt == 1) then
   echo "The file $ctrl_filtered is empty"
  else
   set inst_epic = `echo $ctrl_filtered | cut -c 9-12` 
   if ($inst_epic == "MOS1") then
    set inst_epic_short = "M1"
   endif
   if ($inst_epic == "MOS2") then
    set inst_epic_short = "M2"
   endif
   if ($inst_epic == "PN00") then
    set inst_epic_short = "PN"
   endif
   set N_row_deg = 1
   while($N_row_deg <= $N_filtered_txt)
    if ($N_row_deg == 1) then
     echo "XXX" > new_column.txt
    else
     echo "$inst_epic_short" >> new_column.txt 
    endif
   set N_row_deg = `expr $N_row_deg + 1`
   end
   paste $ctrl_filtered new_column.txt > $ctrl_filtered~ 
   cp $ctrl_filtered~ $ctrl_filtered
   rm $ctrl_filtered~
  endif
set N_filtered = `expr $N_filtered + 1`
end
rm new_column.txt
