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

    if [ X$ResolAtm != X ] ; then
	# ResolAtm is set in config.card
	RESOL_ATM_3D=${ResolAtm}
	RESOL_ATM_Z=$( echo ${RESOL_ATM_3D} | awk "-Fx" '{print $3}' )
	RESOL_ATM_X=$( echo ${RESOL_ATM_3D} | awk "-Fx" '{print $1}' )
	RESOL_ATM_Y=$( echo ${RESOL_ATM_3D} | awk "-Fx" '{print $2}' )

	RESOL_CHM=${RESOL_ATM_X}${RESOL_ATM_Y}-L${RESOL_ATM_Z}
    elif [ -f ${SUBMIT_DIR}/../.resol ] ; then
	[ -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}')
    else
	IGCM_debug_Exit "ResolAtm is not set in config.card and the .resol file does not exist."
	IGCM_debug_Verif_Exit
    fi

    echo 'resol chimie', $RESOL_CHM
    CHEM=${ConfChem}
    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}




    IGCM_debug_PopStack "CHM_Initialize"
}

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

    if [ X${inca_UserChoices_output_frequency_chem} = X ] || [ X${inca_UserChoices_output_frequency_chem} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry name inca1d_chem
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_chem} = X1d ] || [ X${inca_UserChoices_output_frequency_chem} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry name inca${inca_UserChoices_output_frequency_chem}_chem
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry output_freq ${inca_UserChoices_output_frequency_chem}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry name inca1d_chem
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml chemistry output_freq ${inca_UserChoices_output_frequency_chem}

	fi
    fi

    if [ X${inca_UserChoices_output_frequency_emi} = X ] || [ X${inca_UserChoices_output_frequency_emi} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions name inca1d_emi
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_emi} = X1d ] || [ X${inca_UserChoices_output_frequency_emi} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions name inca${inca_UserChoices_output_frequency_emi}_emi
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions output_freq ${inca_UserChoices_output_frequency_emi}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions name inca1d_emi
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml emissions output_freq ${inca_UserChoices_output_frequency_emi}
	fi
    fi


    if [ X${inca_UserChoices_output_frequency_species} = X ] || [ X${inca_UserChoices_output_frequency_species} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species name inca1d_species
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_species} = X1d ] || [ X${inca_UserChoices_output_frequency_species} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species name inca${inca_UserChoices_output_frequency_species}_species
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species output_freq ${inca_UserChoices_output_frequency_species}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species name inca1d_species
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml species output_freq ${inca_UserChoices_output_frequency_species}
	fi
    fi


    if [ X${inca_UserChoices_output_frequency_forcage} = X ] || [ X${inca_UserChoices_output_frequency_forcage} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage name inca1d_forcage
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_forcage} = X1d ] || [ X${inca_UserChoices_output_frequency_forcage} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage name inca${inca_UserChoices_output_frequency_forcage}_forcage
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage output_freq ${inca_UserChoices_output_frequency_forcage}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage name inca1d_forcage
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml forcage output_freq ${inca_UserChoices_output_frequency_forcage}
	fi
    fi


    if [ X${inca_UserChoices_output_frequency_aero} = X ] || [ X${inca_UserChoices_output_frequency_aero} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero name inca1d_aero
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_aero} = X1d ] || [ X${inca_UserChoices_output_frequency_aero} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero name inca${inca_UserChoices_output_frequency_aero}_aero
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero output_freq ${inca_UserChoices_output_frequency_aero}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero name inca1d_aero
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero output_freq ${inca_UserChoices_output_frequency_aero}
	fi
    fi


    if [ X${inca_UserChoices_output_frequency_dep} = X ] || [ X${inca_UserChoices_output_frequency_dep} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel name inca1d_dep
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_dep} = X1d ] || [ X${inca_UserChoices_output_frequency_dep} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel name inca${inca_UserChoices_output_frequency_dep}_dep
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel output_freq ${inca_UserChoices_output_frequency_dep}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel name inca1d_dep
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml dvel output_freq ${inca_UserChoices_output_frequency_dep}
	fi
    fi

    if [ X${inca_UserChoices_output_frequency_washrate} = X ] || [ X${inca_UserChoices_output_frequency_washrate} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout name inca1d_washrate
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_washrate} = X1d ] || [ X${inca_UserChoices_output_frequency_washrate} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout name inca${inca_UserChoices_output_frequency_washrate}_washrate
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout output_freq ${inca_UserChoices_output_frequency_washrate}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout name inca1d_washrate
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml washout output_freq ${inca_UserChoices_output_frequency_washrate}
	fi
    fi

    if [ X${inca_UserChoices_output_frequency_veget} = X ] || [ X${inca_UserChoices_output_frequency_veget} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation name inca1d_veget
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_veget} = X1d ] || [ X${inca_UserChoices_output_frequency_veget} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation name inca${inca_UserChoices_output_frequency_veget}_veget
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation output_freq ${inca_UserChoices_output_frequency_veget}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation name inca1d_veget
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml vegetation output_freq ${inca_UserChoices_output_frequency_veget}
	fi
    fi


    if [ X${inca_UserChoices_output_frequency_reacflux} = X ] || [ X${inca_UserChoices_output_frequency_reacflux} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux name inca1d_reacflux
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_reacflux} = X1d ] || [ X${inca_UserChoices_output_frequency_reacflux} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux name inca${inca_UserChoices_output_frequency_reacflux}_reacflux
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux output_freq ${inca_UserChoices_output_frequency_reacflux}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux name inca1d_reacflux
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml reacflux output_freq ${inca_UserChoices_output_frequency_reacflux}
	fi
    fi


    if [ X${inca_UserChoices_output_frequency_phtrate} = X ] || [ X${inca_UserChoices_output_frequency_phtrate} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate name inca1d_phtrate
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_phtrate} = X1d ] || [ X${inca_UserChoices_output_frequency_phtrate} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate name inca${inca_UserChoices_output_frequency_phtrate}_phtrate
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate output_freq ${inca_UserChoices_output_frequency_phtrate}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate name inca1d_phtrate
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml phtrate output_freq ${inca_UserChoices_output_frequency_phtrate}
	fi
    fi

    if [ X${inca_UserChoices_output_frequency_ges} = X ] || [ X${inca_UserChoices_output_frequency_ges} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges name inca1d_ges
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_ges} = X1d ] || [ X${inca_UserChoices_output_frequency_ges} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges name inca${inca_UserChoices_output_frequency_ges}_ges
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges output_freq ${inca_UserChoices_output_frequency_ges}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges name inca1d_ges
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml ges output_freq ${inca_UserChoices_output_frequency_ges}
	fi
    fi

    if [ X${inca_UserChoices_output_frequency_invariants} = X ] || [ X${inca_UserChoices_output_frequency_invariants} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants name inca1d_invariants
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_invariants} = X1d ] || [ X${inca_UserChoices_output_frequency_invariants} = X1mo ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants name inca${inca_UserChoices_output_frequency_invariants}_invariants
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants output_freq ${inca_UserChoices_output_frequency_invariants}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants name inca1d_invariants
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml invariants output_freq ${inca_UserChoices_output_frequency_invariants}
	fi
    fi

    if [ X${inca_UserChoices_output_frequency_aero_chem} = X ] || [ X${inca_UserChoices_output_frequency_aero_chem} = XNONE ] ; then
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem enabled FALSE
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem name inca1d_aero_chem
	IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem output_freq 1d
	
    else
	if [ X${inca_UserChoices_output_frequency_aero_chem} = X1d ] || [ X${inca_UserChoices_output_frequency_aero_chem} = X1mo ] || [ X${inca_UserChoices_output_frequency_aero_chem} = X1ts ] ; then
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem name inca${inca_UserChoices_output_frequency_aero_chem}_aero_chem
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem output_freq ${inca_UserChoices_output_frequency_aero_chem}
	else
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem enabled TRUE
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem name inca1ts_aero_chem
	    IGCM_comp_modifyXmlFile nonblocker file_def_inca.xml aero_chem output_freq ${inca_UserChoices_output_frequency_aero_chem}
	fi
    fi

    case ${CHEM} in 
	AER)

	    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)

	    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_modifyDefFile nonblocker run.def config_inca chem
	    ;;
	NMHC_AER)

	    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)

	    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_modifyDefFile nonblocker run.def config_inca chem
	esac


    # inca.def : choose if we read input file with xios or netcdf 
    if [ ! X${inca_UserChoices_xios_read} = X ] ; then
	IGCM_comp_modifyDefFile nonblocker inca.def xios_read  ${inca_UserChoices_xios_read}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def xios_read  y
    fi

    # 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_wind_parameter} = X ] ; then 
	IGCM_comp_modifyDefFile nonblocker inca.def  tref ${inca_UserChoices_wind_parameter}
    else
	IGCM_comp_modifyDefFile nonblocker inca.def  tref DEFAULT
    fi

    if [  X${inca_UserChoices_seasalt_corr} = X ] ; then
    IGCM_comp_modifyDefFile nonblocker inca.def  ss_corr  DEFAULT
    else
    IGCM_comp_modifyDefFile nonblocker inca.def  ss_corr ${inca_UserChoices_seasalt_corr}
    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 which xml will be use (with group or no = cmip files) 
    if [ X${inca_UserChoices_cmip_xml} = Xy ] ; then 
	
	IGCM_comp_modifyDefFile nonblocker inca.def  cmip_xml  y
    else
	IGCM_comp_modifyDefFile nonblocker inca.def  cmip_xml  n
    fi
	


    # 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

    if [ -f context_input_inca.xml ] ; then
        echo '<context id="inca" src="./context_input_inca.xml"/>' >> add.tmp
    fi
    if [ -f context_sflx_inca.xml ] ; then
        echo '<context id="inca" src="./context_sflx_inca.xml"/>' >> add.tmp
    fi
    if [ -f context_co2_inca.xml ] ; then
        echo '<context id="inca" src="./context_co2_inca.xml"/>' >> add.tmp
    fi
    if [ -f context_oxydants_inca.xml ] ; then
        echo '<context id="inca" src="./context_oxydants_inca.xml"/>' >> add.tmp
    fi
    if [ -f context_start_inca.xml ] ; then
	if  [ -f chem_start.nc ] ; then 
            echo '<context id="inca" src="./context_start_inca.xml"/>' >> add.tmp
	fi 
    fi
    if [ -f context_dust_inca.xml ] ; then
        echo '<context id="inca" src="./context_dust_inca.xml"/>' >> add.tmp
    fi
    if [ -f context_winds_inca.xml ] ; then
        echo '<context id="inca" src="./context_winds_inca.xml"/>' >> add.tmp
    fi

    cp iodef.xml iodef.xml.tmp
    sed -e "/COMPONENT CONTEXT/r add.tmp" \
	iodef.xml.tmp > iodef.xml
    rm iodef.xml.tmp add.tmp


    # 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

    # remove unlimited from time axis for allowed xios to read all values in one time 
    echo "retire UNLIMITED sur axe temps "
    nccopy -u u10xios.nc tmp.nc
    mv tmp.nc u10xios.nc
    nccopy -u v10xios.nc tmp.nc
    mv tmp.nc v10xios.nc
    nccopy -u oxydants.nc tmp.nc
    mv tmp.nc oxydants.nc


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