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

    RESOL_ATM=$( echo $RESOL | awk "-Fx" '{print $2}' )

    case ${RESOL_ATM} in
	LMD4443)   BandsResol=44x43x19   ;;
	LMD7245)   BandsResol=72x45x19   ;;
	LMD9671)   BandsResol=96x71x19   ;;
	LMD9695)   BandsResol=96x95x19   ;;
	LMD14496)  BandsResol=144x96x19  ;;
	LMD144142) BandsResol=144x142x19 ;;
    esac

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

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

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

    ##--Variables used by LMDZ in physiq.def --
    PAT_INST=$(     grep -v ^# ${SUBMIT_DIR}/PARAM/physiq.def  | grep 'OK_instan'  )
    PAT_JOUR=$(     grep -v ^# ${SUBMIT_DIR}/PARAM/physiq.def  | grep 'OK_journe'  )
    PAT_MOIS=$(     grep -v ^# ${SUBMIT_DIR}/PARAM/physiq.def  | grep 'OK_mensuel' )
    PAT_HFRE=$(     grep -v ^# ${SUBMIT_DIR}/PARAM/physiq.def  | grep 'ok_hf'      )
    ##--Variables used by LMDZ in gcm.def --
    PAT_iphysiq=$(  grep -v ^# ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL_ATM} | grep iphysiq  )
    PAT_iperiod=$(  grep -v ^# ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL_ATM} | grep iperiod  )
    PAT_day_step=$( grep -v ^# ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL_ATM} | grep day_step )
    PAT_ecritphy=$( grep -v ^# ${SUBMIT_DIR}/PARAM/gcm.def_${RESOL_ATM} | grep ecritphy )

    ## Read LMDZ_adjust option in ${compname}.card --> put adjust=y and save Bands_*.dat in ${SUBMIT_DIR}/PARAM
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices LMDZ_adjust
    eval LMDZ_adjust=\${${compname}_UserChoices_LMDZ_adjust} > /dev/null 2>&1

    ##-- This could be define in lmdz.card, inside section [UserChoices]
    ##-- Otherwise we get the value in *.def
    iperiod=$( echo ${PAT_iperiod} | awk -F= '{print $2}' )
    iphysiq=$( echo ${PAT_iphysiq} | awk -F= '{print $2}' )
    # day_step : number of steps per day (multiple of iperiod)
    (( day_step = 48 * iphysiq ))

    #[ -f ${SUBMIT_DIR}/PARAM/Bands_${BandsResol}_7prc.dat ] && IGCM_sys_Cp ${SUBMIT_DIR}/PARAM/Bands_${BandsResol}_7prc.dat .

    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}/" \
	-e "s/ok_hf=.*/ok_hf=${ok_hf}/"                \
	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}/"                 \
	-e "s/_adjust_/${LMDZ_adjust}/"                \
	run.def > run.def.tmp
    IGCM_sys_Mv run.def.tmp run.def

    ## Read ByPass_hgardfou_teta option in ${compname}.card --> divide teta* by 2 if [ $ByPass_hgardfou_teta = 1 ]
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices ByPass_hgardfou_teta
    eval ByPass_hgardfou_teta=\${${compname}_UserChoices_ByPass_hgardfou_teta} > /dev/null 2>&1
    if [ ${ByPass_hgardfou_teta} = y ] ; then
	awk '{ if ($0 ~ /^teta.*=/) {split($0,a,"=") ; print a[1]"="a[2]/2"."} else print $0}' gcm.def > gcm.def.tmp
	IGCM_sys_Mv gcm.def.tmp gcm.def
	echo
	IGCM_debug_Print 1 "ByPass_hgardfou_teta : ^teta*/2 in gcm.def"
	echo
	cat gcm.def
	ByPass_hgardfou_teta=n
	IGCM_card_WriteOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices ByPass_hgardfou_teta "${ByPass_hgardfou_teta}"
    fi

    ## Read ByPass_hgardfou_mats option in ${compname}.card --> purmats=y 2 if [ $ByPass_hgardfou_mats = 1 ]
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices ByPass_hgardfou_mats
    eval ByPass_hgardfou_mats=\${${compname}_UserChoices_ByPass_hgardfou_mats} > /dev/null 2>&1
    if [ ${ByPass_hgardfou_mats} = y ] ; then
	sed -e "s/^purmats=.*/purmats=y/" gcm.def > gcm.def.tmp
        IGCM_sys_Mv gcm.def.tmp gcm.def
        echo
        IGCM_debug_Print 1 "ByPass_hgardfou_mats : purmats=y in gcm.def"
        echo
        cat gcm.def
        ByPass_hgardfou_mats=n
        IGCM_card_WriteOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices ByPass_hgardfou_mats "${ByPass_hgardfou_mats}"
    fi

    if ( ${FirstInitialize} ) ; then

	if ( [ "${config_Restarts_OverRule}" = "n" ] && [ "${config_ATM_Restart}" = "n" ] ); then
	    if [ ${DRYRUN} -le 2 ] ; then
		IGCM_sys_Cp ${R_EXE}/create_etat0_limit.e ${RUN_DIR}/.
		if [ ${DRYRUN} -le 1 ] ; then
		    ./create_etat0_limit.e
		fi
	    else
		echo "EXECUTION of ./create_etat0_limit.e simulated"
		echo "EXECUTION of ./create_etat0_limit.e simulated for DRYRUN = " $DRYRUN >> stack
	    fi
	fi

    fi

    IGCM_debug_PopStack "ATM_Update"
}

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

    [ ${LMDZ_adjust} = y ] && IGCM_sys_Cp Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat ${SUBMIT_DIR}/PARAM

    echo FINALIZE ATM !

    IGCM_debug_PopStack "ATM_Finalize"
}
