#!/bin/ksh
## Driver for the component SRF corresponding to the sechiba part of 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


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

    # Set default values for sechiba1_enabled and sechiba1_freq.
    # These variables are used only to modify file_def_orchidee.xml
    sechiba1_enabled=.FALSE.
    sechiba2_enabled=.FALSE.
    sechiba1_freq=0s
    sechiba2_freq=0s


    # Get WriteFrenquecy for SRF (SECHIBA) from config.card
    # 1 or 2 frequencies can be set in WriteFrenquecy for SRF
    # The first frequency will always be used for the sechiba_history file and the 
    # second frequency will be used for the sechiba_out_2.nc file. 
    # The files are activated only if its corresponding frequency is set in WriteFrequency
    ifreq=0
    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} )
		file_enabled=.TRUE.
		file_freq=${NbYears}y ;;
	    *M|*m)
		NbMonths=$( echo ${frequency} | awk -F '[mM]' '{print $1}' )
		file_enabled=.TRUE.
		file_freq=${NbMonths}mo ;;
	    *D|*d)
		NbDays=$( echo ${frequency} | awk -F '[dD]' '{print $1}' )
		file_enabled=.TRUE.
		file_freq=${NbDays}d ;;
	    *s)
		WriteInSeconds=$( echo ${frequency} | awk -F '[s]' '{print $1}' )
		file_enabled=.TRUE.
		file_freq=${WriteInSeconds}s ;;
            HF|hf) 
		file_enabled=.TRUE. 
		file_freq=10800s ;;
	    *) 
		IGCM_debug_Exit "SRF_Update " ${frequency} " invalid WriteFrequency : choose in xY, xM, xD, xs and HF" 
		IGCM_debug_Verif_Exit ;;
	esac

	(( ifreq = ifreq + 1 ))
	case ${ifreq} in
	    1)
		sechiba1_enabled=${file_enabled}
		sechiba1_freq=${file_freq} ;;
	    2)
		sechiba2_enabled=${file_enabled}
		sechiba2_freq=${file_freq} ;;
        *)
		IGCM_debug_Exit "SRF_Update: It is not possible to set more than 2 output files for sechiba from config.card"
		IGCM_debug_Exit "You must correct WriteFrequancy in SRF secion in config.card."
		IGCM_debug_Exit "Adapt file_def_orchidee.xml directly if you want more output files"
		IGCM_debug_Verif_Exit ;;
	esac
    done


    # Modify file_def_orchidee.xml file
    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}
    IGCM_comp_modifyXmlFile nonblocker file_def_orchidee.xml sechiba2 output_freq ${sechiba2_freq}
    

    # 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

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