#!/usr/bin/env bash

#PBS -q main
#PBS -r y
#PBS -W group_list=cclm
#PBS -l walltime=23:50:00
#PBS -l select=1:ncpus=24:ompthreads=24:mem=63000mb
#PBS -M pierre-yves.barriat@uclouvain.be
#PBS -m abe

exec > ${PBS_O_WORKDIR-$PWD}/${PBS_JOBNAME-"local"}_${PBS_JOBID-"id"}.log
echo "------------------ Job Info --------------------"
echo "jobid : $PBS_JOBID"
echo "jobname : $PBS_JOBNAME"
echo "job type : $PBS_ENVIRONMENT"
echo "submit dir : $PBS_O_WORKDIR"
echo "queue : $PBS_O_QUEUE"
echo "user : $PBS_O_LOGNAME"
echo "threads : $OMP_NUM_THREADS"

set -ue

# Configure and load modules
module purge
module load netCDF-Fortran/4.5.3-gompi-2020b

exp="NE4_SP"
cores="24"
path_out="/scratch/ucl/elic/pbarriat/nemo/archive/${exp}"
path_exe="/home/ucl/elic/pbarriat/modeles/nemo/nemo_dev_4/tools/REBUILD_NEMO"

###

files=${path_out}/output/*
dir=`echo $files | cut -d' ' -f1`
cd ${dir}
#
file=`ls ${exp}_1m_*_grid_T_*.nc | tail -n 1`
xios_split=`echo ${file%.nc} | rev | cut -d'_' -f1 | rev`
xios_split=`expr $xios_split + 1`
#
for dir in ${files}
do
  cd ${dir}
  #
  file=`ls ${exp}_1m_*_grid_T_0000.nc`
  to_year=`echo ${file%_grid_T_0000.nc} | rev | cut -d'_' -f1 | rev`
  from_year=`echo ${file%_grid_T_0000.nc} | rev | cut -d'_' -f2 | rev`
  #
  echo "In ${dir} :"
  echo "Processing ${exp}_1m_${from_year}_${to_year} ..."

  rm -f nam_rebuild_*
  rm -f ${exp}_1m_${from_year}_${to_year}_grid_T.nc 
  rm -f ${exp}_1m_${from_year}_${to_year}_grid_U.nc 
  rm -f ${exp}_1m_${from_year}_${to_year}_grid_V.nc 
  rm -f ${exp}_1m_${from_year}_${to_year}_SBC.nc 
  rm -f ${exp}_1m_${from_year}_${to_year}_icemod.nc  

  if [ -s ${exp}_1m_${from_year}_${to_year}_grid_T_0000.nc ]
  then
    [[ ! -s ${exp}_1m_${from_year}_${to_year}_grid_T.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${from_year}_${to_year}_grid_T ${xios_split}
  fi
  if [ -s ${exp}_1m_${from_year}_${to_year}_grid_U_0000.nc ]
  then
    [[ ! -s ${exp}_1m_${from_year}_${to_year}_grid_U.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${from_year}_${to_year}_grid_U ${xios_split}
  fi  
  if [ -s ${exp}_1m_${from_year}_${to_year}_grid_V_0000.nc ]
  then
    [[ ! -s ${exp}_1m_${from_year}_${to_year}_grid_V.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${from_year}_${to_year}_grid_V ${xios_split}
  fi  
  if [ -s ${exp}_1m_${from_year}_${to_year}_SBC_0000.nc ]
  then
    [[ ! -s ${exp}_1m_${from_year}_${to_year}_SBC.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${from_year}_${to_year}_SBC ${xios_split}
  fi  
  if [ -s ${exp}_1m_${from_year}_${to_year}_icemod_0000.nc ]
  then
    [[ ! -s ${exp}_1m_${from_year}_${to_year}_icemod.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${from_year}_${to_year}_icemod ${xios_split}
  fi 

done

###

files=`ls ${path_out}/restart/ | tail -n 1`
dir=${path_out}/restart/${files}
cd ${dir}

file=`ls ${exp}_*_restart_ice* | tail -n 1`
nemo_split=`echo ${file%.nc} | rev | cut -d'_' -f1 | rev`
nemo_split=`expr $nemo_split + 1`
restart_step=`echo ${file%.nc} | rev | cut -d'_' -f4 | rev`

echo "In ${dir} processing ${exp}_${restart_step}_restart_ice ..."

rm -f ${exp}_${restart_step}_restart_ice.nc
rm -f ${exp}_${restart_step}_restart_oce.nc

if [ -s ${exp}_${restart_step}_restart_ice_0000.nc ]
then
  [[ ! -s ${exp}_${restart_step}_restart_ice.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_${restart_step}_restart_ice ${nemo_split}
fi
if [ -s ${exp}_${restart_step}_restart_oce_0000.nc ]
then
  [[ ! -s ${exp}_${restart_step}_restart_oce.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_${restart_step}_restart_oce ${nemo_split}
fi

echo ----- REBUILD post finished
