#! /bin/csh -f
#A simple script to query a detection number, a certain counterpart, and 
#a particular band. Will be called in other scripts.
set scname=$0
if($#argv != 3) then
   echo Usage: $scname:t 'det number, image number, band number'
   exit(1)
endif
if ($1 < 10) then
   set flname1 = "SEDs/nspawn_000"$1".cat"
   set flname2 = "SEDs/spawn_000"$1".cat"
else if ($1 < 100) then
   set flname1 = "SEDs/nspawn_00"$1".cat"
   set flname2 = "SEDs/spawn_00"$1".cat"
else if ($1 < 1000) then
   set flname1 = "SEDs/nspawn_0"$1".cat"
   set flname2 = "SEDs/spawn_0"$1".cat"
else
   set flname1 = "SEDs/nspawn_"$1".cat"
   set flname2 = "SEDs/spawn_"$1".cat"
endif
set n_search = `wc -l < $flname1`
set all_indx = `cat $flname1 | awk '{print $1}'`
set n_cnt = 0
set n_indx = 0
if ($n_search == 0) then
   echo "Error in grab_photo.csh"
   exit(1)
endif
while ($n_cnt < $n_search)
   @ n_cnt = $n_cnt + 1
   if ($all_indx[$n_cnt] == $2) then
      set n_indx = $n_cnt
      set n_cnt = $n_search
   endif
end
set photo_val = `cat $flname2 | awk 'NR==var1{print $var2}' var1=$n_indx var2=$3`
@ tri_1 = $3 + 1
set photo_err = `cat $flname2 | awk 'NR==var1{print $var2}' var1=$n_indx var2=$tri_1`
set lim_test = `echo "1" | awk '{if (var1<var2) print 1; else print 0}' var1=$photo_val var2=$photo_err`
if ($lim_test == "1") then
   set photo_val = $photo_err
endif
echo $photo_val
exit(0)
