#!/bin/ksh

# MPI Variables
ATM_PARA=0

#-----------------------------------------------------------------
function ATM_Initialize
{
    IGCM_debug_PushStack "ATM_Initialize"

    RESOL_ATM=${RESOL}

    ##--Frequency purpose ....
    ##--  Initialisation  ....
    OK_instan=n
    ##-- 
    OK_journe=n
    OK_mensuel=n

    case ${config_UserChoices_PeriodLength} in
	1Y|1y|1M|1m) OK_mensuel=y ;;
	5D|5d|1D|1d) OK_journe=n ;;
    esac

    for frequency in ${config_ATM_WriteFrequency} ; do
	case ${frequency} in
	    5D|5d|1D|1d) OK_journe=n ;;
	esac
    done

    ##--Variables used by LMDZ --
    PAT_INST=$(     grep 'OK_instan'   ${SUBMIT_DIR}/PARAM/physiq.def )
    PAT_JOUR=$(     grep 'OK_journe'   ${SUBMIT_DIR}/PARAM/physiq.def )
    PAT_MOIS=$(     grep 'OK_mensuel'  ${SUBMIT_DIR}/PARAM/physiq.def )

    PAT_iphysiq=$(  grep 'iphysiq'     ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL} )
    PAT_iperiod=$(  grep 'iperiod'     ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL} | tail -1)
    PAT_day_step=$( grep 'day_step'    ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL} )
    PAT_ecritphy=$( grep 'ecritphy'    ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL} )

    ##-- This could be define in lmdz.card, inside section [UserChoices]
    ##-- Otherwise we get the value in *.def
    iperiod=$(grep iperiod ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL} | awk -F= '{print $2}' | tail -1)
    iphysiq=$(grep iphysiq ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL} | awk -F= '{print $2}')
    # day_step : number of steps per day (multiple of iperiod)
    (( day_step = 48 * iphysiq ))

    IGCM_debug_PopStack "ATM_Initialize"
}

#-----------------------------------------------------------------
function ATM_Update
{
    IGCM_debug_PushStack "ATM_Update"

    ecritphy=${PeriodLengthInDays}

    ##-- Remise ou non a zero de la date initiale de LMDZ pour le fichier run.def
    if [ ${CumulPeriod} -eq 1 ] ; then
	RAZ_DATE=1
    else
	RAZ_DATE=0
    fi

    ## Mise en forme du fichier physiq.def
    sed -e "s/OK_instan=.*/OK_instan=${OK_instan}/"   \
	-e "s/OK_journe=.*/OK_journe=${OK_journe}/"   \
	-e "s/OK_mensuel=.*/OK_mensuel=${OK_mensuel}/" \
	physiq.def > physiq.def.tmp
    IGCM_sys_Mv physiq.def.tmp physiq.def

    ## Mise en forme du fichier gcm.def
    sed  -e "s/${PAT_ecritphy}/ecritphy=${ecritphy}/" \
	-e "s/${PAT_day_step}/day_step=${day_step}/" \
	-e "s/${PAT_iperiod}/iperiod=${iperiod}/" \
	-e "s/${PAT_iphysiq}/iphysiq=${iphysiq}/" \
	gcm.def > gcm.def.tmp
    IGCM_sys_Mv gcm.def.tmp gcm.def

    ## Mise en forme du fichier run.def
    sed -e "s/_dayref_/${InitDay}/" \
	-e "s/_anneeref_/${InitYear}/" \
	-e "s/_nday_/${PeriodLengthInDays}/" \
	-e "s/_raz_date_/${RAZ_DATE}/" \
	run.def > run.def.tmp
    IGCM_sys_Mv run.def.tmp run.def

if ( ${FirstInitialize} ) ; then

   if ( [ "${config_Restarts_OverRule}" = "n" ] && [ "${config_ATM_Restart}" = "n" ] ); then
	   IGCM_sys_Cp ${R_EXE}/create_etat0_limit.e ${RUN_DIR}/.
	   ./create_etat0_limit.e
   fi

fi 

    IGCM_debug_PopStack "ATM_Update"
}

#-----------------------------------
function ATM_Finalize
{
    IGCM_debug_PushStack "ATM_Finalize"

    echo FINALIZE ATM !

    IGCM_debug_PopStack "ATM_Finalize"
}
