#! /bin/csh -f
#A simple script to find all the LAEs that make a certain SN cut.
set SN6 = 4.0
set dSN1 = 0.1
set n_SN = 21
set SNinc = 0.3
set inx_orig = `cat Gui_form.cat | awk '{print $2}'`
set SN_now = `cat Gui_form.cat | awk '{print $3}'`
set flag_LAE = `cat Gui_form.cat | awk '{print $4}'`
set num_aper = `cat Gui_form.cat | awk '{print $4}'`
set n_go = `wc -l < Gui_form.cat`
set cnt_SN = 0
while ($cnt_SN < $n_go)
   @ cnt_SN = $cnt_SN + 1
   set SN_now[$cnt_SN] = `cat detections/diagnose.out | awk 'NR==var1{print $7}' var1=$inx_orig[$cnt_SN]`
   set fibers_now = `cat detections/diagnose.out | awk 'NR==var1{print $2}' var1=$inx_orig[$cnt_SN]`
   set num_aper[$cnt_SN] = `echo $fibers_now | awk -F"," '{print NF}'`
end
set cnt_SN = 0
rm -f cum_LAE.out
while ($cnt_SN < $n_SN)
   @ cnt_SN = $cnt_SN + 1
   set SNBase = `echo "$SN6+$dSN1*($cnt_SN-1)" | bc -l`
   set cnt_i = 0
   set n_pass = 0
   while ($cnt_i < $n_go)
      @ cnt_i = $cnt_i + 1
      if ($flag_LAE[$cnt_i] == "1") then
         set SN_eff = `echo "($SNBase+$SNinc*($num_aper[$cnt_i]-1.))-$SN_now[$cnt_i]<=0" | bc -l`
         if ($SN_eff == "1") then
            @ n_pass = $n_pass + 1
         endif
      endif
   end
   echo "$SNBase $n_pass" >> cum_LAE.out
end
