#!/bin/ksh
#-----------------------------------
CPL_Initialize ()
{
    IGCM_debug_PushStack "CPL_Initialize"
 
    echo INITIALIZE CPL !!!

# Local function to find parameters in LMDZ .def files
    lmdzgrep () {
        grep $1 $2 |grep -v "#" | awk "-F=" '{print $2}' | awk "-F " '{print $1}'
    }

    #RESOL_CPL=$( echo $RESOL | awk "-F-" '{print $1}' )
    RESOL_CPL=${RESOL_OCE}x${RESOL_ATM}
    IGCM_debug_Print 1 "RESOL_CPL      : ${RESOL_CPL}"

    CPL_OkDay="n"
    CPL_OkMonth="n"

    for CPL_Frequency in ${config_CPL_WriteFrequency} ; do
	case ${CPL_Frequency} in
	    *D|*d) CPL_OkDay="y"   ;;
	    *M)    CPL_OkMonth="y" ;;
	esac
    done

    IGCM_debug_PopStack "CPL_Initialize"

}

#-----------------------------------
CPL_Update ()
{
    IGCM_debug_PushStack "CPL_Update"

    ############ Update Time and Counters ##############
    ##-- Calcul de la duree du job (en secondes)
    (( D_JOB_SEC = 3600 * 24 * PeriodLengthInDays ))

    ############ Update Other Parameters  ##############
    ## Read Output_mode option in oasis.card --> Put "y" to have coupling variables in ouptut files
    CPL_OutputMode="n"
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/oasis.card UserChoices OutputMode 
    eval CPL_OutputMode=\${oasis_UserChoices_OutputMode} > /dev/null 2>&1
    echo CPL_OutputMode = ${CPL_OutputMode}
    CPL_OutputVar='EXPORTED'
    if [ ${CPL_OutputMode} = y ] ; then
	CPL_OutputVar='EXPOUT'
    fi

    FreqCoupling=${oasis_UserChoices_FreqCoupling:-86400}

    ## Calculate lag for transfer of fields from atmosphere -> ocean.
    ## LagAtm="1day in sec"/day_step * iphysiq/nsplit_phys
    ## First get day_step, iphyisq and nsplit_phys from LMDZ parameter files
    day_step=$(    lmdzgrep day_step gcm.def )
    iphysiq=$(     lmdzgrep iphysiq gcm.def )
    nsplit_phys=$( lmdzgrep nsplit_phys physiq.def )

    IGCM_debug_Print 3 "Parametre de calcul Lag LMDZ recuperes " ${day_step} ${iphysiq} ${nsplit_phys}

    [[ ${nsplit_phys} -eq 0 || "X${nsplit_phys}" = "X" ]] && nsplit_phys=1

    IGCM_debug_Print 3 "Parametre de calcul Lag LMDZ mis a jour " ${day_step} ${iphysiq} ${nsplit_phys}
                                                              
    (( LagAtm = 86400 / day_step * iphysiq / nsplit_phys ))

    (( LagOce = 86400 / ORCA_NPDT_JOUR ))

    ## Use of Lucia ##
    lucia_ok=""
    if [ ${oasis_UserChoices_Lucia} = y ] ; then
	lucia_ok="-1"
    fi
    
    ############ Update Parameter Files   ##############
    sed -e "s/<total_time>/${D_JOB_SEC}/" \
	-e "s/<lucia_ok>/${lucia_ok}/" \
	-e "s/<output_mode>/${CPL_OutputVar}/g" \
	-e "s/<freq_coupling>/${FreqCoupling}/g" \
	-e "s/<lag_atm>/${LagAtm}/g" \
	-e "s/<lag_oce>/${LagOce}/g" \
	namcouple > namcouple.tmp
    IGCM_sys_Mv namcouple.tmp namcouple
    # For IPSLCM6 and use of OASIS-MCT
    if [ ${CumulPeriod} -eq 1 ]; then
	ncrename -v OIceFrac,OIceFrc sstoc.nc > /dev/null 2>&1
    fi
    # For use of XIOS
    IGCM_comp_modifyXmlFile force iodef.xml using_oasis NONE true

    IGCM_debug_PopStack "CPL_Update"
}

#-----------------------------------
CPL_Finalize ()
{
    IGCM_debug_PushStack "CPL_Finalize"

    if [ "${CPL_OutputMode}" = "y" ] ; then

        [[ ${CPL_OkDay}   = "y" ]] && date >TEMPO_1D_cpl_atm.nc
        [[ ${CPL_OkMonth} = "y" ]] && date >TEMPO_1M_cpl_atm.nc
        [[ ${CPL_OkDay}   = "y" ]] && date >TEMPO_1D_cpl_oce.nc
        [[ ${CPL_OkMonth} = "y" ]] && date >TEMPO_1M_cpl_oce.nc

    fi

    ## Use of Lucia ##
    if [ ${oasis_UserChoices_Lucia} = y ] ; then
	echo "Use of LUCIA"
    #   To be changed 
    #   On Curie
    #   /ccc/scratch/cont003/dsm/p86caub/LUCIA/lucia
    #   On Ada
    #   /linkhome/rech/psl/rpsl035/LUCIA/lucia
    fi

    echo FINALIZE CPL !!!

    IGCM_debug_PopStack "CPL_Finalize"
}
