#!/bin/bash
# script pour lancer une deglaciation Euras-20

# name of the run:
#IPSL
namerun="DGL-EURAS-IPSL"
#namerun="DGL-IPSL-atm1-pdd2"  #Tsai
#namerun="DGL-IPSL-atm1-pdd2-Schoof" # Schoof
#namerun="DGL-IPSL-atm1-pdd2-rap0.07-lap7"  #rappact 0.07
#namerun="DGL-IPSL-atm1-pdd2-rap0.07-lap7-50k"  # restart -50k 
#namerun="DGL-IPSL-atm0-pdd2-rap0.11-lap7-psolid-sigma5.5" #  psolid=0 et sigma=5.5
#namerun="DGL-IPSL-atm0.75-pdd2-rap0.11-lap7-psolid-sigma5.5-calv0"
#namerun="DGL-IPSL-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-lake50" # -lake-1000 50 
#namerun="DGL-IPSL-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5-lake-1000" # sediment + rapides
#namerun="DGL-IPSL-atm0.55-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5"
#FGOALS
#namerun="DGL-FGOA-atm0-pdd2"  #Tsai
#namerun="DGL-FGOA-atm1-pdd2-Schoof" # Schoof
#namerun="DGL-FGOA-atm1-pdd2-rap0.07"  #rappact 0.07
#namerun="DGL-FGOA-atm1-pdd2-rap0.07-lap7-50k"  # restart -50k 
#namerun="DGL-FGOA-atm0-pdd2-rap0.11-lap7-psolid-sigma5.5" #  psolid=0
#namerun="DGL-FGOA-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-lake-1000" # bmelt0.1 0.2 1 - calv0 50 150 - lake-1000 200
#namerun="DGL-FGOA-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5" # sediment + rapides
#namerun="DGL-FGOA-atm0.55-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5"
#MPI
#namerun="DGL-MPI--atm1-pdd2"  #Tsai
#namerun="DGL-MPI--atm0-pdd2-Schoof" # Schoof
#namerun="DGL-MPI--atm1-pdd2-rap0.07-lap7"   #rappact 0.07
#namerun="DGL-MPI--atm1-pdd2-rap0.07-lap7-50k"  # restart -50k
#namerun="DGL-MPI--atm0-pdd2-rap0.11-lap7-psolid-sigma5.5"  #psolid=0 sigma=5.5
#namerun="DGL-MPI--atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-lake50" # bmelt0.1 0.2 1 - calv0 50 150 - lake-1000 200
#namerun="DGL-MPI--atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5-lake-1000" # sediment + rapides
#namerun="DGL-MPI--atm0.55-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5"
#namerun="DGL-MPI--atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5-calv0"
#MRI
#namerun="DGL-MRI--atm0-pdd1"  #Tsai
#namerun="DGL-MRI--atm0-pdd1-Schoof" # Schoof
#MIROC
#namerun="DGL-MIROC-atm0-pdd2"  #Tsai
#namerun="DGL-MIROC-atm1-pdd2-Schoof" # Schoof
#namerun="DGL-MIROC-atm1-pdd2-rap0.07-lap7"   #rappact 0.07
#namerun="DGL-MIROC-atm1-pdd2-rap0.07-lap7-50k"   #restart -50k
#namerun="DGL-MIROC-atm0-pdd2-rap0.11-lap7-psolid-sigma5.5"   # psolid=0 sigma=5.5
#namerun="DGL-MIROC-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-lake-1000" # bmelt0.1 0.2 1 - calv0 50 150 - lake-1000 200
#namerun="DGL-MIROC-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5" # sediment + rapides
#namerun="DGL-MIROC-atm0.55-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5"
#GISS
#namerun="DGL-GISS-atm1-pdd2-rap0.11-lap7-psolid-sigma5.5"   # psolid=0 sigma=5.5
#namerun="DGL-GISS-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-lake50" # bmelt0.1 0.2 1 - calv0 50 150 - lake-1000 200
#namerun="DGL-GISS-atm0.25-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5-lake-1000-out" # sediment + rapides
#namerun="DGL-GISS-atm0.55-pdd2-rap0.11-lap7-psolid-sigma5.5-sedim0.5"


tmodelend="0."
tmodelbegin="-26000."
#tmodelbegin="-50000."
tmodellength="26000."
#tmodellength="50000."
runatmvar="0.25"
runpdd="2"

parallel="yes" # "yes or no?"

#Revision number :
rev=`svn info | grep "Révision : " | awk '{print $2}'`
echo "Code revision svn "$rev

echo "-------------------------------------"
echo "    Now launching run: "   $namerun
echo "      -------------------------      "

#--------------------------------------------------
# Check if times are ok:
#--------------------------------------------------

tcheck=`echo "$tmodelbegin + $tmodellength" | bc -l`
tcheckbis=`echo "$tmodelend + 0" | bc -l`
if [ $tcheck != $tcheckbis ]; then
    echo "The times provided are wrong!"
    echo $tcheck $tcheckbis
    echo "Tbegin:" $tmodelbegin ", Tend:" $tmodelend ", length:" $tmodellength
    exit 0
else
    echo "The times provided are correct."
fi

#--------------------------------------------------
# The different directories:
#--------------------------------------------------
LAUCHING_DIR=`pwd`
GRISLI_DIR=${LAUCHING_DIR%%/lauching_area}
RUNDIR=${GRISLI_DIR}"/RUNS/"${namerun}
MODELMASTER=${GRISLI_DIR}"/SOURCES"
MODEL=${RUNDIR}"/SOURCES"

if [ -d $RUNDIR ]; then
    echo "This run exists, do you really want to overwrite?"
    echo "You have 3 sec. to do a ctr+c..."
    sleep 3
    echo "Ok, your choice."
else
    mkdir $RUNDIR
    echo "      Run will be in:"
    echo "  " $RUNDIR
fi

if [ -d $MODEL ]; then
    echo "model dir already created, cleaning up before copying files"
    rm -rf $MODEL"/"*
else
    mkdir $MODEL
fi
 
cp -R $MODELMASTER"/"* $MODEL"/."
svn status $MODELMASTER > $MODEL"/svn.status.txt"
svn diff $MODELMASTER > $MODEL"/svn.diff.txt"

if [ -d $RUNDIR"/bin" ]; then
    echo "exe in bin/"
else
    mkdir $RUNDIR"/bin"
fi

if [ -d $RUNDIR"/RESULTATS" ]; then
    echo "exe in RESULTATS/"
else
    mkdir $RUNDIR"/RESULTATS"
fi

if [ -d $RUNDIR"/INPUT" ]; then
    echo "inputs are linked to model master"
else
    echo "inputs will be linked to model master"
    mkdir $RUNDIR"/INPUT"
fi


    
echo "Now copying master files to the working directory:"
echo $RUNDIR
echo "And to the model directory:"
echo $MODEL

cp "Master-files/euras_TEMPS-NETCDF-degla.dat" $MODEL"/Fichiers-parametres/euras20_TEMPS-NETCDF.dat"
cp "degla_PMIP_euras20_param_list.dat" $RUNDIR"/bin/euras20_param_list.dat"

if [ $parallel = "yes" ]; then
  cp "Master-files/grisli.run.openmp_euras20" $RUNDIR"/bin/grisli.run"
else
  cp "Master-files/grisli.run_euras20" $RUNDIR"/bin/grisli.run"
fi

cd $RUNDIR"/INPUT"
ln -sf ${GRISLI_DIR}"/INPUT/EURASIE20" .
ln -sf ${GRISLI_DIR}"/INPUT/Forcage" .
ln -sf ${GRISLI_DIR}"/INPUT/tracer.dat" .

#--------------------------------------------------
# Replacing key words:
#--------------------------------------------------

# run spec.:
sed -i "s/RUNNAME/$namerun/" $RUNDIR"/bin/euras20_param_list.dat"
sed -i "s/RUNTBEGIN/$tmodelbegin/" $RUNDIR"/bin/euras20_param_list.dat"
sed -i "s/RUNTEND/$tmodelend/" $RUNDIR"/bin/euras20_param_list.dat"
sed -i "s/RUNRATMVAR/$runatmvar/" $RUNDIR"/bin/euras20_param_list.dat"
sed -i "s/RUNPDDTYPE/$runpdd/" $RUNDIR"/bin/euras20_param_list.dat"

# submission script:
sed -i "s|SOURCESREP|$MODEL|" $RUNDIR"/bin/grisli.run"
sed -i "s|EXEREP|$RUNDIR\/bin|" $RUNDIR"/bin/grisli.run"
sed -i "s/RUNNAME/$namerun/" $RUNDIR"/bin/grisli.run"

# Makefile:
if [ $parallel = "yes" ]; then
    sed -i "s/Makefile.tof-lsce3130.inc/Makefile.tof-asterix-parallel.inc/" $RUNDIR"/SOURCES/Makefile"
else
    sed -i "s/Makefile.tof-lsce3130.inc/Makefile.tof-asterix-sequential.inc/" $RUNDIR"/SOURCES/Makefile"
fi

# param values:
#sed -i "s/PARAMENHFACT/$enh_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMCF/$cf_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMSEUILNEFF/$seuilneff_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMINFILTR/$infiltr_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMKOND0/$kond0_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/BETAMAX/$betamax_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/TOSTICK/$tostick_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/EXPOSLOPE/$exposlope_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMBSLOPE/$bslope_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMBEXPO/$bexpo_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMBMELTCOEF/$bmelt_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMSF/$sf_fact/" $RUNDIR"/bin/anteis1_param_list.dat"


#--------------------------------------------------
# Submitting the job:
#--------------------------------------------------

echo "submitting the job"
qsub $RUNDIR"/bin/grisli.run"
echo "-------------------------------------"
