#!/bin/ksh

#D- Driver for ORCHIDEE stomate component (SBG)

#-----------------------------------------------------------------
function SBG_Initialize
{
    IGCM_debug_PushStack "SBG_Initialize"

    IGCM_debug_PopStack "SBG_Initialize"
}

#-----------------------------------------------------------------
function SBG_Update
{
    IGCM_debug_PushStack "SBG_Update"
    
    # Output management
    # Modify file_def_orchidee.xml file using settings from stomate.card
    # We here suppose that for each file, in stomate.card UserChoices section, if the parameter 
    # output_level_filename is set, then also output_freq_filename must be set. The existance of output_freq_filename will not be checked. 
    # If output_level_filename=NONE or if it is not set, the corresponding file will be deactivated.
    
    if [ X${stomate_UserChoices_output_level_stomate_history} = X ] || [ X${stomate_UserChoices_output_level_stomate_history} = XNONE ] ; then
	# output_level_stomate_history is not set in stomate.card or it is set to NONE.
	# Deactivate the file.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 enabled .FALSE.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 output_level 0
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 output_freq 1mo
    else
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 enabled      .TRUE.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 output_level ${stomate_UserChoices_output_level_stomate_history}
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 output_freq  ${stomate_UserChoices_output_freq_stomate_history}
    fi
    
    if [ X${stomate_UserChoices_output_level_stomate_ipcc_history} = X ] || [ X${stomate_UserChoices_output_level_stomate_ipcc_history} = XNONE ] ; then
	# output_level_stomate_ipcc_history is not set in stomate.card or it is set to NONE.
	# Deactivate the file.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 enabled .FALSE.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 output_level 0
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 output_freq 1mo
    else
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 enabled      .TRUE.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 output_level ${stomate_UserChoices_output_level_stomate_ipcc_history}
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 output_freq  ${stomate_UserChoices_output_freq_stomate_ipcc_history}
    fi

        

    # Define in orchidee.def if restart file should be used
    if ( [ ${CumulPeriod} -eq 1 ] && [ "${config_SBG_Restart}" = "n" ] ) ; then
        echo "STOMATE : without restart"
        IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_RESTART_FILEIN NONE
    else
        IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_RESTART_FILEIN stomate_rest_in.nc
    fi

    IGCM_debug_PopStack "SBG_Update"
}

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