#!/bin/bash
#     keyword :: canesm_nemo_rbld
# description :: Rebuild NEMO files.
set -a
. betapath2  # sets path for new package (required until old package is retired)

#  * ........................... Parmsub Parameters ............................

 runid="runid"; year="yyyy"; mon="mm";
 year_rtdiag_start="yyyy"; months_gcm="mm";
 crawork="crawork";
 model1="mc_${runid}_${year}_m${mon}";
 nemo_jpnij="nnn";
 run_start_year="yyy"
 run_start_month="mm"

 if [ "$year" = "yyyy" -a "$mon" = "mm" ] ; then
   # if not defined, it is assume a one time job at the beginning of the run.
   mon=$(printf "%02d" `expr $run_start_month - $months_gcm + 12`)
   year=`expr $run_start_year - 1`
 fi

 username="username"; user="USER"; nqsprfx=""; nqsext="_${runid}_${year}_m${mon}";
 lopgm="lopgm"; stime="7200"; memory1="4gb";

#  * ............................ Condef Parameters ............................

 noprint=on
 nextjob=on
 debug=on

 nemo_save_hist=1
 canesm_nemo_rbld_save_hist=1
 nemo_rbld_hist_file_suffix_list=""
 nemo_rbld_hist_file_freq_list=""
 nemo_carbon=1
 nemo_trc=1
 jpnij=$nemo_jpnij

#  * ............................. Deck Definition .............................
jobname=rbld-nemo; time=$stime ; memory=$memory1
.   comjcl.cdk
cat > Execute_Script <<'end_of_script'
# Rebuild NEMO tiles on PPP. NCS, April 2018.

# Add job specific functions, definitions etc
  : ; . local_defs

  # Load netcdf libs
  : ; . ssmuse-sh -d /fs/ssm/hpco/tmp/eccc/201402/04/intel-2016.1.150

  # Figure out NEMO dates. We are operating 1 month at a time in coupled mode.
  # Determine run start and stop dates in YYYYMMDD format

  # Currently start_day is always 1
  start_day=1

  # Determine the number of days in the last month of the run
  monlast=`echo $mon $months_gcm | awk '{printf "%02d",$1+$2-1}'`
  if [ $monlast -gt 12 ] ; then
    monlast=`echo $monlast | awk '{printf "%02d",$1-12}'`
    yearlast=`echo $year | awk '{printf "%04d",$1+1}'`
  else
    yearlast=$year
  fi
  case $monlast in
    1|01) stop_day=31 ;;
    2|02) stop_day=28 ;;
    3|03) stop_day=31 ;;
    4|04) stop_day=30 ;;
    5|05) stop_day=31 ;;
    6|06) stop_day=30 ;;
    7|07) stop_day=31 ;;
    8|08) stop_day=31 ;;
    9|09) stop_day=30 ;;
      10) stop_day=31 ;;
      11) stop_day=30 ;;
      12) stop_day=31 ;;
       *) bail "stop_mon=$stop_mon is out of range" ;;
  esac

  # First and last calendar date YYYYMMDD determined from user input start/stop
  start_date=`echo $year $mon $start_day|awk '{printf "%4.4d%2.2d%2.2d",$1,$2,$3}' -`
  stop_date=`echo $yearlast $monlast $stop_day|awk '{printf "%4.4d%2.2d%2.2d",$1,$2,$3}' -`

  # History file suffix and frequency lists. Must be the same lengths. I.E. repeat filenames for multiple freq.
  nemo_rbld_hist_file_suffix_list=${nemo_rbld_hist_file_suffix_list}
  nemo_rbld_hist_file_freq_list=${nemo_rbld_hist_file_freq_list}

  # Get bash arrays with the freq and suffix list
  nemo_hist_file_suffix_list_array=($nemo_rbld_hist_file_suffix_list)
  nemo_hist_file_freq_list_array=($nemo_rbld_hist_file_freq_list)

  # Check that the lists are the same length. Use this number to loop below..
  n_suffix=${#nemo_hist_file_suffix_list_array[@]}
  n_freq=${#nemo_hist_file_freq_list_array[@]}
  if [ "$n_suffix" -ne "$n_freq" ]; then
     bail "ERROR: nemo_hist_file_suffix_list_array and nemo_hist_file_freq_list_array MUST have the same number of elements."
  fi

  # The rebuild executable must be accessable at run time and namelist files present in cwd
  [ -s rebuild_nemo.exe ] || access rebuild_nemo.exe rebuild_nemo.exe nocp=no na
  [ -s rebuild_nemo.exe ] || bail "Unable to access rebuild_nemo.exe"

  # Can use Open MP. But probably only running on one processor.
  export OMP_NUM_THREADS=2

  # the tmpdir we are working in
  wrkdir=`pwd`

  # A list of directories to delete from RUNPATH at the end
  dir_del_list=""

  if [ "$canesm_nemo_rbld_save_hist" -eq "1" ] ; then
     # Loop over the list of history files/freqs to rebuild
     for i in $(seq 0 $(($n_suffix-1))); do
        cd $wrkdir
        # Get the directory with the tiles, and extract them
        sfx=${nemo_hist_file_suffix_list_array[$i]}
        freq=${nemo_hist_file_freq_list_array[$i]}
        lsfx=`echo "$sfx" | tr '[:upper:]' '[:lower:]'`
        indir=${model1}_${freq}_${lsfx}
        access $indir $indir nocp=off
        dir_del_list+=" $indir"
        cd $indir

        # Define the pattern, get the exe, do the rbld, and save.
        pfx=${runid}_${freq}_${start_date}_${stop_date}_$sfx
        ln -s ../rebuild_nemo.exe .
        rebuild_nemo_tiles
        ncsave=${model1}_${freq}_${sfx}.nc
        save ${pfx}.nc $ncsave

        # Move back up and cleanup
        cd $wrkdir
        rm -rf $indir
      done
    fi

    # Rebuild the mesh_mask file created by the model, if present
    # Access the directory, if it is successfull, cd into it
    indir=${model1}_mesh_mask
    access $indir $indir nocp=off na
    if [ -s "$indir" ] ; then
       cd $indir
       dir_del_list+=" $indir"

       # Define the pattern and do the rebld
       pfx=mesh_mask
       ln -s ../rebuild_nemo.exe .
       rebuild_nemo_tiles
       ncsave=${model1}_${pfx}.nc
       save ${pfx}.nc $ncsave

       # cleanup
       cd $wrkdir
       rm -rf $indir
     fi

  # Rebuild the restart files. These will be tarred below and saved alltogether, as
  # is custom for NEMO rs' historically.

  # Access the restart directory, and cd into it.
  modellast="mc_${runid}_${yearlast}_m${monlast}";
  inrs=${modellast}_nemors
  access $inrs $inrs nocp=off
  dir_del_list+=" $inrs"
  cd $inrs
  ln -s ../rebuild_nemo.exe .
  # Figure out the last time step, which is needed for the rs tile names.
  nn_itend=`cat rs_time.step`
  end_step=$(echo $nn_itend | awk '{printf "%8.8d",$1}')

  # The physics rs file
  pfx=${runid}_${end_step}_restart

  if [ ! -s "${pfx}_0000.nc" ]; then
     # Look for files, which might not have the same name as the run
     found_rs=`(ls -1 *_restart_[0-9][0-9][0-9][0-9].nc || : ) 2>/dev/null`
     # Rename them
     for rsfile in $found_rs; do
        sfx=`echo $rsfile|sed 's/^.*\(_[0-9][0-9][0-9][0-9].nc\).*$/\1/'`
        [ "x$sfx" = "x$rsfile" ] && sfx=`echo $rsfile|sed 's/^.*\(\.nc\).*$/\1/'`
        mv $rsfile $pfx$sfx
      done
     # an already rebuilt rs with a different name
     found_rs=`(ls -1 *_restart.nc || : ) 2>/dev/null`
     [ -z "$found_rs" ] || mv $found_rs $pfx.nc
  fi

  # Check if the RS is already rebuilt, in which case do nothing.
  fnpatt=${pfx}_0000.nc
  if [ -s "$fnpatt" ]; then
     rebuild_nemo_tiles
  fi

  # The ice rs file
  pfx=${runid}_${end_step}_restart_ice
  if [ ! -s "${pfx}_0000.nc" ]; then
     # Look for files, which might not have the same name as the run
     found_rs=`(ls -1 *_restart_ice_[0-9][0-9][0-9][0-9].nc || : ) 2>/dev/null`
     # Rename them
     for rsfile in $found_rs; do
        sfx=`echo $rsfile|sed 's/^.*\(_[0-9][0-9][0-9][0-9].nc\).*$/\1/'`
        [ "x$sfx" = "x$rsfile" ] && sfx=`echo $rsfile|sed 's/^.*\(\.nc\).*$/\1/'`
        mv $rsfile $pfx$sfx
      done
     # an already rebuilt rs with a different name
     found_rs=`(ls -1 *_restart_ice.nc || : ) 2>/dev/null`
     [ -z "$found_rs" ] || mv $found_rs $pfx.nc
  fi

  fnpatt=${pfx}_0000.nc
  if [ -s "$fnpatt" ]; then
     rebuild_nemo_tiles
  fi

  # The trc rs file
  pfx=${runid}_${end_step}_restart_trc
  if [ ! -s "${pfx}_0000.nc" ]; then
     # Look for files, which might not have the same name as the run
     found_rs=`(ls -1 *_restart_trc_[0-9][0-9][0-9][0-9].nc || : ) 2>/dev/null`
     # Rename them
     for rsfile in $found_rs; do
        sfx=`echo $rsfile|sed 's/^.*\(_[0-9][0-9][0-9][0-9].nc\).*$/\1/'`
        [ "x$sfx" = "x$rsfile" ] && sfx=`echo $rsfile|sed 's/^.*\(\.nc\).*$/\1/'`
        mv $rsfile $pfx$sfx
      done
     # an already rebuilt rs with a different name
     found_rs=`(ls -1 *_restart_trc.nc || : ) 2>/dev/null`
     [ -z "$found_rs" ] || mv $found_rs $pfx.nc
  fi

  fnpatt=${pfx}_0000.nc
  if [ -s "$fnpatt" ]; then
     rebuild_nemo_tiles
  fi

  # Create the tar archive for the nemors and save it.
  release rebuild_nemo.exe $rbnl_file
  tar -cf ${inrs}.tar *
  # preserve time stamp from restart.nc
  touch -r ${runid}_${end_step}_restart.nc $fnpatt ${inrs}.tar
  save ${inrs}.tar ${inrs}.tar || bail "Could not save ${indir}.tar"
  cd $wrkdir
  rm -rf $inrs

  # since everything has gone successfully, cleanup tile directories from RUNPATH
  mkdir cleanup
  cd cleanup
  for fil in $dir_del_list; do
     access xxx-to-del $fil
     delete xxx-to-del
  done
  cd $wrkdir
  rm -rf cleanup

end_of_script

cat >local_defs << 'end_local_defs'
  rebuild_nemo_tiles(){
     # Function to rebuild NEMO tiles, for files matching
     # the pattern in $pfx. The number of tiles is specified
     # as $ndomain. It is assumed the rebuild_nemo.exe is locally
     # available.

     fnpatt=${pfx}_0000.nc
     ndomain=`ls -l ${pfx}_[0-9]*.nc |wc -l`
     if [ -s "$fnpatt" ]; then
        # Create a namelist file for use with this rebuild
        rbnl_file=nam_rebuild
        echo "&nam_rebuild"           > $rbnl_file
        echo "    filebase='${pfx}'" >> $rbnl_file
        echo "    ndomain=$ndomain"    >> $rbnl_file
        echo "/"                     >> $rbnl_file

        # Do the rbld
        [ -s rebuild_nemo.exe ] && ./rebuild_nemo.exe || bail "rebuild_nemo.exe not available"
        [ -s ${pfx}.nc ] || bail "Failed to rebuild file ${pfx}.nc"
	
	# preserve time stamp from tiles
	touch -r $fnpatt ${pfx}.nc

        # remove individual tiles after rbld
        rm -f ${pfx}_[0-9][0-9][0-9][0-9].nc
      else
          bail "No files found for pattern $pfx"
      fi
  }

  stamp=`date "+%j%H%M%S"$$`

  # this_host will simply be the output from uname
  this_host=`uname -n|awk -F\. '{print \$1}' -`

  # bail is a simple error exit routine
  error_out="${JHOME:-$HOME}/.queue/error_nemo_processing_${runid}_${this_host}_$stamp"
  [ -z "$error_out" ] || rm -f $error_out
  bail_prefix="NEMO RBLD"
  bail(){
    echo `date`" $this_host $runid --- ${bail_prefix}: $*"
    echo `date`" $this_host $runid --- ${bail_prefix}: $*" >>$error_out
    [ -n "$model" ] && echo `date`" $this_host $runid --- ${bail_prefix}: $*" >>haltit
    exit 1
  }

end_local_defs

. endjcl.cdk
#end_of_job
