#!/bin/ksh

# $Date: 2010/06/25 18:48:26 $
# $Author: ssipsl $
# $Revision: 1.5 $
# IPSL (2006)
#  This software is governed by the CeCILL licence see ORCHIDEE/ORCHIDEE_CeCILL.LIC

#D--------------------------------------------------------------------==
#D--------------------------------------------------------------------==
#D-
#D-                      Job to launch FLUXNET spinup simulations
#D-
#D--------------------------------------------------------------------==
#D--------------------------------------------------------------------==
#D-
#                       Job inspired from Sönke Zaehle "site_new.def" job
#                       #################################################



MODIPSL=::modipsl::
libIGCM=${MODIPSL}/libIGCM

# All SubJobs in SPINUP in ONE OR MORE YEARS run (must be used with libIGCM branches/AllPostFred). 



#D--------------------------------------------------------------------==
#D-                   -1. User modification place
#D--------------------------------------------------------------------==

#D- Increased verbosity (1, 2, 3)
#Verbosity=3

#D-
# --------------------------------------------------------------------
#D- ! Optionnal features /!\ Use with care !
# --------------------------------------------------------------------

#D- Low level debug : to bypass lib test checks and stack construction
#D- Default=true
DEBUG_debug=false

#D- Set DEBUG_sys to false to disable output calls of function
#D- Default=true
#DEBUG_sys=false

#D- Turn in dry run mode ? (0,1,2,3)
#D- Default=0
#DRYRUN=3
# YOU MUST COMPILE YOUR EXE FILES FOR DRYRUN MODE !
# -------------------------------------------------------------------------------------
# | DRYRUN=  |  Date computations, | sys_Get    |  Exe    | sys_Put_Out; sys_Put_Rest |
# |          |  Cp/Exe param files |            |  Chmod  |                           |
# |          |      Qsub           |            |         |                           |
# -------------------------------------------------------------------------------------
# |    0     |       yes           |    yes     |  yes    |      yes                  |
# -------------------------------------------------------------------------------------
# |    1     |       yes           |    yes     |  yes    |      no                   |
# -------------------------------------------------------------------------------------
# |    2     |       yes           |    yes     |  no     |      no                   |
# -------------------------------------------------------------------------------------
# |    3     |       yes           |    no      |  no     |      no                   |
# -------------------------------------------------------------------------------------


#D-
#D--------------------------------------------------------------------==
#D-                   0. System Environment 
#D-                      - Define variables for Number of processors
#D-                      - Define MPI variables
#D-                      - Define batch scheduler variables
#D-                      - Source IGCM Library
#D-                      - Get RESOLution in .resol file (temporary)
#D--------------------------------------------------------------------==

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

. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh
        ( ${DEBUG_debug} ) && IGCM_debug_Check
. ${libIGCM}/libIGCM_card/libIGCM_card.ksh
       ( ${DEBUG_debug} ) && IGCM_card_Check
. ${libIGCM}/libIGCM_date/libIGCM_date.ksh
       ( ${DEBUG_debug} ) && IGCM_date_Check
#-------
. ${libIGCM}/libIGCM_sys/libIGCM_sys.ksh

#set -vx
R_BC=${R_IN}/BC

. ${SUBMIT_DIR}/fluxnet.ksh

FLUXNET_initialize


# Launch ATLAS syncho : we have to wait for the last job !

# ATLAS fix parameters :
# Atlas Name : 
AtlasCfg=${fluxnet_SPINUP_AtlasCfg}
#atlas_FLUXNET.cfg

# observation_file
observation_file_path=${fluxnet_SPINUP_observation_file_path}

# old history file
reference_file_path=${fluxnet_SPINUP_reference_file_path}

# Modulo for SpinUp years
Modulo=${fluxnet_SPINUP_Modulo}

ATLAS_ARGS="${AtlasCfg} ${observation_file_path} ${reference_file_path} ${Modulo}"

isite=1
index=0
while [ ${isite} -le ${NumSites} ] ; do

    site=${fluxnet_FLUXNET_Sites[$index]}
    echo $site

    # We test if the END step was moved in SpinUp/SPIN/Output directory.
    # Then we are sure the last historical SpinUp step has finished (HIST)...

    # Define name of the END SubJob (FIN) in SpinUp
    if [ ${fluxnet_SPINUP_duree_final} -gt 0 ] ; then
	BeforeLastJob=${site}${config_UserChoices_JobName}FIN
    else
	echo "We must have an \"duree_final\" non zero to get historical run and post-treatment"
	exit 1
    fi

    # Wait for the end of the last job :
    ((isleep=0))
    while [ ! -d ${R_OUT}/${config_UserChoices_TagName}/${site}${config_UserChoices_JobName}/SPIN/Output/${BeforeLastJob} ] ; do
	ls -la ${R_OUT}/${config_UserChoices_TagName}/${site}${config_UserChoices_JobName}/SPIN/Output/${BeforeLastJob}
	sleep 20
	((isleep=isleep+1))
	if [ isleep -gt 200 ] ; then
	    break ;
	fi
    done

    TIME_YEAR=${fluxnet_FLUXNET_Sites[ $(( index + ColumnLength )) ]}
    YEAR=${fluxnet_FLUXNET_Sites[ $(( index + ColumnYear )) ]}

    # Launch post-treatment for SPINUP !
    echo "****"
    echo "Launch post-treatment for SPINUP for site ${site} !"
    nohup ${SUBMIT_DIR}/post_FLUXNET ${MODIPSL} ${site} ${TIME_YEAR} ${YEAR} ${ATLAS_ARGS} > ${SUBMIT_DIR}/out_post_${site} 2>&1 &
    echo "****"

    (( isite=isite+1 ))
    (( index=index+NumInfosBySite ))
done

IGCM_sys_RmRunDir -Rf ${RUN_DIR_PATH}
