#!/bin/ksh

#D- Driver du script pour ORCHIDEE

#-----------------------------------------------------------------
function SBG_Initialize
{
    IGCM_debug_PushStack "SBG_Initialize"

    RESOL_SBG=ALL
    
    IGCM_debug_PopStack "SBG_Initialize"
}

#-----------------------------------------------------------------
function SBG_Update
{
    IGCM_debug_PushStack "SBG_Update"

    typeset STOMATE_WRITE_STEP

    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
	ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .FALSE. 
	ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .FALSE. 
    fi   
    case ${config_SBG_WriteFrequency} in
	*Y|*y) 
	    WriteInYears=$( echo ${config_SBG_WriteFrequency} | awk -F '[yY]' '{print $1}' )
	    PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
	    (( STOMATE_WRITE_STEP = PeriodLengthInDays * WriteInYears / PeriodLengthInYears )) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 output_freq ${WriteInYears}y
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 name stomate_history 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq ${WriteInYears}y
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history 

	    fi
	    ;;
	1M|1m) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 output_freq 1mo
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 name stomate_history 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq 1mo
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history 
	    fi
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		STOMATE_WRITE_STEP=-1.
		;;
	    *M|*m)
		STOMATE_WRITE_STEP=-1.
		;;
	    *)
		(( STOMATE_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) )) ;;
	    esac
	    ;;
	*M|*m) 
	    WriteInMonths=$( echo ${config_SBG_WriteFrequency} | awk -F '[mM]' '{print $1}' )
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 output_freq ${WriteInMonths}mo
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 name stomate_history 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq ${WriteInMonths}mo
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history 
	    fi
	    case ${config_UserChoices_PeriodLength} in
	    *Y|*y)
		PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
		(( STOMATE_WRITE_STEP = 30 ))
		;;
	    *M|*m)
		PeriodLengthInMonths=$( echo ${config_UserChoices_PeriodLength} | awk -F '[mM]' '{print $1}' )
		(( STOMATE_WRITE_STEP = PeriodLengthInDays * WriteInMonths / PeriodLengthInMonths ))
		;;
	    *)
		(( STOMATE_WRITE_STEP = $( IGCM_date_DaysInMonth $year $month ) )) 
		if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		    ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .TRUE. 
		    ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 output_freq ${STOMATE_WRITE_STEP}s
		    ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 name stomate_history
		    ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		    ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq ${STOMATE_WRITE_STEP}s
		    ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history
		fi
		;;
	    esac
	    ;;
	5D|5d) 
	    (( STOMATE_WRITE_STEP = 5 ))
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 output_freq 5d
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 name stomate_history 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq 5d
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history 
	    fi
	    ;;
	1D|1d) 
	    (( STOMATE_WRITE_STEP = 1 )) 
	    if [ X${orchidee_UserChoices_XIOS} = Xy ] ; then
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 output_freq 1d
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate1 name stomate_history 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq 1d
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history 
	    fi
	    ;;
	*s)
	    WriteInSeconds=$( echo ${config_SBG_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 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 enabled .TRUE. 
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 output_freq ${WriteInSeconds}s
		ORCHIDEE_sed_xml file_def_orchidee.xml stomate2 name stomate_ipcc_history 
	    fi
	    (( STOMATE_WRITE_STEP = 1 )) ;;
	*) 
	    IGCM_debug_Exit "SBG_Update " ${config_SBG_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D." 
	    IGCM_debug_Verif_Exit ;;
    esac


#Use of XIOS library only

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

    IGCM_comp_modifyDefFile nonblocker orchidee.def STOMATE_HIST_DT ${STOMATE_WRITE_STEP}
    IGCM_comp_modifyDefFile nonblocker orchidee.def STOMATE_HISTLEVEL ${stomate_UserChoices_stomate_LEVEL}

    if ( [ ${CumulPeriod} -eq 1 ] && [ "${config_SBG_Restart}" = "n" ] ) ; then
	echo "STOMATE : without restart"
	IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_RESTART_FILEIN NONE
    else
	IGCM_comp_modifyDefFile blocker orchidee.def STOMATE_RESTART_FILEIN stomate_rest_in.nc
    fi

    IGCM_debug_PopStack "SBG_Update"
}

#-----------------------------------------------------------------
function SBG_Finalize
{
#set -vx
    IGCM_debug_PushStack "SBG_Finalize"

    if [ $( IGCM_date_DaysBetweenGregorianDate ${PeriodDateEnd} ${year}1230 ) -ge 0 ] ; then
	IGCM_sys_Put_Out stomate_Cforcing.nc ${R_OUT_SBG_R}/${config_UserChoices_JobName}_${PeriodDateEnd}_stomate_Cforcing.nc
	IGCM_sys_Put_Out stomate_forcing.nc  ${R_OUT_SBG_R}/${config_UserChoices_JobName}_${PeriodDateEnd}_stomate_forcing.nc
# 	rm -f stomate_Cforcing.nc
# 	rm -f stomate_forcing.nc
    fi

    IGCM_debug_PopStack "SBG_Finalize"
}
