#!/bin/ksh
###############################################################################
#                            RUN NEMO-ECEARTH-v2.2
###############################################################################
#
#$ -N run_orca1_uncoup
#$ -cwd
#$ -S /bin/ksh
#$ -V
#$ -pe orte 36
#

# modules setup
source /opt/modules/init/ksh
module load intel/13.0.1
module load openmpi/1.6-intel # OpenMPI
#module load intel/4.0.3.008 # Intel MPI
# tell OpenMPI to only use infiniband
export OMPI_MCA_btl=sm,self,openib

# General settings

set -xuv
date

NEMO=/share/scratch/cfu/models/nemo/ecearth-v2.2
SCRIPTDIR=$NEMO/setup
PATH=${SCRIPTDIR}:${PATH}
TESTDATADIR=$NEMO/inidata/testdata
WRITINGDIR=/share/scratch/cfu/$USER/test
RUN_dir=$WRITINGDIR/run_orca1_uncoup_$JOB_ID

# Resolution dependent part

RUN_length_prev=0
FCLENGTH=48
NPROCNEMOX=6
NPROCNEMOY=6
NPROCNEMO=$((NPROCNEMOX*NPROCNEMOY))
STARTDATE=19900101

# Bin path

NEMOEXE=$NEMO/bin/opa_exe.ORCA1_OASIS3.$NPROCNEMOX.$NPROCNEMOY

# Run setup

if [[ ${RUN_length_prev} = '0' ]]; then
  mkdir -p ${RUN_dir} || true
  rm -fr ${RUN_dir}/*
else
  rm -f ${RUN_dir}/anaisout
  rm -f ${RUN_dir}/mesh_zgr.nc ${RUN_dir}/mesh_hgr.nc ${RUN_dir}/mask.nc ${RUN_dir}/damping.coeff_[0-9][0-9][0-9][0-9].nc
fi
cd ${RUN_dir}

if [[ ${RUN_length_prev} != '0' ]]; then
  setupnemoorca1.ksh -g ORCA1 -e ${NEMO} -l ${FCLENGTH} -p ${RUN_length_prev} -0 ${TESTDATADIR} -1 ${STARTDATE}
else
  setupnemoorca1.ksh -g ORCA1 -e ${NEMO} -l ${FCLENGTH} -0 ${TESTDATADIR} -1 ${STARTDATE}
fi

FORCING=/share/data/cfu/common/forcings/ERAint
YEAR=`echo $STARTDATE | cut -c1-4`

ln -sf ${FORCING}/precip14_ERAINTERIM-ORCA1_${YEAR}.nc precip14_forcing.nc
ln -sf ${FORCING}/precip_ERAINTERIM-ORCA1_${YEAR}.nc precip_forcing.nc
ln -sf ${FORCING}/q2_ERAINTERIM-ORCA1_${YEAR}.nc q2_forcing.nc
ln -sf ${FORCING}/qlw_ERAINTERIM-ORCA1_${YEAR}.nc qlw_forcing.nc
ln -sf ${FORCING}/qsw_ERAINTERIM-ORCA1_${YEAR}.nc qsw_forcing.nc
ln -sf ${FORCING}/snow14_ERAINTERIM-ORCA1_${YEAR}.nc snow14_forcing.nc
ln -sf ${FORCING}/snow_ERAINTERIM-ORCA1_${YEAR}.nc snow_forcing.nc
ln -sf ${FORCING}/t2_ERAINTERIM-ORCA1_${YEAR}.nc t2_forcing.nc
ln -sf ${FORCING}/u10_ERAINTERIM-ORCA1_${YEAR}.nc u10_forcing.nc
ln -sf ${FORCING}/v10_ERAINTERIM-ORCA1_${YEAR}.nc v10_forcing.nc

# Execution

ulimit -s unlimited

t1=$(date +%s)

mpirun -np ${NPROCNEMO} ${NEMOEXE}

t2=$(date +%s)

tr=$(date -d "0 -$t1 sec + $t2 sec" +%T)
date
