#!/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}

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

    enable1=.FALSE.
    enable2=.FALSE.

    for frequency in ${config_CHM_WriteFrequency} ; do
	case ${frequency} in 
	    1D|1d) enable1=.TRUE. ;;
	esac
	case ${frequency} in
	    1M|1m) enable2=.TRUE. ;;
	esac
    done



    IGCM_debug_PopStack "CHM_Initialize"
}

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



    case ${CHEM} in 
	AER)
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   forcage1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml forcage2     enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   aero1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml aero2        enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   chemistry1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml chemistry2   enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   emissions1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml emissions2   enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   species1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml species2     enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   dvel1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml dvel2        enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   washout1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml washout2     enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   reacflux1    enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml reacflux2    enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml   invariants1  enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml invariants2  enabled ${enable2}

	    if [ X${LMDZ_Physics} = X"AP" ] ; then
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	    else
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aeNP
	    fi
	;;
	DUSS)
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml forcage1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml aero1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml emissions1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml species1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml vegetation1  enabled ${enable1}

	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml forcage2     enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml aero2        enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml emissions2   enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml species2     enabled ${enable2}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml vegetation2  enabled ${enable2}

	    if [ X${LMDZ_Physics} = X"AP" ] ; then
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	    else
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aeNP
	    fi
	;;
	NMHC) 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml reacflux1    enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml emissions1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml species1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml chemistry1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml dvel1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml washout1     enabled ${enable1}

	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml reacflux2  enabled ${enable2}  
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml emissions2 enabled ${enable2}  
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml species2   enabled ${enable2}  
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml chemistry2 enabled ${enable2}  
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml dvel2      enabled ${enable2}  
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml washout2   enabled ${enable2}  

	    IGCM_comp_modifyDefFile nonblocker run.def config_inca chem
	    ;;
	NMHC_AER)
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml forcage1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml aero1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml species1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml chemistry1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml emissions1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml dvel1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml phtrate1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml washout1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml reacflux1    enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml vegetation1  enabled ${enable1}

	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml forcage2    enabled ${enable2}  
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml aero2       enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml species2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml chemistry2  enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml emissions2  enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml dvel2       enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml phtrate2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml washout2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml reacflux2   enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml vegetation2 enabled ${enable2} 

	    if [ X${LMDZ_Physics} = X"AP" ] ; then
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	    else
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aeNP
	    fi
	;;

	NMHC_AER_S)
 	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml forcage1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml aero1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml species1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml chemistry1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml emissions1   enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml dvel1        enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml phtrate1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml washout1     enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml reacflux1    enabled ${enable1}
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml vegetation1  enabled ${enable1}

 	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml forcage2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml aero2       enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml species2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml chemistry2  enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml emissions2  enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml phtrate2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml dvel2       enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml washout2    enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml reacflux2   enabled ${enable2} 
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml vegetation2 enabled ${enable2}  

	    if [ X${LMDZ_Physics} = X"AP" ] ; then
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aero
	    else
		IGCM_comp_modifyDefFile nonblocker run.def config_inca aeNP
	    fi
	;;
	GES)
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_daily.xml emissions1    enabled ${enable1}

	    IGCM_comp_modifyXmlFile nonblocker file_def_inca_monthly.xml emissions2    enabled ${enable2}

	    IGCM_comp_modifyDefFile nonblocker run.def config_inca chem
	esac


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

    if [  X${inca_UserChoices_10m_winds_method} = X ] ; then
    IGCM_comp_modifyDefFile nonblocker inca.def  10m_winds_method  threshold
    else
    IGCM_comp_modifyDefFile nonblocker inca.def  10m_winds_method ${inca_UserChoices_10m_winds_method}
    fi


    if [ ! X${inca_UserChoices_wind_parameter} = X ] ; then 
	if [  X${inca_UserChoices_10m_winds_method} = Xthreshold ] ; then 
	    IGCM_comp_modifyDefFile nonblocker inca.def  tref ${inca_UserChoices_wind_parameter}
	    IGCM_comp_modifyDefFile nonblocker inca.def  kref DEFAULT
	else
	    IGCM_comp_modifyDefFile nonblocker inca.def  kref ${inca_UserChoices_wind_parameter}
	    IGCM_comp_modifyDefFile nonblocker inca.def  tref DEFAULT
	fi
    else
	    IGCM_comp_modifyDefFile nonblocker inca.def  kref DEFAULT
	    IGCM_comp_modifyDefFile nonblocker inca.def  tref DEFAULT
    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 coupled model with orchidee  
    if [ ! X${inca_UserChoices_CoupOrchInca} = X ]; then 
	IGCM_comp_modifyDefFile nonblocker inca.def CoupOrchInca ${inca_UserChoices_CoupOrchInca}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def CoupOrchInca n
    fi

    #inca.def : choose sedimentation schema 
    if [ ! X${inca_UserChoices_multilayer_sedim} = X ]; then 
	IGCM_comp_modifyDefFile nonblocker inca.def multilayer_sediment ${inca_UserChoices_multilayer_sedim}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def multilayer_sediment y
    fi

    #inca.def : choose calcul interactif des diagnostiques flux chimique ou non  
    if [ ! X${inca_UserChoices_calcul_flux} = X ]; then 
	IGCM_comp_modifyDefFile nonblocker inca.def calcul_flux ${inca_UserChoices_calcul_flux}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def calcul_flux n
    fi

    #inca.def : choose aircraft emission 
    if [ ! X${inca_UserChoices_flag_plane} = X ]; then 
	IGCM_comp_modifyDefFile nonblocker inca.def flag_plane ${inca_UserChoices_flag_plane}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def flag_plane 0
    fi
    
    #inca.def : choose time interpolation for sflx.nc
    if [ ! X${inca_UserChoices_emi_interp_time} = X ]; then 
	IGCM_comp_modifyDefFile nonblocker inca.def emi_interp_time ${inca_UserChoices_emi_interp_time}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def emi_interp_time 1
    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_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

    # Add LongName as global attribute in XIOS output files (if LongName is not empty)
    if [ ! "X${config_UserChoices_LongName}" = "X" ] ; then
	listfile=$(ls file_def*inca.xml)
	echo "<variable id=\"LongName\" type=\"string\">${config_UserChoices_LongName}</variable>" > add.tmp
	for file in ${listfile}
	do
	    cp ${file} ${file}.tmp
	    sed -e "/<file id/r add.tmp" \
		${file}.tmp > ${file}
	    rm ${file}.tmp 
	done
	rm 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"
}
