#!/bin/ksh

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

    # Local function to find parameters in LMDZ .def files

lmdzgrep2 () {
     grep -w ${1} ${2} | grep -v "#"  | sed -e 's/=//g' -e 's/://g' -e 's/_AUTO_//' -e 's/DEFAULT//' | sed -e "s/${1}//" 
}

    ##--Variables used by MBG --

    # ln_rsttr  boolean term for tracer model restart (true or false)
    # nrsttr    control of the time step (0, 1 or 2)
    
    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
    ##-- Restart configuration
    if ( [ "${opa9_UserChoices_Reproducibility_after_restart}" = "y" ] ) ; then
        TOP_LN_RSTR=.TRUE.
        TOP_NN_RSTR=2

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

        #echo "NO RESTART FOR TOP"
        TOP_LN_RSTR=.FALSE.
        TOP_NN_RSTR=0

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

        #echo "RESTART TOP"
        TOP_LN_RSTR=.TRUE.
        TOP_NN_RSTR=0

        # If we start from IPSLCM5* restart files.
	for file in restart_trc*.nc ; do
            ncrename -v TRBDSi,TRBGSi -v TRNDSi,TRNGSi -v TRNBSi,TRNDSi -v TRBBSi,TRBDSi ${file} > /dev/null 2>&1
	done

    else

        #echo "RESTART TOP"
        TOP_LN_RSTR=.TRUE.
        TOP_NN_RSTR=2

    fi


    ##-- CO2 forcing : update atcco2 value in namelist_pisces_cfg with the same one used in LMDZ
    IGCM_debug_Print 1 "Take same CO2 as for LMDZ, in parameter file config.def."
    # ATM_Update is done before MBG_Update and therefore config.def file contains the correct value for CO2.
    IGCM_debug_Print 1 'Update atcco2 in namelist_pisces_cfg with same value as for LMDZ'
    LMDZ_CO2=$( lmdzgrep2 co2_ppm config.def )
    IGCM_debug_Print 1 "LMDZ_CO2 : ${LMDZ_CO2} "
    IGCM_comp_modifyNamelist force  namelist_pisces_cfg atcco2 ${LMDZ_CO2}

    ##-- Update namelist_top_cfg and namelist_pisces_cfg
    IGCM_comp_modifyNamelist blocker  namelist_top_cfg     ln_rsttr   ${TOP_LN_RSTR}
    IGCM_comp_modifyNamelist blocker  namelist_top_cfg     nn_rsttr   ${TOP_NN_RSTR}
    IGCM_comp_modifyNamelist blocker  namelist_pisces_cfg  nn_pisdmp  ${OPA_NPDT_YEAR}
 
    # 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-pisces.xml 1d_pis enabled ${VT1D_ENABLE}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 5d_pis enabled ${VT5D_ENABLE}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 1m_pis enabled ${VT1M_ENABLE}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 1y_pis enabled ${VT1Y_ENABLE}

    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 1d_pis output_level ${pisces_UserChoices_OutputLevel}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 5d_pis output_level ${pisces_UserChoices_OutputLevel}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 1m_pis output_level ${pisces_UserChoices_OutputLevel}
    IGCM_comp_modifyXmlFile nonblocker file_def_nemo-pisces.xml 1y_pis output_level ${pisces_UserChoices_OutputLevel}

   
    IGCM_debug_PopStack "MBG_Update"
}

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

    echo FINALIZE MBG !!!

    IGCM_debug_PopStack "MBG_Finalize"
}

