#####################################################
# 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 TEST_NAME
#
#  run a job of config GYRE with 1 processor 		   test REPRODUCTINILITY
#  $ ./fcm_job.sh input_GYRE.cfg                     1     REPRO
#
#  run a job of config ORCA2_LIM_PISCES with 8 processors test RESTARTABILITY
#  $ ./fcm_job.sh input_ORCA2_LIM_PISCES.cfg         8	   RESTART
#
#
# 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 RUN"


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
       
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}/.
	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 ${INPUT_DIR} ; tar xvof ${INPUT_DIR}/*.tar ; gunzip -f *gz
	fi
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"
    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
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 *_ptrc_*)" ] && mv *_ptrc_*.nc ${OUTPUT_DIR}/.
[ -n "$(ls *_diad_*)" ] && mv *_diad_*.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}/.
################################################################

################################################################
# SAVE IN NEMO_VALIDATION tree
export LANG=en_US 
cd ${CONFIG_DIR}
cd ../
REVISION_NB=`svn info | grep -i "Revision:" | sed -e "s/ //" | cut -d ":" -f 2`
if [ ${#REVISION_NB} -eq 0 ]
then
	echo "some problems with svn info command"
	echo "some problems with svn info command" >> ${SETTE_DIR}/output.sette
	${REVISION_NB}=`date +%Y%m%d`
	echo "put in ${REVISION_NB} date"
	echo "put in ${REVISION_NB} date" >> ${SETTE_DIR}/output.sette
else
echo "value of revision number of NEMOGCM: ${REVISION_NB}"
fi
cd ${NEMO_VALIDATION_DIR}
export TEST_NAME=$3
`mkdir -p ${NEMO_VALIDATION_DIR}/W${NEW_CONF}/${CMP_NAM}/${REVISION_NB}/${TEST_NAME}`
NEMO_VALID=${NEMO_VALIDATION_DIR}/W${NEW_CONF}/${CMP_NAM}/${REVISION_NB}/${TEST_NAME}
if [ -d ${NEMO_VALID} ] ; then
	echo "created ${NEMO_VALID} directory"   >> ${SETTE_DIR}/output.sette
else 
	echo "problems in creating ${NEMO_VALID} directory"   >> ${SETTE_DIR}/output.sette
	echo "EXIT,"
	exit 1
fi
[ -f ${OUTPUT_DIR}/*ocean.output ] && mv ${OUTPUT_DIR}/*ocean.output ${NEMO_VALID}/.
[ -f ${OUTPUT_DIR}/*solver.stat ] && mv ${OUTPUT_DIR}/*solver.stat ${NEMO_VALID}/.
[ -n "`ls ${OUTPUT_DIR}/*restart*`" ] && mv ${OUTPUT_DIR}/*restart* ${NEMO_VALID}/.
if [ -n "$(ls ${NEMO_VALID}/*solver*)" ] ; then
	echo "moved solver.stat in ${NEMO_VALID} directory"  >> ${SETTE_DIR}/output.sette
	echo "moved solver.stat in ${NEMO_VALID} directory"  
else
	echo "problem in looking for solver.stat file in ${NEMO_VALID} directory"  >> ${SETTE_DIR}/output.sette
	echo "solver.stat IS NOT in ${NEMO_VALID} directory" 
fi
if [ -n "$(ls ${NEMO_VALID}/*ocean.output*)" ] ; then
	echo "moved ocean.output in ${NEMO_VALID} directory"  >> ${SETTE_DIR}/output.sette
	echo "moved ocean.output in ${NEMO_VALID} directory" 
else
	echo "problem in looking for ocean.output file in ${NEMO_VALID} directory"  >> ${SETTE_DIR}/output.sette
	echo "ocean.output IS NOT in ${NEMO_VALID} directory" 
fi
if [ -n "`ls ${NEMO_VALID}/*restart*`" ] ; then
	echo "moved restart files in ${NEMO_VALID} directory"  >> ${SETTE_DIR}/output.sette
	echo "moved restart files in ${NEMO_VALID} directory" 
else
	echo "problem in looking for restart files in ${NEMO_VALID} directory"  >> ${SETTE_DIR}/output.sette
	echo "restart files ARE NOT in ${NEMO_VALID} directory" 
fi
################################################################
