#!/bin/bash --login
#PBS -N AMM7-v0-4
#PBS -l select=65
#PBS -l walltime=48:00:00
#PBS -q long
##PBS -l select=8
##PBS -l walltime=00:20:00
##PBS -q short
#PBS -A n01-PML

# NOTE:
# the simulation year must be passed to this pbs script at runtime
# by defning externally the variable y0 passing it to the pbs script,
# e.g.:
# "qsub -v y0=1980 MultipleYearHindcast.pbs"
#

set -u

NNODES=64
#NNODES=8
NCORES=$(( NNODES*24 ))

echo "Submitting year" $y0 "..."

ystart=1981
yend=2010

RUNDIR=/work/n01/n01/momme/AMM7-v0-erosion
INPUTS=/work/n01/n01/momme/AMM7-INPUTS

cd $RUNDIR

stopflag=0

#general time setting:
dt=300 #time step
nsstart=$(date -d "$ystart-01-01 00:00:00 GMT" +%s) #seconds since EPOCH for total simulation start

for iy in {1..4} #annual increment 
do
 if [ $y0 -le $yend ]
 then
  #cleanup:
  ./annualclean.sh

  #link annual forcing files:
  ./setAnnualLinks.sh $y0

  for m0 in {1..12} #Month loop
  do
   ./monthlyclean.sh
   #Compute previous and next month:
   mp=$(( $m0 + 1 ))
   if [ $mp -eq 13 ]
   then
      mp=1
      yp=$(( y0 + 1 ))
   else
      yp=$y0
   fi
   mm=$(( $m0 - 1 ))
   if [ $mm -eq 0 ]
   then
      mm=12
      ym=$(( y0 - 1 ))
   else
      ym=$y0
   fi

   m0str=$(printf %02d $m0)
   mpstr=$(printf %02d $mp)
   mmstr=$(printf %02d $mm)

   echo "Launching" $y0 $m0 "..."

   ARCHIVEDIR=$RUNDIR/$y0/$m0str

   #restarts:
   rm -rf restart.nc restart_trc.nc restart_[0-9]???.nc restart_trc_[0-9]???.nc
   if [ $y0 -eq $ystart ] && [ $m0 -eq 1 ]
   then
      ln -sf $INPUTS/RESTARTS/restart.nc restart.nc
      ln -sf $INPUTS/RESTARTS/restart_trc.full.nc restart_trc.nc
      rst=0
      euler=0
   else
      if [ -s $RUNDIR/$ym/$mmstr/restart_0000.nc ]
      then 
         ln -sf $RUNDIR/$ym/$mmstr/restart_????.nc .
      elif [ -s $RUNDIR/$ym/$mmstr/restart.nc ]
      then
         ln -sf $RUNDIR/$ym/$mmstr/restart.nc .
      else
         stopflag=1
      fi
      if [ -s $RUNDIR/$ym/$mmstr/restart_trc_0000.nc ]
      then 
         ln -sf $RUNDIR/$ym/$mmstr/restart_trc_????.nc .
      elif [ -s $RUNDIR/$ym/$mmstr/restart_trc.nc ]
      then
         ln -sf $RUNDIR/$ym/$mmstr/restart_trc.nc .
      else
         stopflag=1
      fi
      rst=2
      euler=0
   fi

   #compute run-time:
   case $m0 in
      4|6|9|11) nit=$(( 86400*30/dt )) ;;
      2) if [ $(( y0 % 4 )) -ne 0 -o $(( y0 % 100)) -eq 0 -a $(( $y0 % 400 )) -ne 0 ]; then nit=$(( 86400*28/dt )); else nit=$(( 86400*29/dt )); fi ;;
      *) nit=$(( 86400*31/dt )) ;;
   esac

   #compute start iteration and end iteration:
   ns0=$(date -d "$y0-${m0str}-01 00:00:00 GMT" +%s) #seconds since EPOCH for this chunk
   n0=$(( ns0 - nsstart ))
   n0=$(( n0 / dt + 1 ))
   nend=$(( n0 + nit -1 ))
   d0=$y0${m0str}01
   if [ $y0 -lt 1990 -o $y0 -gt 2009 ]
   then
      lclim=.true.
   else
      lclim=.false.
   fi
   cat namelist.template \
                | sed "s,__DATE0__,$d0,g" \
                | sed "s,__RST__,$rst,g" \
                | sed "s,__N0__,$n0,g" \
                | sed "s,__NEND__,$nend,g" \
                | sed "s,__LCLIM__,$lclim,g" \
                | sed "s,__EULER__,$euler,g" \
                > namelist_cfg
   cat namelist_top.template \
                | sed "s,__RST__,$rst,g" \
                > namelist_top_cfg

   if [ $stopflag -ne 0 ]
   then
      echo "Not ready to launch. Forced exit."
      exit
   fi

   echo "Launching $y0 $m0 at $(date +%s) seconds since 1970-01-01 00:00:00 on $NCORES cores"

   aprun -b -n 1 -N 1 -S 1 ./xios_server.exe : -n $NCORES -N 24 ./nemo.exe
   #aprun -n 192 -N 24 ./nemo.exe

   echo "Finished $y0 $m0 at $(date +%s) seconds since 1970-01-01 00:00:00"

   #prepare archive directory
   mkdir -p $ARCHIVEDIR
   #prepare restart files:
   for file in amm7_*${nend}_restart_????.nc
   do
      fn=${file: -7:4} #file number
      mv $file $ARCHIVEDIR/restart_$fn.nc
   done
   for file in amm7_*${nend}_restart_trc_????.nc
   do
      fn=${file: -7:4} #file number
      mv $file $ARCHIVEDIR/restart_trc_$fn.nc
   done

   #move outputs:
   mv amm7_1d_${d0}_[1-2]???????_*.nc $ARCHIVEDIR
   mv amm7_1m_${d0}_[1-2]???????_ptrc_T.nc $ARCHIVEDIR
   mv amm7_1m_${d0}_[1-2]???????_grid_?.nc $ARCHIVEDIR
   bzip2 ocean.output && mv -f ocean.output.bz2 $ARCHIVEDIR
   mv -f time.step $ARCHIVEDIR

   #archive:
   fname=$y0/$m0str
   echo "Archiving in $fname ..."
   qsub -v folder=$fname archiveFolder.pbs

  done #end of month loop
  fi
  y0=$(( y0+1 ))
done #end of annual loop


#resubmit:
if [ $y0 -le $yend ]
then
   echo 'Submitting' $y0 '...'
   qsub -v year=$yp QuadrAnnualChainHindcast.pbs
   echo "Done."
else
   echo "All done."
fi
