#!/bin/sh
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Daniela Dorner  08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2004
#
#
# ========================================================================
#
#
#
##############################################################################
#
# This script can be used to run the following steps for one sequence:
#  - callisto
#  - merppupdate
#  - star
#
# You have to set the path, where your Mars version can be found, the outpath,
# where you want to store the output and the sequencenumber of the sequence
# you want to process
# 
##############################################################################

#function to create directory
function makedir()
{
   if [ ! -d $@ ]
   then
     mkdir -pv $@ 
     if [ ! -d $@ ]
     then 
        echo "could not make dir "$@
        exit
     fi
   fi
}


#to be set by the user 
mars=/home/user/Mars
outpath=/home/user/outpath
sequence=55221


#needed if not set in .bashrc or when script is e.g. executed in cronjob
export ROOTSYS=/opt/root_v4.04.02g
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
export PATH=$PATH:$ROOTSYS/bin


no=`printf %08d $sequence | cut -c 1-4`
sequfile="/magic/sequences/"$no"/sequence"`printf %08d $sequence`".txt"
calpath=$outpath"/callisto/"`printf %08d $sequence`
starpath=$outpath"/star/"`printf %08d $sequence`

echo "mars:     "$mars
echo "outpath:  "$outpath
echo "sequence: "$sequence
echo "sequfile: "$sequfile
echo "path where calibration files are stored : "$calpath
echo "path where star files are stored        : "$starpath

makedir $calpath
makedir $starpath

cd $mars

echo "run callisto..."
echo "./callisto -b -q -f --log=$calpath/callisto$sequence.log --out=$calpath $sequfile "
./callisto -b -q -f --log=$calpath/callisto$sequence.log --out=$calpath $sequfile 
echo "callisto is finished "
echo ""



calfiles=`find $calpath -name [2][0][0-2][0-9][0,1][0-9][0-3][0-9]_*_Y_*.root`

echo "run merpp update for calibrated files..."
echo "calibrated data files: "$calfiles

for calfile in ${calfiles[@]}
do 
   echo "calfile: "$calfile 
   outpath=`dirname $calfile`
   echo "outpath: "$outpath
   
   merpplogpath=$outpath"/merpplogs"
   makedir $merpplogpath
   
   runno=`echo $calfile | cut -d_ -f2 | sed -e 's/^0//' | sed -e 's/^0//' | sed -e 's/^0//' `
   ccfile=`find /magic/subsystemdata/cc/ -name [2][0][0-2][0-9][0,1][0-9][0-3][0-9]_*${runno}_[P,D,C,S]_*_S.rep`
   cacofile=`find /magic/subsystemdata/caco/ -name dc_[2][0][0-2][0-9]_[0,1][0-9]_[0-3][0-9]_*${runno}_*.txt`
   echo "runno: "$runno 
   echo "ccfile: "$ccfile 
   if [ "$ccfile" = "" ]
   then
      echo "no ccfile found for run "$runno 
      break
   fi
   echo "cacofile: "$cacofile 
   if [ "$cacofile" = "" ]
   then 
      echo "no cacofile found for run "$runno 
      echo "finding cacofile..." 
      for (( i = 0; i <= 10; i++ ))
      do 
         newrun=`echo $runno - $i | bc`
  #      echo "$missingcacorun + $i = $newrun"
         path=`dirname $ccfile`
         path=`echo $path | sed -e 's/cc/caco/'`
         echo "path: "$path 
         cacofile=`find $path -name *$newrun*`
         if [ "$cacofile" = "" ]
         then
            continue
         else
            break
            echo "cacofile: "$cacofile 
         fi
      done
   fi
   echo "./merpp -u --log=$merpplogpath/merppccupdate$runno.log --runfile=$runno $ccfile $calfile "
   ./merpp -u --log=$merpplogpath/merppccupdate$runno.log --runfile=$runno $ccfile $calfile 

   echo "./merpp -u --log=$merpplogpath/merppcacoupdate$runno.log --auto-time $cacofile $calfile "
   ./merpp -u --log=$merpplogpath/merppcacoupdate$runno.log --auto-time $cacofile $calfile 
done
echo "merppupdate is finished"
echo ""



echo "run star..."
echo "./star -b -q -f --log=$starpath/star$sequence.log --ind=$callistopath --out=$starpath $sequfile "
./star -b -q -f --log=$starpath/star$sequence.log --ind=$callistopath --out=$starpath $sequfile 
echo "star is finished "


