#!/bin/sh
#SBATCH -J igsmcont
#SBATCH -p sched_mit_darwin2    # which queue ("partition")
#SBATCH -N 2                    # number of nodes to use (each has 16 cores)
#SBATCH -n 30
#SBATCH --constraint=centos7
#SBATCH --ntasks-per-node 16    # total number of cores to run on
#SBATCH --mem-per-cpu 8000      # memory per core needed (16-core nodes have 64GB)
#SBATCH --time 12:00:00         # run no longer than 16 hours
#SBATCH -x node[665-672]

module use /home/jahn/software/modulefiles

module load intel/2018-01
module load impi/2018-01
module load jahn/netcdf-fortran/4.4.5_intel-2018-01


mpirun ./mitgcmuv_31_16_3_dar6_igsmfix

################################################################
# resubmit if there is a pickup and a data.$enditer
#
istartiter=`cat data | awk '/^ nIter0/{s=gensub(/^.*nIter0.*= */,"","g",$0);s=gensub(/\.*,/,"","g",s);printf(s)}'`
startiter=`echo $istartiter | awk '{printf("%010d",$1)}'`
enditer=`cat data | awk '/^ nTimesteps/{s=gensub(/^.*nTimesteps.*= */,"","g",$0);s=gensub(/\.*,/,"","g",s);printf("%010d",s+'$istartiter')}'`
echo "startiter: $startiter"
echo "enditer: $enditer"
# for global-file output:
#if [ -f pickup.$enditer.data ]; then
# for tile-file output:
if [ -f pickup_ptracers.$enditer.data ]; then
  # move away output
  mkdir output.$startiter
  mv STD* output.$startiter/
  mv darwin*  output.$startiter/
  cp -p data output.$startiter/

  # resubmit
  if [ -e data.$enditer -a ! -e STDOUT.0000 ]; then
    ln -sf data.$enditer data
    echo "Resubmitting"
    sbatch -N 2 -n 30 qsubresub.sh

  fi
fi
################################################################
