#!/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): Martin Merck  05/2004 <mailto:merck@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2004
#
#
# ========================================================================

#/////////////////////////////////////////////////////////////////////////////
#
# runmerpp
#
# This scripts is used to run merpp on a full Period of data. It will run merpp
# on the raw-data file. Then it will update the root file by merpping in the CC
# and CACO data.
# As for some rawdata files no CaCo data files exist (due to DAQ restarting a run)
# we need to use the previous CaCo file. This will add to much dc-current infos
# to the merpped file, but will gaurantee that the right dc-currents are always
# available.
#
# TODO:
#  Due to problems with the DB the automatic DB update option is still disabled.
#

# ------------------------------------
# Which Period to use
INDIR=/data/MAGIC/Period015
# ------------------------------------
#
# For each day of this Period
#
DATES=`ls -d -1 $INDIR/rawdata2/20* | cut -d/ -f6 '-'`
for DATE in $DATES
do
  OUTDIR=$INDIR/rootdata2/$DATE
# Create Output Directory 
  if [ ! -d $OUTDIR ]
  then
    echo $DATE
    mkdir $OUTDIR
  fi

#
# Loop over all raw files for this day
#
  RAWFILES=`ls -1 $INDIR/rawdata2/$DATE/*.raw`
  LOGFILE=$OUTDIR/merpp.log
  TIME=`date`
  echo "-----------------------------------------------------------------" > $LOGFILE
  echo " MERPP run started $TIME" >> $LOGFILE
  echo "-----------------------------------------------------------------" >> $LOGFILE

  PREVIOUS_CACO=
  for i in $RAWFILES
  do
    TIMER=$SECONDS
    IN_FILE=$i
    RUN_NUM=`echo $i | cut -d_ -f4` 
    CC_FILE=`ls -1 $INDIR/ccdata/$DATE/*$RUN_NUM*.rep`
    CACO_FILE=`ls -1 $INDIR/cacodata/$DATE/*$RUN_NUM*.txt 2> /dev/null`
    OUT_FILE=$OUTDIR/`echo $i | cut -d/ -s -f7 '-' | cut -d. -s -f1 '-'`.root
#    merpp -a -f --sql=mysql://hercules:d99swMT\!@hercules $IN_FILE $OUT_FILE >> $LOGFILE

#   MERPP the raw data file
    merpp -a -f $IN_FILE $OUT_FILE >> $LOGFILE

#   MERPP the CC report file
    merpp -a -u $CC_FILE $OUT_FILE >> $LOGFILE

#   If a CACO file exists for this run
#   MERPP the CACO file.
    if [ -n "$CACO_FILE" ]
    then
      merpp -a -u $CACO_FILE $OUT_FILE >> $LOGFILE
      PERVIOUS_CACO=$CACOFILE
    fi

#   If no CACO file is available for this run
#   we use the previous CACO file
#   CACO only nows when CC starts a new run and creates 1 file for this run
#   however the DAQ may create more subruns due to errors or limitaation on the 
#   filesize. The relevant CACO information should be in the CACO file created
#   when CC started this run (e.g. the previous CACO file)
    if [ -z "$CACO_FILE" ]
    then
      if [ -n "$PREVIOUS_CACO" ]
      then
        merpp -a -u $PREVIOUS_CACO $OUT_FILE >> $LOGFILE
      fi
    fi  
    echo "Processed file $IN_FILE in $(( $SECONDS - $TIMER)) secs."
  done
done
