#!/bin/bash
##SBATCH --partition=zen4
#SBATCH --partition=zen16
#SBATCH --ntasks=24
##SBATCH --ntasks=4
##SBATCH --ntasks=1
#SBATCH --time=08:00:00
##SBATCH --mem=6G
#SBATCH --mem=64G

# job information
cat << EOF
------------------------------------------------------------------
Job submit on $SLURM_SUBMIT_HOST by $SLURM_JOB_USER
JobID=$SLURM_JOBID Running_Node=$SLURM_NODELIST
Node=$SLURM_JOB_NUM_NODES Task=$SLURM_NTASKS
------------------------------------------------------------------
EOF
# Begin of section with executable commands


########################################################
## standard run 121x121x61 nodes=1:ppn=4,mem=4gb,vmem=6gb
########################################################
### large-domain run 321x321x61 (OK but slower) nodes=1:ppn=16,mem=16gb,vmem=24gb
########################################################
### large-domain run 321x321x61 nodes=1:ppn=32,mem=32gb,vmem=48gb
########################################################



step=1
#step=4
fold=$PWD

#### PREAMBLE
ulimit -s unlimited

#### STEP 1
if [[ ${step} -le 1 ]] ; then
  cd $fold/gcm
  #
  ./get_startday_from_namelist.sh | ./launch_gcm
  #
  if [[ $SLURM_NTASKS -gt 24 ]] ; then
    echo "--- Total CPU count is above 24"
    echo "--- For the standard GCM resolution, this is too much"
    echo "--- So we run the GCM only with 24 processors"
    $WHERE_MPI/mpirun -np 24 gcm.e > log_gcm
  else
    $WHERE_MPI/mpirun gcm.e > log_gcm
  fi
  #
  cd $fold/prep
  echo 1 | create_readmeteo.exe
  readmeteo.exe < readmeteo.def
fi


##### STEP 2
if [[ ${step} -le 2 ]] ; then
  cd $fold/geogrid
  \rm geo_em.*
  geogrid.exe
  #
  cd $fold/metgrid
  \rm met_em.*
  metgrid.exe
fi

##### STEP 3
if [[ ${step} -le 3 ]] ; then
  cd $fold/
  \rm wrfi* wrfb*
  real.exe
fi

##### STEP 4
if [[ ${step} -le 4 ]] ; then
  rm -rf $fold/run_$SLURM_JOBID
  mkdir $fold/run_$SLURM_JOBID
  mv rsl.error.0000 $fold/run_$SLURM_JOBID/real_rsl.error.0000
  mv rsl.out.0000 $fold/run_$SLURM_JOBID/real_rsl.out.0000
  cp -rfL  $fold/*.def           $fold/run_$SLURM_JOBID/
  cp -rfL  $fold/wrf.exe*        $fold/run_$SLURM_JOBID/
  cp -rfL  $fold/namelist.input  $fold/run_$SLURM_JOBID/
  cp -rfL  $fold/namelist.wps    $fold/run_$SLURM_JOBID/
  cp -rfL  $fold/wrfi*           $fold/run_$SLURM_JOBID/
  cp -rfL  $fold/wrfb*           $fold/run_$SLURM_JOBID/
  cd $fold/run_$SLURM_JOBID/
  ln -sf $fold/data_physics ./
  $WHERE_MPI/mpirun wrf.exe
  #gdb wrf.exe -ex=run
fi
