#! /bin/ksh

# This script will run the gfs_track0 in parallel with the forecast
# and post-processors.  All tracks are produced by this one job.

set -x
PS4=' + exhwrf_fcst_gfs_track0.sh.sms line $LINENO: ' 

sh ${utilscript}/setup.sh
export pgm=exhwrf_fcst_gfs_track0.sh.sms
. prep_step
startmsg

# ----------------------------------------------------------------------
# Source the holdvars file if it exists

if test -f ${COMOUT}/${stormlabel}.holdvars.txt
then
   . ${COMOUT}/${stormlabel}.holdvars.txt
else
   set +x
   echo " "
   echo "!!! ERROR: holdvars.txt file missing"
   echo " "
   set -x
   msg="Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export err=911;err_chk
   err_exit "Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
fi

export ATMOS_DOMAINS="${ATMOS_DOMAINS:-3}"
export CSTREAM=${DATA}/$STORM.$PDY$cyc
export TRKDIR="$CSTREAM/GFS_TRACK0"
export vit=$CSTREAM/MESSAGES/storm_vit

WRFDIR=${DATA}/$STORM.${YYYYMMDDHH}/ATMOS

cat<<EOF
INPUT VARIABLES TO GFS VORTEX FINDER:

CSTREAM  = $CSTREAM
WRFDIR   = $WRFDIR
TRKDIR   = $TRKDIR
STORM    = $STORM $STORMID
CYCLE    = $YYYYMMDDHH
CEN LOC  = ${CENLA}N ${CENLO}E
STORMLOC = ${STORM_LAT}N ${STORM_LON}E
VITALS   = $vit
VITALS CONTENTS:
  "`cat $vit`"
EOF

# ----------------------------------------------------------------------
# Prepare the directories

function fail {
    msg="$*"
    "${utilscript:-${NWPROD:-/nwprod}/util/ush}/setup.sh"
    ./err_exit "$exitmsg"
}

rm -rf "$TRKDIR"
$USHhwrf/hwrf_mkdirp.sh "$TRKDIR"

cd "$TRKDIR"
if [[ "$?" != 0 ]] ; then
    fail "Cannot cd to $TRKDIR"
fi

# ----------------------------------------------------------------------
# Run the tracker script

"$USHhwrf/hwrf_gfs_track0.sh" \
    "$STORM" "$STORMID" "$YYYYMMDDHH" "$CENLA" "$CENLO" \
    "$STORM_LAT" "$STORM_LON" "$WRFDIR" "$vit"
export err="$?"
if [[ "$err" != 0 ]] ; then
    fail "Warning: hwrf_gfs_track0.sh could not find the GFS hour 0 vortex location (return status $err).  Relocation will use the tcvitals storm location instead."
fi
