#!/bin/ksh

# CALLS: rebuild_nemo.exe

#set -ax
usage ()
{
   echo
   echo "  NEMO Rebuild"
   echo "  ************"
   echo
   echo "  usage: ${0##*/} [-l -t -c -m -n] filebase ndomain [rebuild dimensions]"
   echo
   echo "  flags:    -l                 submit to compute node"
   echo "            -t num             use num threads"
   echo "            -c num             split 4D vars into time chuncks of size num"
   echo "            -m                 force masking of global arrays (zero if no mdi)"
   echo "            -d deflate_level   deflate level for output files (key_netcdf4 only)"
   echo "            -n namelist        full path to namelist file to be created (otherwise default nam_rebuild+_process_id is used)"
   echo "            -r memory          Memory to request on compute node including units (Default = 10Gb)"
   echo
   exit 1
}

while getopts c:n:t:d:r:lm opt
do
  case ${opt} in
      l) 
         BATCH="yes"
         BATCH_CMD="qsub" 
         ARCH="XC40_METO"
         echo "Submitting job to compute node"
      ;;
      t)
         OMP_NUM_THREADS=${OPTARG}
      ;;
      c)
         NCHUNKSIZE=${OPTARG}
      ;;
      m) 
         NMASK="TRUE"
         echo "mask"
      ;;
      d)
         DEFLATE=${OPTARG}
      ;;
      n)
         nam_rebuild=${OPTARG}
      ;;
      r)
         MEMORY=${OPTARG}
      ;;
  esac
done
shift $(expr ${OPTIND} - 1)

if [[ $# -lt 2 ]] ; then
   usage
fi

script_dir=$(dirname $0)

file=$1
ndomain=$2
DIM1=$3
DIM2=$4
export OMP_NUM_THREADS=${OMP_NUM_THREADS:-1}
nam_rebuild=${nam_rebuild:-nam_rebuild_$$}
MEMORY=${MEMORY:-10Gb}

#Find out the maximum number of files that can be opened and increase if necessary)
nopen=$(ulimit -n)
if [[ $ndomain -gt $nopen ]] ; then
  nopen=$((ndomain+2))
fi

if [[ -n ${DIM1} && -n ${DIM2} ]] ; then
   dim_str=" dims '${DIM1}','${DIM2}'"
   dims="dims='${DIM1}','${DIM2}'"
fi

echo "file ${file},  num_domains ${ndomain}, num_threads ${OMP_NUM_THREADS}${dim_str}"

cat > $nam_rebuild << EOC
&nam_rebuild
filebase='${file}'
ndomain=${ndomain}
EOC
if [[ -n ${dims} ]] ; then
   echo ${dims} >> $nam_rebuild
fi
if [[ -n ${NCHUNKSIZE} ]] ; then
   echo " nchunksize=${NCHUNKSIZE}" >> $nam_rebuild
fi
if [[ -n ${NMASK} ]] ; then
   echo " l_maskout=.true." >> $nam_rebuild
fi
if [[ -n ${DEFLATE} ]] ; then
   echo " deflate_level=${DEFLATE}" >> $nam_rebuild
fi

echo "/" >> $nam_rebuild

if [[ ${BATCH} == "yes" ]] ; then
   batch_file=rebuild_nemo_batch_${ARCH}

   #Create a modified local copy of the batch submission file
   #The process ID is appended to the end of the file name so it is unique
   cat ${script_dir}/BATCH_TEMPLATES/${batch_file} | sed -e"s/NTHREADS/${OMP_NUM_THREADS}/" \
      -e"s/MEMORY/${MEMORY}/" \
      -e"s:INDIR:${script_dir}:" \
      -e"s/NOPEN/${nopen}/" \
      -e"s/NAMELIST/${nam_rebuild}/" > ${batch_file}_$$.sh
   
   #Submit the job
   $BATCH_CMD ${batch_file}_$$.sh

else
   ulimit -n $nopen
   ${script_dir}/rebuild_nemo.exe $nam_rebuild
fi

