#####################################################
# 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
#
#
# DESCRIPTION
# ===========
#
#
# Simple job for SET TESTS for NEMO (SETTE)
#
#
# EXAMPLES
# ========
#
# ::
#
#  $ ./fcm_job.sh INPUT_FILE_CONFIG_NAME       NUMBER_PROC
#
#  run a job of config GYRE with 1 processor
#  $ ./fcm_job.sh input_GYRE.cfg                     1
#
#  run a job of config ORCA2_LIM_PISCES with 8 processors
#  $ ./fcm_job.sh input_ORCA2_LIM_PISCES.cfg         8
#
#
# TODO
# ====
#
# option debug
#
#
# EVOLUTIONS
# ==========
#
# $Id$
#
#
#
#   * creation
#
#-
#

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


minargcount=2
        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
	        echo "file output already present. Append comments"
 	        echo "`ls ${SETTE_DIR}/output.sette`"
	else
	        echo "file output not present"
	        touch ${SETTE_DIR}/output.sette
	        echo "`ls ${SETTE_DIR}/output.sette`"
	fi
       
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 "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}/.
	cd ${INPUT_DIR} ; tar xvf ${INPUT_DIR}/*.tar ; gunzip *gz
fi 

if [ -f ${INPUT_DIR}/namelist_ice_lim2 ] ; then 
   echo "choosing for namelist ice " >> ${SETTE_DIR}/output.sette
   echo "            " >> ${SETTE_DIR}/output.sette
   cp ${INPUT_DIR}/namelist_ice_lim2 ${INPUT_DIR}/namelist_ice
elif  [ -f ${INPUT_DIR}/namelist_ice_lim3 ] ; then 
   cp ${INPUT_DIR}/namelist_ice_lim3 ${INPUT_DIR}/namelist_ice
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 ${INPUT_DIR}/1_namelist_ice_lim2 ${INPUT_DIR}/1_namelist_ice 
fi
################################################################

################################################################
# RUN OPA
cd ${INPUT_DIR}
NB_PROC=$2
if [ ! -r ${INPUT_DIR}/opa ]
    then
    echo "executable opa does not exist"
    exit 1
fi
if [ ${NB_PROC} == 1 ] ; then
    echo "running opa" >> ${SETTE_DIR}/output.sette
    echo "            " >> ${SETTE_DIR}/output.sette
    ./opa
else
    echo "running opa in MPI" >> ${SETTE_DIR}/output.sette
    echo "            " >> ${SETTE_DIR}/output.sette
# 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
fi
################################################################
# SMALL DEBUG
# echo "control that time.step exists
if [ ! -r ${INPUT_DIR}/time.step ]
    then
    echo "file time.step does not exist"   >> ${SETTE_DIR}/output.sette
    echo "some problems during execution of model"  >> ${SETTE_DIR}/output.sette 
    exit 1
else
    echo "file time.step exists"  >> ${SETTE_DIR}/output.sette
    echo "ok model run"   >> ${SETTE_DIR}/output.sette
fi
################################################################

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

################################################################
# SAVE OUTPUT, DEBUG & RESTART FILES
echo "saving ocean & ice output, debug & restart files" >> ${SETTE_DIR}/output.sette
echo "            " >> ${SETTE_DIR}/output.sette
[ ! -d ${OUTPUT_DIR} ] && mkdir ${OUTPUT_DIR}
[ -f *ocean.output ] && mv *ocean.output ${OUTPUT_DIR}/.
[ -f *solver.stat ] && mv *solver.stat ${OUTPUT_DIR}/.
[ -n "$(ls *_grid_*)" ] && mv *_grid_*.nc ${OUTPUT_DIR}/.
[ -n "$(ls *restart*)" ] && cp *restart*.nc ${OUTPUT_DIR}/.
[ -f ice_evolu ] && mv ice_evolu ${OUTPUT_DIR}/.
[ -n "$(ls *icemod*)" ] && mv *icemod* ${OUTPUT_DIR}/.
#- Save ftrace file
[ -f ftrace.out* ] && cp ftrace.out* ${OUTPUT_DIR}/.
################################################################

