#-Q- curie ######################
#-Q- curie ## CURIE   TGCC/CEA ##
#-Q- curie ######################
#-Q- curie #MSUB -r METRICS_ORCHIDEE       # Nom du job
#-Q- curie #MSUB -o METRICS_ORCHIDEE.out_%I
#-Q- curie #MSUB -e METRICS_ORCHIDEE.out_%I
#-Q- curie #MSUB -n 1              # Reservation du processus
#-Q- curie #MSUB -T 14400          # Limite de temps elapsed du job
#-Q- curie #MSUB -q ::default_node::
#-Q- curie #MSUB -Q normal
#-Q- curie #MSUB -c ::default_core::
#-Q- curie #MSUB -A ::default_project::
#-Q- curie set +x
#-Q- irene ######################
#-Q- irene ## IRENE   TGCC/CEA ##
#-Q- irene ######################
#-Q- irene #MSUB -r METRICS_ORCHIDEE # Job name
#-Q- irene #MSUB -o METRICS_ORCHIDEE.out_%I
#-Q- irene #MSUB -e METRICS_ORCHIDEE.out_%I
#-Q- irene #MSUB -n 1
#-Q- irene #MSUB -T 14400           # Maximum elapsed time
#-Q- irene #MSUB -q ::default_node:: 
#-Q- irene #MSUB -c ::default_core::
#-Q- irene #MSUB -Q normal
#-Q- irene #MSUB -A ::default_post_project::
#-Q- irene #MSUB -m store,work,scratch
#-Q- irene set +x
#-Q- irene-amd ######################
#-Q- irene-amd ## IRENE   TGCC/CEA ##
#-Q- irene-amd ######################
#-Q- irene-amd #MSUB -r METRICS_ORCHIDEE # Job name
#-Q- irene-amd #MSUB -o METRICS_ORCHIDEE.out_%I
#-Q- irene-amd #MSUB -e METRICS_ORCHIDEE.out_%I
#-Q- irene-amd #MSUB -n 1
#-Q- irene-amd #MSUB -T 14400           # Maximum elapsed time
#-Q- irene-amd #MSUB -q ::default_node:: 
#-Q- irene-amd #MSUB -c ::default_core::
#-Q- irene-amd #MSUB -Q normal
#-Q- irene-amd #MSUB -A ::default_post_project::
#-Q- irene-amd #MSUB -m store,work,scratch
#-Q- irene-amd set +x
#-Q- ada #!/bin/ksh
#-Q- ada #######################
#-Q- ada ## ADA         IDRIS ##
#-Q- ada #######################
#-Q- ada # @ job_type = mpich
#-Q- ada # @ requirements = (Feature == "prepost")
#-Q- ada # Temps Elapsed max. d'une requete hh:mm:ss
#-Q- ada # @ wall_clock_limit = 4:00:00
#-Q- ada # Nom du travail LoadLeveler
#-Q- ada # @ job_name   = METRICS_ORCHIDEE
#-Q- ada # Fichier de sortie standard du travail
#-Q- ada # @ output     = $(job_name).$(jobid)
#-Q- ada # Fichier de sortie d'erreur du travail
#-Q- ada # @ error      =  $(job_name).$(jobid)
#-Q- ada # pour recevoir un mail en cas de depassement du temps Elapsed (ou autre pb.)
#-Q- ada # @ notification = error
#-Q- ada # @ environment  = $POST_DIR ; $Script_Post_Output ; $SUBMIT_DIR ; $libIGCM ; $DEBUG_debug ; $BigBrother ; $postProcessingStopLevel ; $R_INIT ; $R_BC ; $StandAlone ; $RESOL_ATM ; $RESOL_OCE ; $RESOL_ICE ; $RESOL_MBG ; $RESOL_SRF ; $RESOL_SBG ; $YEARS ; $DIR_OUT ; $MASTER ; $libIGCM_SX ; wall_clock_limit=$(wall_clock_limit)
#-Q- ada # @ queue
#-Q- lxiv8 ######################
#-Q- lxiv8 ## OBELIX      LSCE ##
#-Q- lxiv8 ######################
#-Q- lxiv8 #PBS -N METRICS_ORCHIDEE
#-Q- lxiv8 #PBS -m a
#-Q- lxiv8 #PBS -j oe
#-Q- lxiv8 #PBS -q test
#-Q- lxiv8 #PBS -o METRICS_ORCHIDEE.$$
#-Q- lxiv8 #PBS -S /bin/ksh
#-Q- ifort_CICLAD ######################
#-Q- ifort_CICLAD ##   CICLAD    IPSL ##
#-Q- ifort_CICLAD ######################
#-Q- ifort_CICLAD #PBS -N METRICS_ORCHIDEE
#-Q- ifort_CICLAD #PBS -m a
#-Q- ifort_CICLAD #PBS -j oe
#-Q- ifort_CICLAD #PBS -q std
#-Q- ifort_CICLAD #PBS -S /bin/ksh
#-Q- default #!/bin/ksh
#-Q- default ##################
#-Q- default ## DEFAULT HOST ##
#-Q- default ##################

#**************************************************************
# Author: Sebastien Denvil
# Contact: Sebastien.Denvil__at__ipsl.jussieu.fr
# $Revision:: 1525                                     $ Revision of last commit
# $Author:: cetlod                                     $ Author of last commit
# $Date:: 2020-04-27 10:39:22 +0200 (Mon, 27 Apr 2020) $ Date of last commit
# IPSL (2006)
#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
#
#**************************************************************

#set -eu
#set -vx

date

#-Q- ada export OMP_NUM_THREADS=1

#D- Task type DO NOT CHANGE (computing, post-processing or checking)
TaskType=post-processing

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

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

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

#set -vx

# ------------------------------------------------------------------
# Test if all was right before proceeding further
# ------------------------------------------------------------------
IGCM_debug_Verif_Exit

if [ ${StandAlone} = true ] ; then
    CARD_DIR=${SUBMIT_DIR}
else
    CARD_DIR=${RUN_DIR_PATH}
    IGCM_sys_Get_Master ${SUBMIT_DIR}/config.card ${RUN_DIR_PATH}
    IGCM_sys_Get_Master ${SUBMIT_DIR}/run.card    ${RUN_DIR_PATH}
    IGCM_sys_Get_Master ${SUBMIT_DIR}/COMP        ${RUN_DIR_PATH}
    IGCM_sys_Get_Master ${SUBMIT_DIR}/POST        ${RUN_DIR_PATH}
fi

#==================================
# First of all
#
# Read libIGCM compatibility version in config.card
# Read UserChoices section
# Read Ensemble section
# Read Post section
# Define all netcdf output directories
#==================================
IGCM_config_CommonConfiguration ${CARD_DIR}/config.card

# ------------------------------------------------------------------
# Activate BigBrother so as to supervise this job
# ------------------------------------------------------------------
IGCM_debug_BigBro_Initialize

#==================================
# Then set up several directories
#==================================
R_OUT_SRF=${R_SAVE}/SRF
DIR_OUT=${DIR_OUT:=SE}
RUN_DIR=${RUN_DIR_PATH}
IGCM_sys_MkdirArchive ${R_FIGR}
IGCM_sys_MkdirWork ${RUN_DIR}
IGCM_sys_Cd ${RUN_DIR}
IGCM_sys_MkdirWork METRICS/SE_${YEARS}

#==================================
# Get the file
#==================================
IGCM_sys_Get ${R_OUT_SRF}/Analyse/${DIR_OUT}/${config_UserChoices_JobName}_SE_${YEARS}_1M_sechiba_history.nc ${RUN_DIR}

#==================================
# Get the fileCompute gpptot
#==================================
IGCM_sys_ncap2 -s 'gpptot = gpp.total($veget)/1000' ${config_UserChoices_JobName}_SE_${YEARS}_1M_sechiba_history.nc tmpout.nc
IGCM_sys_ncatted -O -a units,gpptot,o,c,"kg m-2 s-1" tmpout.nc
IGCM_sys_Mv tmpout.nc ${config_UserChoices_JobName}_SE_${YEARS}_1M_sechiba_history.nc

#==================================
# Group of metrics we will compute
#==================================
GroupsOfMetrics='ORCHIDEE_PCMDI'
MP_Version=v3

#==================================
# SET UP THE PATH (wont be done in libIGCM_sys because setup_runtime.sh is way too intrusive)
#==================================
. ${PCMDI_MP}/work_install_${MP_Version}/PCMDI_METRICS/bin/setup_runtime.sh

for metricsGroupName in ${GroupsOfMetrics} ; do

  #==================================
  # Appropriate configuration template
  #==================================
  IGCM_sys_Cp ${PCMDI_MP}/work_install_${MP_Version}/PCMDI_METRICS/doc/parameter_files/input_parameters_${metricsGroupName}_template.py template.py

  #==================================
  # Configure template for histmth.nc
  #==================================
  IGCM_comp_modifyFile template.py path_and_filename \'./${config_UserChoices_JobName}_SE_${YEARS}_1M_sechiba_history.nc\'
  IGCM_comp_modifyFile template.py filename \'${config_UserChoices_JobName}_SE_${YEARS}_1M_sechiba_history.nc\'
  IGCM_comp_modifyFile template.py attributes_provided \'IPSL_Extended\'
  IGCM_comp_modifyFile template.py Login  \'${LOGIN}\'
  IGCM_comp_modifyFile template.py model_versions \[\'${config_UserChoices_TagName}\'\]
  IGCM_comp_modifyFile template.py experiment  \'${config_UserChoices_ExperimentName}\'
  IGCM_comp_modifyFile template.py realization \'${config_UserChoices_JobName}\'
  IGCM_comp_modifyFile template.py project_id \'IPSL\-${config_UserChoices_SpaceName}\'
  IGCM_comp_modifyFile template.py ModelFreeSpace "\"${config_UserChoices_LongName}\""
  IGCM_comp_modifyFile template.py case_id \'${metricsGroupName}\'
  IGCM_comp_modifyFile template.py obs_data_path  \'${R_IN}/ReferenceDatasets/climatos\'
  IGCM_comp_modifyFile template.py metrics_output_path \'${RUN_DIR}/METRICS/SE_${YEARS}\'
  IGCM_comp_modifyFile template.py model_clims_interpolated_output \'${RUN_DIR}\'

  #==================================
  # Compute metrics
  #==================================
  python ${PCMDI_MP}/work_install_${MP_Version}/PCMDI_METRICS/bin/pcmdi_metrics_driver.py -p template.py

  #==================================
  # Export metrics to mongoDB system
  #==================================
  IGCM_debug_sendAMQP_Metrics METRICS/SE_${YEARS}/${metricsGroupName} ${metricsGroupName}

done

#==================================
# Save files
#==================================
IGCM_sys_Put_Dir METRICS ${R_FIGR}
IGCM_sys_Rm -Rf METRICS
IGCM_sys_Rm ${config_UserChoices_JobName}_SE_${YEARS}_1M_histmth.nc

# Clean RUN_DIR_PATH (necessary for cesium and titane only)
IGCM_sys_RmRunDir -Rf ${RUN_DIR_PATH}

# ------------------------------------------------------------------
# Finalize BigBrother to inform that the jobs end
# ------------------------------------------------------------------
IGCM_debug_BigBro_Finalize
