#!/bin/bash
######################################################
# 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
#
#-
#
# SET INPUT
# get the input tarfile if needed
tar_file=$(sed -ne "1,1p" $1)
if [ "$(cat $1 | grep -c ".tar" )" -ne 0 ] ; then
	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 
   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)
	cp ${INPUT_DIR}/1_namelist_ice_lim2 ${INPUT_DIR}/1_namelist_ice 
fi
################################################################

################################################################
# RUN OPA
cd ${INPUT_DIR}
NB_PROC=$2
 if [ ${NB_PROC} == 1 ] ; then
	./opa
else
# 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
################################################################

################################################################
## -- Save output files

#- Save ocean output files
[ ! -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}/.
[ -f restart* ] && cp restart* ${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}/.
################################################################

