#!/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  12/2005 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script is an intermediate solution to process the montecarlo files.
# As soon as the montecarlo database is running, this script shall be 
# exchanged. 
# 
# The script is building one sequence per directory. As the files are 
# structured by condition, the files in one directory and such in one 
# sequence have the same observation mode, psf, zbin, ... 
# 

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=mcsequences

set -C

scriptlog=$runlogpath/$program`date +%F`.log
date >> $scriptlog 2>&1

# check if script is already running
lockfile=$lockpath/lock-$program.txt
checklock  >> $scriptlog 2>&1

# find montecarlo directories, build one sequence per directory and write sequence file
printprocesslog "INFO building one sequence per mcdirectory"
dirs=`find $mcrawpath -type d`
for dir in ${dirs[@]}
do 
   cont=`echo $dir | cut -d/ -f6`
   if [ "$cont" == "" ]
   then
      continue
   fi
   echo $dir >> $scriptlog 2>&1
   files=`ls $dir`
   calfile=`find $dir -name *_C_*`
   pedfile=`find $dir -name *_P_*`
   calrun=`echo $calfile | cut -d_ -f2`
   pedrun=`echo $pedfile | cut -d_ -f2`
   datruns=`find $dir -name *_D_* | cut -d_ -f2 | tr "\n" " "`
# workaround due to 5digit runnumber for data with runnumber < 35487
#   firstrun=`echo $datruns | cut -c 0-8`
   firstrun=`echo $datruns | cut -c 0-5 | sed -e 's/^0//g' -e 's/^0//g' -e 's/^0//g' -e 's/^0//g'`
   date=`echo $dir | cut -c 22-31 | sed -e 's/\//-/g'`
   
# workaround due to 5digit runnumber for data with runnumber < 35487
#   no=`echo $firstrun | cut -c 0-4`
   no=`printf %08d $firstrun | cut -c 0-4`
   no2=`printf %08d $firstrun`
   sequpath=$mcsequpath/$no
   makedir $sequpath >> $scriptlog 2>&1
#   sequfile=$sequpath/sequence$firstrun.txt
   sequfile=$sequpath/sequence$no2.txt
   echo "writing sequfile "$sequfile >> $scriptlog 2>&1
   printprocesslog "INFO writing sequencefile $sequfile"
   
   echo "Sequence:        $firstrun" >| $sequfile
   echo "Night:           $date" >> $sequfile
   echo "" >> $sequfile
   echo "CalRuns:         $calrun" >> $sequfile
   echo "PedRuns:         $pedrun" >> $sequfile
   echo "DatRuns:         $datruns" >> $sequfile
   echo "" >> $sequfile
   echo "MonteCarlo: Yes" >> $sequfile
   echo "" >> $sequfile
done

finish >> $scriptlog 2>&1

