#!/bin/sh
#=======================================================================
# A parallel dump job                                  --- par_mdump ---
#=======================================================================
# keyword :: par_mdump
# short description :: dump model files in parallel with job string
set -a
. betapath2

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

username=acrnxxx; user=XXX;
runid=job000; uxxx=mc;
crawork=par_mdump

nextjob=on
noprint=on
debug=off

# Alternate path to a directory where .queue/.crawork will be found
JHOME=''

if [ -n "$JHOME" -a x"$JHOME" != x"$HOME" ]; then
  # Allow optional reset of DATAPATH/RUNPATH
  JHOME_DATA=''
  DATAPATH=${JHOME_DATA:=$DATAPATH}
  RUNPATH=${JHOME_DATA:=$RUNPATH}
  # Allow optional reset of CCRNTMP
  JHOME_RUN=''
  CCRNTMP=${JHOME_RUN:=$CCRNTMP}
fi

jobname=par_mdump; time="1800" ; memory="900mb" ;

. comjcl.cdk

cat > Execute_Script <<'end_of_script'

  # This export is required by make_file_name_list
  set -a

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

  # Use -e option if recognized by echo (enable backslash escapes)
  if [ "X`echo -e`" = "X-e" ]; then
    echo_e() { echo ${1+"$@"}; }
  else
    echo_e() { echo -e ${1+"$@"}; }
  fi

  # bail is a simple error exit routine
  this_host=`hostname|cut -d'.' -f1`
  error_out="${JHOME:-$HOME}/.queue/error_par_mdump_${runid}_${this_host}_$stamp"
  [ ! -z "$error_out" ] && rm -f $error_out
  bail(){
    echo_e `date`" $this_host $runid --- par_mdump: $*"
    echo_e `date`" $this_host $runid --- par_mdump: $*" >>$error_out
    exit 1
  }

  ToF(){
    #   usage: ToF var_name
    # purpose: Possibly reset the value of var_name to "0" (false) or "1" (true)
    #          If var_name is null or has a value of "off" or "no" then reset to "0"
    #          If var_name has a value of "on" or "yes" then reset to "1"
    #          Otherwise return with var_name unchanged
    [ -z "$1" ] && bail "ToF requires a variable name as an argument"
    eval ToF_var\=\$$1
    XXX=`echo $ToF_var|sed 's/ //g'`
    eval ToF_var\=$XXX
    if [ -n "$ToF_var" ]; then
      if   [ "$ToF_var" = 'on'  ]; then eval ToF_var\=1
      elif [ "$ToF_var" = 'off' ]; then eval ToF_var\=0
      elif [ "$ToF_var" = 'yes' ]; then eval ToF_var\=1
      elif [ "$ToF_var" = 'no'  ]; then eval ToF_var\=0
      else
        eval ToF_var\=\$$1
      fi
    else
      eval ToF_var\=0
    fi
    eval $1=$ToF_var
  }

  this_mach=`uname -n|awk -F\. '{print \$1}' -`
  case $this_mach in
    ha|ha*) this_mach=hadar ;;
    sp|sp*) this_mach=spica ;;
       c1*) this_mach=spica ;;
       c2*) this_mach=hadar ;;
    za|ze*) this_mach=zeta  ;;
    sa|sa*) this_mach=saiph ;;
       c6*) this_mach=saiph ;;
       c7*) this_mach=zeta  ;;
         *) bail "$this_mach is not a valid machine name." ;;
  esac

  # The list of suffixes of files to be dumped
  mdump_suffix_list='cplhist.nc gs ss'
  par_mdump_suffix_list=$mdump_suffix_list

  # When par_mdump_with_del = on then the files that are dumped will also
  # be deleted from DATAPATH after they are put on cfs
  par_mdump_with_del=off
  ToF par_mdump_with_del

  # When par_mdump_leave_last_mon = on and files are deleted after they are dumped
  # the last month of files is not deleted and the month before the first month of
  # files is deleted (ie the delete is shifted back 1 month)
  par_mdump_leave_last_mon=on
  ToF par_mdump_leave_last_mon

  # shortermdir = on means dump files to short term cfs
  shortermdir=off
  ToF shortermdir

  # masterdir = on means dump files to short term cfs
  masterdir=off
  ToF masterdir

  # sv : on means dump files from pollux
  sv=off

  # svsave : on means save files on the front end when dumping
  #          from the back end or from UVic
  svsave=off

  both=off    # both  : on means dump files from pollux and the back end machine
  vic=off     # vic   : on means dump files from Victoria
  svvic=off   # svvic : on means dump files from pollux and Victoria

  # These variables are set when the job string is created
  run_start_year=NotSet
  run_start_month=NotSet
  run_stop_year=NotSet
  run_stop_month=NotSet
  current_year=NotSet
  current_month=NotSet
  previous_year=NotSet
  previous_month=NotSet
  next_year=NotSet
  next_month=NotSet

  # This invocation of make_file_name_list will process the *_year and *_months
  # variables defined above and output a file containing definitions for
  # start_year, start_mon, stop_year, stop_mon
  tmp_file_list="par_mdump_date_list_${runid}_${stamp}"
  make_file_name_list --dates_only $tmp_file_list >>$error_out 2>&1 ||
    bail "Problem in make_file_name_list --dates_only $tmp_file_list"
  rm -f $error_out

  # Verify that the output list is not empty
  [ ! -s "$tmp_file_list" ] && bail "Unable to create file list"

  # A file list was created ...source it
  # This will add the following variables to the current envoronment
  #   start_year, start_mon, stop_year, stop_mon, days_in_job
  : ; . $tmp_file_list
  rm -f $tmp_file_list

  # Create a list of files to be dumped
  par_mdump_list=dump_list_$stamp
  rm -f $par_mdump_list
  touch $par_mdump_list

  # Create a list of files to be deleted after they are dumped
  par_mdump_del_list=del_list_$stamp
  rm -f $par_mdump_del_list
  touch $par_mdump_del_list

  curr_year=$start_year
  while [ $curr_year -le $stop_year ]; do
    mon1=1
    mon2=12
    if [ $curr_year -eq $start_year ]; then
      mon1=$start_mon
    fi
    if [ $curr_year -eq $stop_year ]; then
      mon2=$stop_mon
    fi

    curr_mon=$mon1
    while [ $curr_mon -le $mon2 ]; do
      for sfx in $par_mdump_suffix_list; do
        # Files to be dumped
        yy=`echo $curr_year | awk '{printf "%3.3d",$1}' -`
        mm=`echo $curr_mon | awk '{printf "%2.2d",$1}' -`
        fname=${uxxx}_${runid}_${yy}_m${mm}_$sfx
        echo "$fname" >> $par_mdump_list

        # Files to be deleted after they are dumped
        if [ $par_mdump_leave_last_mon -eq 1 ]; then
          prev_mon=`echo $curr_mon | awk '{printf "%2.2d",$1-1}' -`
          if [ $prev_mon -lt 1 ]; then
            # This is Dec of the previous year
            prev_mon=12
            prev_year=`echo $curr_year | awk '{printf "%3.3d",$1-1}' -`
          else
            prev_year=$curr_year
          fi
          yy=`echo $prev_year | awk '{printf "%3.3d",$1}' -`
          mm=`echo $prev_mon  | awk '{printf "%2.2d",$1}' -`
          fname=${uxxx}_${runid}_${yy}_m${mm}_$sfx
          echo "$fname" >> $par_mdump_del_list
        else
          echo "$fname" >> $par_mdump_del_list
        fi
        
      done
      curr_mon=`expr $curr_mon + 1`
    done

    curr_year=`expr $curr_year + 1`
  done

  if [ -s $par_mdump_list ]; then
    echo "Files to dump:"
    cat $par_mdump_list
  fi

  if [ $par_mdump_with_del -eq 1 ]; then
    if [ -s $par_mdump_del_list ]; then
      echo "Files to delete:"
      cat $par_mdump_del_list
    fi
  fi

  if [ -s $par_mdump_list ]; then
    # Spawn another job that will dump the files in this list
    if [ $masterdir -eq 1 ]; then
      # Dump to "official" cfs directory
      qdump_opts="-Bm"
    elif [ $shortermdir -eq 1 ]; then
      # Dump to users short term cfs
      qdump_opts="-Bs"
    else
      # Dump to users long term cfs
      qdump_opts="-B"
    fi
    DEL_LIST=''
    if [ $par_mdump_with_del -eq 1 ]; then
      if [ -s $par_mdump_del_list ]; then
        # Add par_mdump_del_list defined above to the qdump command line
        # so these files will also be removed after they are on cfs
        DEL_LIST="del_list=$par_mdump_del_list"
      else
        bail "par_mdump_with_del is defined but unable to create a list of files to delete"
      fi
    fi
    qdump $qdump_opts -d $this_mach list=$par_mdump_list $DEL_LIST
  fi

end_of_script

. endjcl.cdk

#end_of_job

