#- $Id: lmdz.driver 841 2009-12-08 09:48:13Z acosce $
#!/bin/ksh

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

    RESOL_ATM=$( echo $RESOL | awk "-Fx" '{print $2}' )
    case ${RESOL_ATM} in
	LMD7245)   BandsResol=72x45x19   ;;
	LMD9671)   BandsResol=96x71x19   ;;
	LMD9672)   BandsResol=96x72x19   ;;
	LMD9695)   BandsResol=96x95x19   ;;
	LMD14496)  BandsResol=144x96x19  ;;
	LMD144142) BandsResol=144x142x19 ;;
    esac


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

    ##- Default number of processor for lmdz
    NUM_PROC_ATM=1
    if [ X"${BATCH_NUM_PROC_TOT}" != X ] ; then
	NUM_PROC_ATM=${BATCH_NUM_PROC_TOT}
    fi

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

    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 )
    PAT_iperiod=$(  grep 'iperiod'     ${SUBMIT_DIR}/PARAM/gcm.def | tail -1)
    PAT_day_step=$( grep 'day_step'    ${SUBMIT_DIR}/PARAM/gcm.def )
    PAT_ecritphy=$( grep 'ecritphy'    ${SUBMIT_DIR}/PARAM/gcm.def )


    ## Read LMDZ_NbPeriod_adjust option in ${compname}.card 
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices LMDZ_NbPeriod_adjust
    eval LMDZ_NbPeriod_adjust=\${${compname}_UserChoices_LMDZ_NbPeriod_adjust} > /dev/null 2>&1

    if [ ${LMDZ_NbPeriod_adjust} -eq 0 ] ; then
        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/COMP/${compname}.card UserChoices LMDZ_Bands_file_name
        eval LMDZ_Bands_file_name=\${${compname}_UserChoices_LMDZ_Bands_file_name} > /dev/null 2>&1
    fi


    ##-- 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 | awk -F= '{print $2}' | tail -1)
    iphysiq=$(grep iphysiq ${SUBMIT_DIR}/PARAM/gcm.def | 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

    if [ ${CumulPeriod} -eq 1 ] ; then
        if [ ${LMDZ_NbPeriod_adjust} -eq 0 ] ; then
            IGCM_sys_Get ${LMDZ_Bands_file_name} Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat
            IGCM_sys_Put_Out Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat ${R_OUT_ATM_D}/${config_UserChoices_JobName}_Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat_0
            IGCM_debug_Print 1 "Bands file forced to ${LMDZ_Bands_file_name} and stored in ${config_UserChoices_JobName}_Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat_0"
        fi
    fi

    LMDZ_adjust=n
    [ ${CumulPeriod} -le ${LMDZ_NbPeriod_adjust} ] && LMDZ_adjust=y

    RefPeriod=${LMDZ_NbPeriod_adjust} #0 to use a Bands file from Restart
    [ ${CumulPeriod} -le ${LMDZ_NbPeriod_adjust} ] && (( RefPeriod = ${CumulPeriod} - 1 )) 

    [ ${CumulPeriod} -gt 1 ] && IGCM_sys_Get ${R_OUT_ATM_D}/${config_UserChoices_JobName}_Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat_${RefPeriod} Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat

    ## 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

    greg_dat=$( IGCM_date_ConvertFormatToGregorian $PeriodDateBegin ) 
    jul_dat=$( IGCM_date_ConvertGregorianDateToJulian $greg_dat )
    nbjour=$( expr \( $jul_dat \% 1000 \) ) 
    yractu=$( expr $greg_dat / 10000  )
    echo  "dayref = " $nbjour  "year actu = " $yractu

    ## Mise en forme du fichier run.def
    sed -e "s/_dayref_/${nbjour}/" 			\
	-e "s/_anneeref_/${yractu}/" 			\
        -e "s/_calend_/${CalendarTypeForLmdz}/"        	\
	-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

    IGCM_debug_PopStack "ATM_Update"
}

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

    [ ${CumulPeriod} -le ${LMDZ_NbPeriod_adjust} ] && IGCM_sys_Put_Out Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat ${R_OUT_ATM_D}/${config_UserChoices_JobName}_Bands_${BandsResol}_${NUM_PROC_ATM}prc.dat_${CumulPeriod}
    echo FINALIZE ATM !

    IGCM_debug_PopStack "ATM_Finalize"
}
