#- $Id: lmdz.driver 841 2009-12-08 09:48:13Z acosce $
#!/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"

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

    [ -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=earth_366d;;
        noleap)
            CalendarTypeForLmdz=earth_365d;;
        360d)
            CalendarTypeForLmdz=earth_360d;;
        *)
            CalendarTypeForLmdz=earth_360d
    esac


   ##- LMDZ physics version
   ##  Read LMDZ_Physics option in lmdz.card, if not present take default value AP (old physics)
   if [ ! X${lmdz_UserChoices_LMDZ_Physics} = X ] ; then
       LMDZ_Physics=${lmdz_UserChoices_LMDZ_Physics}
   else
       LMDZ_Physics=AP
   fi
   echo LMDZ physics version : ${LMDZ_Physics}


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


    ##-- Output frequency purpose ....
    ##--  Initialisation  ....
    ok_mensuel=y
    ok_journe=n
    ok_hf=n
    ok_hf3h=n
    ok_hf3hm=n
    ok_stn=n



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

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


    IGCM_debug_PopStack "ATM_Initialize"
}

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

    ecritphy=${PeriodLengthInDays}
    LMDZ_ecrit_ISCCP=${PeriodLengthInDays}.
    LMDZ_periodav=${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


    ## algo : For CumulPeriod=1 ; 
    ##        IF NbPeriod_Adjust = 0, Get Bands file directly from server, store it with _0 suffix
    ##        ELSE LMDZ_adjust=y ; save Bands file on file server ;
    ##        For CumulPeriod=2 to LMDZ_NbPeriod_adjust ; Get Bands file from server (CumulPeriod-1) ; LMDZ_adjust=y ; save Bands file on file server ;
    ##        For CumulPeriod > LMDZ_NbPeriod_adjust ; LMDZ_adjust=n ; Get Bands file from server (CumulPeriod=LMDZ_NbPeriod_adjust 0 to force it) ; Use Bands ;

    if [ ${CumulPeriod} -eq 1 ] ; then
        if [ ${LMDZ_NbPeriod_adjust} -eq 0 ] ; then
            if [ ! X${LMDZ_Bands_file_name} = X] ; then
               IGCM_sys_Get ${LMDZ_Bands_file_name} Bands_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat
               IGCM_sys_Put_Out Bands_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat ${R_OUT_ATM_D}/${config_UserChoices_JobName}_Bands_${RESOL_ATM_3D}_${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_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat_0"
      	       IGCM_sys_Chmod u+w Bands_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat
            fi
        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_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat_${RefPeriod} Bands_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat; IGCM_sys_Chmod u+w Bands_${RESOL_ATM_3D}_${NUM_PROC_ATM}prc.dat )

    ## physiq.def parameters modified from initial physiq_L${RESOL_ATM_Z}
    #  columns in phys_output_filekeys refer to the filenames given in phys_out_filenames in output.def :  
    #                                            histmth       histday        histhf    histfh3h   histhf3hm   histstn
    LMDZ_sed physiq.def phys_out_filekeys       "${ok_mensuel} ${ok_journe}   ${ok_hf}  ${ok_hf3h} ${ok_hf3hm} ${ok_stn}"
    LMDZ_sed physiq.def ecrit_ISCCP ${LMDZ_ecrit_ISCCP}

    if [ ! X${lmdz_UserChoices_ok_guide} = X ] ; then
	LMDZ_sed guide.def ok_guide  ${lmdz_UserChoices_ok_guide}
    fi


    ## gcm.def parameters : 
    # Modification only for new physics
    if [ X${LMDZ_Physics} = X"NPv3.1" ] ; then
	LMDZ_sed gcm.def iphysiq     5
    else
	LMDZ_sed gcm.def iphysiq    10
    fi



    ## run.def parameters
    LMDZ_sed run.def dayref   ${InitDay}
    LMDZ_sed run.def anneeref ${InitYear}
    LMDZ_sed run.def calend   ${CalendarTypeForLmdz}
    LMDZ_sed run.def nday     ${PeriodLengthInDays}
    LMDZ_sed run.def raz_date ${RAZ_DATE}
    LMDZ_sed run.def periodav ${LMDZ_periodav}
    LMDZ_sed run.def adjust   ${LMDZ_adjust}

    ## Other specific parameters to this experience 
    if [ ! X${lmdz_UserChoices_aer_type} = X ] ; then
        LMDZ_sed physiq.def aer_type ${lmdz_UserChoices_aer_type}
    fi
    if [ ! X${lmdz_UserChoices_pmagic} = X ] ; then
        LMDZ_sed physiq.def pmagic   ${lmdz_UserChoices_pmagic}
    fi

    IGCM_debug_PopStack "ATM_Update"
}

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

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

    IGCM_debug_PopStack "ATM_Finalize"
}
