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

    sed -e "s/^${2}\ *=.*/${2}= ${3}/" ${1} > ${1}.tmp
    RET=$?
    echo "LMDZ_sed : ${1} ${2} ${3}"
    \mv ${1}.tmp ${1}

    IGCM_debug_PopStack "LMDZ_sed"
    return $RET
}

function ATM_Initialize
{
    IGCM_debug_PushStack "ATM_Initialize"

    [ -f ${SUBMIT_DIR}/../.resol ] && eval $(grep RESOL_ATM_3D ${SUBMIT_DIR}/../.resol) || RESOL_ATM_3D=96x95x19

    RESOL_ATM_Z=$( echo ${RESOL_ATM_3D} | awk "-Fx" '{print $3}' | awk "-F-" '{print $1}' )

    ##-- Calendar type for LMDZ
    case ${config_UserChoices_CalendarType} in
        leap|gregorian)
            CalendarTypeForLmdz=gregorian;;
        noleap)
            CalendarTypeForLmdz=earth_365d;;
        360d)
            CalendarTypeForLmdz=earth_360d;;
        *)
            CalendarTypeForLmdz=earth_360d
    esac

    ##- PARAM directory
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/lmdz.card UserChoices PARAM_DIR
    PARAM_DIR=${lmdz_UserChoices_PARAM_DIR}
    echo parameter file directory : ${PARAM_DIR}


    IGCM_debug_PopStack "ATM_Initialize"
}

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

    ##-- Activate creation of file grilles_gcm.nc only at first period
    if [ ${CumulPeriod} -eq 1 ] ; then
	LMDZ_sed run.def grilles_gcm_netcdf y
    else
	LMDZ_sed run.def grilles_gcm_netcdf n
    fi

    ## run.def parameters
    LMDZ_sed run.def dayref   ${InitDay}
    LMDZ_sed run.def anneeref ${year}
    LMDZ_sed run.def calend   ${CalendarTypeForLmdz}
    LMDZ_sed run.def use_filtre_fft n

    ## Parameters not used for create_etat0_limit
    LMDZ_sed run.def raz_date 9
    LMDZ_sed run.def nday     9
    LMDZ_sed run.def periodav 9
    LMDZ_sed run.def adjust   n
    LMDZ_sed physiq.def ecrit_mth 9.
    LMDZ_sed physiq.def ecrit_ISCCP 9.

    IGCM_debug_PopStack "ATM_Update"
}

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

    echo FINALIZE ATM !

    IGCM_debug_PopStack "ATM_Finalize"
}
