#!/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  05/2007 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script runs corsika.
# 

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=corsika
column=fCorsikaFileAvail

set -C

export FLUPRO='/home/operator/Corsika/Mmcs6500/fluka2006_3'

# printing input card to stdout
function printinputcard()
{
   echo "RUNNR "$corsikarunno
   echo "PRMPAR "$primparticle
   query="SELECT fEnergyMin, fEnergyMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
   echo "ERANGE "$erange
   echo "EVTNR 1"
   echo "NSHOW "$numevts
   echo "ESLOPE "$slope
   echo "THETAP "$zdrange
   echo "PHIP "$azrange
#   echo "VIEWCONE 0. 2.3"
   echo "DIRECT "$outpath
   echo "SEED "$seed1" 0 0 "
   echo "SEED "$seed2" 0 0 "
   echo "SEED "$seed3" 0 0 "
   echo "OBSLEV 2200.E2"
   echo "RADNKG 200.E2"
   echo "MAGNET 29.5 23.0"
   echo "ECUTS 0.3 0.3 0.02 0.02"
   echo "MUADDI F"
   echo "MUMULT T"
   echo "LONGI T 10. T F"
   echo "MAXPRT 0"
   echo "ECTMAP 1.E4"
   echo "STEPFC 0.1"
   echo "DEBUG F 6 F 1000000"
   echo "CWAVLG 290. 600."
   echo "CSCAT 1 0. "$impact
   echo "CERSIZ 1."
   echo "CERFIL T"
   echo "FIXCHI 280"
   echo "DATBAS F"
   echo "CERTEL 1"
   echo " 0. 0. 0. 0. 0. 1800. 1700."
   echo "USER operator"
   echo "ATMOSPHERE 11 T"
   echo "EXIT"
}


scriptlog=$runlogpath/run$program-$datetime.log
date >> $scriptlog 2>&1

# get sequence # 
gettodo >> $scriptlog 2>&1
run=$process
# get corsika runno
query="SELECT fCorsikaRunNumber FROM MCRunData where fMCRunNumber="$run
corsikarunno=`sendquery`

# lock sequ
lockfile=$lockpath/lock-$table-$column-$corsikarunno.txt
checklock >> $scriptlog 2>&1

echo "run $program for run $run..." >> $scriptlog 2>&1
printprocesslog "INFO starting $program for run $run"

primvar=$run
setstatus "start" >> $scriptlog 2>&1

echo "run $program for run $run " >> $scriptlog 2>&1
echo "create input card... " >> $scriptlog 2>&1

# get values for inputcard
query="SELECT fParticleTypeKEY FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
primparticle=`sendquery`
query="SELECT fEnergyMin, fEnergyMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
erange=`sendquery`
query="SELECT fNumEvents FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
numevts=`sendquery`
query="SELECT fEnergySlope FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
slope=`sendquery`
query="SELECT fZenithDistanceMin, fZenithDistanceMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
zdrange=`sendquery`
query="SELECT fAzimuthMin, fAzimuthMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
azrange=`sendquery`
query="SELECT DATE_FORMAT(fRunStart, '%Y/%m/%d') FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
date=`sendquery`
outpath=$mcpath"/"$program"/"$date
query="SELECT fCorsikaSeed FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
seed1=`sendquery`
query="SELECT fCorsikaSeed+1 FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
seed2=`sendquery`
query="SELECT fCorsikaSeed+2 FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
seed3=`sendquery`
query="SELECT fImpactParameter FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
impact=`sendquery`

echo "" >> $scriptlog 2>&1
echo "INPUTCARD:" >> $scriptlog 2>&1
echo "----------" >> $scriptlog 2>&1
echo "" >> $scriptlog 2>&1
echo "" >> $scriptlog 2>&1
printinputcard >> $scriptlog 2>&1

logfile=$outpath/$program"-"`printf %06d $corsikarunno`".log"
makedir $outpath >> $scriptlog 2>&1

cd /home/operator/Corsika/Mmcs6500/

echo "INPUTCARD:" >> $logfile 2>&1
printinputcard >> $logfile 2>&1
echo "" >> $logfile 2>&1
echo "" >> $logfile 2>&1
printinputcard | ./cc6501p-linux >> $logfile 2>&1

check1=$?

case $check1 in
   0)   echo " check1=$check1 -> everything ok " >> $scriptlog 2>&1
        printprocesslog "INFO $program finished successfully for sequence $sequence"
        ;;
   *)   echo " check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
        printprocesslog "ERROR $program failed for sequence $sequence"
        com=$Fcorsika
        check=$check1
        ;;
esac

setstatus "stop" >> $scriptlog 2>&1

finish >> $scriptlog 2>&1

