#!/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 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

    IGCM_debug_PopStack "SRF_Initialize"
}

#-----------------------------------------------------------------
function SRF_PeriodStart
{

    IGCM_debug_PushStack "SRF_PeriodStart"
    if [ ${year} -gt 2004 ] ; then

        PFTmap="${R_BC}/SRF/${config_UserChoices_TagName}/ORCHIDEE_PFTmap_2006to2100_AR5_RCP8.5_MESSAGE.nc"
        CO2_veget="${R_BC}/ATM/${config_UserChoices_TagName}/IPCC_AR5/RCPs/RCP85/CO2_1765_2500.txt"
        if [ ${year} -eq 2005 ] ; then
            IGCM_sys_Rm -f PFTmap.nc
        fi

    else
        PFTmap="${R_BC}/SRF/${config_UserChoices_TagName}/PFTmap_1850to2005_AR5_LUHa.rc2.nc"
        CO2_veget="${R_BC}/ATM/${config_UserChoices_TagName}/IPCC_AR5/RCPs/RCP85/CO2_1765_2500.txt"
    fi

    BC_SRF_PFTmap=${PFTmap}
    BC_SRF_CO2=${CO2_veget}

    IGCM_debug_PopStack "SRF_PeriodStart"

}

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

    typeset SECHIBA_WRITE_STEP

    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 )) ;;
	1M)
	    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}' )
	    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 ))
		;;
	    esac
	    ;;
	5D|5d) 
	    (( SECHIBA_WRITE_STEP = 5 * 86400 )) ;;
	1D|1d) 
	    (( SECHIBA_WRITE_STEP = 86400 )) ;;
	*s)
	    WriteInSeconds=$( echo ${SRF_WriteFrequency} | awk -F '[s]' '{print $1}' )
	    (( SECHIBA_WRITE_STEP = WriteInSeconds )) ;;
	*) 
	    IGCM_debug_Exit "SRF_Update " ${SRF_WriteFrequency} " invalid WriteFrequency : choose in 1Y, 1M, 5D, 1D." 
	    IGCM_debug_Verif_Exit ;;
    esac

    # Dans le cas de test :
    if [ ${year} -eq 1979 ] ; then
	ORCHIDEE_sed VEGET_REINIT y
	ORCHIDEE_sed VEGET_YEAR 131
    fi
    
    # Dans le cas historique :
    if [ ${year} -eq 2005 ] ; then
	ORCHIDEE_sed VEGET_REINIT y
	ORCHIDEE_sed VEGET_YEAR 1
#	ORCHIDEE_sed VEGET_UPDATE '0Y'
    fi

#     # Dans le cas sans changement de carte après 2005 
#     if [ ${year} -gt 2005 ] ; then
# 	ORCHIDEE_sed VEGET_UPDATE '0Y'
#     fi

#IM ajout valeurs CO2_ATM
    ##-- GHG forcing for the current year
    #IPCC_CO2_AMIP=`grep Annee_${year} CO2_1765_2005.txt | awk -F= '{print $2}'`
    IPCC_CO2_AMIP=`grep Annee_${year} CO2_veget.txt | awk -F= '{print $2}'`
    ORCHIDEE_sed FORCE_CO2_VEG y
    ORCHIDEE_sed ATM_CO2 ${IPCC_CO2_AMIP}
 
    ORCHIDEE_sed HYDROL_CWRR ${orchidee_UserChoices_NEWHYDROL}

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

    # Outputs HF in HISTFILE2 if required
    [ X${SRF_ok_hf} = Xy ] && ORCHIDEE_sed SECHIBA_HISTFILE2 y
    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
    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

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