#!/bin/ksh

#-----------------------------------
function MBG_Initialize
{ 
    IGCM_debug_PushStack "MBG_Initialize"
    IGCM_debug_PopStack "MBG_Initialize"
}

#-----------------------------------
function MBG_Update
{
    IGCM_debug_PushStack "MBG_Update"


    ##--Write Frequency Purpose
    Nbfreq=$( echo ${config_MBG_WriteFrequency} | wc -w )
    typeset -Z1 i=1
    while [ $i -le ${Nbfreq} ]; do
       topfrequency=$( echo ${config_MBG_WriteFrequency} | awk -v var="$i" "-F " '{print $var}' )
       topfactor=$( echo ${topfrequency} | sed -e "s/[yYmMdD]//" )
       case ${topfrequency} in
         *Y|*y)
            eval PREFIX_WFT${i}=${config_UserChoices_JobName}_${topfactor}y_${DatesPeriod} ;
            eval R_OUT_MBG_WFT${i}=${R_OUT_MBG_O_Y} ;
            eval WFT${i}=${topfactor}Y ;;
         *M|*m)
            eval PREFIX_WFT${i}=${config_UserChoices_JobName}_${topfactor}m_${DatesPeriod} ;
            eval R_OUT_MBG_WFT${i}=${R_OUT_MBG_O_M} ;
            eval WFT${i}=${topfactor}M ;;
         *D|*d)
            eval PREFIX_WFT${i}=${config_UserChoices_JobName}_${topfactor}d_${DatesPeriod} ;
            eval R_OUT_MBG_WFT${i}=${R_OUT_MBG_O_D} ;
            eval WFT${i}=${topfactor}D ;;
         *)
      esac
       let i=$i+1
    done

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

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

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

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

    else

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

    fi

# on force les sorties PISCES comme les sorties OPA

   IGCM_comp_modifyNamelist blocker    namelist_top_cfg ln_rsttr  ${TOP_LRSTTR}
   IGCM_comp_modifyNamelist blocker    namelist_top_cfg nn_rsttr  ${TOP_NRSTTR}

    IGCM_debug_PopStack "MBG_Update"
}

#-----------------------------------
function MBG_Finalize
{
    IGCM_debug_PushStack "MBG_Finalize"

    echo FINALIZE MBG !!!

    IGCM_debug_PopStack "MBG_Finalize"
}

