#!/usr/bin/ksh

set +x
#####################################################################
echo "-------------------------------------------------------"
echo "exgfdl_tracker.sh.sms - Script to run GFDL tracker"
echo "-------------------------------------------------------"
echo "History: JUN 1997 - First implementation of this new script."
echo "         JAN 1998 - Rewritten by David Michaud to standardize"
echo "         AUG 1998 - Modified by Peter Henrichsen to use script"
echo "                    onebul.sh to make and send storm bulletin."
echo "         NOV 2000 - Modified by Peter Henrichsen to replace "
echo "                    onebul.sh with formbul.pl."
echo "         MAR 2001 - Tim Marchok added save of stats.short file"
echo "                    to STOREhur directory, plus coupled plots"
echo "         JAN 2006 - L. Sager  Modified to use form_ntc.pl to  "
echo "                    send bulletins to TOC via NTC         "
echo "         MAY 2006 - Julia seperated the tracker processing  "
echo "                    from the GFDL_POST job         "
#####################################################################
set -x

PS4=' + exgfdl_tracker.sh.sms line $LINENO: '

label="cat $COMOUT/$stormlabel.$cycle"

if test -f "$label" = "No $stormlabel"
then
   ##############################################
   # Condition if no storms are to be run
   ##############################################
   msg="No hurricane to run."
   postmsg "$jlogfile" "$msg"

   exit
fi

if test -f ${COMIN}/${stormlabel}.holdvars.txt
then
   . ${COMIN}/${stormlabel}.holdvars.txt
else
   msg="Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export pgm=exgfdl_tracker.sh.sms
   export err=911;err_chk
fi

export datein=`echo $DATEIN | tr [A-Z] [a-z]`
export DTEE=$YYMMDD$HRIN

cd $DATA

#--------------------------------------------------------------------#
# Determine script variables from ENVIRONMENTAL VARS....
#--------------------------------------------------------------------#

DATE=$YYMMDD$HRIN
case $HRIN in
  00) hoo="06"
      DATO=$YYMMDD$hoo;;
  06) hoo="12"
      DATO=$YYMMDD$hoo;;
  12) hoo="18"
      DATO=$YYMMDD$hoo;;
  18) hoo="00"
      YYMMDDp1=`sh /nwprod/util/ush/finddate.sh $Y4PREFIX$YYMMDD d+01 | cut -c3-8`
      DATO=$YYMMDDp1$hoo;;
   *):;;
esac

ATCF=`echo $ATCFNAME | cut -c1-4`
ATCC=`echo $ATCFNAME | cut -c1-4`$YEARA
AT=`echo $ATCFNAME | cut -c1-2`
NO=`echo $ATCFNAME | cut -c3-4`

if [ ${NUMLEVS} -eq 18 ]
then
  MODEL='63GFDL'
  MODEO='64GFDO'
else
  MODEL='63GFDL'
  MODEO='66GF2O'
fi

case $AT in
   AL)
     SLFM=${NO}L
     ;;
   EP)
     SLFM=${NO}E
     ;;
   CP)
     SLFM=${NO}C
     ;;
   WP)
     SLFM=${NO}W
     ;;
   *)
     echo  ATCF file name in error
     exit
     ;;
esac

echo $NMO       > nmo
echo $HRIN      > hrin
echo $DAY       > day
echo $STORMENV  > stormenv
echo ${Y4PREFIX}$YEARA   > yeara

echo $SLFM  > slfm
echo $MODEL > model
echo $ATCC  > atcc
echo $DATE  > date
echo $MODEO > modeo
echo $DATO  > dato

rm fort.*

#########################################################################
# Now run the separate tracker script to get another, independent set
# of track points for this run.  This may be useful for cases in which
# the storm traverses mountainous terrain.
#########################################################################

set +x
echo "TIMING: time before call to tracker is `date`"
set -x

export cmodel=gfdl

if [ ${PARAFLAG} = 'YES' ]; then
  export trkrscript=${rundir}
  export exectrkdir=/gpfs/t3/global/save/wx20tm/gfdl/exec/2012_nco
  export exechwrfdir=/gpfs/t3/global/save/wx20tm/gfdl/exec/2012_nco
  export FIXhur=${rundir}
else
  export trkrscript=/nw${envir}/util/ush
  export exectrkdir=/nw${envir}/exec
#  export exechwrfdir=/nw${envir}/hwrf_v6.0.0/exec
  export FIXhur=/nw${envir}/fix
  VERSION_FILE=/nw${envir}/versions/hwrf.ver
  if [ -f $VERSION_FILE ]; then
     . $VERSION_FILE
     export exechwrfdir=/nw${envir}/hwrf.${model_ver}/exec
  else
     echo "***JOB ${SMSNAME} ERROR: Version File $VERSION_FILE does not exist ***"
     exit
  fi
fi

sh $trkrscript/extrkr_gfdl.sh 1 gfdl ${PDY}${cyc}

set +x
echo "TIMING: time after call to tracker is `date`"
set -x

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"

