#!/bin/ksh

#D- Driver du script pour ORCHIDEE

function ORCHIDEE_sed
{
    IGCM_debug_PushStack "ORCHIDEE_sed"
    
    sed -e "s/^${1}\ *=.*/${1}= ${2}/" \
	orchidee.def > orchidee.def.tmp
    RET=$?
    echo "ORCHIDEE_sed : ${1} ${2}"
    \mv orchidee.def.tmp orchidee.def
    
    IGCM_debug_PopStack "ORCHIDEE_sed"
    return $RET
}

function ORCHIDEE_sed_xml
{
# Usage : ORCHIDEE_sed_xml xml_file output_file attribute value 
#         In file xml_file modify at the line containing id="output_file" the attribute "attribute=xxx" into "attribute=value"
    
    # Test if the fichier exist
    if [ ! -f ${1} ] ; then
	echo "WARNING : ${1} file does not exist. Following will not be done : ORCHIDEE_sed : ${1} ${2} ${3} ${4}"
        IGCM_debug_PopStack "LMDZ_sed"
	return
    fi
    sed -e "/id=\"${2}\"/s/\(${3}=\"\)[^\"]*\(\"\)/\1${4}\2/" ${1} > ${1}.tmp
    RET=$?
    echo "ORCHIDEE_sed : ${1} ${2} ${3} ${4}"
    \mv ${1}.tmp ${1}

    return $RET
}

#-----------------------------------------------------------------
function SRF_Initialize
{
    IGCM_debug_PushStack "SRF_Initialize"

    RESOL_SRF=ALL

    for frequency in ${config_SRF_WriteFrequency} ; do
        case ${frequency} in
            HF|hf) SRF_ok_hf=y ;;
        esac
    done

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

    typeset SECHIBA_WRITE_STEP

    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
	ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .FALSE. 
	ORCHIDEE_sed_xml file_def_orchidee.xml sechiba2 enabled .FALSE. 
	ORCHIDEE_sed XIOS_ORCHIDEE_OK y
    fi    

    SRF_WriteFrequency=$( echo ${config_SRF_WriteFrequency} | sed -e 's/\([0-9]*[yYmMdDs]\).*/\1/' ) 
    case ${SRF_WriteFrequency} in
	*Y|*y) 
	    WriteInYears=$( echo ${SRF_WriteFrequency} | awk -F '[yY]' '{print $1}' )
	    PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
	    (( SECHIBA_WRITE_STEP = PeriodLengthInDays * WriteInYears / PeriodLengthInYears * 86400 )) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq ${WriteInYears}y
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    ;;
	1M)
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq 1mo
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		SECHIBA_WRITE_STEP=-1.
		;;
	    *M|*m)
		SECHIBA_WRITE_STEP=-1.
		;;
	    *)
		(( SECHIBA_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) * 86400 ))
		;;
	    esac
	    ;;
	*M|*m) 
	    WriteInMonths=$( echo ${SRF_WriteFrequency} | awk -F '[mM]' '{print $1}' )
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq ${WriteInMonths}mo
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * 86400 / PeriodLengthInYears / 12  ))
		;;
	    *M|*m)
		PeriodLengthInMonths=$( echo ${config_UserChoices_PeriodLength} | awk -F '[mM]' '{print $1}' )
		(( SECHIBA_WRITE_STEP = PeriodLengthInDays * WriteInMonths  * 86400 / PeriodLengthInMonths  ))
		;;
	    *)
		(( SECHIBA_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) * 86400 ))
		if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		    ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		    ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq ${SECHIBA_WRITE_STEP}s
		    ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history
		fi
		;;
	    esac
	    ;;
	5D|5d) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq 5d
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    (( SECHIBA_WRITE_STEP = 5 * 86400 )) 
	    ;;
	1D|1d) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq 1d
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    (( SECHIBA_WRITE_STEP = 86400 )) 
	    ;;
	*s)
	    WriteInSeconds=$( echo ${SRF_WriteFrequency} | awk -F '[s]' '{print $1}' )
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 output_freq ${WriteInSeconds}s
		ORCHIDEE_sed_xml file_def_orchidee.xml sechiba1 name sechiba_history 
	    fi
	    (( SECHIBA_WRITE_STEP = WriteInSeconds )) ;;
	*) 
	    IGCM_debug_Exit "SRF_Update " ${SRF_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D." 
	    IGCM_debug_Verif_Exit ;;
    esac

#Use of XIOS ouputs only

    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
	(( SECHIBA_WRITE_STEP = 0 ))
    fi

    ORCHIDEE_sed WRITE_STEP ${SECHIBA_WRITE_STEP}
    ORCHIDEE_sed SECHIBA_HISTLEVEL ${orchidee_UserChoices_sechiba_LEVEL}

    # Outputs HF in HISTFILE2 if required
    if [ X${SRF_ok_hf} = Xy ] ; then
	ORCHIDEE_sed SECHIBA_HISTFILE2 y
	if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
	    ORCHIDEE_sed_xml file_def_orchidee.xml sechiba2 enabled .TRUE. 
	    ORCHIDEE_sed_xml file_def_orchidee.xml sechiba2 output_freq 10800s
	    ORCHIDEE_sed_xml file_def_orchidee.xml sechiba2 name sechiba_out_2
	fi
    else
	ORCHIDEE_sed SECHIBA_HISTFILE2 n
    fi
    ORCHIDEE_sed SECHIBA_HISTLEVEL2 1
    ORCHIDEE_sed WRITE_STEP2 10800.0

    if ( [ ${CumulPeriod} -ne 1 ] || [ "${config_SRF_Restart}" != "n" ] ) ; then
	ORCHIDEE_sed SECHIBA_restart_in sechiba_rest_in.nc
    else
	ORCHIDEE_sed SECHIBA_restart_in NONE
    fi

# Modify in orchidee.def VEGET_UPDATE and LAND_COVER_CHANGE if they are set in orchidee.card section UserChoices
    if [ ! X${orchidee_UserChoices_VEGET_UPDATE} = X ] ; then
	ORCHIDEE_sed VEGET_UPDATE   ${orchidee_UserChoices_VEGET_UPDATE}
    else
	ORCHIDEE_sed VEGET_UPDATE 0Y
    fi
    if [ ! X${orchidee_UserChoices_LAND_COVER_CHANGE} = X ] ; then
	ORCHIDEE_sed LAND_COVER_CHANGE ${orchidee_UserChoices_LAND_COVER_CHANGE}
    else
	ORCHIDEE_sed LAND_COVER_CHANGE n
    fi

    #IGCM_sys_Cp ${RUN_DIR}/orchidee.def ${RUN_DIR}/run.def
    #IGCM_sys_Put_Out ${RUN_DIR}/run.def ${R_SAVE}/${PREFIX}_run.def


    if [ X${orchidee_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
	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

    IGCM_debug_PopStack "SRF_Update"
}

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

    #IGCM_sys_Put_Out ${RUN_DIR}/used_run.def ${R_SAVE}/${PREFIX}_used_run.def

    echo FINALIZE SRF !!!

    IGCM_debug_PopStack "SRF_Finalize"
}
