#!/bin/bash

function create_exp() { # executable expname output_grid output_freq  - $* passed to rundef_$expname 
    EXEC=$1 ; shift
    EXP=$1 ; shift
    OUTPUT_GRID=$1 ; shift
    OUTPUT_FREQ=$1 ; shift
    EXPDIR=${EXP}_${EXEC}_$1
    echo "Creating $EXPDIR"
    rm -rf $EXPDIR
    mkdir $EXPDIR
    cd $EXPDIR
    ln -sf $ROOT/bin/icosa_${EXEC}.exe gcm.exe
    ln -sf $ROOT/python/post_${EXP}.py post.py
    rundef_$EXP $* > tmp.def
    sort tmp.def | cut -c 7- > run.def
    rm -f tmp.def
    xml_iodef > iodef.xml
    xml_icosa > icosa.xml
    xml_field_def > field_def.xml
    xml_dcmip $EXP $OUTPUT_GRID $OUTPUT_FREQ > filedef_dcmip2016.xml
    job_${EXP} ${EXEC} $* > job.sh
    cd ..
}

function rundef_all() {
    cat <<EOF
100   #------------- Planet ---------------
200   #------------- Dynamics  ------------
300   #-------------- Mesh ----------------
400   #------------ Numerics --------------
500   #----------- Dissipation ------------
600   #-------------- Physics -------------
700   #---------------- Run ---------------
EOF
}

function setvar()
{
    while (($# > 1)) ; do
	ii=${idx[$1]}
	echo "$ii   $1=$2"
	shift ; shift
    done
}
function order()
{
    ii=$1 ; shift
    for name in $* ; do
	((ii+=1))
       idx[${name}]=$ii
#       echo $name ${idx[${name}]}
    done
}

#----------------------------- pre-defined resolutions ---------------------------

function set_nbp_40() {
    setvar dt 480 optim_it 500
    split_${EXEC}_40
}

function set_nbp_80() {
    setvar dt 240 optim_it 1000
    split_${EXEC}_80
}

#-------------------------------- Williamson (1991) ------------------------------

function rundef_W91() {
    NBP=$1 ; TAU=$2
    rundef_all
    set_nbp_$NBP
    setvar caldyn_conserv enstrophy caldyn_eta eta_lag boussinesq .TRUE. llm 1 radius 6.37122e6 g 9.80616
    setvar nitergdiv 2 nitergrot 2 niterdivgrad 2 tau_graddiv $TAU tau_gradrot $TAU tau_divgrad $TAU
    setvar write_period 86400 run_length 2592000 etat0 williamson91.6
}

function job_W91() {
    job_$1_$2 W91_$1
}

#------------------------------------- DCMIP3.1 ----------------------------------

function rundef_DCMIP(){
    rundef_all
    set_nbp_$1
    setvar radius 6.37122e6 g 9.80616 kappa 0.2857143 cpp 1004.5 preff 1e5
    setvar nitergdiv 2 nitergrot 2 niterdivgrad 2 tau_graddiv
}

function rundef_DCMIP31() { # NBP TAU
    NBP=$1 ; TAU=$2
    rundef_DCMIP $NBP
    setvar hydrostatic .FALSE.
    setvar omega 0. llm 10 disvert ncar ncar_T0 300 ncar_p0 1e5 ncar_disvert_c 1 ncar_dz 1000 
    setvar tau_graddiv $TAU tau_gradrot $TAU tau_divgrad $TAU 
    setvar scale_factor 125 write_period 7500 run_length 4.5e5 etat0 dcmip3
}
function rundef_DCMIP41() { # NBP TAU
    NBP=$1 ; TAU=$2
    rundef_DCMIP $NBP
#    setvar hydrostatic .FALSE.
    setvar omega 7.292e-5 llm 30 disvert ncarl30 nqtot 2 itau_adv 3
    setvar tau_graddiv $TAU tau_gradrot $TAU tau_divgrad $TAU
    setvar write_period 21600 run_length 2592000 etat0 dcmip4 dcmip4_testcase 1
}

function job_DCMIP31() { # EXEC NBP
    job_$1_$2 DCMIP31_$1 # e.g. job_mpi_80 DCMIP31_mpi
}
function job_DCMIP41() { # EXEC NBP
    job_$1_$2 DCMIP41_$1
}

#----------------------------- END ----------------------------

function main()
{
    rm -rf RUNS
    mkdir RUNS
    cd RUNS
# the number associated to each run.def parameter is used to sort the lines and order them in a logical way
    declare -A idx
    order 100 radius omega g cpp kappa preff scale_factor
    order 200 boussinesq hydrostatic
    order 300 nbp optim_it nsplit_i nsplit_j llm disvert ncar_disvert_c ncar_dz ncar_T0 ncar_p0
    order 400 caldyn_eta time_scheme caldyn_conserv dt nqtot itau_adv
    order 500 nitergdiv tau_graddiv nitergrot tau_gradrot niterdivgrad tau_divgrad
    order 700 run_length write_period etat0 dcmip4_testcase
# Williamson 1991
#    create_exp serial W91 40 5000 # nbp tau
#    create_exp mpi W91 40 5000 # nbp tau
# DCMIP 2012 
    create_exp mpi DCMIP31 two_degree 60s 40 90000 # nbp tau 
    create_exp mpi DCMIP41 two_degree 24h 40 10000 # nbp tau 
}

source current_args
source bash/${arch}.sh
source bash/xml.sh
ROOT=$PWD
main
