#!/bin/bash
#

source `dirname $0`/../Sourcefile.sh
printprocesslog "INFO starting $0"

# get and check variables
if ! [ "$1" = "" ]
then
   starfile=$1
fi

if [ "$starfile" = "" ]
then
   echo "ERROR variable starfile empty."
   printprocesslog "ERROR variable starfile empty."
   finish
fi

# getting coordinates from raw and drive or DB
date=`echo $starfile | grep -o -E '20[0-9][0-9]/[0-3][0-9]/[0-3][0-9]'`
drivefile=$auxdata"/"$date"/"`basename $starfile | cut -d_ -f1`".DRIVE_CONTROL_SOURCE_POSITION.fits"
rawfile=$rawdata"/"$date"/"`basename $starfile | sed -e 's/_I.root/.fits.fz/'`
if [ -e $rawfile ]
then 
   printprocesslog "INFO processing "$rawfile
   tstopi=`$factpath/fitsdump -h $rawfile  2>/dev/null | grep TSTOPI | grep -E -o '[0-9]+'`
   tstopf=`$factpath/fitsdump -h $rawfile  2>/dev/null | grep TSTOPF | grep -E -o '[0-9][.][0-9]+([E][\-][0-9][0-9])?' | sed -e 's/[E]+*/\\*10\\^/'`
   tstop=`echo " $tstopi + $tstopf " | bc -l`
   #tstop=${tstopi}${tstopf}
else
   printprocesslog "WARN "$rawfile" not found."
fi
if [ "$tstop" == "" ]
then
   printprocesslog "WARN tstop empty "$rawfile
fi
if ! ls $drivefile >/dev/null 2>&1
then 
   printprocesslog "WARN "$drivefile" missing."
fi
if  ls $drivefile >/dev/null 2>&1  && ! [ "$tstop" == "" ]
then
   printprocesslog "INFO getting coordinates from "$drivefile" for time range "$tstart" "$tstop
   #echo "${factpath}/fitsdump ${drivefile} -c Ra_src Dec_src -r --filter='Time<'${tstop}  2>/dev/null"
   coordinates=( `${factpath}/fitsdump ${drivefile} -c Ra_src Dec_src -r --filter='Time<'${tstop}  2>/dev/null | tail -1 2>&1` )
   if [ "${coordinates[0]}" == "" ] || [ "${coordinates[1]}" == "" ]
   then
      printprocesslog "WARN couldn't get coordinates ("${coordinates[@]}") from "$drivefile" @ time "$tstop
      echo "WARN couldn't get coordinates ("${coordinates[@]}") from "$drivefile" @ time "$tstop
      finish
   fi
   if [ "${coordinates[0]}" == "0" ] || [ "${coordinates[1]}" == "0" ]
   then
      printprocesslog "WARN coordinates "${coordinates[@]}" @ time "$tstop
      echo "WARN coordinates "${coordinates[@]}" @ time "$tstop
      finish
   fi
else
   night=`echo $date | sed -e 's/\///g'`
   runid=`basename $starfile | cut -d_ -f2`
   printprocesslog "INFO getting coordinates from DB."
   query="SELECT fSourceKEY FROM RunInfo WHERE fNight="$night" AND fRunID="$runid
   sourcekey=`sendquery`
   if [ "$sourcekey" == "" ]
   then
      printprocesslog "WARN sourcekey empty for "$night"_"$runid" - coordinates"${coordinates[@]}
      finish
   fi
   query="SELECT Round(fRightAscension,6), Round(fDeclination,6) from Source WHERE fSourceKey="$sourcekey
   coordinates=( `sendquery` )
   if [ "${coordinates[0]}" == "" ] || [ "${coordinates[1]}" == "" ]
   then
      printprocesslog "WARN couldn't get coordinates ("${coordinates[@]}") for "$night"_"$runid" from DB."
      finish
   fi
   if [ "${coordinates[0]}" == "0" ] || [ "${coordinates[1]}" == "0" ]
   then
      printprocesslog "WARN coordinates "${coordinates[@]}" for "$night"_"$runid
      finish
   fi
fi
ra=${coordinates[0]}
dec=${coordinates[1]}


outpath=`dirname $starfile | sed -e 's/star/ganymed_run/'`
makedir $outpath

output=$outpath/`basename $starfile | cut -d_ -f1-2`
logfile=$output"-ganymed.log"
ganymedfile=$output"-analysis.root"

cd $mars

# run ganymed 
printprocesslog "INFO starting ganymed.C for starfile "$starfile

printprocesslog "DEBUG root -q -b fact/analysis/ganymed.C\($ra\,$dec\,"\"$starfile\""\,"\"$output\""\) | tee $logfile | intgrep"
check1=`root -q -b fact/analysis/ganymed.C\($ra\,$dec\,"\"$starfile\""\,"\"$output\""\) | tee $logfile | intgrep`

case $check1 in
   0)   printprocesslog "INFO ganymed.C was successful for starfile "$starfile" ra "$ra" dec "$dec" (check1=$check1)"
        
        echo "run numevts.C for "$ganymedfile" for table "$resulttable1 >> $logfile 2>&1
        printprocesslog "INFO run numevts.C for "$ganyemdfile" for table "$resulttable1
        printprocesslog "DEBUG root -q -b -l fact/processing/numevents.C+\("\"$ganymedfile"\"\,"\"$starfile"\"\,"\"$resulttable1"\"\,kFALSE\)"
        check1=`root -q -b -l fact/processing/numevents.C+\("\"$ganymedfile"\"\,"\"$starfile"\"\,"\"$resulttable1"\"\,kFALSE\) | tee $logfile | intgrep`
        case $check1 in
           1)   printprocesslog "INFO filling numevts.C was successfully for "$ganymedfile" and table "$resulttable1" (check1=$check1)"
                # check result for alerts (on daq only)
                if [ "$HOSTNAME" = "daq" ]
                then 
                   `dirname $0`/FlareAlerts.sh
                fi
                ;;
           0)   printprocesslog "WARN connection to DB failed in numevts.C (check1=$check1)"
                ;;
           *)   printprocesslog "ERROR numevts.C failed for "$ganymedfile" and table "$resulttable1" (check1=$check1)"
                ;;
        esac
        ;;
   *)   printprocesslog "ERROR ganymed.C failed for starfile "$starfile" ra "$ra" dec "$dec" (check1=$check1)"
        ;;
esac

finish 

