#!/bin/ksh
###############################################################################
#                           RUN NEMO-ECEARTH-v2.2
###############################################################################
#
#
#@ job_name         = run_orca1_uncoup
#@ wall_clock_limit = 00:30:00
#@ output           = run_orca1_uncoup_%j.out
#@ error            = run_orca1_uncoup_%j.err
#@ total_tasks      = 36
#@ initialdir       = .
#@ tasks_per_node   = 4

# General settings

set -xuv
date

NEMO=/gpfs/projects/ecm86/models/nemo/ecearth-v2.2
SCRIPTDIR=$NEMO/setup
PATH=${SCRIPTDIR}:${PATH}
TESTDATADIR=$NEMO/inidata/testdata
WRITINGDIR=/gpfs/scratch/ecm86/$USER/test
RUN_dir=$WRITINGDIR/run_orca1_uncoup_$SLURM_JOBID

# 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=/gpfs/projects/ecm86/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 -c 1000000

time srun -n $NPROCNEMO -l ${NEMOEXE} 
date
