#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
# Coupler post processing after execution
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

echo " "
echo "#################### Start coupler post-processing #################"

cpl_bail_prefix="coupler postlude"

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

# Optionally define a prefix for all ssh commands
# to reroute through a head node when required
ROUTE_SSH=''

# this_mach will be a known alias (or possibly the actual machine name)
this_mach=$this_host
case $this_mach in
    c1*) this_mach=spica
         ROUTE_SSH="ssh spica"
         ;;
    c2*) this_mach=hadar
         ROUTE_SSH="ssh hadar"
         ;;
   ib3*) this_mach=pollux
         ROUTE_SSH="ssh pollux"
         ;;
 joule*) this_mach=joule
         ROUTE_SSH="ssh joule"
         ;;
esac

if [ -n "$model" ]; then
  # If model is defined then we are likely running from gcmjcl

  # Extract year and month from the file name
  # The variable model is a common prefix for file names
  # This assumes that model has the form *_YYYY_mMM_ or *_YYYY_mMM
  cpl_model_mon=`echo $model |awk -F_ '{if(length($NF)>0){i=NF}else{i=NF-1};
                                        if(i>0){if($i~/^[mM][0-9][0-9]$/){print substr($i,2)}}}' -`
  cpl_model_year=`echo $model|awk -F_ '{if(length($NF)>0){i=NF}else{i=NF-1}; i=i-1;
                                        if(i>0){if($i~/^[0-9][0-9]*$/){print $i}}}' -`
  cpl_model_runid=`echo $model|awk -F_ '{if(length($NF)>0){i=NF}else{i=NF-1}; m=i; y=i-1;
                                         pfx=sprintf("%s_",$1); sfx=sprintf("_%s_%s",$y,$m);
                                         n=index($0,sfx)-length(pfx)-1;
                                         print substr($0,length(pfx)+1,n)}' -`

  echo "===== cpl_postlude:  model = $model  cpl_model_year = $cpl_model_year  cpl_model_mon = $cpl_model_mon  cpl_model_runid = $cpl_model_runid   runid = $runid"

  # If runid is not defined then use the value found in the model prefix
  [ -z "$runid" ] && runid=$cpl_model_runid

  # Define boolean flags first_month_in_curr_job and last_month_in_curr_job
  # that will be used to determine if the current invocation is at the
  # beginning or end of a multi month job submission
  # This is only meaningful when running from gcmjcl
  if [ -n "$month_counter" -a -n "$months" ]; then
    # months and month_counter are defined
    # These variables are define when running in gcmjcl mode
    # Assume month_counter counts down from the value of months to 1
    if [ $months -gt 1 ]; then
      if [ $month_counter -eq $months ]; then
        # This should be the first month of the current submission job
        first_month_in_curr_job=1
        last_month_in_curr_job=0
      elif [ $month_counter -eq 1 ]; then
        # This should be the last month of the current submission job
        first_month_in_curr_job=0
        last_month_in_curr_job=1
      else
        first_month_in_curr_job=0
        last_month_in_curr_job=0
      fi
    else
      # months must be 1 otherwise
      first_month_in_curr_job=1
      last_month_in_curr_job=1
    fi
  else
    # months and month_counter are not defined. Set first/last to False
    first_month_in_curr_job=0
    last_month_in_curr_job=0
  fi
  [ $first_month_in_curr_job -eq 1 ] &&
    echo "===== cpl_postlude: First month in current job  year = $cpl_model_year  month = $cpl_model_mon"
  [ $last_month_in_curr_job -eq 1 ]  &&
    echo "===== cpl_postlude:  Last month in current job  year = $cpl_model_year  month = $cpl_model_mon"

  # Process the coupler history file
  if [ -s cpl_history.nc ]; then
    # Create a run time diagnostics file from the history file
    #xxx ncwa -O -h -I -x -v mask_atm,mask_ocn -a lat_a,lon_a,nlat_o,nlon_o cpl_history.nc curr_cpl_rtd.nc

    # If cpl_rtd is true then execute the cpl run time diagnostics
    cpl_rtd=${cpl_rtd:=on}
    ToF cpl_rtd

    # If cpl_new_rtd is true (on) then do not append the coupler run time diagnostics
    # to any existing file from the previous coupler restart archive
    # By default this is false (off), so by default coupler rtd files will be appended
    # to existing coupler rtd files
    cpl_new_rtd=${cpl_new_rtd:=off}
    ToF cpl_new_rtd
    # Always append after the first month in the job
    [ $first_month_in_curr_job -eq 0 ] && eval cpl_new_rtd\=0

    if [ $cpl_rtd -eq 1 ]; then
      # Collect files required to create the coupler run time diagnostics
      cpl_rtd_exec=${cpl_rtd_exec:=uncs_coupler_rtd_exe_v02}
      [ -s coupler_rtd_exe ] || access coupler_rtd_exe $cpl_rtd_exec nocp=off
      [ -s orca_mesh_mask ]  || access orca_mesh_mask nemo_3.4_orca1_mesh_mask.nc
      [ -s areacella ]       || access areacella areacella_fx_canesm2_historical_r0i0p0.nc
      [ -s agcm_fmask ]      || access agcm_fmask t63_to_orca1_fland.nc

      # Time average the coupler history file
      # cdo monmean cpl_history.nc cplhist

      # Make a copy of the coupler history file for use with coupler_rtd_exe
      cp cpl_history.nc cplhist

      # Generate the coupler run time diagnostics
      # coupler_rtd_exe expects year and month as cmd line args
      # coupler_rtd_exe uses files orca_mesh_mask, agcm_fmask, areacella, cplhist
      # coupler_rtd_exe will create coupler_rtd.nc
      # If coupler_rtd.nc exists then it will be appended to
      if [ $cpl_new_rtd -ne 1 ]; then
        # Copy coupler_rtd.nc from the coupler restart archive
        # This means that the new rtd will be appended to the previous rtd file
        [ -s cplrs_cpl_rtd.nc ] && mv cplrs_cpl_rtd.nc coupler_rtd.nc
      fi
      coupler_rtd_exe $cpl_model_year $cpl_model_mon
      rm -f cplhist
      mv coupler_rtd.nc curr_cpl_rtd.nc
    fi

#    if [ -s cplrs_cpl_rtd.nc -a -s curr_cpl_rtd.nc -a $cpl_new_rtd -eq 0 ]; then
#      # A coupler rtd file was extracted from the restart used at the beginning of this
#      # model run segment. Append the current rtd file to the previous rtd file.
#      #
#      # Note:
#      # ncrcat will rebase time to the origin found in the first file on the command line
#      # ncrcat will always append to the existing file, inserting duplicate times at the
#      # current record dimension, if there are any repeating times in subsequent files
#      #
#      # An alternative is to use "cdo mergetime f1 f2 fout" which will insert
#      # duplicate times at adjacent locations in the record dimension.
#      # This cdo operator will also recognize the env variable SKIP_SAME_TIME and if it
#      # is set to 1 then only the first occurrence of duplicate times will be inserted.
#      # e.g.
#      #   export SKIP_SAME_TIME=1
#      #   cdo mergetime cplrs_cpl_rtd.nc curr_cpl_rtd.nc cpl_rtd_${stamp}.nc
#      # However, this is not what we want here. We want the last occurrence to be kept.
#      # Also CDO will always append to the global history attribute if it is present.
#      # CDO has no option to turn off this "feature" which is undesireable in this situation.
#      # Therefore ncrcat -h ... is the better way to go here, at least for now.
#      #
#      ncrcat -O -h cplrs_cpl_rtd.nc curr_cpl_rtd.nc cpl_rtd_${stamp}.nc
#      mv cpl_rtd_${stamp}.nc curr_cpl_rtd.nc
#    fi

    if [ -s curr_cpl_rtd.nc ]; then
      # Copy this rtd file to Victoria
      chmod ug+rw curr_cpl_rtd.nc
      # local_rtd must be a full pathname
      local_rtd=`pwd`/curr_cpl_rtd.nc
      remote_rtd=ftp1.cccma.ec.gc.ca:/misc/pubplots/CanESM_run_plots/rtdfiles_coupler/sc_${runid}_coupler_rtd.nc
      # If this copy fails then silently ignore the error and continue
      # In most cases this file will simply get copied on the next month of the run
      # Even if this is the last job in a longer string, the
      # rtd file will always be saved to the restart tarball
      $ROUTE_SSH scp -p $local_rtd $remote_rtd || :
    fi

    save_cplhist_tavg=${save_cplhist_tavg:=off}
    ToF save_cplhist_tavg
    if [ $save_cplhist_tavg -eq 1 ]; then
      # Create a time averaged version of the coupler history file and save it to RUNPATH
      rm -f local_cplhist_tavg.nc
      cdo monmean cpl_history.nc local_cplhist_tavg.nc

      cpl_hist_saved_name=${model}cplhist-tavg.nc
      release OLDcplhist
      access OLDcplhist $cpl_hist_saved_name na
      save local_cplhist_tavg.nc $cpl_hist_saved_name
      release local_cplhist_tavg.nc
      delete OLDcplhist na
    fi

    save_cplhist=${save_cplhist:=on}
    ToF save_cplhist
    if [ $save_cplhist -eq 1 ]; then
      # Save the coupler history file to RUNPATH
      cpl_hist_saved_name=${model}cplhist.nc
      echo "===== cpl_postlude: cpl_hist_saved_name = $cpl_hist_saved_name"
      release OLDcplhist
      access OLDcplhist $cpl_hist_saved_name na
      save cpl_history.nc $cpl_hist_saved_name
      release cpl_history.nc
      delete OLDcplhist na
    fi
  fi

  save_cplrs=${save_cplrs:=on}
  ToF save_cplrs
  if [ $save_cplrs -eq 1 ]; then
    # Create a tar archive containing coupler related files
    tarlist=''
    cp -f cpl_restart_out.nc cplrs_cpl_restart.nc || :
    [ -s cplrs_cpl_restart.nc ] && tarlist="$tarlist cplrs_cpl_restart.nc"
    cp -f cpl_main cplrs_cpl_main || :
    [ -s cplrs_cpl_main ] && tarlist="$tarlist cplrs_cpl_main"
    [ -s cplrs_cpl_main ] && chmod 755 cplrs_cpl_main
    cp -f curr_cpl_rtd.nc cplrs_cpl_rtd.nc || :
    [ -s cplrs_cpl_rtd.nc ] && tarlist="$tarlist cplrs_cpl_rtd.nc"
    cp -f cpl_stats cplrs_cpl_stats || :
    [ -s cplrs_cpl_stats ] && tarlist="$tarlist cplrs_cpl_stats"
    cp -f nl_coupler_par cplrs_nl_coupler_par || :
    [ -s cplrs_nl_coupler_par ] && tarlist="$tarlist cplrs_nl_coupler_par"
    cp -f cpl_prelude cplrs_cpl_prelude || :
    [ -s cplrs_cpl_prelude ] && tarlist="$tarlist cplrs_cpl_prelude"
    cp -f cpl_postlude cplrs_cpl_postlude || :
    [ -s cplrs_cpl_postlude ] && tarlist="$tarlist cplrs_cpl_postlude"
    cp -f coupler_env cplrs_coupler_env || :
    [ -s cplrs_coupler_env ] && tarlist="$tarlist cplrs_coupler_env"
    cp -f cpl_access_log cplrs_access_log || :
    [ -s cplrs_access_log ] && tarlist="$tarlist cplrs_access_log"
    cp -f cpl_atm_grid.nc cplrs_atm_grid.nc || :
    [ -s cplrs_atm_grid.nc ] && tarlist="$tarlist cplrs_atm_grid.nc"
    cp -f cpl_ocn_grid.nc cplrs_ocn_grid.nc || :
    [ -s cplrs_ocn_grid.nc ] && tarlist="$tarlist cplrs_ocn_grid.nc"

    # It is an error to have found no files to put into the archive
    [ -z "$tarlist" ] && cpl_bail "Unable to find any restart related files"

    # Ensure sane permissions on files that go into the archive
    chmod 644 $tarlist

    # Save the restart archive to RUNPATH
    rm -f NEXT_CPL_RESTART_ARC
    tar cf NEXT_CPL_RESTART_ARC $tarlist || cpl_bail "Unable to create coupler restart archive"
    save NEXT_CPL_RESTART_ARC ${model}cplrs.tar
    release NEXT_CPL_RESTART_ARC
  fi

  save_interim_cplrs=${save_interim_cplrs:=off}
  ToF save_interim_cplrs
  if [ $save_interim_cplrs -eq 1 ]; then
    # If any interim coupler restart files are created then save them to RUNPATH
    eval cplrs_patt=cplrs-\*${runid}.nc
    found_file=`(ls -1 $cplrs_patt || : ) 2>/dev/null`
    if [ -n "$found_file" ]; then
      for fname in $found_file; do
        save $fname $fname
        release $fname
      done
    fi
  fi

else
  cpl_bail "model must be defined"
fi

echo " "
echo "#################### Stop coupler post-processing #################"
