# $Id$
#
#!/bin/ksh

#D- Driver du script pour ORCHIDEE

function ORCHIDEE_sed
{
    IGCM_debug_PushStack "ORCHIDEE_sed"
    
    sed -e "s/^${1}\ *=.*/${1}= ${2}/" \
	orchidee.def > orchidee.def.tmp
    RET=$?
    echo "ORCHIDEE_sed : ${1} ${2}"
    \mv orchidee.def.tmp orchidee.def
    
    IGCM_debug_PopStack "ORCHIDEE_sed"
    return $RET
}

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

    RESOL_SRF=ALL

    IGCM_debug_PopStack "SRF_Initialize"
}

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

    typeset SECHIBA_WRITE_STEP

    case ${config_SRF_WriteFrequency} in
	*Y|*y) 
	    WriteInYears=$( echo ${1} | awk -F '[yY]' "{print ${config_SRF_WriteFrequency}}" )
	    PeriodLengthInYears=$( echo ${1} | awk -F '[yY]' "{print ${config_UserChoices_PeriodLength}}" )
	    (( SECHIBA_WRITE_STEP = PeriodLengthInDays * WriteInYears / PeriodLengthInYears * 86400 )) ;;
	*M|*m) 
	    WriteInMonths=$( echo ${1} | awk -F '[mM]' "{print ${config_SRF_WriteFrequency}}" )
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		PeriodLengthInYears=$( echo ${1} | awk -F '[yY]' "{print ${config_UserChoices_PeriodLength}}" )
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * 86400 / PeriodLengthInYears / 12  ))
		;;
	    *M|*m)
		PeriodLengthInMonths=$( echo ${1} | awk -F '[mM]' "{print ${config_UserChoices_PeriodLength}}" )
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * WriteInMonths  * 86400 / PeriodLengthInMonths  ))
		;;
	    *)
		(( SECHIBA_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) * 86400 ))
		;;
	    esac
	    ;;
	5D|5d) 
	    (( SECHIBA_WRITE_STEP = 5 * 86400 )) ;;
	1D|1d) 
	    (( SECHIBA_WRITE_STEP = 86400 )) ;;
	*s)
	    WriteInSeconds=$( echo ${1} | awk -F '[s]' "{print ${config_SRF_WriteFrequency}}" )
	    (( SECHIBA_WRITE_STEP = WriteInSeconds )) ;;
	*) 
	    IGCM_debug_Exit "SRF_Update " ${config_SRF_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D." 
	    IGCM_debug_Verif_Exit ;;
    esac

    ORCHIDEE_sed HYDROL_CWRR ${orchidee_UserChoices_NEWHYDROL}

    ORCHIDEE_sed WRITE_STEP ${SECHIBA_WRITE_STEP}
    ORCHIDEE_sed SECHIBA_HISTLEVEL ${orchidee_UserChoices_sechiba_LEVEL}

##    if [ ${year} -eq 1950 ] ; then
#	ORCHIDEE_sed SECHIBA_HISTFILE2 y
# 	ORCHIDEE_sed SECHIBA_HISTLEVEL2 10
# 	ORCHIDEE_sed WRITE_STEP2 86400.0
##    fi

    if ( [ ${CumulPeriod} -ne 1 ] || [ "${config_SRF_Restart}" != "n" ] ) ; then
	ORCHIDEE_sed SECHIBA_restart_in sechiba_rest_in.nc
    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

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