#!/usr/bin/env bash
# Test if there is at least one lightcurve file
for i in out*dat ;do if [ -f $i ]; then echo ok && break ;else echo "ERROR: There are no light curve files!!!" && exit 1 ;fi ;done
echo \# starting candidates-bls.sh \#
echo " "
ALL=`grep -c " " m_sigma_bin.tmp`
echo "$ALL candidates were detected by rms-test"
echo " "
echo -e "Conducting BLS test. \n It takes time...     (Ctrl+C to abort)"
rm -f candidates-bls.lst
#. autogen.sh
N=0
while read M I X Y NAME ;do
 N=`echo "$N+1"|bc -q`
 PERCENT=`echo "$N*100/$ALL"|bc -q`
 echo -ne "[BLS] $N ($PERCENT%) $NAME searching... "
 SEARCH_OK=0
 lib/testor-period "$NAME" && lib/BLS/bls "$NAME" && echo "ok" && SEARCH_OK=1
 #Try to drop a few points
 if [ $SEARCH_OK -eq 0 ];then
  lib/drop_faint_points 5 "$NAME"
  lib/testor-period "$NAME" && lib/BLS/bls outDROP.dat && echo "ok" && SEARCH_OK=1
  rm -f outDROP.dat
 fi
 if [ $SEARCH_OK -eq 0 ];then
  lib/drop_bright_points 5 "$NAME"
  lib/testor-period "$NAME" && lib/BLS/bls outDROP.dat && echo "ok" && SEARCH_OK=1
  rm -f outDROP.dat
 fi

 if [ $SEARCH_OK -eq 0 ];then
  echo "drop it"
 else
  echo $M $I $X $Y $NAME >>  candidates-bls.lst
 fi

done < m_sigma_bin.tmp

# Create emty file if no candidates were found.
if [ ! -f candidates-bls.lst ];then
 touch candidates-bls.lst
fi



echo \# end of candidates-bls.sh \#
