#!/bin/ksh
###############################################################################
#                             RUN NEMO
###############################################################################
#
#@ job_name         = run_nemo-v3.2-orca1
#@ wall_clock_limit = 02:00:00
#@ output           = run_nemo-v3.2-orca1_%j.out
#@ error            = run_nemo-v3.2-orca1_%j.err
#@ total_tasks      = 36
#@ initialdir       = .
#@ tasks_per_node   = 4
#@ tracing          = 1
#

# General settings

date
set -xuv

NEMO=/gpfs/projects/ecm86/models/nemo/v3.2-orca1
TESTDATADIR=$NEMO/inidata
FORCED=DFS4.1
FORCING=/gpfs/projects/ecm86/common/nemo_forcing/$FORCED
WRITINGDIR=/gpfs/scratch/ecm86/$USER/test
RUN_dir=$WRITINGDIR/run_nemo-v3.2-orca1_$SLURM_JOBID

NPROCNEMO=36
YEAR=1990

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

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

cp ${NEMO}/setup/namelist.v3.2.default.${FORCED} namelist
cp ${NEMO}/setup/namelist_ice.v3.2.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_level42_050308_UKMO.nc bathy_level.nc
cp ${TESTDATADIR}/potemp05_1m_z42_nomask.nc data_1m_potential_temperature_nomask.nc
cp ${TESTDATADIR}/salin05_1m_z42_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

# following two files are needed if key_iomput ha been used
#ln -s ${TESTDATADIR}/iodef.xml iodef.xml
#ln -s ${TESTDATADIR}/xmlio_server.def xmlio_server.def

case $FORCED in
 DFS4.1)
  ln -s ${FORCING}/${YEAR}_1d_${FORCED}_lwdn.nc qlw_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_1d_${FORCED}_swdn.nc qsw_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_q2m.nc q2_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_t2m.nc t2_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_u10.nc u10_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_6hr_${FORCED}_v10.nc v10_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_1m_${FORCED}_precip.nc precip_core_y${YEAR}.nc
  ln -s ${FORCING}/${YEAR}_1m_${FORCED}_snow.nc snow_core_y${YEAR}.nc
 ;;
 DFS4.3)
  ln -s ${FORCING}/qlw_${FORCED}_${YEAR}.nc qlw_core_y${YEAR}.nc
  ln -s ${FORCING}/qsw_${FORCED}_${YEAR}.nc qsw_core_y${YEAR}.nc
  ln -s ${FORCING}/q2_${FORCED}_${YEAR}.nc q2_core_y${YEAR}.nc
  ln -s ${FORCING}/t2_${FORCED}_${YEAR}.nc t2_core_y${YEAR}.nc
  ln -s ${FORCING}/u10_${FORCED}_${YEAR}.nc u10_core_y${YEAR}.nc
  ln -s ${FORCING}/v10_${FORCED}_${YEAR}.nc v10_core_y${YEAR}.nc
  ln -s ${FORCING}/precip_${FORCED}_${YEAR}.nc precip_core_y${YEAR}.nc 
  ln -s ${FORCING}/snow_${FORCED}_${YEAR}.nc snow_core_y${YEAR}.nc
 ;;
 *)
 echo "$FORCED is not available"
 exit 1
 ;;
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 srun -n $NPROCNEMO -l opa.xx

date
