#!/bin/ksh

#D- Driver du script pour ORCHIDEE_OL (off-line)

function ORCHIDEE_def
{
    IGCM_debug_PushStack "ORCHIDEE_def"
    
    typeset ORCHIDEE_def_KEY ORCHIDEE_default_KEY
    
    ORCHIDEE_def_KEY=$( grep -e "^${1}\ *=" run.def | gawk -- 'BEGIN {FS="="} {print $2}')
    ORCHIDEE_default_KEY=$( grep -e "^${1}\ *=" ${SUBMIT_DIR}/PARAM/orchidee.default | gawk -- 'BEGIN {FS="="} {print $2}')

    if [ X"${ORCHIDEE_def_KEY}" != X ] ; then
	IGCM_debug_Print 2 "ORCHIDEE : ${1} has already been set in def file."
	IGCM_debug_Print 2 " default value : ${ORCHIDEE_default_KEY}"
	IGCM_debug_Print 2 " script value : ${2}"
	IGCM_debug_Print 2 " USER value : ${ORCHIDEE_def_KEY}"
	IGCM_debug_Print 2 "               We will not set in again !"
    else
	echo "${1}= ${2}" >> run.def
	echo ""  >> run.def
	echo "ORCHIDEE_def : ${1} ${2}"
    fi
    
    IGCM_debug_PopStack "ORCHIDEE_def"
    return $RET
}

#-----------------------------------------------------------------
function OOL_Initialize
{
    IGCM_debug_PushStack "OOL_Initialize"

    NUM_PROC=1
#D- Number of processors used for lmdz and oasis coupler computed from PBS variable
    if [ X"${BATCH_NUM_PROC_TOT}" != X ] ; then
	NUM_PROC=${BATCH_NUM_PROC_TOT}
    fi
    echo BATCH_NUM_PROC_TOT=${BATCH_NUM_PROC_TOT}
    echo NUM_PROC=${NUM_PROC}

    if ( [ X${BATCH_NUM_PROC_TOT} != X ] && [ "${BATCH_NUM_PROC_TOT}" -gt 1 ] ) ; then
	MPIRUN_COMMAND=${HOST_MPIRUN_COMMAND}
    fi

    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/COMP/orchidee_ol.card UserChoices
    RESOL=${orchidee_ol_UserChoices_RESOL}

    IGCM_debug_PopStack "OOL_Initialize"
}

#-----------------------------------------------------------------
function OOL_Update
{
    IGCM_debug_PushStack "OOL_Update"

    if [ X"${orchidee_ol_UserChoices_TIMELENGTH}" = Xy ] ; then
	case ${config_UserChoices_PeriodLength} in
	    *M|*m|*D|*d)
		ORCHIDEE_def TIME_LENGTH ${PeriodLengthInDays}D
		;;
	    *s|*Y)
		ORCHIDEE_def TIME_LENGTH ${config_UserChoices_PeriodLength}
        ;;
        *)
        IGCM_debug_Exit "OOL_Update " ${config_UserChoices_PeriodLength} " invalid PeriodLength in config.card : choose between 1Y, 1M, 5D, 1D."
        IGCM_debug_Verif_Exit
	esac
    fi


    # Set TIME_SKIP only if starting without restart file
    if ( ${FirstInitialize} && [ "${config_OOL_Restart}" = "n" ] ) ; then
	ORCHIDEE_def TIME_SKIP $( IGCM_date_DaysBetweenGregorianDate ${DateBegin} ${year}0101 )D
    fi

    # Set RESTART_FILEIN in run.def
    if ( ${FirstInitialize} && [ "${config_OOL_Restart}" = "n" ] ) ; then
	# Set RESTART_FILEIN=NONE in run.def
	ORCHIDEE_def RESTART_FILEIN NONE
    elif [ X${orchidee_ol_UserChoices_NORESTART} = Xy ] ; then
	# Delete restart file if it exists in run directory and set RESTART_FILEIN=NONE in run.def
	if [ -f driver_rest_in.nc ] ; then
	    IGCM_sys_Rm driver_rest_in.nc
	fi
	ORCHIDEE_def RESTART_FILEIN NONE
    else
        # Default option using restart file, set RESTART_FILEIN=driver_rest_in.nc in orchidee.def
	ORCHIDEE_def RESTART_FILEIN driver_rest_in.nc
    fi

    # Reset the year for the case using CyclicBegin and CyclicEnd in config.card
    if [ X${CyclicYear} = X${config_UserChoices_CyclicBegin} ] ; then
        # Reset the year when coming back to the first year in the cycle. This means that the 
        # year will be read from the forcing file instead of coming from the restart file. 
        ORCHIDEE_def DRIVER_reset_time y
    fi


    if [ X${orchidee_ol_UserChoices_XIOS} = Xy ] ; then
      # Add include of orchidee context in iodef.xml
      # In iodef.xml add on next line after "COMPONENT CONTEXT"
      #  <context id="orchidee" src="./context_orchidee.xml"/>
      echo '<context id="orchidee" src="./context_orchidee.xml"/>' > add.tmp

      # Add inclusion of file context_input_orchidee.xml if this file exists
      if [ -f context_input_orchidee.xml ] ; then
          echo '<context id="orchidee" src="./context_input_orchidee.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

      # Test if SBG component is activated. If not, deactivate stomate output files from file_def_orchidee.xml
      if [ X${config_ListOfComponents_SBG} = X ] ; then
          # Stomate is not activated
          IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 enabled .FALSE.
          IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 enabled .FALSE.
      fi
    fi
          
    IGCM_debug_PopStack "OOL_Update"
}

#-----------------------------------------------------------------
function OOL_Finalize
{
    IGCM_debug_PushStack "OOL_Finalize"
    
    IGCM_debug_PopStack "OOL_Finalize"
}
