#!/bin/sh
#=======================================================================
# auto tune tracer xrefs
# $Id: xtune_jobdef 659 2012-03-21 21:56:58Z acrnrls $
#=======================================================================
#     keyword :: xtune
# description :: auto tune xref's for hybrid tracers
#
 set -a
 . betapath2

 username="acrnxxx"; user="XXX"; uxxx='uxxx'
 runid=xtune; crawork="${runid}_job"

 nextjob=on
 noprint=on

 jobname=xtune_$runid; time="10800" ; memory="8000mb" ;

 . comjcl.cdk

cat > Execute_Script <<'end_of_script'

  # simple error exit routine
  bail(){
    echo "xtune: "$1
    exit 1
  }

  which cccjob || bail "cccjob is not in your path"

  # Indicate how cccjob should be invoked
  # Setting CCCJOB_ROOT will allow a job specific version of cccjob to used
  CCCJOB_ROOT=''
  if [ -z "$CCCJOB_ROOT" ]; then
    CCCJOB_ENV=''
  else
    eval CCCJOB_ENV=\'env CCCJOB_ROOT\=$CCCJOB_ROOT\'
  fi

  # 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

  username="acrnxxx"; user="XXX"; uxxx='uxxx'
  runid=xtune; crawork="${runid}_job"

  noprint=on
  nextjob=on

  # xtune_verbose -gt 0 means copy the submission job
  # created by cccjob back to ~/.queue
  xtune_verbose=1

  years_per_run=6

  xtune_start_year=1
  # ensure that xtune_start_year is formatted 3.3d
  # hide this assignment from the global substitution
  # done in cccjob by escaping the equal sign
  an_unused_vname=`echo $xtune_start_year|awk '{printf "%3.3d",$1}' -`
  eval xtune_start_year\=\$an_unused_vname

  xtune_end_year=`echo $xtune_start_year $years_per_run|awk '{printf "%3.3d",$1+$2-1}' -`

  # previous_start_year and previous_end_year are the start and end years
  # for the history file that will be analysed to determine the current
  # set of xref's and associated values
  # These variables may be different from xtune_start_year and xtune_end_year
  # if xtune_start_year and xtune_end_year are reset after reading the
  # restart file below
  previous_start_year=$xtune_start_year
  previous_end_year=$xtune_end_year

  iyear=2003

  ntrac=15
  modver=gcm17

  # xtune_mjob is the name of a file containing a monthly job string or a keyword that
  # is recognized by the --joblist option of cccjob. If it is a file name
  # then it should be a full path name and must be visible on the execution machine
  xtune_mjob=$modver

  xtune_iter="000"
  # ensure that xtune_iter is formatted 3.3d
  # hide this assignment from the global substitution
  # done in cccjob by escaping the equal sign
  an_unused_vname=`echo $xtune_iter|awk '{printf "%3.3d",$1}' -`
  eval xtune_iter\=\$an_unused_vname
  prefix=${uxxx}_${runid}_i${xtune_iter}
  echo "prefix = $prefix"

  # max_xtune_iter is the maximum number of times a job will be resubmitted
  max_xtune_iter=500

  # stop_tol is the stopping tolerance used below
  # All tracers must satisfy this stopping condition to terminate iterations
  stop_tol=1e-9

  echo "xtune_iter = $xtune_iter   max_xtune_iter = $max_xtune_iter"
  echo "stop_tol = $stop_tol"

  # BERUNPATH must be set if files are to be copied from a machine that is
  # not the default back end machine. BERUNPATH is used in mdump
  BERUNPATH=$BERUNPATH

  # ---Start_submit_ignore_code----

  # dump_to_cfs flags the transfer of intermmediate history files to the cfs
  dump_to_cfs=off

  # subsequent jobs will be executed on the machine named exec_mach
  exec_mach=maia

  # xrefhist is the name of a file that will be used to store a history of all
  # xref values and associated global averages between iterations of the
  # tuning process. xrefhist is saved on DATAPATH.
  xrefhist=${uxxx}_${runid}_xtune_history;

  # updates_fname is the name of a file containing updates
  # It should be a full pathname and the file must exist
  # if a non null value is supplied
  updates_fname=''
  # strip any colon delimited options from updates_fname
  # updtmp=`echo $updates_fname|sed 's/^\(.*\):.*$/\1/'`
  # eval updates_fname\=$updtmp

  # restart_fname is the name of a restart file
  # If not explicitly set the restart file name will be the value
  # of the variable start as found in the gcm model job string
  # e.g. start = ${prefix}_${year_restart}_m${mon_restart}_
  restart_fname=''

  # continue_from_previous_rs flags the use of restart files from the
  # previous iteration by each succesive iteration
  continue_from_previous_rs=on

  # from_initial_conditions flags starting from initial conditions
  # Setting from_initial_conditions=on will override starting from a restart
  # and therfore restart_fname will not be used
  from_initial_conditions=off

  # always_recompile flags a recompile of the model at the start of each iteration
  # if always_recompile=off then the model will be recompiled only on the first
  # iteration
  always_recompile=off

  # A few parameters required for running a coupled model
  coupled=on
  float1=off
  if [ x$coupled = "xon" ]; then
    ocnmod="00002"
    icemod="00001"
    ksteps="   96"
  else
    ocnmod="00000"
    icemod="00000"
    ksteps=" 2976"
  fi

  # When a restart file is specified, access it and extract kfinal and delt values
  # which may then be used to set the start time for the gcm model run
  if [ -n "$restart_fname" -a "x$from_initial_conditions" != "xon" ]; then
    access RS $restart_fname na
    [ ! -s RS ] && bail "Unable to access restart file $restart_fname"
    getstep RS kount_file
    rs_kfinal=`sed 's/ *\([^ ]*\) */\1/' kount_file`
    rm kount_file
    separmc RS RS1 PARM PARC
    if [ -s PARC ]; then
      binach PARC PAR.txt
    elif [ -s PARM ]; then
      binach PARM PAR.txt
    fi
    if [ -s PAR.txt ]; then
      rs_delt=`sed -n '/delt=/p' PAR.txt|\
                 awk -F\; '{for (i=1;i<=NF;i++) print $i}' -|\
                 awk -F\= '{if (/delt/) print $2}' -|\
                 sed -e 's/^ *\"* *//' -e 's/ *\"* *$//'`
      if [ -n "$rs_delt" ]; then
        st_year_day=`echo $rs_kfinal $rs_delt|\
                     awk '{k=1.0*$1; d=1.0*$2;
                       perday=86400.0/d; peryear=365.0*perday;
                       y=int(k/peryear); d=int((k-y*peryear)/perday);
                       if (d==0) {y=y+1;d=1}; printf "%d;%d",y,d}' -`
        st_year=`echo $st_year_day|awk -F\; '{printf "%3.3d",$1}' -`
        st_days=`echo $st_year_day|awk -F\; '{printf "%d",$2}' -`
        [ -z "$st_year" ] && bail "st_year is missing"
        [ -z "$st_days" ] && bail "st_days is missing"
        if [ $st_days -ne 1 ]; then
          bail "Starting from other than an end of year restart is not implimented"
        fi
        if [ $st_year -ne $xtune_start_year ]; then
          # set xtune_start_year to the starting year determined from the restart
          echo " "
          echo "*** WARNING *** resetting xtune_start_year to $st_year"
          echo "                to be consistent with restart file $restart_fname"
          echo " "
          eval xtune_start_year\=$st_year
          nd_year=`echo $xtune_start_year $years_per_run|\
                     awk '{printf "%3.3d",$1+$2-1}' -`
          eval xtune_end_year\=$nd_year
        fi
      else
        bail "unable to determine delt from restart file $restart_fname"
      fi
    else
      bail "unable to determine start time from restart file $restart_fname"
    fi
  fi

#  # parc_check flags insertion of gcmjcl updates to turn
#  # off the check on critical parameters
#  parc_check=off

  # define a set of initial xref values to start things off
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    # any init_xref?? not explicitly defined will have a value of 0.0
    eval init_xref${nn}=0.0
  done
  # These initial values are those used for gcm15h
  init_sref=6.52E-3
  init_xref01=0.0     ;   init_xref41=0.0;   init_xref81=0.0
  init_xref02=0.0     ;   init_xref42=0.0;   init_xref82=0.0
  init_xref03=3.77E-10;   init_xref43=0.0;   init_xref83=0.0
  init_xref04=4.71E-10;   init_xref44=0.0;   init_xref84=0.0
  init_xref05=1.08E-9 ;   init_xref45=0.0;   init_xref85=0.0
  init_xref06=3.59E-9 ;   init_xref46=0.0;   init_xref86=0.0
  init_xref07=1.12E-8 ;   init_xref47=0.0;   init_xref87=0.0
  init_xref08=9.20E-8 ;   init_xref48=0.0;   init_xref88=0.0
  init_xref09=2.15E-7 ;   init_xref49=0.0;   init_xref89=0.0
  init_xref10=3.78E-7 ;   init_xref50=0.0;   init_xref90=0.0
  init_xref11=3.23E-10;   init_xref51=0.0;   init_xref91=0.0
  init_xref12=6.29E-9 ;   init_xref52=0.0;   init_xref92=0.0
  init_xref13=1.70E-9 ;   init_xref53=0.0;   init_xref93=0.0
  init_xref14=0.0     ;   init_xref54=0.0;   init_xref94=0.0
  init_xref15=0.0     ;   init_xref55=0.0;   init_xref95=0.0
  init_xref16=0.0     ;   init_xref56=0.0;   init_xref96=0.0
  init_xref17=0.0     ;   init_xref57=0.0;   init_xref97=0.0
  init_xref18=0.0     ;   init_xref58=0.0;   init_xref98=0.0
  init_xref19=0.0     ;   init_xref59=0.0;   init_xref99=0.0
  init_xref20=0.0     ;   init_xref60=0.0;
  init_xref21=0.0     ;   init_xref61=0.0;
  init_xref22=0.0     ;   init_xref62=0.0;
  init_xref23=0.0     ;   init_xref63=0.0;
  init_xref24=0.0     ;   init_xref64=0.0;
  init_xref25=0.0     ;   init_xref65=0.0;
  init_xref26=0.0     ;   init_xref66=0.0;
  init_xref27=0.0     ;   init_xref67=0.0;
  init_xref28=0.0     ;   init_xref68=0.0;
  init_xref29=0.0     ;   init_xref69=0.0;
  init_xref30=0.0     ;   init_xref70=0.0;
  init_xref31=0.0     ;   init_xref71=0.0;
  init_xref32=0.0     ;   init_xref72=0.0;
  init_xref33=0.0     ;   init_xref73=0.0;
  init_xref34=0.0     ;   init_xref74=0.0;
  init_xref35=0.0     ;   init_xref75=0.0;
  init_xref36=0.0     ;   init_xref76=0.0;
  init_xref37=0.0     ;   init_xref77=0.0;
  init_xref38=0.0     ;   init_xref78=0.0;
  init_xref39=0.0     ;   init_xref79=0.0;
  init_xref40=0.0     ;   init_xref80=0.0;

  # Define a set of flags to indicate which tracers' xref values are
  # to be modified. The default is all tracers, however, any tracers for
  # which xref is initially 0 will never be altered.

  # explicitly set all values to 0. If the user does not modify any of these
  # then they will all be set to 1 (true) below. If the user sets any of these
  # values non-zero (true) then the default will be to leave all the other
  # unmodified values as 0 (false).
  # NOTE: There are a maximum of 99 tracers
  tune_sref=0
  tune_xref01=0; tune_xref21=0; tune_xref41=0; tune_xref61=0; tune_xref81=0
  tune_xref02=0; tune_xref22=0; tune_xref42=0; tune_xref62=0; tune_xref82=0
  tune_xref03=0; tune_xref23=0; tune_xref43=0; tune_xref63=0; tune_xref83=0
  tune_xref04=0; tune_xref24=0; tune_xref44=0; tune_xref64=0; tune_xref84=0
  tune_xref05=0; tune_xref25=0; tune_xref45=0; tune_xref65=0; tune_xref85=0
  tune_xref06=0; tune_xref26=0; tune_xref46=0; tune_xref66=0; tune_xref86=0
  tune_xref07=0; tune_xref27=0; tune_xref47=0; tune_xref67=0; tune_xref87=0
  tune_xref08=0; tune_xref28=0; tune_xref48=0; tune_xref68=0; tune_xref88=0
  tune_xref09=0; tune_xref29=0; tune_xref49=0; tune_xref69=0; tune_xref89=0
  tune_xref10=0; tune_xref30=0; tune_xref50=0; tune_xref70=0; tune_xref90=0
  tune_xref11=0; tune_xref31=0; tune_xref51=0; tune_xref71=0; tune_xref91=0
  tune_xref12=0; tune_xref32=0; tune_xref52=0; tune_xref72=0; tune_xref92=0
  tune_xref13=0; tune_xref33=0; tune_xref53=0; tune_xref73=0; tune_xref93=0
  tune_xref14=0; tune_xref34=0; tune_xref54=0; tune_xref74=0; tune_xref94=0
  tune_xref15=0; tune_xref35=0; tune_xref55=0; tune_xref75=0; tune_xref95=0
  tune_xref16=0; tune_xref36=0; tune_xref56=0; tune_xref76=0; tune_xref96=0
  tune_xref17=0; tune_xref37=0; tune_xref57=0; tune_xref77=0; tune_xref97=0
  tune_xref18=0; tune_xref38=0; tune_xref58=0; tune_xref78=0; tune_xref98=0
  tune_xref19=0; tune_xref39=0; tune_xref59=0; tune_xref79=0; tune_xref99=0
  tune_xref20=0; tune_xref40=0; tune_xref60=0; tune_xref80=0;
  nn=0
  any_set=0
  if [ "$tune_sref" != "0" ]; then
    any_set=1
  fi
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    # look for at least 1 tune_xref?? value to be set
    eval xx=\$tune_xref${nn}
    # any null values will be converted to 0 values
    [ -z "$xx" ] && eval tune_xref${nn}=0
    [ "$xx" != "0" ] && any_set=1
  done
  if [ $any_set -eq 0 ]; then
    # No tune_xref?? variables were set by the user so set them all
    # to the default value of 1 (true)
    tune_sref=1
    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval tune_xref${nn}=1
    done
    echo "Tuning will be done on all tracers"
  fi
  if [ $any_set -eq 1 ]; then
    # If any of the tune_xref?? variables were set by the user, print them all
    eval echo \"tune_sref = \$tune_sref\"
    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval echo \"tune_xref${nn} = \$tune_xref${nn}\"
    done
  fi

########################### write_xrefhist ################################
#cat <<EOF >write_xrefhist
#EOF

################################ resubmit #################################
# Resubmit the job after incrementing the iteration number
#
resubmit() {
  # resubmit a job with the new xref values

  # ftmp will contain a list of vname=value pairs that will be passed
  # on to subsequent iterations of the tuning process
  ftmp=a`date "+%j"$$`
  cat > $ftmp <<EOF
  months=6
  uxxx="$uxxx"
  runid="$runid"
  ntrac="$ntrac"
  xrefhist="$xrefhist"
  years_per_run="$years_per_run"
  max_xtune_iter="$max_xtune_iter"
  stop_tol="$stop_tol"
  debug=off
  xtune_mjob="$xtune_mjob"
  BERUNPATH="$BERUNPATH"
  dump_to_cfs=$dump_to_cfs
  exec_mach=$exec_mach
  from_initial_conditions=$from_initial_conditions
  always_recompile=$always_recompile
  noprint=on
  nextjob=on
  xtune_verbose=0
  iyear="$iyear"
  coupled=$coupled
  float1=$float1
  ocnmod="$ocnmod"
  icemod="$icemod"
  ksteps="$ksteps"
EOF

  # append xtune_start_year and hide this assignment from the global
  # substitution done in cccjob by escaping the equal sign
  eval echo \"xtune_start_year\=\$xtune_start_year\" >> $ftmp
  eval echo \"xtune_end_year\=\$xtune_end_year\" >> $ftmp

  if [ "x$always_recompile" = "xoff" ]; then
    # Recompile on the first iteration but not on subsequent iterations
    if [ $xtune_iter -gt 0 ]; then
      eval echo \"samerun\=on\" >> $ftmp
    else
      eval echo \"samerun\=off\" >> $ftmp
    fi
  else
    # ensure that samerun = off is always sent to cccjob so that
    # the program will always be recompiled at the start of each iteration
    eval echo \"samerun\=off\" >> $ftmp
  fi

  # Increment the iteration number and append to ftmp
  # hide this assignment from the global substitution
  # done in cccjob by escaping the equal sign
  an_unused_vname=`echo $xtune_iter|awk '{i=1+$1;printf "%3.3d",i}' -`
  eval echo \"xtune_iter\=\$an_unused_vname\" >> $ftmp

  # define prefix using this value of xtune_iter and append to ftmp
  next_prefix=${uxxx}_${runid}_i$an_unused_vname
  eval echo \"prefix\=$next_prefix\" >> $ftmp
  eval echo \"mdump_prefix\=$next_prefix\" >> $ftmp

  # define restart_fname as the restart from the end of the current
  # iteration or the restart name supplied in the initial iteration
  # depending on the value of the variable continue_from_previous_rs
  if [ "x$continue_from_previous_rs" = "xon" ]; then
    eval echo \"restart_fname\=${next_prefix}_${xtune_end_year}_m12_rs\" >> $ftmp
  else
    if [ -n "$restart_fname" ]; then
      eval echo \"restart_fname\=\$restart_fname\" >> $ftmp
    fi
  fi

  # append parmsub parameters model and start so that user
  # supplied jobs will produce correct model file names
  eval echo \'model\=${next_prefix}_\$\{year\}_m\$\{mon\}_\' >> $ftmp

  eval echo \"sref\=\$sref\" >> $ftmp
  eval echo \"init_sref\=\$init_sref\" >> $ftmp
  eval echo \"tune_sref\=\$tune_sref\" >> $ftmp
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval echo \"xref${nn}\=\$xref$nn\" >> $ftmp
    eval echo \"init_xref${nn}\=\$init_xref$nn\" >> $ftmp
    eval echo \"tune_xref${nn}\=\$tune_xref$nn\" >> $ftmp
  done

  # fout must be assigned as follows to get the iteration number right
  fout=${uxxx}_${runid}_i${an_unused_vname}_job

  [ -n "$fout" ] && rm -f $fout
  if [ -n "$updates_fname" ]; then
    update_opt="--updates=$updates_fname"
  else
    update_opt=''
  fi
  if [ x"$dump_to_cfs" = x"on" ]; then
    mdump='mdump:y'
  else
    mdump=''
  fi
  if [ x"$from_initial_conditions" = x"on" ]; then
    initial_conditions_opt="--initial_conditions"
    xtune_start_year='001'
    xtune_end_year=`echo $xtune_start_year $years_per_run|awk '{printf "%3.3d",$1+$2-1}' -`
    rs_opt=''
  else
    initial_conditions_opt=''
    if [ -n "$restart_fname" ]; then
      rs_opt="--restart=$restart_fname"
    else
      bail "Continuing from a restart but restart_fname is not defined"
    fi
  fi

  ls -l /opt/ssm/repack-libdb_3.3.11_aix51-ppc-64/lib ||
    echo "Missing /opt/ssm/repack-libdb_3.3.11_aix51-ppc-64/lib"

  # Reset the value of iyear in the restart file so that the same multi year
  # boundary condition data is always repeated for each model submission
  if [ -n "$rs_opt" ]; then
    # This model job will start from a restart named restart_fname
    [ -z "$restart_fname" ] &&
      bail "Starting from a restart but restart_fname is not defined."
    [ -z "$iyear" ] &&
      bail "Attempting to reset iyear in $restart_fname but iyear is not defined."
    which rstime || bail "rstime is not on your path"
    rm -f RSIN RSOUT
    access RSIN $restart_fname
    rstime RSIN RSOUT iyear=$iyear
    delete RSIN
    release RSIN
    save RSOUT $restart_fname
  fi

  # Provide command line defs for JHOME and JHOME_DATA
  # if these variables are defined in the current env
  JHOME_opt=''
  [ -n "$JHOME" ]      && JHOME_opt="JHOME=$JHOME "
  [ -n "$JHOME_DATA" ] && JHOME_opt="$JHOME_opt JHOME_DATA=$JHOME_DATA "
  [ -n "$JHOME_RUN" ]  && JHOME_opt="$JHOME_opt JHOME_RUN=$JHOME_RUN "

  $CCCJOB_ENV cccjob --output=$fout --joblist="${xtune_mjob}:m $mdump xtune:s" \
         --start_time="$xtune_start_year" --stop_time="$xtune_end_year" \
         $rs_opt $ftmp $update_opt $initial_conditions_opt $JHOME_opt
  if [ -s "$fout" ]; then
    if [ $xtune_verbose -gt 0 ]; then
      if [ -d ${JHOME:-$HOME}/.queue ]; then
        cp $fout ${JHOME:-$HOME}/.queue/$fout
        echo "xtune: Model submission job sent to ${JHOME:-$HOME}/.queue/$fout"
      elif [ -d ${JHOME:-$HOME} ]; then
        cp $fout ${JHOME:-$HOME}/$fout
        echo "xtune: ***WARNING*** No ${JHOME:-$HOME}/.queue directory. Output sent to ${JHOME:-$HOME}"
      else
        echo 'xtune: ***ERROR*** No home directory!!!'
        exit 1
      fi
    fi
    rsub $fout $exec_mach
    echo "xref auto tune job $fout submitted to $exec_mach"
  else
    echo "Submission file $fout is missing or empty"
    exit 1
  fi
  test 1
}
############################# end of resubmit ##############################

################################ xtune_delete #############################
# Delete model history files once the averages are calculated and recorded
#
xtune_delete() {
  del_curr_year=`echo $previous_start_year|awk '{printf "%d",$1-1}' -`
  while [ $del_curr_year -lt $previous_end_year ]; do
    del_curr_year=`echo $del_curr_year|awk '{m=1+$1;printf "%3.3d", m}' -`
    del_curr_mon=0
    while [ $del_curr_mon -lt 12 ]; do
      del_curr_mon=`echo $del_curr_mon|awk '{m=1+$1;printf "%2.2d", m}' -`
      # access and delete files for this month and year
      # Note missing files will not result in an error signal
      rm -f lfile
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_gs
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_ss
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_cm
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      # Always delete the script file
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}__script
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      if [ $del_curr_year -eq $previous_end_year -a $del_curr_mon -eq 12 ]; then
        # never delete the last restart or "an" or "ab" file
        continue
      fi
      # delete this restart
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_rs
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_cs
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      # Delete ab and an files
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_ab
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_ob
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
      fname=${prefix}_${del_curr_year}_m${del_curr_mon}_an
      release lfile
      access lfile $fname nocp na
      [ -f lfile ] && delete lfile na
    done
  done
  # Also delete the set of restarts used to start the previous chunk
  prev_rs_year=`echo $previous_start_year|awk '{printf "%3.3d",$1-1}' -`
  release lfile
  access lfile ${prefix}_${prev_rs_year}_m12_rs nocp na
  [ -f lfile ] && delete lfile na
  release lfile
  access lfile ${prefix}_${prev_rs_year}_m12_cs nocp na
  [ -f lfile ] && delete lfile na
  release lfile
  access lfile ${prefix}_${prev_rs_year}_m12_ab nocp na
  [ -f lfile ] && delete lfile na
  release lfile
  access lfile ${prefix}_${prev_rs_year}_m12_ob nocp na
  [ -f lfile ] && delete lfile na
  release lfile
  access lfile ${prefix}_${prev_rs_year}_m12_an nocp na
  [ -f lfile ] && delete lfile na
  release lfile
  
  test 1
}
############################ end of xtune_delete ##########################

################################ write_db #################################
# Define a function to write one set of records to a data base file.
# The file will be overwritten if it exists.
# Each line of this file is either a sequence of 5 hash marks or of the form
# seqno  tag  vals
# where seqno is an integer sequence number (the iteration number),
# tag is a character string used to identify the line and
# vals is a whitespace separated list of numbers
# The five hash marks delimit the start of a set (a set is a group of
# lines with the same sequence number)
# The current set of tags is
# #####
# 00000 years
# 00000 name
# 00000 sref_1
# 00000 sref_2
# 00000 sref_3
# 00000 qhavg_1
# 00000 qhavg_2
# 00000 qfavg
# 00000 qmavg
# 00000 name
# 00000 xref_1
# 00000 xref_2
# 00000 xref_3
# 00000 vhavg_m01
# 00000 vhavg_m02
# 00000 vhavg_m03
# 00000 vhavg_m04
# 00000 vhavg_m05
# 00000 vhavg_m06
# 00000 vhavg_m07
# 00000 vhavg_m08
# 00000 vhavg_m09
# 00000 vhavg_m10
# 00000 vhavg_m11
# 00000 vhavg_m12
# 00000 vhavg_1
# 00000 vhavg_2
# 00000 vfavg
# 00000 vmavg
#
# All lines except the years line will have ntrac values.
# The years line will have 1 value.
write_db() {
  if [ -z "$1" ]; then
    [ -z "$DBname" ] && DBname=XREFHIST
    echo "write_db No file name supplied on command line ...using $DBname"
  else
    DBname=$1
  fi
  echo "Creating data base file $DBname"
  rm -f $DBname
  touch $DBname

  if [ -z "$2" ]; then
    [ -z "$seqno" ] && seqno='00000'
    seqno=`echo $seqno|awk '{printf "%5.5d",$1}' -`
    echo "write_db No sequence number supplied on command line ...using $seqno"
  else
    seqno=`echo $2|awk '{printf "%5.5d",$1}' -`
  fi

  if [ -z "$3" ]; then
    [ -z "$years_per_run" ] && years_per_run=1
    kyears_per_run=`echo $years_per_run|awk '{printf "%d",$1}' -`
    eval years_per_run\=\$kyears_per_run
    echo "write_db No years_per_run supplied on command line ...using $years_per_run"
  else
    kyears_per_run=`echo $3|awk '{printf "%d",$1}' -`
    eval years_per_run\=\$kyears_per_run
  fi

  # Each set begins with a delimiter line
  echo "#####" > $DBname

  # followed by a single value, years_per_run
  echo "${seqno}    years $years_per_run" >> $DBname

  # Write tracer names to the file
  # Tracer names are found in variables named it?? where ??=01..ntrac
  nn=0
  line="${seqno}     name"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$it$nn
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # The next 3 lines contain sref values for the last 3 iterations
  echo "${seqno}   sref_1 $a_sref" >> $DBname
  echo "${seqno}   sref_2 $p_sref" >> $DBname
  echo "${seqno}   sref_3 $sref"   >> $DBname

  # the next 4 lines contain moisture mass anomalies and total mass
  echo "${seqno}  qhavg_1 $qhavg_1" >> $DBname
  echo "${seqno}  qhavg_2 $qhavg_2" >> $DBname
  echo "${seqno}    qfavg $qfavg"   >> $DBname
  echo "${seqno}    qmavg $qmavg"   >> $DBname

  # the xref_1 record contains the antepenultimate xref values
  # These values will be taken from variables named a_xref?? where ??=01..ntrac
  nn=0
  line="${seqno}   xref_1"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$a_xref$nn
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # the xref_2 record contains the penultimate xref values
  # These values will be taken from variables named p_xref?? where ??=01..ntrac
  nn=0
  line="${seqno}   xref_2"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$p_xref$nn
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # the xref_3 record contains the most recent estimates for the xref values
  # These values will be taken from variables named xref?? where ??=01..ntrac
  nn=0
  line="${seqno}   xref_3"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$xref$nn
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # each vhavg_m?? record contains the global average for that month
  # These values will be taken from variables named vhavg??_m??
  # where the tracer number is the first ??=01..ntrac and the month is
  # the second ??=01..12
  curr_mon=0
  while [ $curr_mon -lt 12 ]; do
    curr_mon=`echo $curr_mon|awk '{m=1+$1;printf "%2.2d", m}' -`
    nn=0
    line="${seqno} vhavg_m${curr_mon}"
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval an_unused_vname=\$vhavg${nn}_m${curr_mon}
      an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
      line="$line $an_unused_vname"
    done
    echo "$line" >> $DBname
  done

  # the vhavg_1 record contains antepenultimite global/annual averages
  # for each tracers' mass loss or gain per unit time
  # These values will be taken from variables named vhavg??_1
  # where the tracer number is ??=01..ntrac
  nn=0
  line="$seqno   vhavg_1"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$vhavg${nn}_1
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # the vhavg_2 record contains penultimate global/annual averages
  # for each tracers' mass loss or gain per unit time
  # These values will be taken from variables named vhavg??_2
  # where the tracer number is ??=01..ntrac
  nn=0
  line="$seqno   vhavg_2"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$vhavg${nn}_2
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # the vfavg record contains current global/annual averages
  # for each tracers' vertically integrated hole filling
  # These values will be taken from variables named vfavg??
  # where the tracer number is ??=01..ntrac
  nn=0
  line="$seqno     vfavg"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$vfavg${nn}
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname

  # the vmavg record contains current global/annual averages
  # for each tracers' vertically integrated mass
  # These values will be taken from variables named vmavg??
  # where the tracer number is ??=01..ntrac
  nn=0
  line="$seqno     vmavg"
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval an_unused_vname=\$vmavg${nn}
    an_unused_vname=`echo $an_unused_vname|awk '{printf "%11.4E",$1}' -`
    line="$line $an_unused_vname"
  done
  echo "$line" >> $DBname
  test 1
}
############################# end of write_db ##############################

  ############################################################################
  # See if this is an initial submission and if so simply resubmit using the
  # initial xref values
  ############################################################################

  # name a lock file to be used to check runaway resubmits
  xflock="${JHOME:-$HOME}/.queue/.crawork/xt_${runid}_lock_file_1"
  [ ! -d `dirname "$xflock"` ] && bail "Invalid lock file name $xflock"

  # Attempt to access the gs file for Jan of previous_start_year
  gsname=${prefix}_${previous_start_year}_m01_gs;
  rm -f GS
  access GS $gsname na
  if [ ! -s GS ]; then

    # First check for a lock file
    if [ -f "$xflock" ]; then
      rm -f $xflock
       bail "Lock file 1 exists"
    fi
    touch $xflock

    rm -f XREFHIST
    access XREFHIST $xrefhist na
    if [ ! -s XREFHIST ]; then
      # When there is no existing xref history file initiate
      # the tuning process using the initial xref/sref values

      # Set xref?? to the initial xref values
      eval sref\=\$init_sref
      nn=0
      while [ $nn -lt $ntrac ]; do
        nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
        eval xref${nn}=\$init_xref$nn
      done
    else
      # When an xref history file is available use the xref/sref values
      # from the last set found in that file

      # strip the last 29 lines from the data base file and create
      # a separate file containing only the last set to be read below
      tail -29 XREFHIST > prev_XREFHIST
      if [ `wc -l prev_XREFHIST|awk '{print $1}' -` -ne 29 ]; then
        bail "ERROR reading data base file"
      fi

      # read the file once to determine the last sequence number
      all_seqno=`while read seqno tag line; do
                   echo $seqno
                 done < prev_XREFHIST`
      last_seqno=`echo $all_seqno|awk '{print $NF}' -`
      echo "found in XREFHIST=${xrefhist}: last_seqno=$last_seqno"

      # read most recent sref_[123] values from the database file
      sref_3=`while read seqno tag vals; do
                if [ "$seqno" = "$last_seqno" ]; then
                  if [ "$tag" = "sref_3" ]; then
                    echo $vals
                  fi
                fi
              done < prev_XREFHIST`
      echo "found in XREFHIST=${xrefhist}: sref_3 = $sref_3"
      eval sref\=\$sref_3

      # read most recent xref??_3 values from the database file
      vals=`while read seqno tag vals; do
              if [ "$seqno" = "$last_seqno" ]; then
                if [ "$tag" = "xref_3" ]; then
                  echo $vals
                fi
              fi
            done < prev_XREFHIST`
      echo "found in XREFHIST=${xrefhist}: xref_3 = $vals"
      nn=0
      for x in $vals; do
        nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
        eval xref${nn}=$x
      done
    fi

    # resubmit with the new xref values
    resubmit

    # Normal exit
    exit 0
  fi

  rm -f $xflock

  ############################################################################
  # for each member of VHlist determine a global average for each month then
  # calculate an annual average from the monthly global averages
  # these averages will be put into variables named vhavg?? where ?? is 01 .. ntrac
  # globally averaged monthly means will be saved in variables named vhavg??_m??
  # where the first ?? is tracer number and the last ?? is month
  ############################################################################

  # Assign VHlist and VMlist with names of history file variables to be processed
  VHlist=''
  VMlist=''
  VFlist=''
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    # Each of the VH?? history file variables contains a grid of the vertically
    # and monthly averaged mass loss or gain per unit time of tracer ??
    VHlist="$VHlist VH$nn"
    # initialize all vhavg?? variables to zero
    eval vhavg${nn}=0.0

    # Each of the VM?? history file variables contains a grid of the vertically
    # and monthly averaged mass of tracer ??
    VMlist="$VMlist VM$nn"
    # initialize all vmavg?? variables to zero
    eval vmavg${nn}=0.0

    # Each of the VF?? history file variables contains a grid of the vertically
    # and monthly averaged hole filling of tracer ??
    VFlist="$VFlist VF$nn"
    # initialize all vfavg?? variables to zero
    eval vfavg${nn}=0.0
  done

#  # determine the number of input cards used by select done below
#  # require enough cards to hold the ntrac fields plus 1 (QHM, PWM or QFM)
#  ncards=`echo $ntrac|\
#            awk '{t=0+$1;
#                  if(t<4){print "1"}
#                  else{n=1+(t-3)/18;if((t-3)%18!=0)n=n+1;printf "%d",n}}' -`
#  [ -z "$ncards" ]  && bail "ncards = $ncards  ntrac = $ntrac"
#  [ $ncards -lt 1 ] && bail "ncards = $ncards  ntrac = $ntrac"
#
#  # define the first input card for each select done below
#  if [ $ntrac -eq 0 ]; then
#    ncards=0
#  elif [ $ntrac -eq 1 ]; then
#    vhcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01  QHM"
#    vmcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01  PWM"
#    vfcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01  QFM"
#  elif [ $ntrac -eq 2 ]; then
#    vhcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02  QHM"
#    vmcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02  PWM"
#    vfcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02  QFM"
#  elif [ $ntrac -eq 3 ]; then
#    vhcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02 VH03  QHM"
#    vmcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02 VH03  PWM"
#    vfcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02 VH03  QFM"
#  elif [ $ntrac -ge 4 ]; then
#    vhcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02 VH03 VH04"
#    vmcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02 VH03 VH04"
#    vfcard01="  SELECT.  STEP         1 999999999    1     -9001 1000 NAME VH01 VH02 VH03 VH04"
#  else
#    bail "ntrac = $ntrac is out of range"
#  fi
#
#  # append any remaining input cards depending on the value of ntrac
#  nn=1
#  while [ $nn -lt $ncards ]; do
#    nn=`echo $nn|awk '{n=$1+1;printf "%2.2d",n}' -`
#    card=`echo $nn $VHlist QHM|\
#          awk '{printf "\n%s","  SELECT.  ";
#                n=6+($1-2)*14;for(i=n;i<n+14;i++)printf "%s ",$i}' -`
#    vhcards="$vhcard01$card"
#    card=`echo $nn $VMlist PWM|\
#          awk '{printf "\n%s","  SELECT.  ";
#                n=6+($1-2)*14;for(i=n;i<n+14;i++)printf "%s ",$i}' -`
#    vmcards="$vmcard01$card"
#    card=`echo $nn $VFlist QFM|\
#          awk '{printf "\n%s","  SELECT.  ";
#                n=6+($1-2)*14;for(i=n;i<n+14;i++)printf "%s ",$i}' -`
#    vfcards="$vfcard01$card"
#  done

  # initialize all vhavg??_m?? and vmavg??_m?? variables to zero
  qhavg=0.0
  qfavg=0.0
  qmavg=0.0
  curr_mon=0
  while [ $curr_mon -lt 12 ]; do
    curr_mon=`echo $curr_mon|awk '{m=1+$1;printf "%2.2d", m}' -`
    eval qhavg_m${curr_mon}=0.0
    eval qfavg_m${curr_mon}=0.0
    eval qmavg_m${curr_mon}=0.0
    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval vhavg${nn}_m${curr_mon}=0.0
      eval vfavg${nn}_m${curr_mon}=0.0
      eval vmavg${nn}_m${curr_mon}=0.0
    done
  done

  r_curr_year=`echo $previous_start_year|awk '{y=$1-1;printf "%3.3d",y}' -`
  while [ $r_curr_year -lt $previous_end_year ]; do
    r_curr_year=`echo $r_curr_year|awk '{y=1+$1;printf "%3.3d", y}' -`

    # loop over 12 months of history files extracting fields to be averaged.
    # Each field is globally averaged and these averages are stored in
    # the vhavg??_m?? and vmavg??_m?? variables while the accumulated
    # global/monthly averages are saved in the vhavg?? and vmavg?? variables
    curr_mon=0
    while [ $curr_mon -lt 12 ]; do
      curr_mon=`echo $curr_mon|awk '{m=1+$1;printf "%2.2d", m}' -`

      # access the gs file for this month
      gsname=${prefix}_${r_curr_year}_m${curr_mon}_gs;
      echo "curr_mon=$curr_mon   gsname=$gsname"
      rm -f GS
      access GS $gsname

      if [ $r_curr_year -eq $previous_start_year -a $curr_mon -eq 1 ]; then
        # determine tracer names from the parc/parm record in the first gs file

        # initialize all tracer names with Xnn for nn=1..ntrac
        nn=0
        while [ $nn -lt $ntrac ]; do
          nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
          eval it${nn}=X$nn
          # eval echo \"it${nn}=\$it$nn\"
        done

        # Extract PARC/PARM records from gs file and convert to ascii
        rm -f XXX PARC PARM
        separmc GS XXX PARM PARC
        rm -f PARM.txt PARC.txt
        [ -s PARM ] && bin2txt PARM PARM.txt >/dev/null
        [ -s PARC ] && bin2txt PARC PARC.txt >/dev/null
        rm -f XXX PARC PARM

        # Extract tracer names from parc/parm record
        it_list=''
        if [ -s PARC.txt ]; then
          it_list=`sed -n '/it[0-9][0-9]=/p' PARC.txt|sed '/^[ \t]*#/d'`
        fi
        if [ x"$it_list" = x ]; then
          if [ -s PARM.txt ]; then
            it_list=`sed -n '/it[0-9][0-9]=/p' PARM.txt|sed '/^[ \t]*#/d'`
          fi
        fi
        # rm -f PARM.txt PARC.txt

        # If it_list is empty then tracer names default to the form Xnn
        if [ x"$it_list" != x ]; then
          # ensure there is at least 1 space after each semicolon
          it_list=`echo $it_list|sed 's/;/; /g'`
          for it in $it_list; do
            nn=`echo $it|sed 's/^it\([0-9][0-9]\)=.*/\1/'`
            # if nn is null then this is not a tracer name definition
            [ -z "$nn" ] && continue
            tr_name=`echo $it|sed 's/^it[0-9][0-9]="*\([a-zA-Z0-9_]*\).*/\1/'`
            if [ -z "$tr_name" ]; then
              tr_name=X$nn
            fi
            eval it${nn}=$tr_name
          done
        fi

        # echo all tracer names to stdout
        nn=0
        while [ $nn -lt $ntrac ]; do
          nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
          eval echo \"it${nn}=\$it$nn\"
        done
      fi

      nn=0
      while [ $nn -lt $ntrac ]; do
        nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
        # extract vertically averaged tracer conservation from the gs file
        vhname="VH$nn"
        # extract vertically averaged tracer hole filling from the gs file
        vfname="VF$nn"
        # extract vertically averaged tracer from the gs file
        vmname="VM$nn"
        ccc select GS $vhname $vfname $vmname <<EOF
  SELECT.  STEP         1 999999999    1     -9001 1000 NAME $vhname $vfname $vmname
EOF
      done

      # extract monthly averaged conservation correction (QHM),
      # hole filling (QFM) and precipitable water (PWM) from the gs file
      ccc select GS QHM QFM PWM <<EOF
  SELECT.  STEP         1 999999999    1     -9001 1000 NAME  QHM  QFM  PWM
EOF

      # accumulate sums in the q[hfm]avg variables
      # and save monthly averages in q[hfm]avg_mm variables

      # QHM will contain vertically averaged moisture mass anomaly
      [ ! -s QHM ] && bail "xtune: QHM is missing from $gsname"

      # val will contain the global average for the current month
      val=`globavg QHM|awk '{if(NF>10){printf "%g",$11}}' -`

      # accumulate the current month average in qhavg_m${curr_mon}
      eval xx=\$qhavg_m${curr_mon}
      xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
      eval qhavg_m${curr_mon}=\$xx

      # accumulate the global/annual average in qhavg
      eval xx=\$qhavg
      xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
      eval qhavg=\$xx
      rm -f QHM

      # QFM will contain vertically averaged moisture hole filling
      [ ! -s QFM ] && bail "xtune: QFM is missing from $gsname"

      # val will contain the global average for the current month
      val=`globavg QFM|awk '{if(NF>10){printf "%g",$11}}' -`

      # accumulate the current month average in qfavg_m${curr_mon}
      eval xx=\$qfavg_m${curr_mon}
      xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
      eval qfavg_m${curr_mon}=\$xx

      # accumulate the global/annual average in qfavg
      eval xx=\$qfavg
      xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
      eval qfavg=\$xx
      rm -f QFM

      # PWM will contain vertically averaged moisture total mass
      [ ! -s PWM ] && bail "xtune: PWM is missing from $gsname"

      # val will contain the global average for the current month
      val=`globavg PWM|awk '{if(NF>10){printf "%g",$11}}' -`

      # accumulate the current month average in qmavg_m${curr_mon}
      eval xx=\$qmavg_m${curr_mon}
      xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
      eval qmavg_m${curr_mon}=\$xx

      # accumulate the global/annual average in qmavg
      eval xx=\$qmavg
      xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
      eval qmavg=\$xx
      rm -f PWM

      # accumulate sums in the v[hm]avgnn variables
      # and save monthly averages in v[hm]avgnn_mm variables
#      for VHfile in $VHlist; do
#        nn=`echo $VHfile|sed 's/^VH\(..\)$/\1/'`
      nn=0
      while [ $nn -lt $ntrac ]; do
        nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
        VHfile="VH$nn"
        [ ! -s $VHfile ] && bail "xtune: $VHfile is missing from $gsname"

        # val will contain the global average for the current month
        val=`globavg $VHfile|awk '{if(NF>10){printf "%g",$11}}' -`

        # accumulate the current month average in vhavg${nn}_m${curr_mon}
        eval xx=\$vhavg${nn}_m${curr_mon}
        xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
        eval vhavg${nn}_m${curr_mon}=\$xx

        # accumulate the global/annual average in vhavg${nn}
        eval xx=\$vhavg$nn
        xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
        eval vhavg${nn}=\$xx
        rm -f $VHfile

        VFfile="VF$nn"
        [ ! -s $VFfile ] && bail "xtune: $VFfile is missing from $gsname"

        # val will contain the global average for the current month
        val=`globavg $VFfile|awk '{if(NF>10){printf "%g",$11}}' -`

        # accumulate the current month average in vfavg${nn}_m${curr_mon}
        eval xx=\$vfavg${nn}_m${curr_mon}
        xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
        eval vfavg${nn}_m${curr_mon}=\$xx

        # accumulate the global/annual average in vfavg${nn}
        eval xx=\$vfavg$nn
        xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
        eval vfavg${nn}=\$xx
        rm -f $VFfile

        VMfile="VM$nn"
        [ ! -s $VMfile ] && bail "xtune: $VMfile is missing from $gsname"

        # val will contain the global average for the current month
        val=`globavg $VMfile|awk '{if(NF>10){printf "%g",$11}}' -`

        # accumulate the current month average in vmavg${nn}_m${curr_mon}
        eval xx=\$vmavg${nn}_m${curr_mon}
        xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
        eval vmavg${nn}_m${curr_mon}=\$xx

        # accumulate the global/annual average in vmavg${nn}
        eval xx=\$vmavg$nn
        xx=`echo $xx $val|awk '{a=$1+$2;printf "%g",a}' -`
        eval vmavg${nn}=\$xx
        rm -f $VMfile
      done
    done
  done

  # divide each sum in v[hm]avg??_m?? by years_per_run to get the monthly averages
  mm=0
  while [ $mm -lt 12 ]; do
    mm=`echo $mm|awk '{m=1+$1;printf "%2.2d", m}' -`

    # water mass anomaly
    eval xx=\$qhavg_m${mm}
    xx=`echo $xx $years_per_run|awk '{x=(1.0*$1)/(1.0*$2); printf "%g",x}' -`
    eval qhavg_m${mm}=\$xx

    # water hole filling
    eval xx=\$qfavg_m${mm}
    xx=`echo $xx $years_per_run|awk '{x=(1.0*$1)/(1.0*$2); printf "%g",x}' -`
    eval qfavg_m${mm}=\$xx

    # water total mass
    eval xx=\$qmavg_m${mm}
    xx=`echo $xx $years_per_run|awk '{x=(1.0*$1)/(1.0*$2); printf "%g",x}' -`
    eval qmavg_m${mm}=\$xx

    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval xx=\$vhavg${nn}_m${mm}
      xx=`echo $xx $years_per_run|awk '{x=(1.0*$1)/(1.0*$2); printf "%g",x}' -`
      eval vhavg${nn}_m${mm}=\$xx
      eval xx=\$vfavg${nn}_m${mm}
      xx=`echo $xx $years_per_run|awk '{x=(1.0*$1)/(1.0*$2); printf "%g",x}' -`
      eval vfavg${nn}_m${mm}=\$xx
      eval xx=\$vmavg${nn}_m${mm}
      xx=`echo $xx $years_per_run|awk '{x=(1.0*$1)/(1.0*$2); printf "%g",x}' -`
      eval vmavg${nn}_m${mm}=\$xx
    done
  done

  # divide each sum in v[hm]avg?? by 12*years_per_run to get the annual averages
  # annual averages will be saved in variables named v[hm]avg??

  # water mass anomaly
  eval xx=\$qhavg
  xx=`echo $xx $years_per_run|awk '{a=(1.0*$1)/(12.0*$2); printf "%g",a}' -`
  eval qhavg=\$xx

  # water hole filling
  eval xx=\$qfavg
  xx=`echo $xx $years_per_run|awk '{a=(1.0*$1)/(12.0*$2); printf "%g",a}' -`
  eval qfavg=\$xx

  # water total mass
  eval xx=\$qmavg
  xx=`echo $xx $years_per_run|awk '{a=(1.0*$1)/(12.0*$2); printf "%g",a}' -`
  eval qmavg=\$xx

  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xx=\$vhavg$nn
    xx=`echo $xx $years_per_run|awk '{a=(1.0*$1)/(12.0*$2); printf "%g",a}' -`
    eval vhavg${nn}=\$xx

    eval xx=\$vfavg$nn
    xx=`echo $xx $years_per_run|awk '{a=(1.0*$1)/(12.0*$2); printf "%g",a}' -`
    eval vfavg${nn}=\$xx

    eval xx=\$vmavg$nn
    xx=`echo $xx $years_per_run|awk '{a=(1.0*$1)/(12.0*$2); printf "%g",a}' -`
    eval vmavg${nn}=\$xx
  done


  #####################################################################
  # obtain a file containing a history of the tuning parameters to date
  #####################################################################
  rm -f XREFHIST
  access XREFHIST $xrefhist na

  # name a lock file to be used to check runaway resubmits
  xflock="${JHOME:-$HOME}/.queue/.crawork/xt_${runid}_lock_file_2"
  [ ! -d `dirname "$xflock"` ] && bail "Invalid lock file name $xflock"

  if [ ! -s XREFHIST ]; then

    # First check for a lock file
    if [ -f "$xflock" ]; then
      rm -f $xflock
      bail "Lock file 2 exists"
    fi
    touch $xflock

    # if the file does not exist create one here
    echo "Creating NEW data base file for xref auto tuning: $xrefhist"

    # Note: setting a_xref?? and p_xref?? to the same values as well as
    # setting vhavg??_1 and vhavg??_2 to the same values will ensure
    # that the correct interpolation (between x2,v2 and x3,v3 values)
    # is done on the first call to next_xref below

    # initialize all xref?? values with init_xref?? values defined above
    eval a_sref\=$init_sref
    eval p_sref\=$init_sref
    eval sref\=$init_sref
    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval a_xref${nn}=\$init_xref$nn
      eval p_xref${nn}=\$init_xref$nn
      eval xref${nn}=\$init_xref$nn
    done

    # set both vhavg??_1 and vhavg??_2 variables to the current vhavg?? values

    qhavg_1=$qhavg
    qhavg_2=$qhavg
    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval an_unused_vname=\$vhavg${nn}
      an_unused_vname=`echo $an_unused_vname|awk '{printf "%g",$1}' -`
      eval vhavg${nn}_1=\"$an_unused_vname\"
      eval vhavg${nn}_2=\"$an_unused_vname\"
    done

    # Determine new sref value based on initial value
    newsref=`echo $qhavg $init_sref|\
      awk '{if ($1>0.0)  {x=0.95*$2};
            if ($1<0.0)  {x=1.05*$2};
            if ($1==0.0) {x=$2};
            printf "%g",x}' -`
    eval sref\=$newsref

    # Determine new xref values based on initial values
    nn=0
    while [ $nn -lt $ntrac ]; do
      nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
      eval tune=\$tune_xref$nn
      eval vhavg=\$vhavg$nn
      eval oldxref=\$init_xref$nn
      if [ $tune -eq 0 ]; then
        # Do not adjust this xref value
        newxref=$oldxref
      else
        # Make an initial guess for the current xref value
        newxref=`echo $vhavg $oldxref|\
          awk '{if ($1>0.0)  {x=0.8*$2};
                if ($1<0.0)  {x=1.2*$2};
                if ($1==0.0) {x=$2};
                printf "%g",x}' -`
      fi
      # redefine the xrefnn variables with new values
      eval xref${nn}=\$newxref
    done

    # Create and save the xref history file
    seqno=1
    write_db XREFHIST $seqno $years_per_run
    save XREFHIST $xrefhist

    # resubmit a job with these new xref values
    resubmit

    # delete model history files from the last iteration
    xtune_delete

    # Normal exit
    exit 0

  fi

  # Clean up lock file
  rm -f $xflock

  ############################################################
  # read previous xref values from a file and update the xrefs
  ############################################################

  next_xref() {
    # next_xref will return the next guess for xref given the previous
    # three xref and vhavg values
    [ -z "$1" ] && bail "x1 is missing from the next_xref command line"
    [ -z "$2" ] && bail "v1 is missing from the next_xref command line"
    [ -z "$3" ] && bail "x2 is missing from the next_xref command line"
    [ -z "$4" ] && bail "v2 is missing from the next_xref command line"
    [ -z "$5" ] && bail "x3 is missing from the next_xref command line"
    [ -z "$6" ] && bail "v3 is missing from the next_xref command line"
    [ -z "$7" ] && bail "alpha is missing from the next_xref command line"
    x1="$1"
    v1="$2"
    x2="$3"
    v2="$4"
    x3="$5"
    v3="$6"
    alpha="$7"
    x4="0.0"
    echo "next_xref: x1=$x1  v1=$v1  x2=$x2  v2=$v2  x3=$x3  v3=$v3   alpha=$alpha"

    # Set the "constants" xc1,vc1,xc2,vc2 depending on the sign of v3
    xc2=$x3
    vc2=$v3
    # sign_v3 will be 1 if v3>0, 0 if v3=0 and -1 if v3<0
    sign_v3=`echo $v3|\
      awk '{if($1==0.0){print "0";exit};
            if($1>0.0){print "1"}else{print "-1"}}' -`
    # echo "sign_v3=$sign_v3"

    if [ $sign_v3 -eq 1 ]; then
      # v3 is positive
      if [ `echo $x1 $x2|awk '{if($1<$2){print "1"}else{print "0"}}' -` ]; then
        # x1 is less than x2
        xc1=$x1
        vc1=$v1
      else
        # x2 is less than or equal to x1
        xc1=$x2
        vc1=$v2
      fi
    else
      # v3 is negative or zero
      if [ `echo $x1 $x2|awk '{if($1>$2){print "1"}else{print "0"}}' -` ]; then
        # x1 is greater than x2
        xc1=$x1
        vc1=$v1
      else
        # x2 is greater than or equal to x1
        xc1=$x2
        vc1=$v2
      fi
    fi
    # echo "next_xref: xc1=$xc1  vc1=$vc1  xc2=$xc2  vc2=$vc2"

    # Interpolate to (x4,v=0) on the line v-vc1 = ((vc2-vc1)/(xc2-xc1)) * (x-xc1)
    # => x4 = (xc1*vc2 - xc2*vc1)/(vc2 - vc1)
    zero_den=`echo $vc2 $vc1|\
      awk '{d=1.0*($2-$1);  if (d==0.0) {print "1";exit};
            dd=sqrt(1.0*$1*$1)+sqrt(1.0*$2*$2); if (dd==0.0) {print "1";exit};
            r=sqrt(d*d)/dd; if (r<1e-4) {print "1"} else {print "0"}}' -`
    if [ $zero_den -eq 1 ]; then
      # The denominator is zero or very small
      if [ $sign_v3 -eq 1 ]; then
        # v3 is positive
        x4=`echo $xc2|awk '{x=0.8*$1;printf "%g",x}' -`
      else
        x4=`echo $xc2|awk '{x=1.2*$1;printf "%g",x}' -`
      fi
      echo "Zero denominator. vc1=$vc1  vc2=$vc2 ...x4 set to $x4"
    else
      x4=`echo $xc1 $vc1 $xc2 $vc2|awk '{x=($1*$4-$3*$2)/($4-$2);printf "%g",x}' -`
    fi
    x4_neg=`echo $x4|awk '{x=1.0*$1; if(x<0.0){print "1"}else{print "0"}}' -`
    if [ $x4_neg -eq 1 ]; then
      echo "next_xref: x4=$x4 is less than zero"
      echo "           xc1=$xc1 vc1=$vc1 xc2=$xc2 vc2=$vc2"
      if [ $sign_v3 -eq 1 ]; then
        # v3 is positive
        x4=`echo $xc2|awk '{x=0.8*$1;printf "%g",x}' -`
      else
        x4=`echo $xc2|awk '{x=1.2*$1;printf "%g",x}' -`
      fi
      echo "           x4 is reset to $x4"
    fi

    # Under relax st x4 = alpha*x4 + (1-alpha)*x3  where 0 <= alpha <= 1
    # ie: for alpha=1 x4 is calcualted as above whereas for alpha<1 the next
    # guess is averaged with the previous value of x3
    alpha_ok=`echo $alpha|awk '{if($1<0.0 || $1>1.0){print "0"}else{print "1"}}' -`
    [ $alpha_ok -eq 0 ] && bail "alpha is out of range in next_xref"
    # echo "next_xref: before x3 = $x3   x4 = $x4   alpha = $alpha"
    x4=`echo $x3 $x4 $alpha|awk '{x=$3*$2+(1.0-$3)*$1;printf "%g",x}' -`
    # echo "next_xref: after x3 = $x3   x4 = $x4   alpha = $alpha"
  }

  # strip the last 29 lines from the data base file and create
  # a separate file containing only the last set to be read below
  tail -29 XREFHIST > prev_XREFHIST
  if [ `wc -l prev_XREFHIST|awk '{print $1}' -` -ne 29 ]; then
    bail "ERROR reading data base file"
  fi

  # read the file once to determine the last sequence number
  all_seqno=`while read seqno tag line; do
               echo $seqno
             done < prev_XREFHIST`
  last_seqno=`echo $all_seqno|awk '{print $NF}' -`
  echo "found in XREFHIST=${xrefhist}: last_seqno=$last_seqno"

  # read most recent sref_[123] values from the database file
  sref_1=''
  sref_2=''
  sref_3=''
  qhavg_1=''
  qhavg_2=''
  sref_1=`while read seqno tag vals; do
            if [ "$seqno" = "$last_seqno" ]; then
              if [ "$tag" = "sref_1" ]; then
                echo $vals
              fi
            fi
          done < prev_XREFHIST`
  sref_2=`while read seqno tag vals; do
            if [ "$seqno" = "$last_seqno" ]; then
              if [ "$tag" = "sref_2" ]; then
                echo $vals
              fi
            fi
          done < prev_XREFHIST`
  sref_3=`while read seqno tag vals; do
            if [ "$seqno" = "$last_seqno" ]; then
              if [ "$tag" = "sref_3" ]; then
                echo $vals
              fi
            fi
          done < prev_XREFHIST`
  qhavg_1=`while read seqno tag vals; do
             if [ "$seqno" = "$last_seqno" ]; then
               if [ "$tag" = "qhavg_1" ]; then
                 echo $vals
               fi
             fi
           done < prev_XREFHIST`
  qhavg_2=`while read seqno tag vals; do
             if [ "$seqno" = "$last_seqno" ]; then
               if [ "$tag" = "qhavg_2" ]; then
                 echo $vals
               fi
             fi
           done < prev_XREFHIST`
  echo "found in XREFHIST=${xrefhist}: sref_1 = $sref_1"
  echo "found in XREFHIST=${xrefhist}: sref_2 = $sref_2"
  echo "found in XREFHIST=${xrefhist}: sref_3 = $sref_3"
  echo "found in XREFHIST=${xrefhist}: qhavg_1 = $qhavg_1"
  echo "found in XREFHIST=${xrefhist}: qhavg_2 = $qhavg_2"
  # set qhavg_3 to the value of qhavg determined above
  qhavg_3=$qhavg
  echo "calculated from current history files: qhavg_3 = $qhavg_3"

  # set all xref??_1 null
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xref${nn}_1=''
  done
  # read most recent xref??_1 values from the database file
  vals=`while read seqno tag vals; do
          if [ "$seqno" = "$last_seqno" ]; then
            if [ "$tag" = "xref_1" ]; then
              echo $vals
            fi
          fi
        done < prev_XREFHIST`
  echo "found in XREFHIST=${xrefhist}: xref_1 = $vals"
  nn=0
  for x in $vals; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xref${nn}_1=$x
  done

  # set all xref??_2 null
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xref${nn}_2=''
  done
  # read most recent xref??_2 values from the database file
  vals=`while read seqno tag vals; do
          if [ "$seqno" = "$last_seqno" ]; then
            if [ "$tag" = "xref_2" ]; then
              echo $vals
            fi
          fi
        done < prev_XREFHIST`
  echo "found in XREFHIST=${xrefhist}: xref_2 = $vals"
  nn=0
  for x in $vals; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xref${nn}_2=$x
  done

  # set all xref??_3 null
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xref${nn}_3=''
  done
  # read most recent xref??_3 values from the database file
  vals=`while read seqno tag vals; do
          if [ "$seqno" = "$last_seqno" ]; then
            if [ "$tag" = "xref_3" ]; then
              echo $vals
            fi
          fi
        done < prev_XREFHIST`
  echo "found in XREFHIST=${xrefhist}: xref_3 = $vals"
  nn=0
  for x in $vals; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval xref${nn}_3=$x
  done

  # set all vhavg??_1 null
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval vhavg${nn}_1=''
  done
  # read most recent vhavg??_1 values from the database file
  vals=`while read seqno tag vals; do
          if [ "$seqno" = "$last_seqno" ]; then
            if [ "$tag" = "vhavg_1" ]; then
              echo $vals
            fi
          fi
        done < prev_XREFHIST`
  echo "found in XREFHIST=${xrefhist}: vhavg_1 = $vals"
  nn=0
  for x in $vals; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval vhavg${nn}_1=$x
  done

  # set all vhavg??_2 null
  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval vhavg${nn}_2=''
  done
  # read most recent vhavg??_2 values from the database file
  vals=`while read seqno tag vals; do
          if [ "$seqno" = "$last_seqno" ]; then
            if [ "$tag" = "vhavg_2" ]; then
              echo $vals
            fi
          fi
        done < prev_XREFHIST`
  echo "found in XREFHIST=${xrefhist}: vhavg_2 = $vals"
  vhavg_3=''
  nn=0
  for x in $vals; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval vhavg${nn}_2=$x
    eval an_unused_vname=\$vhavg$nn
    vhavg_3="$vhavg_3 $an_unused_vname"
  done
  echo "    calc in xtune: vhavg_3 = $vhavg_3"

  # at this point we have the following
  # sref_1 :: antepenultimate sref value
  # sref_2 :: penultimate sref value
  # sref_3 :: last sref value
  # qhavg_1 :: qhavg value corresponding to antepenultimate sref
  # qhavg_2 :: qhavg value corresponding to penultimate sref
  # qhavg   :: qhavg value corresponding to last sref
  # xref??_1 :: antepenultimate xref values
  # xref??_2 :: penultimate xref values
  # xref??_3 :: last xref values
  # vhavg??_1 :: vhavg values corresponding to antepenultimate xrefs
  # vhavg??_2 :: vhavg values corresponding to penultimate xrefs
  # vhavg??   :: vhavg values corresponding to last xrefs

  # Update sref and qhavg values
  tune=$tune_sref
  x1=$sref_1
  x2=$sref_2
  x3=$sref_3
  v1=$qhavg_1
  v2=$qhavg_2
  v3=$qhavg
  echo "moisture:  v1=$v1  v2=$v2  v3=$v3  x1=$x1  x2=$x2  x3=$x3"

  if [ $tune -eq 0 ]; then
    # keep the previous value of xref for this tracer
    x4=$x3
  else
    # Adjust the sref value for moisture
    x4="0.0"
    if [ `echo $x3|awk '{if($1==0.0){print "0"}else{print "1"}}' -` -eq 1 ]; then
      # x3 is nonzero  ...interpolate to get next guess for xref
      next_xref $x1 $v1 $x2 $v2 $x3 $v3 '0.2'
    fi
  fi

  # x4=`echo $qhavg $x3|\
  #   awk '{if ($1>0.0)  {x=0.95*$2};
  #         if ($1<0.0)  {x=1.05*$2};
  #         if ($1==0.0) {x=$2};
  #         printf "%g",x}' -`
  echo "moisture:  x4=$x4"

  # update variables that will be written to the xref history file
  a_sref=$x2
  p_sref=$x3
  eval sref\=$x4
  qhavg_1=$v2
  qhavg_2=$v3

  nn=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval tune=\$tune_xref$nn
    eval x1=\$xref${nn}_1
    eval x2=\$xref${nn}_2
    eval x3=\$xref${nn}_3
    eval v1=\$vhavg${nn}_1
    eval v2=\$vhavg${nn}_2
    eval v3=\$vhavg${nn}
    echo "nn=$nn  v1=$v1  v2=$v2  v3=$v3  x1=$x1  x2=$x2  x3=$x3"

    if [ $tune -eq 0 ]; then
      # keep the previous value of xref for this tracer
      x4=$x3
    else
      # Adjust the xref value for this tracer
      x4="0.0"
      if [ `echo $x3|awk '{if($1==0.0){print "0"}else{print "1"}}' -` -eq 1 ]; then
        # x3 is nonzero  ...interpolate to get next guess for xref
        alpha_x=0.2
        # nn=02,15 implies LSE,DSE for gcm15e tuning
        # constrain these to change very slowly
        [ "$nn" = "02" ] && alpha_x=0.05
        [ "$nn" = "15" ] && alpha_x=0.05
        next_xref $x1 $v1 $x2 $v2 $x3 $v3 $alpha_x
      fi
    fi
    echo "nn=$nn  x4=$x4"

    # update variables that will be written to the xref history file
    eval a_xref${nn}=\$x2
    eval p_xref${nn}=\$x3
    eval xref${nn}=\$x4
    eval vhavg${nn}_1=\$v2
    eval vhavg${nn}_2=\$v3
  done

  # append to the xref history file and save it
  seqno=`expr $last_seqno + 1`
  write_db curr_XREFHIST $seqno $years_per_run
  cat XREFHIST curr_XREFHIST > new_XREFHIST
  delete XREFHIST
  save new_XREFHIST $xrefhist

  # delete model history files from the last iteration
  xtune_delete

  # Evaluate stopping criterion
  resub=1
  nn=0
  number_bad=0
  while [ $nn -lt $ntrac ]; do
    nn=`echo $nn|awk '{n=1+$1;printf "%2.2d", n}' -`
    eval tune=\$tune_xref$nn
    [ $tune -eq 0 ] && continue  # do nothing unless tuning is requested

    eval vhavg=\$vhavg${nn}  # current vhavg for tracer nn
    eval vmavg=\$vmavg${nn}  # current vmavg for tracer nn
    ratio=`echo $vhavg $vmavg|\
             awk '{if($2==0.0){printf "%g",$2}
                   else{if ($1<0.0) {p=-1.0} else {p=1.0};
                        if ($2<0.0) {q=-1.0} else {q=1.0};
                        r=(p*$1)/(q*$2); printf "%g",r}}' -`
    bad=`echo $ratio $stop_tol|awk '{if($1>$2){print "1"}else{print "0"}}' -`
    number_bad=`echo $number_bad $bad|awk '{printf "%d",$1+$2}' -`
    echo "vhavg vmavg ratio stop_tol bad: $vhavg $vmavg $ratio $stop_tol $bad"
  done
  [ $number_bad -eq 0 ] && resub=0
  [ $xtune_iter -ge $max_xtune_iter ] && resub=0

  echo "Number of tracers that exceed $stop_tol tolerance $number_bad"
  # If the solution has not converged then resubmit the job
  [ $resub -eq 1 ] && resubmit

  # normal exit
  exit 0

  # ---Stop_submit_ignore_code----

end_of_script

 . endjcl.cdk

#end_of_job

