#####################################################
# Author : Simona Flavoni for NEMO
# Contact : sflod@locean-ipsl.upmc.fr
#
# Some scripts called by sette.sh 
# fcm_job.sh   : simple job to run NEMO with fcm 
######################################################
#set -x
set -o posix
#set -u
#set -e
#+
#
# ================
# fcm_job.sh
# ================
#
# --------------------------
# Simple job for NEMO tests 
# --------------------------
#
# SYNOPSIS
# ========
#
# ::
#
#  $ ./fcm_job.sh INPUT_FILE_CONFIG_NAME NUMBER_PROC TEST_NAME
#
#
# DESCRIPTION
# ===========
#
# Simple job for SET TESTS for NEMO (SETTE)
# 
#   get input files (if needed) : tar file  
#  (note this job needs to have an input_CONFIG.cfg in which can be found input tar file name)
#
#   runs job in interactive or batch mode : all jobs using 1 process are run interactive, and all MPP jobs are
#
#   run in batch (MPI_INTERACT="no") or interactive (MPI_INTERACT="yes") see sette.sh and BATCH_TEMPLATE directory
#
#   and call post_test_tidyup function (that moves in NEMO_VALIDATION_DIR solver.stat, tracer.stat (for LOBSTER & PISCES) & ocean.output)
#
# EXAMPLES
# ========
#
# ::
#
#  $ ./fcm_job.sh INPUT_FILE_CONFIG_NAME NUMBER_PROC TEST_NAME
#
#  run a job of config GYRE with 1 processor SHORT test ( 5 days )
#  $ ./fcm_job.sh input_GYRE.cfg 1 SHORT
#
#  run a job of config ORCA2_LIM_PISCES	with 8 processors test RESTARTABILITY
#  $ ./fcm_job.sh input_ORCA2_LIM_PISCES.cfg 8 LONG
#
#
# TODO
# ====
#
# option debug
#
#
# EVOLUTIONS
# ==========
#
# $Id$
#
#
#
#   * creation
#
#-
#

usage=" Usage : ./fcm_job.sh input_CONFIG_NAME.cfg  NUMBER_OF_PROCS TEST_NAME"
usage=" example : ./fcm_job.sh input_ORCA2_LIM_PISCES.cfg 8 SHORT"


minargcount=3
        if [ ${#} -lt ${minargcount} ]
        then
                echo "not enought arguments for fcm_job.sh script"
                echo "control number of argument of fcm_job.sh in sette.sh"
                echo "${usage}"
        exit 1
        fi
        unset minargcount
	if [ ! -f ${SETTE_DIR}/output.sette ] ; then
	        touch ${SETTE_DIR}/output.sette
	fi
       
#
# set and export TEST_NAME. It will be used within the post_test_tidyup function
#
export TEST_NAME=$3

# export EXE_DIR. This directory is used to execute model 
#
export EXE_DIR
#
#
echo "date: `date`" >> ${SETTE_DIR}/output.sette
echo "" >> ${SETTE_DIR}/output.sette
echo "running config: ${NEW_CONF}" >> ${SETTE_DIR}/output.sette
echo "" >> ${SETTE_DIR}/output.sette
echo "list of cpp_keys: " >> ${SETTE_DIR}/output.sette
echo "`more ../CONFIG/${NEW_CONF}/cpp_${NEW_CONF}.fcm`" >> ${SETTE_DIR}/output.sette
echo "" >> ${SETTE_DIR}/output.sette
echo "compiling with: ${CMP_NAM}" >> ${SETTE_DIR}/output.sette
echo "" >> ${SETTE_DIR}/output.sette
echo "executing script : \"fcm_job $@\" " >> ${SETTE_DIR}/output.sette
echo "            " >> ${SETTE_DIR}/output.sette

################################################################
# SET INPUT
# get the input tarfile if needed
tar_file=$(sed -ne "1,1p" $1)
if [ "$(cat $1 | grep -c ".tar" )" -ne 0 ] ; then
	echo "looking for tar file" >> ${SETTE_DIR}/output.sette
	echo "            " >> ${SETTE_DIR}/output.sette
	cp ${FORCING_DIR}/${tar_file} ${INPUT_DIR}/.
	echo "file in ${INPUT_DIR}"
	if [ ! -f ${INPUT_DIR}/${tar_file} ] ; then 
		echo "PROBLEM during copy of tar file" >> ${SETTE_DIR}/output.sette
		echo "tar file IS NOT present in ${INPUT_DIR} directory " >> ${SETTE_DIR}/output.sette
		echo "            " >> ${SETTE_DIR}/output.sette
		echo "PROBLEM during copy of tar file" 
		exit 1
	else  
		cd ${EXE_DIR} ; tar xvof ${INPUT_DIR}/*.tar ; gunzip -f *gz
	fi
fi 

if [ ! -f ${EXE_DIR}/namelist_ice ] ; then 
if [ -f ${EXE_DIR}/namelist_ice_lim2 ] ; then 
   echo "choosing for namelist ice " >> ${SETTE_DIR}/output.sette
   echo "            " >> ${SETTE_DIR}/output.sette
   cp ${EXE_DIR}/namelist_ice_lim2 ${EXE_DIR}/namelist_ice
elif  [ -f ${EXE_DIR}/namelist_ice_lim3 ] ; then 
   cp ${EXE_DIR}/namelist_ice_lim3 ${EXE_DIR}/namelist_ice
fi
fi


if [ "$(cat ${CONFIG_DIR}/${NEW_CONF}/cpp_${NEW_CONF}.fcm | grep -c "agrif" )" -ne 0 ] ; then
	#- Namelist for ocean and ice (agrif fine grid)
   	echo "choosing for namelist for AGRIF" >> ${SETTE_DIR}/output.sette
   	echo "            " >> ${SETTE_DIR}/output.sette
	cp ${EXE_DIR}/1_namelist_ice_lim2 ${EXE_DIR}/1_namelist_ice 
fi
################################################################

################################################################
# RUN OPA
cd ${EXE_DIR}
NB_PROC=$2
if [ ! -r ${EXE_DIR}/opa ]
    then
    echo "executable opa does not exist"
    echo "executable opa does not exist, exit"  >> ${SETTE_DIR}/output.sette
    exit 1
fi
if [ ${NB_PROC} == 1 ] ; then
    echo "running opa" >> ${SETTE_DIR}/output.sette
    echo "            " >> ${SETTE_DIR}/output.sette
    ./opa
#
# Tidy out output from this test and populate the NEMO_VALIDATION_DIR tree
#
    post_test_tidyup
else
    echo "running opa in MPI" >> ${SETTE_DIR}/output.sette
    echo "            " >> ${SETTE_DIR}/output.sette

    if [ ${MPI_INTERACT} == "yes" ] ; then
	#
	# example for brodie (NEC SX8) machine
	#	mpirun -np ${NB_PROC} opa
	# example for dedale machine
	#	mpirun --mca btl self,tcp -np ${NB_PROC} opa
	# example for vargas (IBM Power6) machine
	mpiexec -n ${NB_PROC} opa
	#
	post_test_tidyup
    fi
#
# example for NOCS Altix system using PBS batch submission (requires ${SETTE_DIR}/sette_batch_template file)
#
    if [ ${MPI_INTERACT} == "no" ] ; then
		case ${COMPILER} in 
			ALTIX_NAUTILUS_MPT)
                                NB_REM=$( echo $NB_PROC | awk '{print $1 % 4}')
		        	if [ ${NB_REM} == 0 ] ; then
					# number of processes required is an integer multiple of 4
					#
					NB_NODES=$( echo $NB_PROC | awk '{print $1 / 4}')
				else
					#
					# number of processes required is not an integer multiple of 4
					# round up the number of nodes required.
					#
					NB_NODES=$( echo $NB_PROC | awk '{printf("%d",$1 / 4 + 1 )}')
	       			fi
				;;
			*)
				NB_NODES=${NB_PROC}
				;;

		esac
#
# Pass settings into job file by using sed to edit predefined strings
#
        cat ${SETTE_DIR}/job_batch_template | sed -e"s/NODES/${NB_NODES}/" -e"s/NPROCS/${NB_PROC}/" \
             -e"s:DEF_SETTE_DIR:${SETTE_DIR}:" -e"s:DEF_INPUT_DIR:${INPUT_DIR}:" \
             -e"s:DEF_EXE_DIR:${EXE_DIR}:" \
             -e"s:DEF_CONFIG_DIR:${CONFIG_DIR}:" \
             -e"s:DEF_NEMO_VALIDATION:${NEMO_VALIDATION_DIR}:" -e"s:DEF_NEW_CONF:${NEW_CONF}:" \
             -e"s:DEF_CMP_NAM:${CMP_NAM}:" -e"s:DEF_TEST_NAME:${TEST_NAME}:" > run_sette_test.job
#
# submit job to batch system 
	${BATCH_COMMAND} run_sette_test.job 
    fi

fi
