#!/bin/bash

#################################### Site-specific functions #######################
# In order to accomodate various computing environments, testing.sh relies on the following 
# site-specific functions which must be provided by include_site.sh
#
# submit_run_and_post EXP
#   Submit the run job run.job to the batch scheduler
#   Submit the post-processing job post.job to the batch scheduler 
#   so that it starts after successful completion of run.job
#
# qstat
#   Display cluster status : jobs running, waiting, ect.
#
# print_run_job()
#   Print run.job on standard output
#   Standard output is redirected to run.job by caller
#
# print_post_job()
#   Print beginning of post.job on standard output. The caller will print additional lines depending on the test case.
#   Standard output is redirected to file post.job by caller
#   Script post.job must source include_site.sh, include_post.sh 
#   and either include_post_xios.sh or include_post_noxios.sh .
#   These two scripts define functions suitable for output produced with/without XIOS
#   These functions are called at the end of post.job, in the test-case-dependent part appended by the caller
#   These functions call wrapper functions NCKS and CDO which must also be defined in include_site.sh

################################ Helper functions ##############################

function redef()
{
    while (($# > 0)) ; do
	sed -i "s/$1[ ]*=[ ]*[[:alnum:]\.]*/$1=$2/g" run.def
	shift; shift;
    done
}

function create_exp()
{
# EXP_NAME param_dir run grid post_XX
    mkdir -p $TESTING/$WORK/$1
    cd $TESTING/$WORK/$1
    ln -sf $DYNAMICO/bin/icosa_gcm.exe .
    ln -sf $TESTING/$WORK/$4 mygrid
    ln -sf $TESTING/include_*.sh .
    ln -sf $TESTING/iodef.xml $TESTING/lmdz_physics.xml .
    cp $TESTING/$WORK/param_sets/$2/*.def .
    cp -f "$3.def" tmp.def
    mv -f tmp.def run.def
    redef nsplit_i 2 nsplit_j 2
    print_run_job > run.job
    print_post_job > post.job
    echo $5 >> post.job
    chmod a+rx *.job
}

function grid()
{
    echo gridtype = lonlat
    echo xsize    = $1
    echo xfirst   = $2
    echo xinc     = $3
    echo ysize    = $4
    echo yfirst   = $5
    echo yinc     = $6
}


########################### Resolution-dependent parameters ####################
function nbpX() { redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3
}
function nbp32() { nbpX 32 600 21600
}
function nbp64() { nbpX 64 300 10800
}
function nbp128() { nbpX 128 150 5400
}
function nbp40() { nbpX 40 480 18000
}
function nbp80() { nbpX 80 240 9000
}
function nbp160() { nbpX 160 120 4500
}
################################### Test cases #################################

# Williamson (1991)

function W91()
{
    EXP=W91_6_$1
    echo "$EXP : nbp=$1 dt=$2 grid=$4"
    create_exp $EXP shallow_water/williamson91 run6 $4 post_W91
    ln -sf $TESTING/icosa_SW.xml icosa.xml
    redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3 
    redef write_period 86400 run_length 2592000
    submit_run_and_post $EXP
}

# Jablonowski & Williamson (2006)

function JW06()
{
    EXP=JW06_$1
    echo "$EXP : nbp=$1 dt=$2 grid=$4"
    create_exp $EXP dcmip2012 run41 $4 post_JW06
    ln -sf $TESTING/icosa_JW06.xml icosa.xml
#    redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3 
    nbp$1
    redef write_period 86400 run_length 864000 etat0 jablonowsky06
    submit_run_and_post $EXP
}

# DCMIP1.2 Transport by Hadley-like circulation

function DCMIP12()
{
    EXP=DCMIP12_$1_$2
    echo "$EXP : nbp=$1 llm=$2 ncar_dz=$3 dt=$4 grid=$5"
    create_exp $EXP dcmip2012 run11 $5 post_DCMIP12
    redef nqtot 1 nbp $1 llm $2 ncar_dz $3 dt $4
    redef dcmip1_shape hadley dcmip1_wind hadley run_length 86400 write_period 3600 
    submit_run_and_post $EXP
}

# DCMIP2.0 Rest over orography (PGF error)

function DCMIP20()
{
    EXP=DCMIP20_$1_$2
    echo "$EXP : nbp=$1 llm=$2 ncar_dz=$3 dt=$4 grid=$5"
    create_exp $EXP dcmip2012 run20 $5 post_DCMIP20
#    redef nbp $1 dt $4
    nbp$1
    redef nqtot 0 llm $2 ncar_dz $3
# from make_rundefs.sh : group 'Time' run_length 518400 write_period 21600
# comment out the following line to conform to DCMIP document
#    redef run_length 86400 write_period 3600 
    submit_run_and_post $EXP
}

# DCMIP2.1 Orographic wave

function DCMIP21()
{
    EXP=DCMIP21_$1_$2
    echo "$EXP : nbp=$1 llm=$2 ncar_dz=$3 dt=$4 grid=$5"
    create_exp $EXP dcmip2012 run21 $5 post_DCMIP21
#    redef nbp $1 dt $4
    nbp$1
    redef nqtot 0 llm $2 ncar_dz $3 run_length 86400 write_period 3600 
    submit_run_and_post $EXP
}

# DCMIP3.1 Non-orographic gravity wave

function DCMIP31()
{
    EXP=DCMIP31_$1_$2
    echo "$EXP : nbp=$1 llm=$2 ncar_dz=$3 dt=$4 grid=$5"
    create_exp $EXP dcmip2012 run3 $5 post_DCMIP31
#    redef nbp $1 dt $4
    nbp$1
    redef nqtot 0 llm $2 ncar_dz $3 run_length 86400 write_period 3600
    submit_run_and_post $EXP
}

# DCMIP4.1 JW06 on a small planet with tracers

function DCMIP41()
{
    EXP=DCMIP41_$1
    echo "$EXP : nbp=$1 dt=$2 tau_dissip=$3 grid=$4"
    create_exp $EXP dcmip2012 run41 $4 post_DCMIP41
#    redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3
    nbp$1
    redef nqtot 2 write_period 86400 run_length 864000
    submit_run_and_post $EXP
}

# DCMIP5 Tropical cyclone

function DCMIP51()
{
    EXP=DCMIP51_$1
    echo "$EXP : nbp=$1 dt=$2 grid=$4"
    create_exp $EXP dcmip2012 run51 $4 post_DCMIP51
    nbp$1
#    redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3 
    redef itau_physics 6
    submit_run_and_post $EXP
}

# Held & Suarez (1994)

function HS94()
{
    EXP=HS94_$1_$3
    echo "$EXP : nbp=$1 dt=$2 grid=$4"
    create_exp $EXP climate/Held_Suarez run $4 post_HS
    nbp$1
#    redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3
    submit_run_and_post $EXP
}

# Saturn : start from rest, vertical temperature profile read from file

function Saturn()
{
    EXP=SATURN_$1
    echo "$EXP : nbp=$1 dt=$2 grid=$4"
    create_exp $EXP planets run_saturn $4
    for x in saturn_const.def callphys.def gases.def icosa.xml apbp.txt temp_profile.txt DATAGENERIC; do
    ln -s $TESTING/SATURN/TEST/$x
    done
    ln -sf $TESTING/icosa_SW.xml icosa.xml
    redef nbp $1 dt $2 tau_graddiv $3 tau_gradrot $3 tau_divgrad $3 
    redef nsplit_i 1 nsplit_j 1 nqtot 0 
    redef run_length 38052    # 1     Saturn day
#    redef run_length 3805200  # 100   Saturn days
#    redef run_length 76104000 # 20000 Saturn day
#    echo post_SATURN >> post.job
#    submit_run_and_post $EXP
}

########################################  Main #######################################

function reset()
{
    echo "Testing DYNAMICO revision $revision"
    set -x
    rm -rf $TESTING/$WORK
    mkdir $TESTING/$WORK
    set +x

    mkdir $TESTING/$WORK/param_sets
    cd $TESTING/$WORK/param_sets
    $TESTING/trunk/param_sets/make_rundefs.sh

    cd $TESTING/$WORK
    grid 720 0.25 0.5 360 -89.75 0.5 > fine_grid
    grid 360 0.5  1   180 -89.5  1   > medium_grid
    grid 180 1    2    90 -89    2   > coarse_grid
}

function debug()
{
#    W91 32 600 21600 coarse_grid
#    HS94 32 600 86400 coarse_grid
    JW06 32 600 21600 coarse_grid
#    DCMIP12 40 30 400 600 coarse_grid
#    DCMIP12 80 60 200 300 medium_grid
#    DCMIP12 160 120 100 150 medium_grid
#    DCMIP20 40 30 400 480 coarse_grid
#    DCMIP21 40 60 500 480 coarse_grid
#    DCMIP31 40 10 1000 480 coarse_grid
#    DCMIP41 32 600 21600 coarse_grid
#    DCMIP51 32 600 21600 coarse_grid
#    cp -pr $TESTING/trunk/param_sets/planets $TESTING/$WORK/param_sets
#    Saturn 10 475.65 5000 coarse_grid
}

function fast()
{
    W91 32 600 21600 coarse_grid
    JW06 32 600 21600 coarse_grid
    DCMIP12 40 30 400 600 coarse_grid
    DCMIP20 40 30 400 480 coarse_grid
    DCMIP21 40 60 500 480 coarse_grid
    DCMIP31 40 10 1000 480 coarse_grid
    DCMIP41 32 600 21600 coarse_grid
    DCMIP51 32 600 43200 coarse_grid
}

function GMD_2014()
{
#    W91 32 600 21600 coarse_grid
#    JW06 32 600 21600 coarse_grid
    HS94 32 600 86400 coarse_grid
}

function extended()
{
    W91      64 300 10800 fine_grid
    W91     128 150 5400  fine_grid
    JW06     64 300 10800 fine_grid
    JW06    128 150 5400  fine_grid
    DCMIP51  64 300 21600 fine_grid
    DCMIP51 128 150 10800 fine_grid
    HS94     32 600 86400 fine_grid
    HS94     32 600 5400  fine_grid
    HS94     40 480 50000 fine_grid
    HS94     40 480 21600 fine_grid
    HS94     64 300 10800 fine_grid
}

#set -o errexit

# include machine-specific helper/wrapper functions : 
#     print_run_job print_post_job submit_run_and_post qstat

source include_site.sh

clear
echo "Usage : testing.sh [reset] [debug|fast|extended]"
TESTING=$PWD
DYNAMICO=$TESTING/trunk
# find out revision number
cd $DYNAMICO
svn info --xml | grep revision | head -n 1 > info.xml
sed -i "s/>//g" info.xml
. info.xml    
WORK="r$revision"

for x in $* ; do
echo $x
$x
done

#qstat
