#-Q- cesium #!/bin/ksh
#-Q- cesium ######################
#-Q- cesium ## CESIUM   CEA ##
#-Q- cesium ######################
#-Q- cesium #MSUB -r SBGcorr             # Nom du job                
#-Q- cesium #MSUB -N 1              # Reservation du noeud
#-Q- cesium #MSUB -n 1              # Reservation du processus
#-Q- cesium #MSUB -T 86400          # Limite de temps elapsed du job
#-Q- cesium #MSUB -E "-j o"
#-Q- cesium #MSUB -E "-S /bin/ksh"
#-Q- platine #!/usr/bin/ksh
#-Q- platine ###################
#-Q- platine ## PLATINE   CEA ##
#-Q- platine ###################
#-Q- platine #BSUB -J SBGcorr                     # Nom du job
#-Q- platine #BSUB -N                        # message a la fin du job
#-Q- platine #BSUB -n 1                      # reservation des processeurs pour le job
#-Q- platine #BSUB -W 1:00                   # Limite temps
#-Q- platine #BSUB -q post                   # Passage en queue post
#-Q- sx8brodie #!/bin/ksh
#-Q- sx8brodie #######################
#-Q- sx8brodie ## SX8BRODIE   IDRIS ##
#-Q- sx8brodie #######################
#-Q- sx8brodie # Temps Elapsed max. d'une requete hh:mm:ss
#-Q- sx8brodie # @ wall_clock_limit = 20:00:00
#-Q- sx8brodie # Nom du travail LoadLeveler
#-Q- sx8brodie # @ job_name   = SBGcorr
#-Q- sx8brodie # Fichier de sortie standard du travail       
#-Q- sx8brodie # @ output     = $(job_name).$(jobid)
#-Q- sx8brodie # Fichier de sortie d'erreur du travail
#-Q- sx8brodie # @ error      =  $(job_name).$(jobid)
#-Q- sx8brodie # pour recevoir un mail en cas de depassement du temps Elapsed (ou autre pb.)
#-Q- sx8brodie # @ notification = error
#-Q- sx8brodie # @ environment  = $CURRENT_LOOP ; $imin ; $imax
#-Q- sx8brodie # @ queue
#-Q- aix6 #!/bin/ksh
#-Q- aix6 #######################
#-Q- aix6 ##   VARGAS   IDRIS  ##
#-Q- aix6 #######################
#-Q- aix6 # Temps Elapsed max. d'une requete hh:mm:ss
#-Q- aix6 # @ wall_clock_limit = 20:00:00
#-Q- aix6 # Nom du travail LoadLeveler
#-Q- aix6 # @ job_name   = SBGcorr
#-Q- aix6 # Fichier de sortie standard du travail
#-Q- aix6 # @ output     = $(job_name).$(jobid)
#-Q- aix6 # Fichier de sortie d'erreur du travail
#-Q- aix6 # @ error      =  $(job_name).$(jobid)
#-Q- aix6 # pour recevoir un mail en cas de depassement du temps Elapsed (ou autre pb.)
#-Q- aix6 # @ notification = error
#-Q- aix6 # @ environment  = $CURRENT_LOOP ; $imin ; $imax
#-Q- aix6 # @ queue
#-Q- ulam #!/bin/ksh
#-Q- ulam #######################
#-Q- ulam ##   ULAM   IDRIS  ##
#-Q- ulam #######################
#-Q- ulam # Temps Elapsed max. d'une requete hh:mm:ss
#-Q- ulam # @ wall_clock_limit = 20:00:00
#-Q- ulam # Nom du travail LoadLeveler
#-Q- ulam # @ job_name   = SBGcorr
#-Q- ulam # Fichier de sortie standard du travail
#-Q- ulam # @ output     = $(job_name).$(jobid)
#-Q- ulam # Fichier de sortie d'erreur du travail
#-Q- ulam # @ error      =  $(job_name).$(jobid)
#-Q- ulam # pour recevoir un mail en cas de depassement du temps Elapsed (ou autre pb.)
#-Q- ulam # @ notification = error
#-Q- ulam # @ environment  = $CURRENT_LOOP ; $imin ; $imax
#-Q- ulam # @ queue
#-Q- sx8mercure #!/bin/ksh
#-Q- sx9mercure #########################
#-Q- sx9mercure ## CESIUM FOR SX9  CEA ##
#-Q- sx9mercure #########################
#-Q- sx9mercure #MSUB -r SBGcorr             # Nom du job                
#-Q- sx9mercure #MSUB -N 1              # Reservation du noeud
#-Q- sx9mercure #MSUB -n 1              # Reservation du processus
#-Q- sx9mercure #MSUB -T 86400          # Limite de temps elapsed du job
#-Q- sx9mercure #MSUB -E "-j o"
#-Q- sx9mercure #MSUB -E "-S /bin/ksh"
#-Q- sx8mercure ######################
#-Q- sx8mercure ## SX8MERCURE   CEA ##
#-Q- sx8mercure ######################
#-Q- sx8mercure #PBS -N SBGcorr                   # Nom du job
#-Q- sx8mercure #PBS -j o                    # regroupement des stdout et stderr
#-Q- sx8mercure #PBS -S /usr/bin/ksh         # shell de soumission
#-Q- sx8mercure #PBS -l memsz_job=1gb        # Limite memoire a 1 Go
#-Q- sx8mercure #PBS -l cputim_job=24:00:00   # Limite temps a 2 heures
#-Q- sx8mercure #PBS -q scalaire
#-Q- sx9mercure #!/bin/ksh
#-Q- sx9mercure #########################
#-Q- sx9mercure ## CESIUM FOR SX9  CEA ##
#-Q- sx9mercure #########################
#-Q- sx9mercure #MSUB -r SBGcorr             # Nom du job                
#-Q- sx9mercure #MSUB -N 1              # Reservation du noeud
#-Q- sx9mercure #MSUB -n 1              # Reservation du processus
#-Q- sx9mercure #MSUB -T 86400          # Limite de temps elapsed du job
#-Q- sx9mercure #MSUB -E "-j o"
#-Q- sx9mercure #MSUB -E "-S /bin/ksh"
#-Q- titane #!/bin/ksh
#-Q- titane ######################
#-Q- titane ## TITANE   CEA ##
#-Q- titane ######################
#-Q- titane #MSUB -r SBGcorr             # Nom du job                
#-Q- titane #MSUB -N 1              # Reservation du noeud
#-Q- titane #MSUB -n 1              # Reservation du processus
#-Q- titane #MSUB -T 86400          # Limite de temps elapsed du job
#-Q- titane #MSUB -E "-j o"
#-Q- titane #MSUB -E "-S /bin/ksh"
#-Q- lxiv8 ######################
#-Q- lxiv8 ## OBELIX      LSCE ##
#-Q- lxiv8 ######################
#-Q- lxiv8 #PBS -N SBGcorr
#-Q- lxiv8 #PBS -m a
#-Q- lxiv8 #PBS -j oe
#-Q- lxiv8 #PBS -q medium
#-Q- lxiv8 #PBS -o SBGcorr.$$
#-Q- lxiv8 #PBS -S /bin/ksh
#-Q- default #!/bin/ksh
#-Q- default ##################
#-Q- default ## DEFAULT HOST ##
#-Q- default ##################

#**************************************************************
# Author: Martial Mancip
# Contact: Martial.Mancip__at__ipsl.jussieu.fr
# $Revision::                                          $ Revision of last commit
# $Author::                                            $ Author of last commit
# $Date::                                              $ Date of last commit
# IPSL (2006)
#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
#
#**************************************************************


#**************************************************************
# usage :
#
#  This script is only used for correction of stomate_ipcc_history files created 
# with orchidee_1_9_5 version of ORCHIDEE model checkouted before 01/0   
# Version of ORCHIDEE/src_stomate/stomate_lpj.f90 before CVS 1.26 had a bug
# that give a wrong Land Cover Change (fLuc) variable and a wrong nbp balance variable.
# This file will correct all those IPCC CMIP5 history files in a simulation.
# You must read 'Documentation' paragraph below to parameter the chain of jobs for 
# specific schedulers.
# This script must only be used in a libIGCM directory
#     (but submit from a CONFIG directory).
# Default parameters in this job are given for mercure CCRT X64 frontend.  
#
#**************************************************************




date

#-Q- sx8brodie export OMP_NUM_THREADS=1
#-Q- aix6 export OMP_NUM_THREADS=1
#-Q- ulam export OMP_NUM_THREADS=1

########################################################################

#D- Low level debug : to bypass lib test checks and stack construction
#D- Default : value from AA_job if any
libIGCM=${libIGCM:=::modipsl::/libIGCM}
# WARNING for StandAlone used : To run this script on some machine,
# you must check MirrorlibIGCM variable in sys library.
# If this variable is true, you must use libIGCM_POST path instead
# of your running libIGCM directory.


#D- Flag to determine job's output directory
#D- Default : value from libIGCM_post.ksh if any
POST_DIR=${POST_DIR:=${PBS_O_WORKDIR:=$(pwd)}}

#D- Increased verbosity (1, 2, 3)
#D- Default : value from AA_job if any
Verbosity=${Verbosity:=3}

#D- Experience type : DEB(ug), DEV(elopment), RUN (default)
JobType=RUN


### Documentation : 
##      1) To start again this script in StandAlone mode 
##         (because it fails with a problem with the system or a timeout, for example),
##         you must check the initialisation of three variables after the "Start loop" line :
##    CURRENT_LOOP=${CURRENT_LOOP:=1} give the iteration to begin the main loop
##    imin=${imin:=0}                 give the corresponding min index in the list of stomate_history files
##    imax=${imax:=${NbFilesLOOP}}    give the corresponding max index in the list of stomate_history files
##         nb: the main loop is parametered by UNIX_MAX_LIMIT (in system library) value.
##    imax is a factor of NbFilesLOOP = UNIX_MAX_LIMIT / 2 value
##    imin is the previous imax (see previous output of the script if exist)
##      2) This script will re-submit itself automaticly after calculate the next values of those three variables. 

##   To prevent timeout because of a large number of files (long simulation) and maximum elapse time 
## in the PBS queue, you must give the correct value of next line LOOP_MAX variable.
## It gives the maximum of iterations of the MAIN LOOP during the elapse time required in the PBS.
LOOP_MAX=15
#
## For example: if UNIX_MAX_LIMIT=360 and the number of stomate_history for a run if 3800,
##              the number of files that has to be demigrated if 7600 (stomate_history + stomate_ipcc_history).
##              The NBRE_TOUR (see previous output) calculate is 22.
##              If the elasped time required is 24H (86400s) and each 10 years (120x2 = 240 files) take one hour,
##              you must give LOOP_MAX= 240 * 24 / 360 = 16 max.
##              To prevent system slow, the default value is 15.


########################################################################

. ${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

IGCM_sys_MkdirWork ${RUN_DIR_PATH}
IGCM_sys_Cd ${RUN_DIR_PATH}


#
# First of all
#
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
#
echo
IGCM_debug_Print 1 "DefineArrayFromOption  : config_UserChoices"
IGCM_debug_PrintVariables 3 config_UserChoices_JobName
IGCM_debug_PrintVariables 3 config_UserChoices_LongName
IGCM_debug_PrintVariables 3 config_UserChoices_TagName
IGCM_debug_PrintVariables 3 config_UserChoices_SpaceName
IGCM_debug_PrintVariables 3 config_UserChoices_ExperimentName
IGCM_debug_PrintVariables 3 config_UserChoices_CalendarType
IGCM_debug_PrintVariables 3 config_UserChoices_DateBegin
IGCM_debug_PrintVariables 3 config_UserChoices_DateEnd
IGCM_debug_PrintVariables 3 config_UserChoices_PeriodLength
echo

#==================================
#R_SAVE : Job output directory
if ( [ ! X${config_UserChoices_SpaceName} = X ] && [ ! X${config_UserChoices_ExperimentName} = X ] ) ; then
    FreeName=$( echo ${config_UserChoices_JobName} | sed 's/.*_//' )
    R_SAVE=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_SpaceName}/${config_UserChoices_ExperimentName}/${FreeName}
    R_DODS=${config_UserChoices_TagName}/${config_UserChoices_SpaceName}/${config_UserChoices_ExperimentName}/${FreeName}
else
    R_SAVE=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}
    R_DODS=${config_UserChoices_TagName}/${config_UserChoices_JobName}
fi


########################################################################
#
#                          DEFINE LOOP PARAMETERS
#
########################################################################

R_OUT_SBG=${R_SAVE}/SBG
file_path=${R_OUT_SBG}/Output/MO

set +A LISTE_FILE_SBG -- $( IGCM_sys_RshArchive "find ${file_path} -name \"*stomate_history.nc\" " )
CountLISTE_FILE_SBG=${#LISTE_FILE_SBG[@]}
echo "Nombre de stomate_history : " ${CountLISTE_FILE_SBG}

NBRE_FILE_TOT=$(( 2 * CountLISTE_FILE_SBG ))
echo "Nombre de fichiers à démigrer : " ${NBRE_FILE_TOT}

if [ ${NBRE_FILE_TOT} -le ${UNIX_MAX_LIMIT} ] ; then
    NBRE_TOUR=1
    NbFilesLOOP=${CountLISTE_FILE_SBG}
elif [ ${NBRE_FILE_TOT}%${UNIX_MAX_LIMIT} -eq 0 ] ; then
    (( NBRE_TOUR = NBRE_FILE_TOT / UNIX_MAX_LIMIT ))
    (( NbFilesLOOP = UNIX_MAX_LIMIT / 2 ))
else
    (( NBRE_TOUR = NBRE_FILE_TOT / UNIX_MAX_LIMIT + 1 ))
    (( NbFilesLOOP = UNIX_MAX_LIMIT / 2 ))
fi
echo "NBRE_TOUR = ${NBRE_TOUR} "
echo "NbFilesLOOP = ${NbFilesLOOP} "

########################################################################
#
#                          START THE MAIN LOOP
#
########################################################################

# PARAMETERS OF THE LOOP :
# Cumultate CURRENT LOOP number over submitions
CURRENT_LOOP=${CURRENT_LOOP:=1}
# min index for list of local files
imin=${imin:=0}
# max index for list of local files
imax=${imax:=${NbFilesLOOP}}

# DEPEND OF THE ELAPSED TIME RESERVED FOR LOOP_MAX ITERATIONS
LOOP_MAX=${LOOP_MAX:=NBRE_TOUR}

IGCM_debug_Verif_Exit_Post

# MAIN LOOP
# LOCAL CURRENT LOOP number less than LOOP_MAX
CURRENT_LOOP0=1
while [ ${CURRENT_LOOP} -le ${NBRE_TOUR} ] ; do

    echo "###############################################################"
    echo "LOOP : " ${CURRENT_LOOP}
    echo "imin/imax : " $imin " / " $imax
    
    unset liste_file
    unset liste_file_local
    unset liste_file_ipcc
    unset liste_file_ipcc_local
    
    (( i = imin ))
    while [ ${i} -lt ${imax} ] ; do
	file=${LISTE_FILE_SBG[${i}]}
	file_ipcc=$( echo ${file} | sed -e "s/stomate_history/stomate_ipcc_history/" )

	liste_file[${#liste_file[*]}]=${file}
	liste_file_local[${#liste_file_local[*]}]=$( basename ${file} )
        RET=$?
        if [ ${RET} -gt 0 ] ; then
	    echo "erreur basename liste_file_local " ${file}
	    echo "file = " ${file}
	    exit 1
	fi
	liste_file[${#liste_file[*]}]=${file_ipcc}

	liste_file_ipcc[${#liste_file_ipcc[*]}]=${file_ipcc}
	liste_file_ipcc_local[${#liste_file_ipcc_local[*]}]=$( basename ${file_ipcc} )
        RET=$?
        if [ ${RET} -gt 0 ] ; then
	    echo "erreur basename liste_file_ipcc_local " ${file_ipcc}
	    echo "file = " ${file_ipcc}
	    exit 3
	fi

	(( i = i + 1 ))
    done
    #echo ${liste_file_local[@]}
    #echo ${liste_file[@]}

    # Get selected files
    IGCM_sys_Get /l liste_file[*] ${RUN_DIR_PATH}
    IGCM_debug_Verif_Exit_Post

    # chmod sur dépôt
    IGCM_sys_RshArchive "chmod u+w ${liste_file_ipcc[@]}"

    # Faire le nco/ncap2
    (( i = imin ))
    while [ ${i} -lt ${imax} ] ; do
	file=$( basename ${LISTE_FILE_SBG[${i}]} )
        RET=$?
        if [ ${RET} -gt 0 ] ; then
	    echo "erreur basename"
	    echo "i, imin, imax = " i imin imax
	    echo "file = " ${file}
	    exit 4
	fi
	file_ipcc=$( echo ${file} | sed -e "s/stomate_history/stomate_ipcc_history/" )
	ls -la ${file_ipcc}

	IGCM_sys_ncks -a -A -x -v GPP ${file} stomate.nc
	IGCM_sys_ncks -a -A ${file_ipcc} stomate.nc

	IGCM_sys_Cp ${file_ipcc} _${file_ipcc}
	IGCM_sys_Chmod u+w _${file_ipcc}

	IGCM_sys_ncap2 -s 'fLuc=float(fLuc*0.+(CONVFLUX+CFLUX_PROD10+CFLUX_PROD100)/1e3/86400*CONTFRAC)' \
                       -s 'nbp=float(nbp*0.f+(gpp-ra-rh-fFire-fLuc-fHarvest))' -v stomate.nc stomate1.nc
	IGCM_debug_Verif_Exit_Post

	IGCM_sys_ncks -a -A stomate1.nc _${file_ipcc}
	IGCM_sys_Mv -f _${file_ipcc} ${file_ipcc}
	IGCM_sys_Chmod u-w ${file_ipcc}

	ls -la ${file_ipcc}
	IGCM_sys_Rm stomate.nc stomate1.nc 
	IGCM_debug_Verif_Exit_Post

        # Put_Out des fichiers ipcc corrigés
	IGCM_sys_Put_Out ${file_ipcc} ${file_path}/${file_ipcc}

	(( i = i + 1 ))
    done

    # Nettoyage
    IGCM_sys_Rm ${liste_file_local[*]}


    # GO AHEAD IN DEEPEST LOOP
    (( imin = imax ))
    (( CURRENT_LOOP = CURRENT_LOOP + 1 ))
    (( CURRENT_LOOP0 = CURRENT_LOOP0 + 1 ))
    if [ ${CURRENT_LOOP} -lt ${NBRE_TOUR} ] ; then
	(( imax = imax + ${NbFilesLOOP} ))
    else
	if [ ${CURRENT_LOOP} -eq ${NBRE_TOUR} ] ; then
	    echo "Dernier tour : "
	    if [ ${NBRE_FILE_TOT}%${UNIX_MAX_LIMIT} -eq 0 ] ; then
		echo "imin NEW = " ${imax}
		(( imax = imax + ${NbFilesLOOP} ))
		echo "imax NEW = " ${imax}
	    else
		echo "imin NEW = " ${imax}
		(( imax = imax + ( NBRE_FILE_TOT % UNIX_MAX_LIMIT ) / 2 ))
		echo "imax NEW = " ${imax}
	    fi
	fi
    fi
    if ( [ ${CURRENT_LOOP} -le ${NBRE_TOUR} ] && \
         [ ${CURRENT_LOOP0} -gt ${LOOP_MAX} ] ) ; then
	export CURRENT_LOOP
	export imin
	export imax
	IGCM_sys_Cd ${SUBMIT_DIR}
	Script_Post_Output=SBGcorr.${imin}.${imax}
	listVarEnv="CURRENT_LOOP,imin,imax"
	echo "Submit next job."
	IGCM_sys_QsubPost stomate_flux_correction
        break ;	
    fi
done                 # while [ ${CURRENT_LOOP} -le  ${LOOP_MAX} ] do
# FINISH


#D-
# --------------------------------------------------------------------
#D- Test if all was right
# --------------------------------------------------------------------
IGCM_debug_Verif_Exit_Post

# Clean RUN_DIR_PATH (necessary for cesium)
IGCM_sys_Cd ${SUBMIT_DIR}
IGCM_sys_RmRunDir -Rf ${RUN_DIR_PATH}

date
