#!/bin/ksh

#D--------------------------------------------------------------------==

function FLUXNET_initialize
{
    IGCM_debug_PushStack "FLUXNET_initialize"

    # config.card : 
    # -------------
    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/config.card UserChoices
    typeset option
    for option in ${config_UserChoices[*]} ; do
	IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card UserChoices ${option}
    done
    
    DateBegin=$( IGCM_date_ConvertFormatToGregorian ${config_UserChoices_DateBegin} )
    IGCM_date_GetYearMonth ${DateBegin} year month
    
    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/config.card SRF
    typeset option
    for option in ${config_SRF[*]} ; do
	IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SRF ${option}
    done
    if [ X"${fluxnet_UserChoices_ok_stomate}" = "Xy" ] ; then
	IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/config.card SBG
	typeset option
	for option in ${config_SBG[*]} ; do
	    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SBG ${option}
	done
    fi
    
    # Subjobs configuration : 
    # -----------------------

    # 1) UserChoices section of fluxnet.card

    # This section groups standard options to be passed to ORCHIDEE.
    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/fluxnet.card UserChoices
    set +A fluxnet_UserChoices -- ${fluxnet_UserChoices[*]} > /dev/null 2>&1
    typeset option
    for option in ${fluxnet_UserChoices[*]} ; do
	IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/fluxnet.card UserChoices ${option}
    done

    # 2) FLUXNET section :

    # This section groups specific options for FLUXNET spinup :
    # - Fluxnet files path
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET FluxnetPath

    # - List of the sites/information to be treated :
    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET Sites
    echo ${fluxnet_FLUXNET_Sites[*]}
    # Number of columns by site in variable site : name, filename, length, + 13 PFT
    NumInfosBySite=30
    # Number of sites :
    NumSites=$(( ${#fluxnet_FLUXNET_Sites[*]} / NumInfosBySite ))
    # Num of Columns : name, filename, length, + 13 PFT
    ColumnName=0
    ColumnFilename=1
    ColumnYear=2
    ColumnLength=3
    ColumnPFT=4
    ColumnLAI=17

    # 3) SPINUP section : files path and spinup parameters
    # This section groups specific options for SPINUP :
    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/fluxnet.card SPINUP
    set +A fluxnet_SPINUP -- ${fluxnet_SPINUP[*]} > /dev/null 2>&1
    typeset option
    for option in ${fluxnet_SPINUP[*]} ; do
	IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/fluxnet.card SPINUP ${option}
    done

    # 4) Subjob orchidee.def parametrisation : SubJobParams in fluxnet.card
    # This section give flags to put in orchidee.def of Subjobs.
    IGCM_card_DefineArrayFromSection ${SUBMIT_DIR}/fluxnet.card SubJobParams
    set +A fluxnet_SubJobParams -- ${fluxnet_SubJobParams[*]} > /dev/null 2>&1
    eval first_option=${fluxnet_SubJobParams[0]} > /dev/null 2>&1
    if [ X${first_option} != X"Error:" ] ; then
	typeset option
	for option in ${fluxnet_SubJobParams[*]} ; do
	    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/fluxnet.card SubJobParams ${option}
	done
    fi

    IGCM_debug_PopStack "FLUXNET_initialize"
}

# Function to sed orchidee.def in Subjob dirs
function ORCHIDEE_sed
{
    IGCM_debug_PushStack "ORCHIDEE_sed"
    
    sed -e "s/^${1}\ *=.*/${1}= ${2}/" \
	${New_SUBMIT_DIR}/PARAM/orchidee.def > ${New_SUBMIT_DIR}/PARAM/orchidee.def.tmp
    RET=$?
    echo "ORCHIDEE_sed : ${1} ${2}"
    \mv ${New_SUBMIT_DIR}/PARAM/orchidee.def.tmp ${New_SUBMIT_DIR}/PARAM/orchidee.def

    IGCM_debug_PopStack "ORCHIDEE_sed"
    return $RET
}

