#! /bin/csh -f
#A simple script to assign 1's to objects that are unique in their imaging catalog counterpart, and 
#zeros to repeats. The first entry for a particular object is kept as unique regardless of which 
#line is may be and all the later entries are given 0's.
set scname=$0
if($#argv != 0) then
   echo Usage: $scname:t
   exit(1)
endif

set n_dets = `wc -l < classify_j1.out`
set counter_val = `cat classify_j1.out | awk '$1!="#"{split($0,a," "); print a[11]}'`
set saved_vals = `cat classify_j1.out | awk '$1!="#"{split($0,a," "); print "0"}'`
set class_flag = `cat classify_j1.out | awk '$1!="#"{split($0,a," "); print a[8]}'`
set n_saved = "0"
set cnt_n = 0
rm -f unique_obj.out
while ($cnt_n < $n_dets)
   @ cnt_n = $cnt_n + 1
   if ($class_flag[$cnt_n] != "-1") then
      if ($counter_val[$cnt_n] != "-99") then
         if ($n_saved > 0) then
            set tru_test = 1
            set cnt_n2 = 0
            while ($cnt_n2 < $n_saved && $tru_test != 0)
               @ cnt_n2 = $cnt_n2 + 1
               if ($counter_val[$cnt_n] == $saved_vals[$cnt_n2]) then
                  set tru_test = 0
               endif
            end
            if ($tru_test == 1) then
               @ n_saved = $n_saved + 1
               set saved_vals[$n_saved] = $counter_val[$cnt_n]
               echo "1" >> unique_obj.out
            else
               echo "0" >> unique_obj.out
            endif
         else
            @ n_saved = $n_saved + 1
            set saved_vals[$n_saved] = $counter_val[$cnt_n]
            echo "1" >> unique_obj.out
         endif
      else
         echo "1" >> unique_obj.out
      endif
   else
      echo "0" >> unique_obj.out
   endif
end
exit(0)
