#!/bin/ksh
###############################################################################
#                             RUN NEMO
###############################################################################
#
#$ -N run_nemo-v3.3-orca1
#$ -cwd
#$ -S /bin/ksh
#$ -V
#$ -pe orte 36
#

# General settings
source /opt/modules/init/ksh
module load intel/12.1
module load openmpi/1.6-intel
# tell OpenMPI to only use infiniband
export OMPI_MCA_btl=sm,self,openib

date
set -xuv

NEMO=/scratch/cfu/models/nemo/v3.3
TESTDATADIR=$NEMO/inidata
FORCED=DFS4.1
FORCING=/share/data/cfu/common/nemo_forcing/$FORCED
WRITINGDIR=/scratch/cfu/$USER/test
RUN_dir=$WRITINGDIR/run_nemo-v3.3-orca1_$JOB_ID

NPROCNEMO=36
YEAR=1990

mkdir -p ${RUN_dir} || true
cd ${RUN_dir}

cp ${NEMO}/bin/nemo.exe opa.xx
chmod 770 opa.xx

cp ${NEMO}/setup/namelist.v3.3.default.${FORCED} namelist
cp ${NEMO}/setup/namelist_ice_lim2.v3.3.default namelist_ice

cp ${TESTDATADIR}/EMPave_old.dat EMPave_old.dat
cp ${TESTDATADIR}/geothermal_heating.nc geothermal_heating.nc
cp ${TESTDATADIR}/coordinates_ukorca1.nc coordinates.nc
cp ${TESTDATADIR}/bathy_meter_050308_UKMO.nc bathy_meter.nc
cp ${TESTDATADIR}/basinmask_050308_UKMO.nc basinmasks.nc
cp ${TESTDATADIR}/runoff_1m_ORCA1.nc runoff_1m_nomask.nc
cp ${TESTDATADIR}/sst_1m_ORCA1.nc sst_1m.nc
cp ${TESTDATADIR}/sss_1m_ORCA1.nc sss_1m.nc
cp ${TESTDATADIR}/dust_1m_ORCA1.nc dust_1m.nc
cp ${TESTDATADIR}/ahmcoef ahmcoef

cp ${TESTDATADIR}/bathy_level46_050308_UKMO.nc bathy_level.nc
cp ${TESTDATADIR}/potemp_1m_z46_nomask.nc data_1m_potential_temperature_nomask.nc
cp ${TESTDATADIR}/salin_1m_z46_nomask.nc data_1m_salinity_nomask.nc

ln -s ${TESTDATADIR}/weights_grid02_bicubic_orca1.nc weights_grid02_bicubic_orca1.nc
ln -s ${TESTDATADIR}/weights_grid02_bilinear_orca1.nc weights_grid02_bilinear_orca1.nc
ln -s ${TESTDATADIR}/weights_grid03_bicubic_orca1.nc weights_grid03_bicubic_orca1.nc
ln -s ${TESTDATADIR}/weights_grid03_bilinear_orca1.nc weights_grid03_bilinear_orca1.nc

case $FORCED in
 DFS4.1)
  ln -s ${FORCING}/${YEAR}_1d_${FORCED}_lwdn.nc qlw_core.nc
  ln -s ${FORCING}/${YEAR}_1d_${FORCED}_swdn.nc qsw_core.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_q2m.nc q2_core.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_t2m.nc t2_core.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_u10.nc u10_core.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_v10.nc v10_core.nc
  ln -s ${FORCING}/${YEAR}_1m_${FORCED}_precip.nc precip_core.nc
  ln -s ${FORCING}/${YEAR}_1m_${FORCED}_snow.nc snow_core.nc
 ;;
 DFS4.3)
  ln -s ${FORCING}/qlw_${FORCED}_${YEAR}.nc qlw_core.nc
  ln -s ${FORCING}/qsw_${FORCED}_${YEAR}.nc qsw_core.nc
  ln -s ${FORCING}/q2_${FORCED}_${YEAR}.nc q2_core.nc
  ln -s ${FORCING}/t2_${FORCED}_${YEAR}.nc t2_core.nc
  ln -s ${FORCING}/u10_${FORCED}_${YEAR}.nc u10_core.nc
  ln -s ${FORCING}/v10_${FORCED}_${YEAR}.nc v10_core.nc
  ln -s ${FORCING}/precip_${FORCED}_${YEAR}.nc precip_core.nc
  ln -s ${FORCING}/snow_${FORCED}_${YEAR}.nc snow_core.nc
 ;;
 *)
 echo "$FORCED is not available"
 ;;
esac

for ((l=0; l<$NPROCNEMO; l++)); do
 ln -sf coordinates.nc coordinates_$(printf "%03d" $l).nc
 ln -sf geothermal_heating.nc geothermal_heating_$(printf "%03d" $l).nc
done

ls -alF

ulimit -s unlimited

time mpirun opa.xx

date
