#!/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 SubJobPost
    typeset option
    for option in ${config_SubJobPost[*]} ; do
	IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SubJobPost ${option}
    done
    
    # 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

    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
    
    # 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 :
    #   * Number of PFTs
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET NbPFTs
    NbPFTs=${fluxnet_FLUXNET_NbPFTs}

    #   * Number of physical parameters on each sites per PFTs
    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET NbSitesParam
    #   * ORCHIDEE name for physical parameters on each sites
    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET NameSitesParam
    #   * Sites descriptions
    #    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET Sites

    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET Groups

    if [ X${fluxnet_FLUXNET_Groups[0]} == X"Option" ] ; then
	typeset name=fluxnet_FLUXNET_Groups
	eval unset ${name}
	eval ${name}[0]=${NULL_STR}
	set +A ${name} -- Sites
    fi
    for group in ${fluxnet_FLUXNET_Groups[*]} ; do
	IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/fluxnet.card FLUXNET $group
    done

    echo ${fluxnet_FLUXNET_Sites[*]}

    # Number of columns by site in variable site : name, filename, length, + ${NbPFTs} PFT
    NumInfosBySite=$(( 4 + ( NbPFTs * fluxnet_FLUXNET_NbSitesParam ) ))

    # Number of sites :
    NumSites=$(( ${#fluxnet_FLUXNET_Sites[*]} / NumInfosBySite ))
    # Num of Columns : name, filename, length, + ${NbPFTs} PFT
    ColumnName=0
    ColumnFilename=1
    ColumnYear=2
    ColumnLength=3

    # 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 run.def parametrisation : SubJobParams in fluxnet.card
    # This section give flags to put in run.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 parameter files in Subjob dirs
# For run.def :
function ORCHIDEE_def
{
    IGCM_debug_PushStack "ORCHIDEE_def"
    
    typeset ORCHIDEE_def_KEY ORCHIDEE_default_KEY

    ORCHIDEE_def_KEY=$( grep -e "^${1}\ *=" ${New_SUBMIT_DIR}/PARAM/run.def | gawk -- 'BEGIN {FS="="} {print $2}')
    if [ -f ${New_SUBMIT_DIR}/PARAM/orchidee.default ] ; then
	ORCHIDEE_default_KEY=$( grep -e "^${1}\ *=" ${New_SUBMIT_DIR}/PARAM/orchidee.default | gawk -- 'BEGIN {FS="="} {print $2}')
    fi

    if [ X"${ORCHIDEE_def_KEY}" != X ] ; then
	IGCM_debug_Print 2 "ORCHIDEE : ${1} has already been set in def file."
	if [ -f ${New_SUBMIT_DIR}/PARAM/orchidee.default ] ; then
	    IGCM_debug_Print 2 " default value : ${ORCHIDEE_default_KEY}"
	fi
	IGCM_debug_Print 2 "ORCHIDEE : ${1} has already been set in run.def file."
	IGCM_debug_Print 2 " default value : ${ORCHIDEE_default_KEY}"
	IGCM_debug_Print 2 " script value : ${2}"
	IGCM_debug_Print 2 " USER value : ${ORCHIDEE_def_KEY}"
	IGCM_debug_Print 2 " We will NOT set in again !"
    else
	echo "${1}= ${2}" >> ${New_SUBMIT_DIR}/PARAM/run.def
	echo "" >> ${New_SUBMIT_DIR}/PARAM/run.def
	echo "ORCHIDEE_def : ${1} ${2}"
    fi

    IGCM_debug_PopStack "ORCHIDEE_def"
    return $RET
}

# Function to grep parameter files in Subjob dirs
# They search for the argmument value in the file.
# The result of the function is given in ${SearchParam} global variable :
# * "line" if the parameter is defined with the form 
#   PARAM__1=value1
#   PARAM__2=value2
#   etc...
# * "vector" if the parameter is defined with the form
#   PARAM=value1 value2 etc...
# * "undefined" if the parameter is not definied in the parameters file
#   This should give an error in the script.

# For run.def :
function ORCHIDEE_grep
{
    IGCM_debug_PushStack "ORCHIDEE_grep"
    typeset Output

    if [ -f ${New_SUBMIT_DIR}/PARAM/run.def ] ; then
	set +A Output -- $( grep "^${1}__[0-9]*\ *=" ${New_SUBMIT_DIR}/PARAM/run.def )
	if [ X${Output[0]} != X ] ; then
	    SearchParam=line
	else
	    unset Output
	    set +A Output -- $( grep "^${1}\ *=" ${New_SUBMIT_DIR}/PARAM/run.def )
	    if [ X${Output[0]} != X ] ; then
		SearchParam=vector
	    else
		unset Output
                if [ -f ${New_SUBMIT_DIR}/PARAM/orchidee.default ] ; then
            	    set +A Output -- $( grep "^${1}__[0-9]*\ *=" ${New_SUBMIT_DIR}/PARAM/orchidee.default )
            	    if [ X${Output[0]} != X ] ; then
            		SearchParam=line
            	    else
            		unset Output
            		set +A Output -- $( grep "^${1}\ *=" ${New_SUBMIT_DIR}/PARAM/orchidee.default )
            		if [ X${Output[0]} != X ] ; then
            		    SearchParam=vector
            		else
			    SearchParam=undefined
			fi
		    fi
		else
		    SearchParam=undefined
		    echo "$@ : file not found !!"
		fi
	    fi
	fi
    fi

    IGCM_debug_PopStack "ORCHIDEE_grep"
    return $RET
}


