#!/bin/ksh
#D- Driver du script pour ORCHIDEE
#-----------------------------------------------------------------
function SRF_Initialize
{
    IGCM_debug_PushStack "SRF_Initialize"

    RESOL_SRF=ALL

    for frequency in ${config_SRF_WriteFrequency} ; do
        case ${frequency} in
            HF|hf) SRF_ok_hf=y ;;
        esac
    done

    ##- Define variable DefSuffix set in orchidee.card
    ##  This variable is used in orchidee.card to choose
    ##  parameter file(orchidee.def_DefSuffix).
    if [ ! X${orchidee_UserChoices_DefSuffix} = X ] ; then
        DefSuffix=${orchidee_UserChoices_DefSuffix}
    else
        DefSuffix=Choi
    fi

    IGCM_debug_PopStack "SRF_Initialize"
}

#-----------------------------------------------------------------
function SRF_Update
{
    IGCM_debug_PushStack "SRF_Update"

    # Activate STOMATE if the compontent SBG=stomate is set in config.card
    if [ X${config_ListOfComponents_SBG} = Xstomate ] ; then
        IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_OK_STOMATE y
    else
        IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_OK_STOMATE n
    fi

    # Default init : files are desactivated
    IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .FALSE.
    IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 enabled .FALSE.

    SRF_WriteFrequency=$( echo ${config_SRF_WriteFrequency} | sed -e 's/\([0-9]*[yYmMdDs]\).*/\1/' )
    case ${SRF_WriteFrequency} in
        *Y|*y)
            WriteInYears=$( echo ${SRF_WriteFrequency} | awk -F '[yY]' '{print $1}' )
            PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE.
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq ${WriteInYears}y
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history
            ;;
        *M|*m)
            WriteInMonths=$( echo ${SRF_WriteFrequency} | awk -F '[mM]' '{print $1}' )
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE.
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq ${WriteInMonths}mo
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history
            ;;
        5D|5d)
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE.
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq 5d
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history
            ;;
        1D|1d)
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE.
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq 1d
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history
            ;;
        *s)
            WriteInSeconds=$( echo ${SRF_WriteFrequency} | awk -F '[s]' '{print $1}' )
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE.
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq ${WriteInSeconds}s
            IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history
            ;;
        *)
            IGCM_debug_Exit "SRF_Update " ${SRF_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D."
            IGCM_debug_Verif_Exit ;;
    esac

    # Outputs HF in HISTFILE2 if required. Output frequency is 3 hours by default.
    if [ X${SRF_ok_hf} = Xy ] ; then
        IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 enabled .TRUE.
        IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 output_freq 10800s
        IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 name sechiba_out_2
    fi

    if ( [ ${CumulPeriod} -ne 1 ] || [ "${config_SRF_Restart}" != "n" ] ) ; then
        IGCM_comp_modifyDefFile blocker orchidee.def SECHIBA_restart_in sechiba_rest_in.nc
    else
        IGCM_comp_modifyDefFile blocker orchidee.def SECHIBA_restart_in NONE
    fi

    # Modify in orchidee.def VEGET_UPDATE and LAND_COVER_CHANGE if they are set in orchidee.card section UserChoices
    if [ ! X${orchidee_UserChoices_VEGET_UPDATE} = X ] ; then
        IGCM_comp_modifyDefFile nonblocker orchidee.def VEGET_UPDATE   ${orchidee_UserChoices_VEGET_UPDATE}
    else
        IGCM_comp_modifyDefFile nonblocker orchidee.def VEGET_UPDATE 0Y
    fi
    if [ ! X${orchidee_UserChoices_LAND_COVER_CHANGE} = X ] ; then
        IGCM_comp_modifyDefFile nonblocker orchidee.def LAND_COVER_CHANGE ${orchidee_UserChoices_LAND_COVER_CHANGE}
    else
        IGCM_comp_modifyDefFile nonblocker orchidee.def LAND_COVER_CHANGE n
    fi

    #IGCM_sys_Cp ${RUN_DIR}/orchidee.def ${RUN_DIR}/run.def
    #IGCM_sys_Put_Out ${RUN_DIR}/run.def ${R_SAVE}/${PREFIX}_run.def

    #Temporary way to fix bug on cimean variable
    IGCM_comp_modifyXmlFile force field_def_orchidee.xml cimean enabled .FALSE.

    # Add include of orchidee context in iodef.xml
    # In iodef.xml add on next line after "COMPONENT CONTEXT"
    #  <context id="orchidee" src="./context_orchidee.xml"/>
    echo '<context id="orchidee" src="./context_orchidee.xml"/>' > add.tmp
    cp iodef.xml iodef.xml.tmp
    sed -e "/COMPONENT CONTEXT/r add.tmp" iodef.xml.tmp > iodef.xml
    rm iodef.xml.tmp add.tmp

    IGCM_debug_PopStack "SRF_Update"
}

#-----------------------------------------------------------------
function SRF_Finalize
{
    IGCM_debug_PushStack "SRF_Finalize"

    #IGCM_sys_Put_Out ${RUN_DIR}/used_run.def ${R_SAVE}/${PREFIX}_used_run.def

    echo FINALIZE SRF !!!

    IGCM_debug_PopStack "SRF_Finalize"
}
