#!/bin/ksh
#-----------------------------------------------------------------
function OCE_Initialize
{
    IGCM_debug_PushStack "OCE_Initialize"

    RESOL_OCE=$( echo ${config_UserChoices_ExperimentName} | cut -c1-5 )
    EXPTYPE=$( echo ${config_UserChoices_ExperimentName} | cut -c6- )

# Local function to find namelists parameters
supergrep () {
    grep "^ *$1 *=" $2 | sed -e "s% *\!.*%%"
}

    OPA_RDT=$( supergrep rn_rdt  ${SUBMIT_DIR}/PARAM/namelist_${RESOL_OCE}${EXPTYPE}_cfg | sed 's/ *rn_rdt *=//' | sed 's/\. *,//' )

    # Period Length In Days between DateBegin and first day of calendar 0001 01 01
    # /!\ Needed by OPA namelist to compute file names /!\
    (( DaysSinceJC = $( IGCM_date_DaysSinceJC ${DateBegin} ) + 1 ))

    (( OPA_NDT_DAY  =  86400 / OPA_RDT ))
    (( OPA_NPDT_YEAR  =  OPA_NPDT_DAY * 365     ))

    # Definition from opa9.card of List_jobsbc and List_runsbc used to do to interannual and climatological runs 
	IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/COMP/opa9.card Interannual List_jobsbc
        set -A ListFormulationJobsbc -- \${opa9_Interannual_Listjobsc${opa9_Interannual_List_jobsbc}[*]}

    IGCM_debug_PopStack "OCE_Initialize"
}

#-----------------------------------------------------------------
function OCE_Update
{
    IGCM_debug_PushStack "OCE_Update"

    NbFreq=$( echo ${config_OCE_WriteFrequency} | wc -w )
    ##--Write Frequency Purpose ....
    typeset -Z1 i=1
    while [ $i -le ${NbFreq} ]; do
        frequency=$( echo ${config_OCE_WriteFrequency} | awk -v var="$i" "-F " '{print $var}' )
    	factor=$( echo ${frequency} | sed -e "s/[yYmMdDhH]//" )

    	case ${frequency} in
    	    *Y|*y)
    	        eval PREFIX_WF${i}=${config_UserChoices_JobName}_${factor}y_${DatesPeriod} ;
    	        eval R_OUT_OCE_WF${i}=${R_OUT_OCE_O_Y} ;
    	        eval WF${i}=${factor}Y ;;
    	    *M|*m)
    	        eval PREFIX_WF${i}=${config_UserChoices_JobName}_${factor}m_${DatesPeriod} ;
    	        eval R_OUT_OCE_WF${i}=${R_OUT_OCE_O_M} ;
    	        eval WF${i}=${factor}M ;;
    	    *D|*d)
    	        eval PREFIX_WF${i}=${config_UserChoices_JobName}_${factor}d_${DatesPeriod} ;
    	        eval R_OUT_OCE_WF${i}=${R_OUT_OCE_O_D} ;
    	        eval WF${i}=${factor}D ;;
    	    *H|*h)
    	        eval PREFIX_WF${i}=${config_UserChoices_JobName}_${factor}h_${DatesPeriod} ;
    	        eval R_OUT_OCE_WF${i}=${R_OUT_OCE_O_H} ;
    	        eval WF${i}=${factor}H ;;
    	    *)
    	esac
        let i=$i+1

    done
    # Period Length In Days between DateBegin and first day of calendar 0001 01 01
    # Needed by OPA namelist to compute correct file names 
    (( PeriodDaysSinceJC =  $( IGCM_date_DaysSinceJC ${PeriodDateBegin} ) + 1  ))

    ## Count of number of time steps from begin of OPA job
    (( OPA_NIT000 = ( PeriodDaysSinceJC - DaysSinceJC ) * OPA_NDT_DAY + 1 ))
    (( OPA_NDT   = PeriodLengthInDays * OPA_NDT_DAY ))
    (( OPA_NITEND = OPA_NIT000 + OPA_NDT - 1))

    ## Forcing to write one restart file at the end of period
    OPA_NSTOCK="${OPA_NITEND}"

    ## Verification of number of time steps per day
    (( NB_SEC_DAY_MODEL = OPA_NDT_DAY * OPA_RDT ))
    (( NB_SEC_DAY = 60 * 60 * 24 ))

    if [ ${NB_SEC_DAY_MODEL} -ne ${NB_SEC_DAY} ]
	then
        echo " NB_SEC_DAY_MODEL "${NB_SEC_DAY_MODEL} "NB_SEC_DAY" ${NB_SEC_DAY} "OPA_NDT_DAY" ${OPA_NDT_DAY} "OPA_RDT" ${OPA_RDT}
        echo " VERIFY OPA_NDT_DAY in opa9.card "
        exit
    fi

    ##-- Restart configuration
    if ( [ "${CumulPeriod}" -eq 1 ] && [ "${config_OCE_Restart}" = "n" ] ) ; then

        #echo "NO OPA RESTART"
	OPA_LRSTAR=.FALSE.
	OPA_NRSTDT=0
        #Put OPA_NMSH=0 when OPA runnig in parallel mode 
	OPA_NMSH=1
	( [ X${mpiTasks} != X ] && [ "${mpiTasks}" -gt 1 ] ) && OPA_NMSH=0

    elif ( [ "${CumulPeriod}" -eq 1 ] && [ "${config_OCE_Restart}" = "y" ] ) ; then

        #echo "OPA RESTART"
	OPA_LRSTAR=.TRUE.
	OPA_NRSTDT=0
	OPA_NMSH=0

    else

        #echo "OPA RESTART"
	OPA_LRSTAR=.TRUE.
	OPA_NRSTDT=2
	OPA_NMSH=0

    fi
   
     ## nleapy configuration
	case ${config_UserChoices_CalendarType} in
        	leap)
            	OPA_NLEAPY=1;;
        	noleap)
            	OPA_NLEAPY=0;;
        	360d)
            	OPA_NLEAPY=30;;
    	esac


        typeset -r PRECIS=8
	NEMO_END=$( echo $( awk "BEGIN { printf \"%0${PRECIS}d\",${OPA_NITEND} }" ) ) 

   IGCM_comp_modifyNamelist blocker    namelist_cfg cn_exp    ${config_UserChoices_JobName}
   IGCM_comp_modifyNamelist blocker    namelist_cfg nn_it000  ${OPA_NIT000}
   IGCM_comp_modifyNamelist blocker    namelist_cfg nn_itend  ${OPA_NITEND}
   IGCM_comp_modifyNamelist blocker    namelist_cfg ln_rstart ${OPA_LRSTAR}
   IGCM_comp_modifyNamelist blocker    namelist_cfg nn_stock ${OPA_NSTOCK}
   IGCM_comp_modifyNamelist blocker    namelist_cfg nn_rstctl ${OPA_NRSTDT}
   IGCM_comp_modifyNamelist blocker    namelist_cfg nn_date0 ${PeriodDateBegin}
   IGCM_comp_modifyNamelist nonblocker namelist_cfg nn_msh    ${OPA_NMSH}
   IGCM_comp_modifyNamelist blocker    namelist_cfg nn_leapy  ${OPA_NLEAPY}  

   IGCM_debug_PopStack "OCE_Update"
}

#-----------------------------------
function OCE_Finalize
{
    IGCM_debug_PushStack "OCE_Finalize"

    if [ -f date.file ] ; then
        # Prefix use in opa.card AND in lim2.card :
	DATE_OPA=$( cat date.file | \
	    sed "s/\ ${config_UserChoices_JobName}_[0-9]*[a-z]_\([0-9]*_[0-9]*\)_\ */\1/g" )
        ###DATE_OPA=$( $DATE_OPABF | cut -c1-30 )
	MainPrefix=${config_UserChoices_JobName}_1d_${DATE_OPA}
	SecondPrefix=${config_UserChoices_JobName}_5d_${DATE_OPA}
    fi

    echo FINALIZE OCE !!!

    IGCM_debug_PopStack "OCE_Finalize"
}

