#!/bin/ksh

#D- Driver du script pour ORCHIDEE_OL (off-line)

#-----------------------------------------------------------------
function SRF_Initialize
{
    IGCM_debug_PushStack "SRF_Initialize"

    IGCM_debug_PopStack "SRF_Initialize"
}

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

    case ${config_SRF_WriteFrequency} in
	1Y|1y) 
	    (( STOMATE_WRITE_STEP = PeriodLengthInDays ))
	    (( SECHIBA_WRITE_STEP = PeriodLengthInDays * 86400 )) ;;
	1M|1m) 
	    if [ ${config_UserChoices_PeriodLength} = 1Y ] ; then
		(( STOMATE_WRITE_STEP = PeriodLengthInDays / 12 ))
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * 86400 / 12  ))
	    else
		(( STOMATE_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) ))
		(( SECHIBA_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) * 86400 ))
	    fi ;;
	5D|5d) 
	    (( STOMATE_WRITE_STEP = 5 ))
	    (( SECHIBA_WRITE_STEP = 5 * 86400 )) ;;
	1D|1d) 
	    (( STOMATE_WRITE_STEP = 1 ))
	    (( SECHIBA_WRITE_STEP = 86400 )) ;;
	*) 
	    IGCM_debug_Exit "SRF_Update " ${config_SRF_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D." 
	    IGCM_debug_Verif_Exit ;;
    esac


    if ( [ ${CumulPeriod} -eq 1 ] && [ "${config_SRF_Restart}" = "n" ] ) ; then
	sed -e "s/_start_sech_/default/" \
	    -e "s/_write_step_/${SECHIBA_WRITE_STEP}/" \
	    orchidee.def > orchidee.def.tmp
## For STOMATE
#	    -e "s/STOMATE_HIST_DT= 10./STOMATE_HIST_DT = ${STOMATE_WRITE_STEP}/" \

## For ORCHIDEE_WATCHOUT_FILE : 
#            -e "s/ORCHIDEE_WATCHOUT= n/ORCHIDEE_WATCHOUT= y/" \
#            -e "s/DT_WATCHOUT= 1800/DT_WATCHOUT= 3600/" \
    else
	sed -e "s/_start_sech_/start_sech.nc/" \
	    -e "s/_write_step_/${SECHIBA_WRITE_STEP}/" \
	    orchidee.def > orchidee.def.tmp
## For STOMATE
#            -e "s/STOMATE_RESTART_FILEIN= NONE/STOMATE_RESTART_FILEIN = stomate_rest_in.nc/" \
#            -e "s/STOMATE_HIST_DT= 10./STOMATE_HIST_DT = ${STOMATE_WRITE_STEP}/" \

## For ORCHIDEE_WATCHOUT_FILE : 
#            -e "s/ORCHIDEE_WATCHOUT= n/ORCHIDEE_WATCHOUT= y/" \
#            -e "s/DT_WATCHOUT= 1800/DT_WATCHOUT= 3600/" \
    fi
    IGCM_sys_Mv orchidee.def.tmp orchidee.def

    IGCM_debug_PopStack "SRF_Update"
}

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

# For STOMATE :
#     if [ $( IGCM_date_DaysBetweenGregorianDate ${PeriodDateEnd} ${year}1230 ) -ge 0 ] ; then
# 	IGCM_sys_Put_Out stomate_Cforcing.nc ${R_OUT_SRF_O_M}/${PREFIX}_stomate_Cforcing.nc
# 	IGCM_sys_Put_Out stomate_forcing.nc  ${R_OUT_SRF_O_M}/${PREFIX}_stomate_forcing.nc
#     fi

    echo FINALIZE SRF !!!

    IGCM_debug_PopStack "SRF_Finalize"
}
