#! /bin/ksh
# Jun 2009 Revised for HWRF 2009 implementation, Vijay Tallapragada
set -x
# Script written by Tim Marchok

if [ $# -lt 2 ]; then
  echo " "
  echo "  Script `basename $0` will get the last TC Vitals records for a "
  echo "  particular storm at an input yyyymmddhh.  At the command line, "
  echo "  you need to supply the 3-character storm identifier (eg, 02L, "
  echo "  05E, etc) and the date (yyyymmddhh)."
  echo " "
  echo "  Usage: `basename $0` stormid yyyymmddhh"
  echo " "
  exit 8
fi

stid=$1
ymdh=$2

yyyy=` echo $ymdh | cut -c1-4`
yymmdd=` echo $ymdh | cut -c3-8`
hh=` echo $ymdh | cut -c9-10`

basinchar=` echo $stid | cut -c3-3 | tr '[A-Z]' '[a-z]'`
STID=` echo $stid | tr '[a-z]' '[A-Z]'`

if [[ "$PARAFLAG" == YES ]] ; then
    invit=$( $USHhwrf/hwrf_syndat_tcvitals.sh "$ymdh" )
else
    invit=/com/arch/prod/syndat/syndat_tcvitals.$yyyy
fi

if [ ${yyyy} -ge 2003 ]; then
    vitfile=$invit
    grep -i $stid ${vitfile}                      | \
            grep "${yymmdd} ${hh}"                                | \
            awk '{ if (length($0) > 93) {print $0} }'             | \
            awk 'substr($0,6,3) ~ storm {print $0}' storm=$STID   | \
            tail -1
else
  vitdir=/global/save/wx20vt/syndat
  vitfile=$vitdir/syndat_tcvitals.${yyyy}
  if [ $basinchar = 'l' -o $basinchar = 'e' -o $basinchar = 'c' ]; then
    grep -i $stid ${vitfile}                                      | \
            grep "${yymmdd} ${hh}"                                | \
            awk 'substr($0,6,3) ~ storm {print $0}' storm=$STID   | \
            awk '{ if (length($0) > 93) {print $0} }'             | \
            tail -1
  else
    grep -i $stid ${vitfile}                                      | \
            awk 'substr($0,6,3) ~ storm {print $0}' storm=$STID   | \
            grep "${yymmdd} ${hh}"                                | \
            tail -1
  fi
fi
