#!/bin/ksh
#D- Driver du script pour ORCHIDEE
#-----------------------------------------------------------------
function SRF_Initialize
{
    IGCM_debug_PushStack "SRF_Initialize"

    ##- Define variable DefSuffix set in orchidee.card
    ##  This variable is used in orchidee.card to choose
    ##  parameter file(orchidee.def_DefSuffix).
    if [ ! X${orchidee_UserChoices_DefSuffix} = X ] ; then
        DefSuffix=${orchidee_UserChoices_DefSuffix}
    else
        DefSuffix=Choi
    fi

    IGCM_debug_PopStack "SRF_Initialize"
}

#-----------------------------------------------------------------
function SRF_Update
{
    IGCM_debug_PushStack "SRF_Update"

    ## 1. Modifications in orchidee.def parameter file

    # Activate STOMATE if the compontent SBG=stomate is set in config.card
    if [ X${config_ListOfComponents_SBG} = Xstomate ] ; then
	# Activate stomate in orchidee.def
        IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_OK_STOMATE y
    else
	# Deactivate stomate in orchidee.def
        IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_OK_STOMATE n
	# Deactivate output files for stomate
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 enabled .FALSE.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate1 output_freq 1mo
        IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 enabled .FALSE.
	IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml stomate2 output_freq 1mo
    fi

    # Define in orchidee.def if restart file should be used
    if ( [ ${CumulPeriod} -ne 1 ] || [ "${config_SRF_Restart}" != "n" ] ) ; then
        IGCM_comp_modifyDefFile blocker orchidee.def SECHIBA_restart_in sechiba_rest_in.nc
    else
        IGCM_comp_modifyDefFile blocker orchidee.def SECHIBA_restart_in NONE
    fi

    # Modify in orchidee.def VEGET_UPDATE if it is set in orchidee.card section UserChoices
    if [ ! X${orchidee_UserChoices_VEGET_UPDATE} = X ] ; then
        IGCM_comp_modifyDefFile nonblocker orchidee.def VEGET_UPDATE   ${orchidee_UserChoices_VEGET_UPDATE}
    else
        IGCM_comp_modifyDefFile nonblocker orchidee.def VEGET_UPDATE 0Y
    fi

    # Activate creation of river description file only for the first period
    if [ ${CumulPeriod} -eq 1 ] ; then
        IGCM_comp_modifyDefFile nonblocker orchidee.def RIVER_DESC y
    else
        IGCM_comp_modifyDefFile nonblocker orchidee.def RIVER_DESC n
    fi

    # Activate calcul of chemistry BVOC
    if [ ! X${orchidee_UserChoices_CHEMISTRY_BVOC} = X ] ; then
        IGCM_comp_modifyDefFile nonblocker orchidee.def CHEMISTRY_BVOC ${orchidee_UserChoices_CHEMISTRY_BVOC}
    else
        IGCM_comp_modifyDefFile nonblocker orchidee.def CHEMISTRY_BVOC n
    fi

    ## 2. Mangement of output and modifications of related xml files

    # Set default values for sechiba1_enabled, sechiba1_freq and sechiba2_enabled.
    sechiba1_enabled=.FALSE.
    sechiba2_enabled=.FALSE.
    sechiba1_freq=0s

    # Get WriteFrenquecies from config.card for SECHIBA
    # Note that it is only possible to have one HF and one other option. 
    # If more options are set in config.card only the latest will be used.
    for frequency in ${config_SRF_WriteFrequency} ; do
	case ${frequency} in
	    *Y|*y) 
		NbYears=$( echo ${frequency} | awk -F '[yY]' '{print $1}' )
		NbDaysYear=$( IGCM_date_DaysInYear ${year} )
		sechiba1_enabled=.TRUE.
		sechiba1_freq=${NbYears}y ;;
	    *M|*m)
		NbMonths=$( echo ${frequency} | awk -F '[mM]' '{print $1}' )
		sechiba1_enabled=.TRUE.
		sechiba1_freq=${NbMonths}mo ;;
	    *D|*d)
		NbDays=$( echo ${frequency} | awk -F '[dD]' '{print $1}' )
		sechiba1_enabled=.TRUE.
		sechiba1_freq=${NbDays}d ;;
	    *s)
		WriteInSeconds=$( echo ${frequency} | awk -F '[s]' '{print $1}' )
		sechiba1_enabled=.TRUE.
		sechiba1_freq=${WriteInSeconds}s ;;
            HF|hf) 
		sechiba2_enabled=.TRUE. ;;
	    *) 
		IGCM_debug_Exit "SRF WriteFrequency=${frequency} is invalid. Choose between xY, xM, xD, xs and HF for Sechiba." ;;
	esac
    done

    # Do the modifications in file_def_orchidee.xml
    IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml sechiba1 enabled ${sechiba1_enabled}
    IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml sechiba1 output_freq ${sechiba1_freq}
    IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml sechiba2 enabled ${sechiba2_enabled}

    # 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
    cp iodef.xml iodef.xml.tmp
    sed -e "/COMPONENT CONTEXT/r add.tmp" iodef.xml.tmp > iodef.xml
    rm iodef.xml.tmp add.tmp

    #Long Name as global attribute (if LongName is not empty)
	if [ ! "X${config_UserChoices_LongName}" = "X" ] ; then
	    listfile=$(ls file_def*orchidee.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 "SRF_Update"
}

#-----------------------------------------------------------------
function SRF_Finalize
{
    IGCM_debug_PushStack "SRF_Finalize"

    IGCM_debug_PopStack "SRF_Finalize"
}
