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

    ##- Horizontal resolution read from dynamicolmdz.card
    if [ ! X${dynamicolmdz_UserChoices_RESOL_NBP} = X ] ; then
	RESOL_NBP=${dynamicolmdz_UserChoices_RESOL_NBP}
    else
	# Set default resolution
	RESOL_NBP=20
    fi

    ##- Horizontal resolution read from dynamicolmdz.card
    if [ ! X${dynamicolmdz_UserChoices_RESOL_LLM} = X ] ; then
	RESOL_LLM=${dynamicolmdz_UserChoices_RESOL_LLM}
    else
	# Set default resolution
	RESOL_LLM=79
    fi

    ##- LMDZ physics version
    ##  Read LMDZ_Physics option in lmdz.card, if not present take default value NPv6.1.3 
    if [ ! X${dynamicolmdz_UserChoices_LMDZ_Physics} = X ] ; then
        LMDZ_Physics=${dynamicolmdz_UserChoices_LMDZ_Physics}
    else
        LMDZ_Physics=NPv6.0.11
    fi
    echo LMDZ physics version : ${LMDZ_Physics}


    IGCM_debug_PopStack "ATM_Initialize"
}

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

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

    ##-- Calendar type for LMDZ and create_etat0_limit
    case ${config_UserChoices_CalendarType} in
        leap|gregorian)
        IGCM_comp_modifyDefFile blocker run.def calend  gregorian ;;
        noleap)
        IGCM_comp_modifyDefFile blocker run.def calend  earth_365d ;;
        360d)
        IGCM_comp_modifyDefFile blocker run.def calend  earth_360d ;;
        *)
        IGCM_comp_modifyDefFile blocker run.def calend  earth_360d ;;
    esac
    
    IGCM_comp_modifyDefFile blocker run.def dayref    ${InitDay}
    IGCM_comp_modifyDefFile blocker run.def anneeref ${InitYear}

    IGCM_comp_modifyDefFile blocker run_icosa.def run_length $(( ${PeriodLengthInDays} * 86400 ))

    if [ X${RESOL_NBP} == X40 ] || [ X${RESOL_NBP} == X20 ]; then
      IGCM_comp_modifyDefFile nonblocker run_icosa.def   nbp       ${RESOL_NBP}
      IGCM_comp_modifyDefFile nonblocker run_icosa.def   nsplit_i  $(( ${RESOL_NBP} / 10 ))
      IGCM_comp_modifyDefFile nonblocker run_icosa.def   nsplit_j  $(( ${RESOL_NBP} / 10 ))
      IGCM_comp_modifyDefFile nonblocker run_icosa.def   llm       79
      IGCM_debug_Print 1 "Maximum nubmer of MPI for resolution nbp=${RESOL_NBP} is $(( ${RESOL_NBP} * ${RESOL_NBP} / 10 ))"
    else
      IGCM_debug_Print 1 "nbp=${RESOL_NBP} is set in dynamicolmdz.card"
      IGCM_debug_Exit "This resolution has not been prepared in dynamicolmdz.driver" 
      IGCM_debug_Verif_Exit
    fi

    if [ X${dynamicolmdz_UserChoices_Aquaplanet} == Xy ] ; then
      IGCM_comp_modifyDefFile nonblocker earth_const.def solarlong0   1000.0
      IGCM_comp_modifyDefFile nonblocker config.def      R_ecc        0
      IGCM_comp_modifyDefFile nonblocker config.def      read_climoz  -1
      IGCM_comp_modifyDefFile nonblocker run_icosa.def   etat0        held_suarez
    else
      IGCM_comp_modifyDefFile nonblocker earth_const.def solarlong0   -9999.999
      IGCM_comp_modifyDefFile nonblocker config.def      R_ecc        0.016715
      IGCM_comp_modifyDefFile nonblocker config.def      read_climoz  1
#      IGCM_comp_modifyDefFile nonblocker run_icosa.def   etat0        database
      IGCM_comp_modifyDefFile nonblocker run_icosa.def   etat0        start_file 
    fi

    IGCM_debug_PopStack "ATM_Update"
}

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

    IGCM_debug_PopStack "ATM_Finalize"
}

