#!/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

    typeset SECHIBA_WRITE_STEP

    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
	IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .FALSE. 
	IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 enabled .FALSE. 
	IGCM_comp_modifyDefFile nonblocker orchidee.def XIOS_ORCHIDEE_OK y
    else
	IGCM_comp_modifyDefFile nonblocker orchidee.def XIOS_ORCHIDEE_OK n
    fi    

    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}' )
	    (( SECHIBA_WRITE_STEP = PeriodLengthInDays * WriteInYears / PeriodLengthInYears * 86400 )) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		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 
	    fi
	    ;;
	1M)
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE. 
		IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq 1mo
		IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		SECHIBA_WRITE_STEP=-1.
		;;
	    *M|*m)
		SECHIBA_WRITE_STEP=-1.
		;;
	    *)
		(( SECHIBA_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) * 86400 ))
		;;
	    esac
	    ;;
	*M|*m) 
	    WriteInMonths=$( echo ${SRF_WriteFrequency} | awk -F '[mM]' '{print $1}' )
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		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 
	    fi
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * 86400 / PeriodLengthInYears / 12  ))
		;;
	    *M|*m)
		PeriodLengthInMonths=$( echo ${config_UserChoices_PeriodLength} | awk -F '[mM]' '{print $1}' )
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * WriteInMonths  * 86400 / PeriodLengthInMonths  ))
		;;
	    *)
		(( SECHIBA_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) * 86400 ))
		if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		    IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 enabled .TRUE. 
		    IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 output_freq ${SECHIBA_WRITE_STEP}s
		    IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba1 name sechiba_history
		fi
		;;
	    esac
	    ;;
	5D|5d) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		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 
	    fi
	    (( SECHIBA_WRITE_STEP = 5 * 86400 )) 
	    ;;
	1D|1d) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		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 
	    fi
	    (( SECHIBA_WRITE_STEP = 86400 )) 
	    ;;
	*s)
	    WriteInSeconds=$( echo ${SRF_WriteFrequency} | awk -F '[s]' '{print $1}' )
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		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 
	    fi
	    (( SECHIBA_WRITE_STEP = WriteInSeconds )) ;;
	*) 
	    IGCM_debug_Exit "SRF_Update " ${SRF_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D." 
	    IGCM_debug_Verif_Exit ;;
    esac

#Use of XIOS ouputs only

    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
	(( SECHIBA_WRITE_STEP = 0 ))
    fi
    IGCM_comp_modifyDefFile nonblocker orchidee.def WRITE_STEP ${SECHIBA_WRITE_STEP}
    IGCM_comp_modifyDefFile nonblocker orchidee.def SECHIBA_HISTLEVEL ${orchidee_UserChoices_sechiba_LEVEL}

    # Outputs HF in HISTFILE2 if required
    if [ X${SRF_ok_hf} = Xy ] ; then
	IGCM_comp_modifyDefFile nonblocker orchidee.def SECHIBA_HISTFILE2 y
	if [ X${orchidee_UserChoices_XIOS} = 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
    else
	IGCM_comp_modifyDefFile nonblocker orchidee.def SECHIBA_HISTFILE2 n
    fi
    IGCM_comp_modifyDefFile nonblocker orchidee.def SECHIBA_HISTLEVEL2 1
    IGCM_comp_modifyDefFile nonblocker orchidee.def WRITE_STEP2 10800.0

    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 blocker orchidee.def VEGET_UPDATE   ${orchidee_UserChoices_VEGET_UPDATE}
    else
	IGCM_comp_modifyDefFile blocker orchidee.def VEGET_UPDATE 0Y
    fi
    if [ ! X${orchidee_UserChoices_LAND_COVER_CHANGE} = X ] ; then
	IGCM_comp_modifyDefFile blocker orchidee.def LAND_COVER_CHANGE ${orchidee_UserChoices_LAND_COVER_CHANGE}
    else
	IGCM_comp_modifyDefFile blocker 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


    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
    # 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
    fi

    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"
}
