#!/bin/ksh

#-----------------------------------
function TOP_Initialize
{ 
    IGCM_debug_PushStack "TOP_Initialize"

    ##--Variables used by TOP --

    # nwritetrc frequency of write in the tracer output file
    # lrsttr    boolean term for tracer model restart (true or false)
    # nrsttr    control of the time step (0, 1 or 2)
    # ntrd_trc   time step frequency of write in the trends file for tracers
    # ln_trdmld_trc_restart boolean term for trc trends restart (true or false)
    
    ##--Variables used by PISCES --
 
    # nwritedia frequency of write in the additional diag output file

# Local function to find namelists parameters
supergrep () {
    grep "^ *$1 *=" $2 | sed -e "s% *\!.*%%"
}

    PAT_TOP_NWRITETRC=$( supergrep nwritetrc             ${SUBMIT_DIR}/PARAM/namelist_top_${RESOL_OCE} )
    PAT_TOP_LRSTTR=$( supergrep ln_rsttr                   ${SUBMIT_DIR}/PARAM/namelist_top_${RESOL_OCE} )
    PAT_TOP_NRSTTR=$( supergrep nrsttr                   ${SUBMIT_DIR}/PARAM/namelist_top_${RESOL_OCE} )
    PAT_TOP_NWRITETRD=$( supergrep ntrd_trc              ${SUBMIT_DIR}/PARAM/namelist_top_${RESOL_OCE} )
    PAT_TOP_RSTRTTRD=$( supergrep ln_trdmld_trc_restart  ${SUBMIT_DIR}/PARAM/namelist_top_${RESOL_OCE} )

    PAT_PIS_NWRITEDIA=$( supergrep nwritedia             ${SUBMIT_DIR}/PARAM/namelist_pisces_${RESOL_OCE} )

    IGCM_debug_PopStack "TOP_Initialize"
}

#-----------------------------------
function TOP_Update
{
    IGCM_debug_PushStack "TOP_Update"

    ##--Write Frequency Purpose ....
    topfrequency=$( echo ${config_TOP_WriteFrequency} | awk "-F " '{print $1}' )
    topfactor=$( echo ${topfrequency} | sed -e "s/[yYmMdD]//" )
    case ${topfrequency} in
        1Y|1y)
            (( TOP_NWRITE = ORCA_NPDT_JOUR * topfactor * $( IGCM_date_DaysInYear  ${year} ) ))          ;
            PREFIX_NWRITETRC=${config_UserChoices_JobName}_${topfactor}y ;
            R_OUT_TOP_NWRITE=${R_OUT_TOP_O_Y} ;
            WFT1=${topfactor}Y ;;
        1M|1m)
            (( TOP_NWRITE = ORCA_NPDT_JOUR * topfactor * $( IGCM_date_DaysInMonth  ${year} ${month} ) ))          ;
            PREFIX_NWRITETRC=${config_UserChoices_JobName}_${topfactor}m ;
            R_OUT_TOP_NWRITE=${R_OUT_TOP_O_M} ;
            WFT1=${topfactor}M ;;
        *D|*d)
            (( TOP_NWRITE = ORCA_NPDT_JOUR * topfactor  ))          ;
            PREFIX_NWRITETRC=${config_UserChoices_JobName}_${topfactor}d ;
            R_OUT_TOP_NWRITE=${R_OUT_TOP_O_D} ;
            WFT1=${topfactor}D ;;
        *)
    esac

    ##-- Restart configuration
    if ( [ "${CumulPeriod}" -eq 1 ] && [ "${config_TOP_Restart}" = "n" ] ) ; then

        #echo "NO RESTART FOR TOP"
        TOP_LRSTTR=.FALSE.
        TOP_NRSTTR=0

    elif ( [ "${CumulPeriod}" -eq 1 ] && [ "${config_TOP_Restart}" = "y" ] ) ; then

        #echo "RESTART TOP"
        TOP_LRSTTR=.TRUE.
        TOP_NRSTTR=0

    else

        #echo "RESTART TOP"
        TOP_LRSTTR=.TRUE.
        TOP_NRSTTR=2
    fi

    sed -e "s%${PAT_TOP_NWRITETRC}%       nwritetrc=${TOP_NWRITE}%"               \
        -e "s%${PAT_TOP_NWRITETRD}%       ntrd_trc=${TOP_NWRITE}%"                \
        -e "s%${PAT_TOP_LRSTTR}%          ln_rsttr=${TOP_LRSTTR}%"                   \
        -e "s%${PAT_TOP_NRSTTR}%          nrsttr=${TOP_NRSTTR}%"                   \
        namelist_top > namelist_top.tmp

    IGCM_sys_Mv namelist_top.tmp namelist_top

# on force les sorties PISCES comme les sorties OPA

    sed -e "s%${PAT_PIS_NWRITEDIA}%       nwritedia=${TOP_NWRITE}%"              \
        namelist_pisces > namelist_pisces.tmp

    IGCM_sys_Mv namelist_pisces.tmp namelist_pisces

    echo 'Variables automatically updated in namelists'
    grep AUTO namelist*

    IGCM_debug_PopStack "TOP_Update"
}

#-----------------------------------
function TOP_Finalize
{
    IGCM_debug_PushStack "TOP_Finalize"

    echo FINALIZE TOP !!!

    IGCM_debug_PopStack "TOP_Finalize"
}

