#!/bin/ksh

#D- Driver du script pour INCA
#-----------------------------------------------------------------
function INCA_sed_replace
{
    IGCM_debug_PushStack "INCA_sed_replace"

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

    IGCM_debug_PopStack "INCA_sed_replace"
    return $RET
}
#-----------------------------------------------------------------
function CHM_Initialize
{
    IGCM_debug_PushStack "CHM_Initialize"

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

    [ -f ${SUBMIT_DIR}/../.resol ] && eval $(grep RESOL_ATM_3D ${SUBMIT_DIR}/../.resol) || RESOL_ATM_3D=96x95x19
    RESOL_CHM_Z=$( echo ${RESOL_ATM_3D} | awk "-Fx" '{print $3}' | awk "-F-" '{print $1}')
    echo 'resol chimie', $RESOL_CHM

    [ -f ${SUBMIT_DIR}/../.chimie ] && eval $(grep CHEM ${SUBMIT_DIR}/../.chimie) 
    INCA_sed_replace ${SUBMIT_DIR}/config.card \${CHEM} ${CHEM}
    echo "INCA Tag : LMDZ-INCA" ${config_UserChoices_TagName}

    IGCM_debug_PopStack "CHM_Initialize"
}

#-----------------------------------------------------------------
function CHM_Update
{
    IGCM_debug_PushStack "CHM_Update"

    case ${CHEM} in 
	AER)
	IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	;;
	DUST)
	IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	;;
	NMHC) 
	IGCM_comp_modifyDefFile nonblocker run.def config_inca chem
	;;
	NMHC_AER)
	IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	;;
	CH4)
	IGCM_comp_modifyDefFile nonblocker run.def config_inca chem
	;;
	CH4_AER)
	IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	;;
	GES)
	IGCM_comp_modifyDefFile nonblocker run.def config_inca chem
	esac

    # inca.def : Activate reading of 10m wind in files if climatological=n in inca.card
    if [ ! X${inca_UserChoices_climatological} = X ] ; then
	IGCM_comp_modifyDefFile nonblocker inca.def climatological  ${inca_UserChoices_climatological}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def climatological  n
    fi

    # inca.def : choose write frequency for inca_avgr in inca.card
    if [ ! X${inca_UserChoices_freq_write_chem} = X ] ; then
	IGCM_comp_modifyDefFile nonblocker inca.def freq_write_chem  ${inca_UserChoices_freq_write_chem}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def  freq_write_chem  86400
    fi

    #inca.def : choose aerosol effect with feedback variable
    if [ ! X${inca_UserChoices_feedb} = X ] ; then
	IGCM_comp_modifyDefFile nonblocker inca.def  feedb  ${inca_UserChoices_feedb}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def feedb  0
    fi
    
    #inca.def : choose output library 
    if [  X${inca_UserChoices_XIOS} = Xy ] ; then
	IGCM_comp_modifyDefFile nonblocker inca.def  XIOS_INCA_OK  y
    else
	IGCM_comp_modifyDefFile nonblocker inca.def  XIOS_INCA_OK n
    fi

    if [  X${inca_UserChoices_IOIPSL} = Xy ] ; then
	IGCM_comp_modifyDefFile nonblocker inca.def  IOIPSL_INCA_OK  y
    else
	IGCM_comp_modifyDefFile nonblocker inca.def  IOIPSL_INCA_OK n
    fi


    if [  X${inca_UserChoices_wind_method} = X ] ; then
    IGCM_comp_modifyDefFile nonblocker inca.def  wind_method  threshold
    else
    IGCM_comp_modifyDefFile nonblocker inca.def  wind_method ${inca_UserChoices_wind_method}
    fi



    if [ X${inca_UserChoices_XIOS}  = Xy ] ; then
    # Add include of INCA context in iodef.xml
    # In iodef.xml add on next line after "COMPONENT CONTEXT"
    #  <context id="inca" src="./context_inca.xml"/>
	echo '<context id="inca" src="./context_inca.xml"/>' > add.tmp
	cp iodef.xml iodef.xml.tmp
	sed -e "/COMPONENT CONTEXT/r add.tmp" \
	    iodef.xml.tmp > iodef.xml
	rm iodef.xml.tmp add.tmp
    fi

    IGCM_debug_PopStack "CHM_Update"
}

#-----------------------------------------------------------------
function CHM_Finalize
{
    IGCM_debug_PushStack "CHM_Finalize"

    echo FINALIZE CHM !!!
#
#    IGCM_sys_Mkdir ${R_OUT_EXE}
#    cd ${MODIPSL}
#    tar cf src_${config_UserChoices_JobName}.tar  modeles
#    IGCM_sys_Mv src_${config_UserChoices_JobName}.tar ${R_OUT_EXE}/.
#    cd -
#

    IGCM_debug_PopStack "CHM_Finalize"
}
