#!/bin/bash

module purge
#module load releases/2018b netCDF-Fortran/4.4.4-intel-2018b
module load netCDF-Fortran/4.5.3-gompi-2020b

exp="NE4_08"
start_year="1960"
cores="8"
xios_split="24"
path_out="/scratch/ucl/elic/pbarriat/nemo/archive/${exp}/output"
#path_exe="/home/ucl/elic/pbarriat/modeles/nemo/4.0.6/tools/REBUILD_NEMO"
path_exe="/home/ucl/elic/pbarriat/modeles/nemo/nemo_dev_4/tools/REBUILD_NEMO"

files=${path_out}/0*
year=${start_year}
for dir in ${files}
do
  cd ${dir}
  echo "In ${dir} processing ${exp}_1m_${year}0101_${year}1231 ..."

  #[[ ! -s ${exp}_1m_${year}0101_19610101_grid_T.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_19610101_grid_T ${xios_split}
  #mv ${exp}_1m_${year}0101_19610101_grid_T.nc ${exp}_1m_${year}0101_${year}1231_grid_T.nc
  #[[ ! -s ${exp}_1m_${year}0101_19610101_grid_U.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_19610101_grid_U ${xios_split}
  #mv ${exp}_1m_${year}0101_19610101_grid_U.nc ${exp}_1m_${year}0101_${year}1231_grid_U.nc
  #[[ ! -s ${exp}_1m_${year}0101_19610101_grid_V.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_19610101_grid_V ${xios_split}
  #mv ${exp}_1m_${year}0101_19610101_grid_V.nc ${exp}_1m_${year}0101_${year}1231_grid_V.nc
  #[[ ! -s ${exp}_1m_${year}0101_19610101_SBC.nc ]]    && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_19610101_SBC    ${xios_split}
  #mv ${exp}_1m_${year}0101_19610101_SBC.nc ${exp}_1m_${year}0101_${year}1231_SBC.nc
  #[[ ! -s ${exp}_1m_${year}0101_19610101_icemod.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_19610101_icemod ${xios_split}
  #mv ${exp}_1m_${year}0101_19610101_icemod.nc ${exp}_1m_${year}0101_${year}1231_icemod.nc

  [[ ! -s ${exp}_1m_${year}0101_${year}1231_grid_T.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_${year}1231_grid_T ${xios_split}
  [[ ! -s ${exp}_1m_${year}0101_${year}1231_grid_U.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_${year}1231_grid_U ${xios_split}
  [[ ! -s ${exp}_1m_${year}0101_${year}1231_grid_V.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_${year}1231_grid_V ${xios_split}
  [[ ! -s ${exp}_1m_${year}0101_${year}1231_SBC.nc ]]    && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_${year}1231_SBC    ${xios_split}
  [[ ! -s ${exp}_1m_${year}0101_${year}1231_icemod.nc ]] && ${path_exe}/rebuild_nemo -t ${cores} ${exp}_1m_${year}0101_${year}1231_icemod ${xios_split}
  year=$((${year}+1))
  cd ..
done
