#! /bin/tcsh -f

echo "******************************"
echo "*  start search_var_bin.csh  *"
echo "******************************"
date

cd $result_dir/
set det=$result_dir/$dir0/detection
set prod_dir=$result_dir/$dir0/WP4products

echo "OBS $an_dir"
cp $progr_dir/search_var.py $an_dir 
cd $an_dir/
set evt_file_epic="$det"/"$dir0"_"$b_tot".ds
if ( -e $evt_file_epic) then
 set out_check=`ls "$dir0"_*.out | wc -l | awk '{print $1}'`
 if ($out_check != 0) then
  echo "$evt_file_epic exists"
  set mergevt=`echo "$evt_file_epic"`
  set newdir = $an_dir
  set bandw = "$dir0"_"$b_tot"
  set eventfile = "$mergevt"
  if ($ambient == "cluster") then
   echo "#\!/bin/bash" > $an_dir/search_var.csh
   echo "python search_var.py \044@" >> $an_dir/search_var.csh
   chmod 755 search_var.csh
   ./search_var.csh $newdir $bandw $trans $var $rat $eventfile $ext_threshold
   rm $an_dir/search_var.csh
  endif
  if ($ambient == "mac") then
   echo "./search_var.py $newdir $bandw $trans $var $rat $eventfile $ext_threshold" > $an_dir/search_var.csh
   source $an_dir/search_var.csh
   rm $an_dir/search_var.csh
  endif
 else
  echo "Skipping search_var_bin.csh: there are not files .out..."
  echo "WARNING: Skipped search_var_bin_3xmm.csh: there are not files .out..." >> $prod_dir_final/NO_TRANSIENT.txt
 endif
endif
cd $start_dir/

date
echo "stop search_var_bin.csh"
