#!/bin/ksh

#-----------------------------------
function MBG_Initialize
{ 
    IGCM_debug_PushStack "MBG_Initialize"

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

    IGCM_debug_PopStack "MBG_Initialize"
}

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

    
    ##--Write Frequency for iomput
    ## Differents frequencies are allowed for MBG files

    VT1D_ENABLE=".FALSE."
    VT5D_ENABLE=".FALSE."
    VT1M_ENABLE=".FALSE."
    VT1Y_ENABLE=".FALSE."

    for frequency in ${config_MBG_WriteFrequency} ; do
        case ${frequency} in
            ( 1D|1d ) VT1D_ENABLE=".TRUE." ;;
            ( 5D|5d ) VT5D_ENABLE=".TRUE." ;;
            ( 1M|1m ) VT1M_ENABLE=".TRUE." ;;
            ( *[yY] ) VT1Y_ENABLE=".TRUE." ;;
        esac
    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

   if [ X"${trc_UserChoices_age}" = X"y" ] ; then
      echo "Activate age tracer"
      TOP_AGE=.TRUE.
      IGCM_comp_modifyXmlFile force file_def_nemo-top.xml file38 enabled .TRUE.
   else
      TOP_AGE=.FALSE.
   fi
##
   if [ X"${trc_UserChoices_cfc11}" = X"y" ] ; then
      echo "Activate CFC11  tracer"
      TOP_CFC11=.TRUE.
      IGCM_comp_modifyXmlFile force file_def_nemo-top.xml file39 enabled .TRUE.
   else
      TOP_CFC11=.FALSE.
   fi
##
   if [ X"${trc_UserChoices_cfc12}" = X"y" ] ; then
      echo "Activate CFC12  tracer"
      TOP_CFC12=.TRUE.
      IGCM_comp_modifyXmlFile force file_def_nemo-top.xml file40 enabled .TRUE.
   else
      TOP_CFC12=.FALSE.
   fi
##
   if [ X"${trc_UserChoices_sf6}" = X"y" ] ; then
      echo "Activate SF6  tracer"
      TOP_SF6=.TRUE.
      IGCM_comp_modifyXmlFile force file_def_nemo-top.xml file41 enabled .TRUE.
   else
      TOP_SF6=.FALSE.
   fi
##
  if [ X"${trc_UserChoices_c14}" = X"y" ] ; then
      echo "Activate RC14  tracer"
      TOP_C14=.TRUE.
      IGCM_comp_modifyXmlFile force file_def_nemo-top.xml file42 enabled .TRUE.
   else
      TOP_C14=.FALSE.
   fi

##
   ##-- Update namelist_top_cfg and namelist_trc_cfg
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     ln_rsttr  ${TOP_LRSTTR}
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     nn_rsttr  ${TOP_NRSTTR}
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     ln_age    ${TOP_AGE}
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     ln_cfc11  ${TOP_CFC11}
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     ln_cfc12  ${TOP_CFC12}
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     ln_sf6    ${TOP_SF6}
    IGCM_comp_modifyNamelist blocker    namelist_top_cfg     ln_c14    ${TOP_C14}
 
    # Update iodef.xml

    IGCM_debug_Print 1 'Informations into iodef.xml : VT1D_ENABLE VT5D_ENABLE VT1M_ENABLE VT1Y_ENABLE '
    IGCM_debug_Print 1 ${VT1D_ENABLE}  ${VT5D_ENABLE} ${VT1M_ENABLE} ${VT1Y_ENABLE}

    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 1d_pis enabled ${VT1D_ENABLE}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 5d_pis enabled ${VT5D_ENABLE}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 1m_pis enabled ${VT1M_ENABLE}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 1y_pis enabled ${VT1Y_ENABLE}

    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 1d_pis output_level ${trc_UserChoices_OutputLevel}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 5d_pis output_level ${trc_UserChoices_OutputLevel}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 1m_pis output_level ${trc_UserChoices_OutputLevel}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-top.xml 1y_pis output_level ${trc_UserChoices_OutputLevel}



   IGCM_debug_PopStack "MBG_Update"
}

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

    echo FINALIZE MBG !!!

    IGCM_debug_PopStack "MBG_Finalize"
}

