#!/bin/ksh

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

#-----------------------------------------------------------------
function OOL_Initialize
{
    IGCM_debug_PushStack "OOL_Initialize"

    NUM_PROC=1
#D- Number of processors used for lmdz and oasis coupler computed from PBS variable
    if [ X"${BATCH_NUM_PROC_TOT}" != X ] ; then
	NUM_PROC=${BATCH_NUM_PROC_TOT}
    fi
    echo BATCH_NUM_PROC_TOT=${BATCH_NUM_PROC_TOT}
    echo NUM_PROC=${NUM_PROC}

    if ( [ X${BATCH_NUM_PROC_TOT} != X ] && [ "${BATCH_NUM_PROC_TOT}" -gt 1 ] ) ; then
	MPIRUN_COMMAND=${HOST_MPIRUN_COMMAND}
    fi

    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/COMP/orchidee_ol.card UserChoices
    RESOL=${orchidee_ol_UserChoices_RESOL}

    IGCM_debug_PopStack "OOL_Initialize"
}

#-----------------------------------------------------------------
function OOL_Update
{
    IGCM_debug_PushStack "OOL_Update"

    case ${config_UserChoices_PeriodLength} in
	*Y|*y|*M|*m|*D|*d)
	    ORCHIDEE_sed TIME_LENGTH ${PeriodLengthInDays}D
	    ;;
	*s)
	    ORCHIDEE_sed TIME_LENGTH ${config_UserChoices_PeriodLength}
    esac

    if ( ${FirstInitialize} ) ; then
	if ( [ "${config_OOL_Restart}" = "y" ] ) ; then
	    ORCHIDEE_sed RESTART_FILEIN driver_rest_in.nc
	else
	    ORCHIDEE_sed TIME_SKIP $( IGCM_date_DaysBetweenGregorianDate ${DateBegin} ${year}0101 )D
	fi
    else
	if [ X${orchidee_ol_UserChoices_NORESTART} = Xn ] ; then
	    ORCHIDEE_sed RESTART_FILEIN driver_rest_in.nc
	else
	    rm driver_rest_in.nc
	    ORCHIDEE_sed RESTART_FILEIN NONE
	fi
    fi

    IGCM_sys_Mv orchidee.def run.def
#    IGCM_sys_Put_Out run.def ${R_OUT_OOL}/${PREFIX}_run.def
    FileToBeDeleted[${#FileToBeDeleted[@]}]=run.def

    IGCM_debug_PopStack "OOL_Update"
}

#-----------------------------------------------------------------
function OOL_Finalize
{
    IGCM_debug_PushStack "OOL_Finalize"

    echo FINALIZE OOL !!!

    IGCM_debug_PopStack "OOL_Finalize"
}
