####  UNIX Script Documentation Block
#
# Script name:         tropcy_relocate.sh
# Script description:  Performs tropical cyclone relocation processing
#
# Author:        D.A. Keyser        Org: NP22         Date: 2007-04-05
#
# Abstract: This script attempts to relocate tropical cyclones in the Global
#   guess fields.  It points to tropical cyclone location records (tcvitals) as
#   input files.  The tcvitals file is normally generated by the previous
#   SYNDAT_QCTROPCY program.  As part of this relocation processing, global
#   sigma guess files valid 3-hours before, 3-hours after, and at the center
#   date/time for the relocation processing are generated.  These files are
#   used later by the Global analysis.  In Global and Regional networks, this
#   new "relocated" global guess file can later be encoded into the PREPBUFR
#   file for use by the quality control programs in the PREPBUFR processing.
#   Here the relocated guess is valid at the center date/time for the PREPBUFR
#   processing).  This script has been designed to be executed by either an
#   operational job script, a test job script, a parallel job script, or a
#   stand-alone batch run initiated by a user.
# 
# Script history log:
# 2006-06-12  Dennis A. Keyser -- Original version for implementation - split
#      off from USH script prepobs_makeprepbufr.sh, this was done to allow
#      the new TROPCY_QC_RELOC job executing this script (which runs after
#      TROPCY_QC_RELOC performs qctropcy processing, moved from the DUMP job)
#      to run at the same time as the DUMP job in order to speed up overall obs
#      processing and remove variability in the subsequent PREP job (i.e., the
#      PREP job had run faster when no tropical cyclones were present)
# 2006-10-19  R. Treadon - replace XLFUNIT_21,22,23,53,56,59 with soft links
#      to avoid conflict with new relocation code which explicitly opens
#      these units to specific filenames.  Also remove fort.12 reference
#      since no longer necessary.
# 2007-04-05  Dennis A. Keyser -- Store pathname to original center-time sigma
#      guess (input to relocation) in /com (with .pre-relocate. qualifier) so
#      it can be identified later
# 2012-08-01  Luke Lin -- alerts inform.relocate, tcvitals.relocate, tropcy_relocation_status
#
# Usage:  tropcy_relocate.sh yyyymmddhh
#
#   Input script positional parameters:
#     1             String indicating the center date/time for the relocation
#                   processing <yyyymmddhh> - if missing, then this time
#                   is obtained from the /com/date/$cycle file unless
#                   the imported variable MACHINE=sgi in which case the
#                   script exits abnormally
#
#   Imported Shell Variables:
#
#     These must ALWAYS be exported to this script by the parent script --
#
#     NET           String indicating system network (either "gfs", "gdas" or
#                   "nam")
#                   NOTE: NET is changed to gdas in the parent Job script for
#                         the RUN=gdas1 (was gfs - NET remains gfs for RUN=gfs)
#     RUN           String indicating model run (either "gfs", "gdas1", "nam",
#                   or "ndas")
#     cycle         String indicating the center cycle hour for relocation
#                   processing {"txxz", where xx is two-digit hour of the day
#                   (UTC)}
#                   {NOTE: This is required ONLY if input script positional
#                          parameter 1 is missing (see above)}
#     DATA          String indicating the working directory path (usually a
#                   temporary location)
#     COMSP         String indicating the directory/filename path to:
#                     -input tropical cyclone location (tcvitals) file output
#                       from previous qctropcy processing
#                     -tropical cyclone relocation (tcvitals.relocate.$tmmark)
#                       file and information (inform.relocate.$tmmark) file,
#                       both output from this processing
#                   (e.g., "/com/gfs/prod/gdas.20060612/gdas1.t12z.")
#
#     These will be set to their default value in this script if not exported
#      to this script by the parent script --
#
#     MACHINE       String indicating machine on which this job is running
#                   Default is "`hostname -s | cut -c 1-3`"
#     envir         String indicating environment under which job runs ('prod'
#                   or 'test')
#                   Default is "prod"
#     HOMEALL       String indicating parent directory path for all files under
#                   which job runs
#                   If the imported variable MACHINE!=sgi, then the default is
#                   "/nw${envir}"; otherwise the default is
#                   "/disk1/users/snake/prepobs"
#     envir_getges  String indicating environment under which GETGES utility
#                   ush runs (see documentation in $USHGETGES/getges.sh for
#                   more information)
#                   Default is "$envir"
#     network_getges
#                   String indicating job network under which GETGES utility
#                   ush runs (see documentation in $USHGETGES/getges.sh for
#                   more information)
#                   Default is "global" unless the center relocation processing
#                   date/time is not a multiple of 3-hrs, then the default is
#                   "gfs"
#     pgmout        String indicating file containing standard output (output
#                   always contatenated onto this file)
#                   Default is "/dev/null"
#     tstsp         String indicating the directory/filename path to input
#                   tropical cyclone location (tcvitals) file output from
#                   previous qctropcy processing that is to override the
#                   corresponding file in $COMSP (this should be imported with
#                   the same naming convention as $COMSP; e.g.,
#                   "/stmp/wx22dk/test_dump/ndas.20060612/ndas.t12z." -
#                   (if tstsp is not imported, the default is used and no
#                   overriding file would exist; if tstsp is imported then any
#                   file found would override the corresponding file in $COMSP)
#                   Default is "/tmp/null/"
#     tmmark      - string indicating hour for center relocation processing
#                   date/time relative to the analysis time embedded in $tstsp
#                   or $COMSP (e.g., "tm12", "tm09", "tm06", "tm03", "tm00")
#                   Default is "tm00"
#     POE_OPTS      String indicating options to use with poe command
#                   Default is "-pgmmodel mpmd -ilevel 2 -labelio yes \
#                   -stdoutmode ordered"
#     USHGETGES     String indicating directory path for GETGES utility ush
#                   file
#                   Default is "${HOMEALL}/util/ush"
#     USHRELO       String indicating directory path for RELOCATE ush files
#                   Default is "${HOMEALL}/ush"
#     EXECRELO      String indicating directory path for RELOCATE executables
#                   Default is "${HOMEALL}/exec"
#     FIXRELO       String indicating directory path for RELOCATE data fix-
#                   field files
#                   Default is "${HOMEALL}/fix"
#     EXECUTIL      String indicating directory path for utility program
#                   executables
#                   If the imported variable MACHINE!=sgi, then the default is
#                   "/nwprod/util/exec"; otherwise the default is
#                   "${HOMEALL}/util/exec"
#     RELOX         String indicating executable path for RELOCATE_MV_NVORTEX
#                   program 
#                   Default is "$EXECRELO/relocate_mv_nvortex"
#     SUPVX         String indicating executable path for SUPVIT utility
#                   program
#                   Default is "$EXECUTIL/supvit"
#     GETTX         String indicating executable path for GETTRK utility
#                   program
#                   Default is "$EXECUTIL/gettrk"
#     SENDCOM       String when set to "YES" copies output files to $COMSP
#                   Default is "YES"
#     SENDDBN       String when set to "YES" alerts output files to $COMSP
#     NDATE         String indicating executable path for NDATE utility program
#                   Default is "$EXECUTIL/ndate"
#
#     These do not have to be exported to this script.  If they are, they will
#      be used by the script.  If they are not, they will be skipped
#      over by the script.
#
#     jlogfile      String indicating path to joblog file
#
#   Exported Shell Variables:
#     CDATE10       String indicating the center date/time for the relocation
#                   processing <yyyymmddhh>
#     CMODEL        String indicating model on which hurricane tracker should
#                   run (this is passed to child script
#                   tropcy_relocate_extrkr.sh - if "$CMODEL" is not set here,
#                   it defaults to "$RUN")
#   
#
#   Modules and files referenced:
#                  Herefile: RELOCATE_GES
#                  $USHRELO/tropcy_relocate_extrkr.sh
#                  $USHGETGES/getges.sh
#                  $NDATE (here and in child script
#                        $USHRELO/tropcy_relocate_extrkr.sh)
#                  /usr/bin/poe
#                  $DATA/postmsg
#                  $DATA/prep_step (here and in child script
#                        $USHRELO/tropcy_relocate_extrkr.sh)
#                  $DATA/err_exit (here and in child script
#                        $USHRELO/tropcy_relocate_extrkr.sh)
#                  $DATA/err_chk (here and in child script
#                        $USHRELO/tropcy_relocate_extrkr.sh)
#        NOTE 1: $DATA/postmsg above is required ONLY if "$jlogfile" is
#                present.
#        NOTE 2: The last three scripts above are NOT REQUIRED utilities.
#                If $DATA/prep_step not found, a scaled down version of it is
#                executed in-line.  If $DATA/err_exit or $DATA/err_chk are not
#                found and a fatal error has occurred, then the script calling
#                it will kill itself and exit with a 555 return code causing
#                all parent scripts to be killed.
#
#     programs   :
#          RELOCATE_MV_NVORTEX - executable $RELOX
#                                 T126 GRIB global land/sea mask:
#                                          $FIXRELO/global_slmask.t126.grb
#          SUPVIT               - executable $SUPVX
#          GETTRK               - executable $GETTX
#
# Remarks:
#
#   Condition codes
#      0 - no problem encountered
#     >0 - some problem encountered
#
# Attributes:
#   Language: POSIX shell
#   Machine: IBM-SP, SGI
#
####

set -aux

MACHINE=${MACHINE:-`hostname -s | cut -c 1-3`}

SENDCOM=${SENDCOM:-YES}

if [ ! -d $DATA ] ; then mkdir -p $DATA ;fi

cd $DATA

qid=$$


#  obtain the center date/time for relocation processing
#  -----------------------------------------------------

if [ $# -ne 1 ] ; then
   if [ $MACHINE != sgi ]; then
      cp /com/date/$cycle ncepdate
      err0=$?
      CDATE10=`cut -c7-16 ncepdate`
   else
      err0=1
   fi
else 
   CDATE10=$1
   if [ "${#CDATE10}" -ne '10' ]; then
      err0=1
   else
      cycle=t`echo $CDATE10|cut -c9-10`z
      err0=0
   fi
fi

if test $err0 -ne 0
then
#  problem with obtaining date record so exit
   set +x
   echo
   echo "problem with obtaining date record;"
   echo "ABNORMAL EXIT!!!!!!!!!!!"
   echo
   set -x
   if [ -s $DATA/err_exit ]; then
      $DATA/err_exit
   else
######kill -9 ${qid}
      exit 555
   fi
   exit 9
fi

pdy=`echo $CDATE10|cut -c1-8`
cyc=`echo $CDATE10|cut -c9-10`
modhr=`expr $cyc % 3`

set +x
echo
echo "CENTER DATE/TIME FOR RELOCATION PROCESSING IS $CDATE10"
echo
set -x

set +u
if [ -z "$XLFRTEOPTS" ]; then
   export XLFRTEOPTS="nlwidth=132"
else
   export XLFRTEOPTS="${XLFRTEOPTS}:nlwidth=132"
fi
set -u
export XLSMPOPTS=parthds=2:stack=1024000000

#----------------------------------------------------------------------------

#  Create variables needed for this script and its children
#  --------------------------------------------------------

envir=${envir:-prod}

if [ $MACHINE != sgi ]; then
   HOMEALL=${HOMEALL:-/nw${envir}}
else
   HOMEALL=${HOMEALL:-/disk1/users/snake/prepobs}
fi

envir_getges=${envir_getges:-$envir}
if [ $modhr -eq 0 ]; then
   network_getges=${network_getges:-global}
else
   network_getges=${network_getges:-gfs}
fi

pgmout=${pgmout:-/dev/null}

tstsp=${tstsp:-/tmp/null/}
tmmark=${tmmark:-tm00}

POE_OPTS=${POE_OPTS:-"-pgmmodel mpmd -ilevel 2 -labelio yes \
 -stdoutmode ordered"}

USHRELO=${USHRELO:-${HOMEALL}/ush}
USHGETGES=${USHGETGES:-${HOMEALL}/util/ush}

EXECRELO=${EXECRELO:-${HOMEALL}/exec}

FIXRELO=${FIXRELO:-${HOMEALL}/fix}

RELOX=${RELOX:-$EXECRELO/relocate_mv_nvortex}

if [ $MACHINE != sgi ]; then
   EXECUTIL=${EXECUTIL:-/nwprod/util/exec}
else
   EXECUTIL=${EXECUTIL:-${HOMEALL}/util/exec}
fi

SUPVX=${SUPVX:-$EXECUTIL/supvit}
GETTX=${GETTX:-$EXECUTIL/gettrk}
TIMEIT=""
[ -s $DATA/timex ] && TIMEIT=$DATA/timex

NDATE=${NDATE:-$EXECUTIL/ndate}

################################################
# EXECUTE TROPICAL CYCLONE RELOCATION PROCESSING
################################################

#  attempt to perform tropical cyclone relocation
#  ----------------------------------------------

msg="Attempt to perform tropical cyclone relocation for $CDATE10"
set +u
[ -n "$jlogfile" ] && $DATA/postmsg "$jlogfile" "$msg"
set -u

if [ $modhr -ne 0 ]; then

#  if center date/time for relocation processing isn't a multiple of 3-hrs, exit
#  -----------------------------------------------------------------------------

   set +x
   echo
   echo "cannot perform tropical cyclone processing because cycle hour is \
not a multiple of 3-hrs;"
   echo "ABNORMAL EXIT!!!!!!!!!!!"
   echo
   set -x
   if [ -s $DATA/err_exit ]; then
      $DATA/err_exit
   else
######kill -9 ${qid}
      exit 555
   fi
   exit 9
fi

for fhr in 6 12 ;do
   if [ ! -s tcvitals.m${fhr} ]; then   # This should never exist, right ????

#  create a null tcvitals file for 06 or 12 hours ago
#  use getges to overwrite with any found

      >tcvitals.m${fhr}
      set +x
      echo
echo "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV"
echo "       Get TCVITALS file valid for -$fhr hrs relative to center"
echo "                    relocation processing date/time"
echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      echo
      set -x
      $USHGETGES/getges.sh -e $envir_getges -n $network_getges \
       -v $CDATE10 -f $fhr -t tcvges tcvitals.m${fhr}
      set +x
      echo
echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      echo
      set -x
   fi
done

for fhr in -6 -3 0 +3 ;do
   if [ $fhr = "-6" ] ; then
      sges=NULL
      pges=pgm6prep
      ptype=pgbgm6
      echo $sges
   elif [ $fhr = "-3" ] ; then
      sges=sgm3prep
      stype=siggm3
      echo $sges
      pges=pgm3prep
      ptype=pgbgm3
      echo $sges
   elif [ $fhr = "0"  ] ; then
      sges=sgesprep
      stype=sigges
      echo $sges
      pges=pgesprep
      ptype=pgbges
      echo $sges
   elif [ $fhr = "+3" ] ; then
      sges=sgp3prep
      stype=siggp3
      echo $sges
      pges=pgp3prep
      ptype=pgbgp3
      echo $sges
   fi
   if [ $sges != NULL -a ! -s $sges ]; then
      set +x
      echo
echo "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV"
echo "     Get global sigma GUESS valid for $fhr hrs relative to center"
echo "                    relocation processing date/time"
echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      echo
      set -x
      $USHGETGES/getges.sh -e $envir_getges -n $network_getges \
       -v $CDATE10 -t $stype $sges
      errges=$?
      if test $errges -ne 0; then
#  problem obtaining global sigma first guess so exit
         set +x
         echo
         echo "problem obtaining global sigma guess valid $fhr hrs relative \
to center relocation date/time;"
         echo "ABNORMAL EXIT!!!!!!!!!!!"
         echo
         set -x
         if [ -s $DATA/err_exit ]; then
            $DATA/err_exit
         else
############kill -9 ${qid}
            exit 555
         fi
         exit 9
      fi

#  For center time sigma guess file obtained via getges, store pathname from
#   getges into ${COMSP}sgesprep_pre-relocate_pathname.$tmmark and, for now,
#   also in ${COMSP}sgesprep_pathname.$tmmark - if relocation processing stops
#   due to an error or due to no input tcvitals records found, then the center
#   time sigma guess will not be modified and this getges file will be read in
#   subsequent PREP processing; if relocation processing continues and the
#   center sigma guess is modified, then ${COMSP}sgesprep_pathname.$tmmark will
#   be removed later in this script {the subsequent PREP step will correctly
#   update ${COMSP}sgesprep_pathname.$tmmark to point to the sgesprep file
#   updated here by the relocation}
#  ----------------------------------------------------------------------------

      if [ $fhr = "0"  ]; then
         $USHGETGES/getges.sh -e $envir_getges -n $network_getges -v $CDATE10 \
          -t $stype > ${COMSP}sgesprep_pre-relocate_pathname.$tmmark
         cp ${COMSP}sgesprep_pre-relocate_pathname.$tmmark \
          ${COMSP}sgesprep_pathname.$tmmark
      fi
      set +x
      echo
echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      echo
      set -x
   fi
   if [ ! -s $pges ]; then
      set +x
      echo
echo "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV"
echo "  Get global pressure grib GUESS valid for $fhr hrs relative to center"
echo "                    relocation processing date/time"
echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      echo
      set -x
      $USHGETGES/getges.sh -e $envir_getges -n $network_getges \
       -v $CDATE10 -t $ptype $pges
      errges=$?
      if test $errges -ne 0; then
#  problem obtaining global pressure grib guess so exit
         set +x
         echo
         echo "problem obtaining global pressure grib guess valid $fhr hrs \
relative to center relocation date/time;"
         echo "ABNORMAL EXIT!!!!!!!!!!!"
         echo
         set -x
         if [ -s $DATA/err_exit ]; then
            $DATA/err_exit
         else
############kill -9 ${qid}
            exit 555
         fi
         exit 9
      fi
      set +x
      echo
echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      echo
      set -x
   fi
done

#  Next line needed to assure that only an analysis file will have the
#   relocation codes run on it

CMODEL=gdas

if [ -f ${tstsp}syndata.tcvitals.$tmmark ]; then
   cp ${tstsp}syndata.tcvitals.$tmmark tcvitals.now
else
   cp ${COMSP}syndata.tcvitals.$tmmark tcvitals.now
fi


[ -s tcvitals.m12 ]  && cat tcvitals.m12  > VITL
[ -s tcvitals.m6  ]  && cat tcvitals.m6  >> VITL
[ -s tcvitals.now ]  && cat tcvitals.now >> VITL

MP_PULSE=0
MP_TIMEOUT=600
GDATE10=$($NDATE -06 $CDATE10)

#  make unique combined tcvitals file for t-12, t-6 and t+0 -- 
#  if tcvitals does not contains record from current time, skip relocation
#  processing
#  -----------------------------------------------------------------------

grep "$pdy $cyc" VITL
errgrep=$?
> tcvitals
if [ $errgrep -ne 0 ] ; then
   msg="NO TCVITAL RECORDS FOUND FOR $CDATE10 - EXIT TROPICAL CYCLONE \
RELOCATION PROCESSING"
   set +u
   [ -n "$jlogfile" ] && $DATA/postmsg "$jlogfile" "$msg"
   set -u

# The existence of ${COMSP}tropcy_relocation_status.$tmmark file will tell the
#  subsequent PREP processing that RELOCATION processing occurred, echo
#  "NO RECORDS to process" into it to further tell PREP processing that records
#   were not processed by relocation and the global sigma guess was NOT
#   modified by tropical cyclone relocation (because no tcvitals records were
#   found)
#   Note:  When tropical cyclone relocation does run to completion and the
#          global sigma guess is modified, the parent script to this will echo
#          "RECORDS PROCESSED" into ${COMSP}tropcy_relocation_status.$tmmark
#          assuming it doesn't already exist (meaning "NO RECORDS to process"
#          was NOT echoed into it here)
# ----------------------------------------------------------------------------

   echo "NO RECORDS to process" > ${COMSP}tropcy_relocation_status.$tmmark
   [ ! -s ${COMSP}tcvitals.relocate.$tmmark ]  &&  \
    cp /dev/null   ${COMSP}tcvitals.relocate.$tmmark
else

   cat VITL >>tcvitals
   grep "$pdy $cyc" VITL > tcvitals.now1 


#  create model forecast track location file
#   $DATA/$RUN.$cycle.relocate.model_track.tm00
#  --------------------------------------------

   $TIMEIT $USHRELO/tropcy_relocate_extrkr.sh
   err=$?
   if [ $err -ne 0 ]; then

#  problem: script tropcy_relocate_extrkr.sh failed
#  ------------------------------------------------

      set +x
      echo
      echo "$USHRELO/tropcy_relocate_extrkr.sh failed"
      echo "ABNORMAL EXIT!!!!!!!!!!!"
      echo
      set -x
      if [ -s $DATA/err_exit ]; then
         $DATA/err_exit "Script $USHRELO/tropcy_relocate_extrkr.sh failed"
      else
#########kill -9 ${qid}
         exit 555
      fi
      exit 9
   fi

#  relocate model tropical cyclone vortices in ges sigma files
# ------------------------------------------------------------

#  READ DATA fort.11 fort.60
#  DATA OUT only from _t09 fort.55

##VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV##
##                          HEREFILE RELOCATE_GES                            ##
##VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV##

cat <<\EOF >RELOCATE_GES

{ echo

set -aux


DATA=$1
gesfhr=$2
multi=$3
DATATMP=$4
inunit=$5
outunit=$6
filename=$7

status=$DATA/mstatus_rel${multi} ; > $status
mp_pgmout=$DATA/mp_rel_pgmout${multi}  ; > $mp_pgmout

{ echo
set +x
echo
echo "********************************************************************"
echo This is task $multi executing on node  `hostname -s`
echo Guess forecast hour is $gesfhr
echo Starting time: `date`
echo "********************************************************************"
echo
set -x
} >> $mp_pgmout

#####if [ $gesfhr = 03 ] ; then sges=sgm3prep ;fi
#####if [ $gesfhr = 06 ] ; then sges=sgesprep ;fi
#####if [ $gesfhr = 09 ] ; then sges=sgp3prep ;fi

cd $DATATMP


echo "$RELOX for gesfhr=${gesfhr}" > $DATA/relocate_exec_name${multi}

echo "$gesfhr" > $DATA/gesfhr${multi}

pgm=`basename  $RELOX`
#-----mimics prep_step-----
set +x
echo $pgm > pgmname
set +u
[ -z "$mp_pgmout" ] && echo "Variable mp_pgmout not set"
set -u
[ -s $DATA/break ] && paste pgmname $DATA/break >> $mp_pgmout
rm pgmname
[ -f errfile ] && rm errfile
export XLFUNITS=0
unset `env | grep XLFUNIT | awk -F= '{print $1}'`

set +u
if [ -z "$XLFRTEOPTS" ]; then
  export XLFRTEOPTS="unit_vars=yes"
else
  export XLFRTEOPTS="${XLFRTEOPTS}:unit_vars=yes"
fi
set -u

[ -s $DATA/tracer ] && cat $DATA/tracer > errfile
set -x
#--------------------------

export XLFUNIT_11=$DATA/tcvitals.now1
export XLFUNIT_30=$DATA/model_track.all
export XLFUNIT_52=$DATA/rel_inform${multi}
export XLFUNIT_55=$DATA/tcvitals.relocate${multi}

# The new relocation code opens GFS spectral coefficient i/o files
# using sigio routines which explicit the files via
# open(unit=number,file=filename) statements.  This conflicts with
# the XLFUNIT statements above.   One can either remove the explicit
# open statements in the relocation code or replace the above
# XLFUNIT lines with soft links.  The soft link approach is taken
# below

ln -s -f $DATA/$filename fort.$inunit

ln -s -f $DATA/$filename.relocate fort.$outunit

echo $gesfhr $LONB $LATB | $TIMEIT $RELOX > relocate.stdout.$gesfhr.$cycle 2>&1
cat relocate.stdout.$gesfhr.$cycle >> $mp_pgmout
errcat=$?
set +x
echo
#echo "The foreground exit status for $RELOX is " $errcat
echo "The foreground exit status for $pgm is " $errcat
echo
set -x

##echo "$multi finished -- err$RELOX = $errcat" > $status
echo "$multi finished -- err$pgm = $errcat" > $status

{ echo
set +x
echo
echo "********************************************************************"
echo Finished executing on node  `hostname -s`
echo Guess forecast hour is $gesfhr
echo Ending time  : `date`
echo "********************************************************************"
echo
set -x
} >> $mp_pgmout

} 2> $DATA/mp_rel_poe${3}.errfile

exit 0
EOF
chmod 775 RELOCATE_GES

##AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA##
##                       end of HEREFILE RELOCATE_GES                        ##
##AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA##

   mkdir -p $DATA/relocate.03
   mkdir -p $DATA/relocate.06
   mkdir -p $DATA/relocate.09

   nprocs=$(echo $LOADL_PROCESSOR_LIST|wc -w)
   >cmd
   cmd[0]="RELOCATE_GES $DATA 03 \$n $DATA/relocate.03 20 53 sgm3prep"
   cmd[1]="RELOCATE_GES $DATA 06 \$n $DATA/relocate.06 21 56 sgesprep"
   cmd[2]="RELOCATE_GES $DATA 09 \$n $DATA/relocate.09 22 59 sgp3prep"
   m=-1
   n=-1
   while [ $((n+=1)) -le $nprocs ] ;do
      while [ $((m+=1)) -le 2 ] ;do
         eval echo ${cmd[m]} | tee -a cmd
         ((n+=1))
      done
     echo "echo do-nothing" >>cmd
   done

# Effective with transition to frost/snow, DO NOT execute a time command
#  with poe command!!!
#########/usr/bin/timex /usr/bin/poe -cmdfile cmd $POE_OPTS
   /usr/bin/poe -cmdfile cmd $POE_OPTS

   errSTATUS=0
   n=-1
   while [ $((n+=1)) -le $nprocs ] ;do
      if [ -s $DATA/mp_rel_pgmout${n} ]; then
         cat $DATA/mp_rel_pgmout${n} >> relocate.out
         cat $DATA/mp_rel_pgmout${n} >> $pgmout

#  check for success
# ------------------

         status=$DATA/mstatus_rel${n}
         gesfhr=`cat gesfhr${n}`
         if [ ! -s $status ]; then
            set +x
            echo
   echo "********************************************************************"
   echo "                   P  R  O  B  L  E  M   !   !   !                  "
   echo "********************************************************************"
   echo " ###> RELOCATE_GES Stream (Task) $n FAILED - Cycle date: $CDATE10"
   echo "       Guess forecast hour is $gesfhr "
   echo "       Current working directory: $DATA                             "
   echo "********************************************************************"
            echo
            set -x
            errSTATUS=99
         fi
         set +x
         echo
   echo "********************************************************************"
   echo "    ++  Script trace from RELOCATE_GES for Stream (Task) $n        ++"
   echo "                 Guess forecast hour is $gesfhr "
   echo "********************************************************************"
         echo

         cat mp_rel_poe${n}.errfile

         echo
   echo "********************************************************************"
   echo " ++  End of Script trace from RELOCATE_GES for Stream (Task) $n    ++"
   echo "                 Guess forecast hour is $gesfhr "
   echo "********************************************************************"
         echo
         set -x
         if [ "$errSTATUS" -gt '0' ]; then
            if [ -s $DATA/err_exit ]; then
               $DATA/err_exit "Script RELOCATE_GES (herefile) failed"
            else
###############kill -9 ${qid}
               exit 555
            fi
            exit 9
         fi
         err=`cut -f 2 -d = $status`
         RELOX=`cat relocate_exec_name${n}`
         pgm=`basename  "$RELOX"`
         touch errfile
         if [ -s $DATA/err_chk ]; then
            $DATA/err_chk
         else
            if test "$err" -gt '0'
            then
###############kill -9 ${qid}
               exit 555
            fi
         fi
         [ "$err" -gt '0' ]  &&  exit 9
      fi
   done

#  further check for success
#  -------------------------

   for sges in sgm3prep sgesprep sgp3prep; do
      if [ -s $sges.relocate ] ; then
         mv $sges.relocate $sges
      else

#  problem: $sges.relocate does not exist
#  --------------------------------------

         if [ -s $DATA/err_exit ]; then
            $DATA/err_exit "The file $sges.relocate does not exist"
         else
############kill -9 ${qid}
            exit 555
         fi
         exit 9
      fi
   done

   if [ -s tcvitals.relocate0 ]; then
      mv tcvitals.relocate0 tcvitals
   else
      >tcvitals
   fi
   rm RELOCATE_GES cmd

   if [ "$SENDCOM" = "YES" ]; then
      cp rel_inform1 ${COMSP}inform.relocate.$tmmark
      cp tcvitals   ${COMSP}tcvitals.relocate.$tmmark
      if [ "$SENDDBN" = "YES" ]; then
         if test "$RUN" = "gdas1"
         then
            $DBNROOT/bin/dbn_alert MODEL GDAS1_TCI $job ${COMSP}inform.relocate.$tmmark
            $DBNROOT/bin/dbn_alert MODEL GDAS1_TCI $job ${COMSP}tcvitals.relocate.$tmmark
         fi
      fi
   fi

   if [ "$SENDDBN" = "YES" ]; then
      if test "$RUN" = "gdas1"
      then
         $DBNROOT/bin/dbn_alert MODEL GDAS1_TCI $job ${COMSP}tropcy_relocation_status.$tmmark
      fi
   fi
#  Since relocation processing has ended sucessfully (and the center sigma
#   guess has been modified), remove ${COMSP}sgesprep_pathname.$tmmark (which
#   had earlier had getges center sigma guess pathname written into it - in
#   case of error or no input tcvitals records found) - the subsequent PREP
#   step will correctly update ${COMSP}sgesprep_pathname.$tmmark to point to
#   the sgesprep file updated here by the relocation
#  --------------------------------------------------------------------------

   rm ${COMSP}sgesprep_pathname.$tmmark

   msg="TROPICAL CYCLONE RELOCATION PROCESSING SUCCESSFULLY COMPLETED FOR \
$CDATE10"
   set +u
   [ -n "$jlogfile" ] && $DATA/postmsg "$jlogfile" "$msg"
   set -u

# end GFDL ges manipulation
#------------------------------------------------------------

fi

exit 0

