#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
# 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/gcmrun

  # 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
    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
    mkdir NEXT_CPL_RESTART_ARC
    #tar cf NEXT_CPL_RESTART_ARC $tarlist || cpl_bail "Unable to create coupler restart archive"
    cp $tarlist NEXT_CPL_RESTART_ARC || cpl_bail "Unable to create coupler restart directory"
    if [ -n "$model_rs" ]; then
      # Assume we are running from gcmrun
      save NEXT_CPL_RESTART_ARC ${model_rs}cplrs
    elif [ -n "$model" ]; then
      # Assume we are running from gcmjcl
      save NEXT_CPL_RESTART_ARC ${model}cplrs
    fi
    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 #################"
