#!/usr/bin/ksh

export PS4=' + genesis_tracker.sh line $LINENO: '

set +x
##############################################################################
echo " "
echo "------------------------------------------------"
echo "xxxx - Track vortices in model GRIB output"
echo "------------------------------------------------"
echo "History: Mar 1998 - Marchok - First implementation of this new script."
echo "         Apr 1999 - Marchok - Modified to allow radii output file and"
echo "                              to allow reading of 4-digit years from"
echo "                              TC vitals file."
echo "         Oct 2000 - Marchok - Fixed bugs: (1) copygb target grid scanning"
echo "                              mode flag had an incorrect value of 64"
echo "                              (this prevented Eta, NGM and ECMWF from"
echo "                              being processed correctly);" 
echo "                              Set it to 0."
echo "                              (2) ECMWF option was using the incorrect"
echo "                              input date (today's date instead of "
echo "                              yesterday's)."
echo "         Jan 2001 - Marchok - Hours now listed in script for each model"
echo "                              and passed into program.  Script included"
echo "                              to process GFDL & Ensemble data.  Call to"
echo "                              DBN included to pass data to OSO and the"
echo "                              Navy.  Forecast length extended to 180"
echo "                              hours for GFS."
echo "         Nov 2004 - Burroughs - Modified scripts to make extratropical"
echo "                              storm tracker operational.  Added hooks in"
echo "                              post processing jobs for various models."
echo "         May 2006 - Burroughs - Modified scripts to accommodate the new"
echo "                              gefs file structure.  Cut the number of"
echo "                              files used by going from 6-h frequency to"
echo "                              12-h frequency from 120 - 180-h."
echo "       May 2007 - Guang P Lou - Expanded scripts to include the following"
echo "                    changes: "
echo "                  1. Short range ensembles(sref) expanded to 21 from 14;  "
echo "                     model forecast length increased from 63hrs to 87hrs      "
echo "                  2. Canadian model is added                                 "
echo "                  3. Canadian ensemble with 17 perturbation runs are initiated  "
echo "                  4. ECMWF model frequency increased from 1 (00z) to 2 (00z,12z); "
echo "                     model length extended from 180hrs to 240hrs    "
echo "                  5. ECMWF ensemble runs with 50 components are added; "
echo "                     ECMWF ensemble runs once a day only at 12z to 240hrs "
echo "                  6. Region domain boundaries have changed for ecml and wpcg"
echo "                     and domain cptg has been removed                         "
echo "                  7. Tracks are sorted to order and to files according "
echo "                     to region and model and saved for future usage "
echo "                  8. All cyclones lived shorter than 24hrs are removed "
echo "                  9. Tracks are also grouped for graphics in post processing "
echo "                 10. The cyclone center pressure gradient is relaxed to 1hPa from 2hPa  "
echo "  "
echo "                    In the event of a crash, you can contact Guang Ping Lou "
echo "                    at NCEP/EMC at (301) 763-8000x7252 or "
echo "                    guang.ping.lou@noaa.gov or Tim Marchok at "
echo "                    tpm@gfdl.gov"
echo " "
echo "Current time is: `date`"
echo " "
##############################################################################
set -x

##############################################################################
#
#    FLOW OF CONTROL
#
# 1. Define data directories and file names for the input model 
# 3. Define extratropical and tropical regions
# 3. Process input starting date/cycle information
# 4. Update TC Vitals file and select storms to be processed
# 5. Cut apart input GRIB files to select only the needed parms and hours
# 6. Execute the tracker
# 7. Copy the output track files to various locations
#
##############################################################################

########################################
msg="has begun for ${cmodel} at ${cyc}z"
postmsg "$jlogfile" "$msg"
########################################

# This script runs the hurricane tracker using operational GRIB model output.  
# This script makes sure that the data files exist, it then pulls all of the 
# needed data records out of the various GRIB forecast files and puts them 
# into one, consolidated GRIB file, and then runs a program that reads the TC 
# Vitals records for the input day and updates the TC Vitals (if necessary).
# It then runs genesis_gettrk, which actually does the tracking.
# 
# Environmental variable inputs needed for this script:
#  PDY      -- The date for data being processed, in YYYYMMDD format
#  CYL      -- The numbers for the cycle for data being processed (00, 06, 09, 
#              12, 18, 21)
#  cmodel   -- Model being processed (gfs, ukmet, ecmwf, nam, ngps, gdas, gfdl, 
#              gefs (ncep GFS ensemble), sref (nam ensemble), cens (canadian 
#              ensemble; to be added), north american ensemble - to be added)
#  envir    -- 'prod', 'para', or 'test'
#  SENDCOM  -- 'YES' or 'NO'
#  stormenv -- This is only needed by the tracker run for the GFDL model.
#              'stormenv' contains the name/id that is used in the input
#              grib file names.
#  pert     -- This is only needed by the tracker run for the NCEP ensembles.
#              'pert' contains the ensemble member id (e.g., n2, p4, etc.)
#              which is used as part of the grib file names.
#  loopnum  -- This is used to determine which region is used by the program;
#              region types (regtype) include:
#                 ecml = Extratropical cyclogenesis, mid-latitude Atlantic and
#                        Pacific; loopnum = 1
#                 altg = Atlantic Basin, tropical cyclogenesis; loopnum = 2
#                 eptg = Eastern Pacific Basin, tropical cyclogenesis;
#                        loopnum = 3
#                 wptg = Western Pacific Basin, tropical cyclogenesis; 
#                        loopnum = 4
#
# For testing script interactively in non-production set following vars:
#     gfsvitdir  - Directory for GFS Error Checked Vitals
#     namvitdir  - Directory for Eta Error Checked Vitals
#     gltrkdir   - Directory for output tracks
#     homesyndir - Directory with syndir scripts/exec/fix 
#     archsyndir - Directory with syndir scripts/exec/fix 
#
#-----------------------------------------------------------------------------
# This job runs the tracker for an input cmodel, loopnum, PDY, and CYL
#-----------------------------------------------------------------------------

set +x
echo " "
echo "Time at beginning of `basename $0` is `date`"
set -x

qid=$$

#--------------------------------------------------
#   Get input information
#--------------------------------------------------

export PDY=${PDY:-$1}
export CYL=${CYL:-$2}
export CYCLE=t${CYL}z
export cmodel=${cmodel:-$3}
export loopnum=${loopnum:-$4}
export jobid=${jobid:-prodjob}
export envir=${envir:-prod}
export SENDCOM=${SENDCOM:-YES}
export PARAFLAG=${PARAFLAG:-NO}

scc=`echo ${PDY} | cut -c1-2`
syy=`echo ${PDY} | cut -c3-4`
smm=`echo ${PDY} | cut -c5-6`
sdd=`echo ${PDY} | cut -c7-8`
shh=${CYL}
symd=`echo ${PDY} | cut -c3-8`
syyyy=`echo ${PDY} | cut -c1-4`
export CENT=${scc}

export DATA=${DATA:-/ptmp/wx22gl/extrkr}

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

cd $DATA
/nwprod/util/ush/setup.sh

if [ ${PARAFLAG} = 'YES' ]; then
   /nwprod/util/ush/setup.sh
fi

if [ ${#PDY} -eq 0 -o ${#CYL} -eq 0 -o ${#cmodel} -eq 0 -o ${#loopnum} -eq 0 ]
then
   set +x
   echo " "
   echo "Something wrong with input data.  One or more input variables has "
   echo "length 0. PDY = ${PDY}; CYL = ${CYL}; cmodel = ${cmodel};"
   echo "loopnum = ${loopnum}."
   echo "EXITING.........."
   set -x
   err_exit " FAILED ${jobid} -- BAD INPUTS AT LINE $LINENO IN TRACKER SCRIPT - ABNORMAL EXIT"
else
  set +x
  echo " "
  echo " #-----------------------------------------------------------------#"
  echo " At beginning of tracker script, the following imported variables "
  echo " are defined: "
  echo "   PDY ................................... $PDY"
  echo "   CYL ................................... $CYL"
  echo "   CYCLE ................................. $CYCLE"
  echo "   cmodel ................................ $cmodel"
  echo "   loopnum................................ $loopnum"
  echo "   jobid ................................. $jobid"
  echo "   envir ................................. $envir"
  echo "   SENDCOM ............................... $SENDCOM"
  echo " "
  set -x
fi

if [ ${cmodel} = 'gefs' ]; then
# Global ensemble
  pertstring=' p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20 c00'
elif [ ${cmodel} = 'sref' ]; then
# Short range ensemble
  pertstring=' sac1 san1 sap1 san2 sap2 sec1 sec2 sen1 sen2 sep1 sep2 snc1 snn1 snp1 snn2 snp2 src1 srn1 srp1 srn2 srp2'
elif [ ${cmodel} = 'eens' ]; then
# ECMWF ensemble
  pertstring=' p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20 p21 p22 p23 p24 p25 n01 n02 n03 p04 n05 n06 n07 n08 n09 n10 n11 n12 n13 n14 n15 n16 n17 n18 n19 n20 n21 n22 n23 n24 n25'
elif [ ${cmodel} = 'cens' ]; then
# Canadian ensemble
  pertstring=' c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16'
else 
  pertstring=' xxxx'
fi

set +x
echo " "
echo " "
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo " PROCESSING LOOP NUMBER $loopnum IN tracker.sh"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo " "
echo " "
set -x

for pert in ${pertstring}
do
   export pert

   if [ ${loopnum} -eq 1 ]; then
      # ecml: Extratropical cyclogenesis, mid-latitude Atlantic and Pacific
      export trkrtype=midlat
      export trkrebd=350.0
      export trkrwbd=150.0
#      export trkrwbd=195.0
      export trkrnbd=88.0
      export trkrsbd=20.0
      export mslpthresh=0.0015
      export v850thresh=1.5000
      export regtype=ecml
   fi

   if [ ${loopnum} -eq 2 ]; then
      # altg: Atlantic Basin, tropical cyclogenesis
      export trkrtype=tcgen
      export trkrebd=350.0
      export trkrwbd=260.0
      export trkrnbd=40.0
      export trkrsbd=1.0
      export mslpthresh=0.0015
      export v850thresh=1.5000
      export regtype=altg
   fi

   if [ ${loopnum} -eq 3 ]; then
      # eptg: eastern Pacific Basin, tropical cyclogenesis
      export trkrtype=tcgen
      export trkrebd=280.0
      export trkrwbd=220.0
      export trkrnbd=40.0
      export trkrsbd=1.0
      export mslpthresh=0.0015
      export v850thresh=1.5000
      export regtype=eptg
   fi

   if [ ${loopnum} -eq 4 ]; then
      # wptg: western Pacific Basin, tropical cyclogenesis
      export trkrtype=tcgen
      export trkrebd=180.0
      export trkrwbd=105.0
      export trkrnbd=50.0
      export trkrsbd=1.0
      export mslpthresh=0.0015
      export v850thresh=1.5000
      export regtype=wptg
   fi

   echo "TIMING: Time before call to tracker for loop ${loopnum} is `date`"

   export TRKDATA=${DATA}/${cmodel}
   if [ ! -d ${TRKDATA} ]; then mkdir -p ${TRKDATA}; fi

   export gfsvitdir=${gfsvitdir:-/com/gfs/prod/gfs.$PDY}
   export namvitdir=${namvitdir:-/com/nam/prod/nam.$PDY}
   export gltrkdir=${gltrkdir:-/com/hur/${envir}/global}

   export homesyndir=${homesyndir:-/nwprod/util}
   export exectrkdir=${exectrkdir:-${homesyndir}/exec}
   export ushtrkdir=${ushtrkdir:-${homesyndir}/ush}
   export archsyndir=${archsyndir:-/com/arch/prod/syndat}

   export savedir=${gltrkdir}/${syyyy}
if [ ! -d ${savedir} ];   then mkdir -p ${savedir}; fi

   if [ -s /nwprod/util/exec/wgrib ]; then
      wgrib=/nwprod/util/exec/wgrib
   else
      set +x
      echo " "
      echo "!!! ERROR: wgrib is not available, script will crash.  Exiting...."
      echo " "
      set -x
      err_exit " FAILED ${jobid} -- line= $LINENO IN TRACKER SCRIPT - ABNORMAL EXIT"
   fi

   wgrib_parmlist=" HGT:850 HGT:700 UGRD:850 UGRD:700 UGRD:500 VGRD:850 VGRD:700 VGRD:500 SurfaceU SurfaceV ABSV:850 ABSV:700 PRMSL:MSL "

   wgrib_ec_hires_parmlist=" GH:850 GH:700 U:850 U:700 U:500 V:850 V:700 V:500 10U:sfc 10V:sfc MSL:sfc "

   export maxtime=65    # Max number of forecast time levels

#----------------------------------------------------------------#
#
#    --- Define data directories and data file names ---
#               
# Convert the input model to lowercase letters and check to see 
# if it's a valid model, and assign a model ID number to it.  
# This model ID number is passed into the Fortran program to 
# let the program know what set of forecast hours to use in the 
# ifhours array.  Also, set the directories for the operational 
# input GRIB data files and create templates for the file names.
# While only 1 of these sets of directories and file name 
# templates is used during a particular run of this script, 
# "gfsvitdir" is used every time, because that is the directory 
# that contains the error-checked TC vitals file that Steve Lord 
# produces, and so it is included after the case statement.
#
# NOTE: The varible PDY is now defined within the J-Jobs that
# call this script.  Therefore there is no reason to do this
# here.
#
# NOTE: The script that processes the ECMWF data defines PDY as
# the current day, and in this script we need PDY to be 
# yesterday's date (for the ecmwf ONLY).  So instead, the ecmwf
# script will pass the variable PDYm1 to this script, and in the
# case statement below we change that to PDY.
#
# NOTE: Do NOT try to standardize this script by changing all of 
# these various data directories' variable names to be all the 
# same, such as "datadir".  As you'll see in the data cutting 
# part of this script below, different methods are used to cut 
# apart different models, thus it is important to know the 
# difference between them....
#----------------------------------------------------------------#

   cmodel=`echo ${cmodel} | tr "[A-Z]" "[a-z]"`

   case ${cmodel} in 

      gfs) set +x                                         ;
         echo " "; echo " ++ operational GFS chosen"      ;
         echo " "                                         ;
         set -x                                           ;
         gfsdir=/com/gfs/prod/gfs.${PDY}                  ;
         gfsgfile=gfs.t${CYL}z.pgrbf                      ;
         gfsifile=gfs.t${CYL}z.pgrbif                     ;
         COM=/com/gfs/${envir}/gfs.${PDY}                 ;
         fcstlen=180                                      ;
         fcsthrs=' 00 06 12 18 24 30 36 42 48 54 60 66 72 78
                   84 90 96 102 108 114 120 126 132 138 144
                   150 156 162 168 174 180  99  99  99  99
                   99  99  99  99  99  99  99  99  99  99
                   99  99  99  99  99  99  99  99  99  99
                   99  99  99  99  99  99  99  99  99  99' ;
         atcfnum=15                                       ;
         atcfname="gfso"                                  ;
         atcfout="gfso"                                    ;
         grmodel="gfso"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=1                                         ;;

      ukmet) set +x                                       ; 
         echo " "; echo " ++ operational UKMET chosen"    ;
         echo " "                                         ;
         set -x                                           ;
         ukmetdir=/com/mrf/prod/ukmet.${PDY}              ;
         ukmetgfile=ukmet.t${CYL}z.ukmet                  ;
         ukmetifile=ukmet.t${CYL}z.ukmeti                 ;
         COM=/com/mrf/${envir}/ukmet.${PDY}               ;
         fcstlen=72                                       ;
         fcsthrs=' 00 12 24 36 48 60 72 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99' ;
         atcfnum=17                                       ;
         atcfname="ukx "                                  ;
         atcfout="ukx"                                    ;
         grmodel="ukx"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=3                                         ;;

      ecmwf) set +x                                       ;
         echo " "; echo " ++ operational ECMWF chosen"    ;
         echo " "                                         ;
         set -x                                           ;
#         ecmwfdir=/com/mrf/prod/ecmwf.${PDY}              ;
#         ecmwfgfile=ecmgrb25.t${CYL}z                     ;
#         ecmwfifile=ecmgrbi25.t${CYL}z                    ;
         ecmwfdir=/dcom/us007003/${PDY}/wgrbbul/ecmwf      ;
         ecmwfgfile=                                      ;
         ecmwfifile=                                      ;
         COM=/com/mrf/${envir}/ecmwf.${PDY}               ;
         fcstlen=240                                      ;
         fcsthrs=' 00 12 24 36 48 60 72 84 96 108 120 132 144
                   156 168 180 192 204 216 228 240 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99' ;
         atcfnum=19                                       ;
         atcfname="emx "                                  ;
         atcfout="emx"                                    ;
         grmodel="emx"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=4                                         ;;

      nam) set +x                                         ;
         echo " "; echo " ++ operational Early Eta chosen";
         echo " "                                         ;
         set -x                                           ;
         namdir=/com/nam/prod/nam.${PDY}                  ;
         namgfile=nam.t${CYL}z.awip32                     ;
         namifile=nam.t${CYL}z.awip32i                    ;
         COM=/com/nam/${envir}/nam.${PDY}                 ;
         fcstlen=84                                       ;
         fcsthrs=' 00 06 12 18 24 30 36 42 48 54 60 66 72 78
                   84 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99';
         atcfnum=73                                       ;
         atcfname="nam "                                  ;
         atcfout="nam"                                    ;
         grmodel="nam"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='regional'                                ;
         model=6                                         ;;

      ngps) set +x                                        ;
         echo " "; echo " ++ operational NOGAPS chosen"   ;
         echo " "                                         ;
         set -x                                           ;
         ngpsdir=/com/fnmoc/prod/nogaps.${PDY}            ;
         ngpsgfile=nogaps_${PDY}${CYL}f                   ; 
         ngpsifile=
         COM=/com/fnmoc/prod/nogaps.${PDY}/track          ;
         if [ ! -d $COM ]; then mkdir -p $COM ; fi        ;
         fcstlen=144                                      ;
         fcsthrs=' 00 12 24 36 48 60 72 84 96 108 120 132 144
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99' ;
         atcfnum=29                                       ;
         atcfname="ngx "                                  ;
         atcfout="ngx"                                    ;
         grmodel="ngx"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=7                                         ;; 

      gdas) set +x                                        ;
         echo " "; echo " ++ operational GDAS (relocation) chosen";
         echo " "                                         ;
         set -x                                           ;
         gdasdir=/com/gfs/prod/gdas.${PDY}                ;
         gdasgfile=gdas1.t${CYL}z.pgrbf                   ;
         gdasifile=gdas1.t${CYL}z.pgrbif                  ;
         COM=/com/gfs/${envir}/gdas.${PDY}                ;
         fcstlen=9                                        ;
         fcsthrs=' 00 03 06 09 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99' ;
         atcfnum=72                                       ;
         atcfname="gdas"                                  ;
         atcfout="gdas"                                   ;
         grmodel="gdas"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=8                                         ;;

      gfdl) set +x                                        ;
         echo " "; echo " ++ operational GFDL chosen"     ;
         echo " "                                         ;
         set -x                                           ;
         gfdldir=/com/hur/prod/hur.${PDY}${CYL}           ;
         gfdlgfile=${stormenv}.${PDY}${CYL}.grib3rd.f     ;
         gfdlifile=${stormenv}.${PDY}${CYL}.grib3rd.if    ;
         COM=/com/hur/${envir}/hur.${PDY}${CYL}           ;
         fcstlen=126                                      ;
         fcsthrs=' 00 06 12 18 24 30 36 42 48 54 60 66 72 78
                   84 90 96 102 108 114 120 126 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99';
         stormid=${atcfid}                                ;
         atcfnum=81                                       ;
         atcfname="gfdt"                                  ;
         atcfout="gfdt"                                   ;
         grmodel="gfdt"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='regional'                                ;
         model=9                                         ;;

      gefs) set +x                                         ;
         echo " "                                         ;
         echo " ++ operational ensemble member ${pert} chosen";
         pert=` echo ${pert} | tr '[A-Z]' '[a-z]'`        ;
         PERT=` echo ${pert} | tr '[a-z]' '[A-Z]'`        ;
         echo " "                                         ;
         set -x                                           ;
         ensdir=/com/gens/${envir}/gefs.${PDY}/${CYL}/pgrba   ;
         ensgfile=ge${pert}.t${CYL}z.pgrbaf               ;
         ensifile=ge${pert}.t${CYL}z.pgrbaif              ;
         COM=/com/gens/${envir}/gefs.${PDY}/${CYL}/track  ;
         fcstlen=240                                      ;
         fcsthrs=' 00 06 12 18 24 30 36 42 48 54 60 66 72 78
                   84 90 96 102 108 114 120 126 132 138 144
                   150 156 162 168 174 180 186 192 198 204 210
                   216 222 228 234 240  99  99  99  99  99  99
                    99  99  99  99  99  99  99  99  99  99  99
                    99  99  99  99  99  99  99';
         atcfnum=91                                       ;
         pert_posneg=` echo "${pert}" | cut -c1-1`        ;
         pert_num=`    echo "${pert}" | cut -c2-3`        ;
         atcfname="a${pert_posneg}${pert_num}"            ;
         atcfout="a${pert_posneg}${pert_num}"             ;
         grmodel="a${pert_posneg}${pert_num}"             ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=10                                        ;;

 ensr) set +x                                          ;
       echo " "; echo " ++ ensemble RELOCATION chosen for member ${pert}";
       echo " "                                         ;
       set -x                                           ;
       if [ ${ensclen} -eq 6 ]; then                      
         fcstlen=6                                       ;
         fcsthrs=' 00 06 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99' ;
         atcfname="y${pert}"                             ;
         atcfout="y${pert}"                              ;
         grmodel="y${pert}"                              ;
       else
         fcstlen=24                                      ;
         fcsthrs=' 24 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99' ;
         atcfname="z${pert}"                             ;
         atcfout="z${pert}"                              ;
         grmodel="z${pert}"                              ;
       fi
       pert=` echo ${pert} | tr '[A-Z]' '[a-z]'`        ;
       PERT=` echo ${pert} | tr '[a-z]' '[A-Z]'`        ;
       atcfnum=72                                       ;
       ensrdir=${COMIN}                                 ;
#      ensrgfile=en${pert}.t${CYL}z.pgrbf               ;
       ensrgfile=en${pert}.t${CYL}z.reloc.pgrbf         ;
#      ensrifile=en${pert}.t${CYL}z.pgrbif              ;
       ensrifile=en${pert}.t${CYL}z.reloc.pgrbif        ;
       COM={ensrdir}                                    ;
       mslpthresh=0.0020                                ;
       v850thresh=2.0000                                ;
       modtyp='global'                                  ;
       model=11                                        ;;

  aear) set +x                                          ;
       echo " "; echo " ++ GFS ensemble control analysis (for relocation purposes) chosen";
       echo " "                                         ;
       set -x                                           ;
       fcstlen=0                                       ;
       fcsthrs=' 00 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99' ;
       atcfname="aear"                                  ;
       atcfout="aear"                                   ;
       grmodel="aear"                                   ;
       aeardir=${COMIN}                                 ;
       aeargfile=en${pert}.t${CYL}z.reloc.pgrbf         ;
       aearifile=en${pert}.t${CYL}z.reloc.pgrbif        ;
       COM={aeardir}                                    ;
       mslpthresh=0.0020                                ;
       v850thresh=2.0000                                ;
       modtyp='global'                                  ;
       model=12                                        ;;

    sref) set +x                                        ;
       echo " "; echo " ++ operational SREF ensemble member ${pert} chosen"     ;
       echo " "                                         ;
       set -x                                           ;
       pert=` echo ${pert} | tr '[A-Z]' '[a-z]'`        ;
       PERT=` echo ${pert} | tr '[a-z]' '[A-Z]'`        ;
       srtype=` echo ${pert} | cut -c1-2`               ;
       srpertnum=` echo ${pert} | cut -c3-4`            ;
       if [ ${srtype} = 'se' ]; then
         # Eta members
         if [ ${srpertnum} = 'c1' ]; then
           srefgfile=sref_eta.t${CYL}z.pgrb221.ctl1.f
         elif [ ${srpertnum} = 'c2' ]; then
           srefgfile=sref_eta.t${CYL}z.pgrb221.ctl2.f
         else
           srefgfile=sref_eta.t${CYL}z.pgrb221.${srpertnum}.f
         fi
       elif [ ${srtype} = 'sa' ]; then
         # ARW members
         if [ ${srpertnum} = 'c1' ]; then
           srefgfile=sref_em.t${CYL}z.pgrb221.ctl.f
         else
           srefgfile=sref_em.t${CYL}z.pgrb221.${srpertnum}.f
         fi
       elif [ ${srtype} = 'sr' ]; then
         # RSM members
         if [ ${srpertnum} = 'c1' ]; then
           srefgfile=sref_rsm.t${CYL}z.pgrb221.ctl1.f
         else
           srefgfile=sref_rsm.t${CYL}z.pgrb221.${srpertnum}.f
         fi
       elif [ ${srtype} = 'sn' ]; then
         # NMM members
         if [ ${srpertnum} = 'c1' ]; then
           srefgfile=sref_nmm.t${CYL}z.pgrb221.ctl.f
         else
           srefgfile=sref_nmm.t${CYL}z.pgrb221.${srpertnum}.f
         fi
       else
         set +x
         echo " "
         echo "!!! ERROR: SREF MEMBER NOT RECOGNIZED. "
         echo "!!!        USER INPUT SREF MEMBER = --->${pert}<---"
         echo " "
         set -x
         exit 8
       fi
       srefdir=/com/sref/${envir}/sref.${PDY}/${cyc}/pgrb   ;
       COM=/com/sref/${envir}/sref.${PDY}/${cyc}/track  ;
       fcstlen=87                                       ;
       fcsthrs=' 00 03 06 09 12 15 18 21 24 27 30 33 36 39
                 42 45 48 51 54 57 60 63 66 69 72 75 78 81
                 84 87 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99';
       atcfnum=92                                       ;
       atcfname="${pert}"                               ;
       atcfout="${pert}"                                ;
       modtyp='regional'                                ;
       model=13                                        ;;

  eens) set +x                                           ;
       echo " "; echo " ++ ECMWF ensemble member ${pert} chosen"     ;
       pert=` echo ${pert} | tr '[A-Z]' '[a-z]'`        ;
       PERT=` echo ${pert} | tr '[a-z]' '[A-Z]'`        ;
       echo " "                                         ;
       set -x                                           ;
       ecedir=/com/mrf/prod/wsr.${PDY}                  ;
       ecegfile=                                        ;
       eceifile=                                        ;
       COM=/com/mrf/prod/wsr.${PDY}                     ;
       fcstlen=240                                      ;
       fcsthrs=' 00 12 24 36 48 60 72 84 96 108 120 132 144
                 156 168 180 192 204 216 228 240 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99
                 99 99 99 99 99 99 99 99 99 99' ;
       atcfnum=91                                       ;
       pert_posneg=` echo "${pert}" | cut -c1-1`        ;
       pert_num=`    echo "${pert}" | cut -c2-3`        ;
       atcfname="e${pert_posneg}${pert_num}"            ;
       atcfout="e${pert_posneg}${pert_num}"             ;
       modtyp='global'                                  ;
       model=21                                        ;;

   cmc) set +x                                        ;
         echo " "; echo " ++ operational Canadian global model chosen"   ;
         echo " "                                         ;
         set -x                                           ;
         cmcdir=/dcom/us007003/${PDY}/wgrbbul/cmc         ;
         cmcgfile=cmc_${PDY}${CYL}f                       ;
         cmcifile=nonexistant                             ;
         COM=/com/gens/prod/cmce.${PDY}/${CYL}/track      ;
         if [ ! -d $COM ]; then mkdir -p $COM ; fi        ;
         fcstlen=144                                      ;
         fcsthrs=' 00 06 12 18 24 30 36 42 48 54 60 66 72 78
                   84 90 96 102 108 114 120 126 132 138 144
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99 99 99
                   99 99 99 99 99 99 99 99 99 99 99 99' ;
         atcfnum=39                                       ;
         atcfname="cmc "                                  ;
         atcfout="cmc"                                    ;
         grmodel="cmc"                                   ;
         mslpthresh=0.0015                                ;
         v850thresh=1.5000                                ;
         modtyp='global'                                  ;
         model=15                                         ;;

  cens) set +x                                            ;
       echo " "; echo " ++ Canadian ensemble member ${pert} chosen";
       pert=` echo ${pert} | tr '[A-Z]' '[a-z]'`        ;
       PERT=` echo ${pert} | tr '[a-z]' '[A-Z]'`        ;
       echo " "                                         ;
       set -x                                           ;
       ccedir=/com/gens/prod/cmce.${PDY}/${CYL}/pgrba   ;
       ccegfile=cmc_ge${pert}.t${CYL}z.pgrbaf           ;
       cceifile=does_not_exist                          ;
       COM=/com/gens/prod/cmce.${PDY}/${CYL}/track      ;
         if [ ! -d $COM ]; then mkdir -p $COM ; fi        ;
       fcstlen=240                                      ;
       fcsthrs=' 00 06 12 18 24 30 36 42 48 54 60 66 72 78
                 84 90 96 102 108 114 120 126 132 138 144
                 150 156 162 168 174 180 186 192 198 204 210
                 216 222 228 234 240  99  99  99  99  99  99
                  99  99  99  99  99  99  99  99  99  99  99
                  99  99  99  99  99  99  99';
       atcfnum=91                                       ;
       pert_posneg=` echo "${pert}" | cut -c1-1`        ;
       pert_num=`    echo "${pert}" | cut -c2-3`        ;
       atcfname="c${pert_posneg}${pert_num}"            ;
       atcfout="c${pert_posneg}${pert_num}"             ;
       modtyp='global'                                  ;
       model=16                                        ;;

  *) set +x                                            ;
         echo " "                                         ;
         echo " !!! Model selected is not recognized."    ;
         echo " Model= --> ${cmodel} <-- ... Please submit the script again...";
         echo " "                                         ;
         set -x                                           ;
         err_exit " FAILED ${jobid} -- UNKNOWN cmodel IN TRACKER SCRIPT -\
    ABNORMAL EXIT";;

   esac

#------------------------------------------------------------------------#
#
#      --------  TC Vitals processing   --------
#
# Check Steve Lord's operational tcvitals file to see if any 
# vitals records were processed for this time by his system.  
# If there were, then you'll find a file in /com/gfs/prod/gfs.yymmdd 
# with the vitals in it.  Also check the raw TC Vitals file in
# /com/arch/prod/syndat , since this may contain storms that Steve's 
# system ignored (Steve's system will ignore all storms that are 
# either over land or very close to land);  We still want to track 
# these inland storms, AS LONG AS THEY ARE NHC STORMS (don't 
# bother trying to track inland storms that are outside of NHC's 
# domain of responsibility -- we don't need that info).
# UPDATE 5/12/98 MARCHOK: The script is updated so that for the
#   global models, the gfs directory is checked for the error-
#   checked vitals file, while for the regional models, the 
#   nam directory is checked for that file.
#------------------------------------------------------------------------#

# First check to see if the vitals file is in gfsvitdir or not.  If 
# it's not, then run Hua-Lu's ftp script to get the file from one
# of the other machines.  If it's still not there, then no big 
# deal; this script will exit just a little further down once it
# realizes there are not any storms to process.

   d6ago_ymdh=` /nwprod/util/exec/ndate -6 ${PDY}${CYL}`
   d6ago_4ymd=` echo ${d6ago_ymdh} | cut -c1-8`
   d6ago_ymd=` echo ${d6ago_ymdh} | cut -c3-8`
   d6ago_hh=`  echo ${d6ago_ymdh} | cut -c9-10`
   d6ago_str="${d6ago_ymd} ${d6ago_hh}00"

   d6ahead_ymdh=` /nwprod/util/exec/ndate 6 ${PDY}${CYL}`
   d6ahead_4ymd=` echo ${d6ahead_ymdh} | cut -c1-8`
   d6ahead_ymd=` echo ${d6ahead_ymdh} | cut -c3-8`
   d6ahead_hh=`  echo ${d6ahead_ymdh} | cut -c9-10`
   d6ahead_str="${d6ahead_ymd} ${d6ahead_hh}00"

   if [ ${modtyp} = 'global' ]; then

      synvitdir=/com/gfs/prod/gfs.${PDY}
      synvitfile=gfs.t${CYL}z.syndata.tcvitals.tm00
      synvit6ago_dir=/com/gfs/prod/gfs.${d6ago_4ymd}
      synvit6ago_file=gfs.t${d6ago_hh}z.syndata.tcvitals.tm00
      synvit6ahead_dir=/com/gfs/prod/gfs.${d6ahead_4ymd}
      synvit6ahead_file=gfs.t${d6ahead_hh}z.syndata.tcvitals.tm00

   else
      
      synvitdir=/com/nam/prod/nam.${PDY}
      synvitfile=nam.t${CYL}z.syndata.tcvitals.tm00
      synvit6ago_dir=/com/nam/prod/nam.${d6ago_4ymd}
      synvit6ago_file=nam.t${d6ago_hh}z.syndata.tcvitals.tm00
      synvit6ahead_dir=/com/nam/prod/nam.${d6ahead_4ymd}
      synvit6ahead_file=nam.t${d6ahead_hh}z.syndata.tcvitals.tm00

   fi

   set +x
   echo " "
   echo "              -----------------------------"
   echo " "
   echo " Now sorting and updating the TC Vitals file.  Please wait...."
   echo " "
   set -x

   dnow_str="${symd} ${CYL}00"

   if [ -s ${synvitdir}/${synvitfile} -o\
        -s ${synvit6ago_dir}/${synvit6ago_file} -o\
        -s ${synvit6ahead_dir}/${synvit6ahead_file} ]; then
      grep "${d6ago_str}" ${synvit6ago_dir}/${synvit6ago_file}        \
                  >${TRKDATA}/tmpsynvit.${atcfout}.${PDY}${CYL}
      grep "${dnow_str}"  ${synvitdir}/${synvitfile}                  \
                 >>${TRKDATA}/tmpsynvit.${atcfout}.${PDY}${CYL}
      grep "${d6ahead_str}" ${synvit6ahead_dir}/${synvit6ahead_file}  \
                 >>${TRKDATA}/tmpsynvit.${atcfout}.${PDY}${CYL}
   else
      set +x
      echo " "
      echo " There is no (synthetic) TC vitals file for ${CYL}z in "
      echo "${synvitdir}, nor is there a TC vitals file for ${d6ago_hh}z in "
      echo "${synvit6ago_dir}, nor is there a TC vitals file for ${d6ahead_hh}z"
      echo " in ${synvit6ahead_dir}.  Checking the raw TC Vitals file ....."
      echo " "
      set -x
   fi

# Take the vitals from Steve Lord's /com/gfs/prod tcvitals file,
# and cat them with the NHC-only vitals from the raw, original
# /com/arch/prod/synda_tcvitals file.  Do this because the nwprod
# tcvitals file is the original tcvitals file, and Steve runs a
# program that ignores the vitals for a storm that's over land or
# even just too close to land, and for tracking purposes for the
# US regional models, we need these locations.  Only include these
# "inland" storm vitals for NHC (we're not going to track inland 
# storms that are outside of NHC's domain of responsibility -- we 
# don't need that info).  
# UPDATE 5/12/98 MARCHOK: nawk logic is added to screen NHC 
#   vitals such as "91L NAMELESS" or "89E NAMELESS", since TPC 
#   does not want tracks for such storms.

   grep "${d6ago_str}" ${archsyndir}/syndat_tcvitals.${CENT}${syy}   | \
      grep NHC | grep -v TEST | nawk 'substr($0,6,1) !~ /[8-9]/ {print $0}' \
      >${TRKDATA}/tmprawvit.${atcfout}.${PDY}${CYL}
   grep "${dnow_str}"  ${archsyndir}/syndat_tcvitals.${CENT}${syy}   | \
      grep NHC | grep -v TEST | nawk 'substr($0,6,1) !~ /[8-9]/ {print $0}' \
      >>${TRKDATA}/tmprawvit.${atcfout}.${PDY}${CYL}
   grep "${d6ahead_str}" ${archsyndir}/syndat_tcvitals.${CENT}${syy} | \
      grep NHC | grep -v TEST | nawk 'substr($0,6,1) !~ /[8-9]/ {print $0}' \
      >>${TRKDATA}/tmprawvit.${atcfout}.${PDY}${CYL}

#PRODTEST#
#PRODTEST# Use the next couple lines to test the tracker on the SP.
#PRODTEST# These next couple lines use data from a test TC Vitals file that 
#PRODTEST# I generate.  When you are ready to test this system, call me and
#PRODTEST# I'll create one for the current day, and then uncomment the next
#PRODTEST# couple lines in order to access the test vitals file.
#
#   ttrkdir=/nfsuser/g01/wx20tm/trak/prod/data
#   ttrkdir=/nfsuser/g01/wx20tm/trak/para/scripts
#   grep "${dnow_str}" ${ttrkdir}/tcvit.01l >>${TRKDATA}/tmprawvit.${atcfout}.${PDY}${CYL}

# IMPORTANT:  When "cat-ing" these files, make sure that the vitals
# files from the "raw" TC vitals files are first in order and Steve's
# TC vitals files second.  This is because Steve's vitals file has
# been error-checked, so if we have a duplicate tc vitals record in
# these 2 files (very likely), program supvit.x below will
# only take the last vitals record listed for a particular storm in
# the vitals file (all previous duplicates are ignored, and Steve's
# error-checked vitals records are kept).

   cat ${TRKDATA}/tmprawvit.${atcfout}.${PDY}${CYL} ${TRKDATA}/tmpsynvit.${atcfout}.${PDY}${CYL} \
        >${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}

# If we are doing the processing for the GFDL model, then we want
# to further cut down on which vitals we allow into this run of the
# tracker.  The reason is that this program will be called from 
# each individual run for a storm, so the grib files will be 
# specific to each storm.  So if 4 storms are being run at a 
# particular cycle, then this script is run 4 separate times from
# within the GFDL_POST job.

   if [ ${cmodel} = 'gfdl' ]; then
      grep -i ${stormid} ${gltrkdir}/${ATCFNAME}.vitals.${syy}${smm}${sdd}${shh} >${TRKDATA}/tmpvit
      mv ${TRKDATA}/tmpvit ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}
   fi

#--------------------------------------------------------------#
# Now run a fortran program that will read all the TC vitals
# records for the current dtg and the dtg from 6h ago, and
# sort out any duplicates.  If the program finds a storm that
# was included in the vitals file 6h ago but not for the current
# dtg, this program updates the 6h-old first guess position
# and puts these updated records as well as the records from
# the current dtg into a temporary vitals file.  It is this
# temporary vitals file that is then used as the input for the
# tracking program.
#--------------------------------------------------------------#

   ymdh6ago=` /nwprod/util/exec/ndate -6 ${PDY}${CYL}`
   syy6=`echo ${ymdh6ago} | cut -c3-4`
   smm6=`echo ${ymdh6ago} | cut -c5-6`
   sdd6=`echo ${ymdh6ago} | cut -c7-8`
   shh6=`echo ${ymdh6ago} | cut -c9-10`
   symd6=${syy6}${smm6}${sdd6}

   ymdh6ahead=` /nwprod/util/exec/ndate 6 ${PDY}${CYL}`
   syyp6=`echo ${ymdh6ahead} | cut -c3-4`
   smmp6=`echo ${ymdh6ahead} | cut -c5-6`
   sddp6=`echo ${ymdh6ahead} | cut -c7-8`
   shhp6=`echo ${ymdh6ahead} | cut -c9-10`
   symdp6=${syyp6}${smmp6}${sddp6}

   echo "&datenowin   dnow%yy=${syy}, dnow%mm=${smm},"       >${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}
   echo "             dnow%dd=${sdd}, dnow%hh=${CYL}/"      >>${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}
   echo "&date6agoin  d6ago%yy=${syy6}, d6ago%mm=${smm6},"  >>${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}
   echo "             d6ago%dd=${sdd6}, d6ago%hh=${shh6}/"  >>${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}
   echo "&date6aheadin  d6ahead%yy=${syyp6}, d6ahead%mm=${smmp6},"  >>${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}
   echo "               d6ahead%dd=${sddp6}, d6ahead%hh=${shhp6}/"  >>${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}

   numvitrecs=`cat ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL} | wc -l`
   if [ ${numvitrecs} -eq 0 ]; then
      if [ ${trkrtype} = 'tracker' ]; then
         set +x
         echo " "
         echo "!!! NOTE -- There are no vitals records for this time period."
         echo "!!! File ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL} is empty."
         echo "!!! It could just be that there are no storms for the current"
         echo "!!! time.  Please check the dates and submit this job again...."
         echo " "
         set -x
         exit 1
      fi
   fi

# - - - - - - - - - - - - -
# Before running the program to read, sort and update the vitals,
# first run the vitals through some awk logic, the purpose of 
# which is to convert all the 2-digit years into 4-digit years.
# Beginning 4/21/99, NHC and JTWC will begin sending the vitals
# with 4-digit years, however it is unknown when other global
# forecasting centers will begin using 4-digit years, thus we
# need the following logic to ensure that all the vitals going
# into supvit.f have uniform, 4-digit years in their records.
#
# 1/8/2000: sed code added by Tim Marchok due to the fact that 
#       some of the vitals were getting past the syndata/qctropcy
#       error-checking with a colon in them; the colon appeared
#       in the character immediately to the left of the date, which
#       was messing up the "(length($4) == 8)" statement logic.
# - - - - - - - - - - - - -

   sed -e "s/\:/ /g"  ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL} > ${TRKDATA}/tempvit
   mv ${TRKDATA}/tempvit ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}

   awk '
   {
     yycheck = substr($0,20,2)
   if ((yycheck == 20 || yycheck == 19) && (length($4) == 8)) {
    printf ("%s\n",$0)
   }
   else {
      if (yycheck >= 0 && yycheck <= 50) {
        printf ("%s20%s\n",substr($0,1,19),substr($0,20))
        }
      else {
         printf ("%s19%s\n",substr($0,1,19),substr($0,20))
         }
   }
   } ' ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL} >${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}.y4

   mv ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}.y4 ${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}

   if [ ${numvitrecs} -gt 0 ]; then

      export pgm=supvit
#      . prep_step

      export XLFUNIT_31=${TRKDATA}/vitals.${atcfout}.${PDY}${CYL}       
      export XLFUNIT_51=${TRKDATA}/vitals.upd.${atcfout}.${PDY}${CYL}  

      msg="$pgm start for $atcfout at ${CYL}z"
      postmsg "$jlogfile" "$msg"

      ${exectrkdir}/supvit <${TRKDATA}/suv_input.${atcfout}.${PDY}${CYL}
      suvrcc=$?

      if [ ${suvrcc} -eq 0 ]; then
         msg="$pgm end for $atcfout at ${CYL}z completed normally"
         postmsg "$jlogfile" "$msg"
      else
         set +x
         echo " "
         echo "!!! ERROR -- An error occurred while running supvit.x, "
         echo "!!! which is the program that updates the TC Vitals file."
         echo "!!! Return code from supvit.x = ${suvrcc}"
         echo "!!! model= ${atcfout}, forecast initial time = ${PDY}${CYL}"
         echo "!!! Exiting...."
         echo " "
         set -x
         err_exit " FAILED ${jobid} - ERROR RUNNING SUPVIT IN TRACKER SCRIPT- ABNORMAL EXIT"
      fi

   else

      touch ${TRKDATA}/vitals.upd.${atcfout}.${PDY}${CYL}

   fi

#------------------------------------------------------------------#
# Now select all storms to be processed, that is, process every
# storm that's listed in the updated vitals file for the current
# forecast hour.  If there are no storms for the current time,
# then exit.
#------------------------------------------------------------------#

   numvitrecs=`cat ${TRKDATA}/vitals.upd.${atcfout}.${PDY}${CYL} | wc -l`
   if [ ${numvitrecs} -eq 0 ]; then
      if [ ${trkrtype} = 'tracker' ]; then
         set +x
         echo " "
         echo "!!! NOTE -- There are no vitals records for this time period "
         echo "!!! in the UPDATED vitals file."
         echo "!!! It could just be that there are no storms for the current"
         echo "!!! time.  Please check the dates and submit this job again...."
         echo " "
         set -x
         exit 1
      fi
   fi

   set +x
   echo " "
   echo " *--------------------------------*"
   echo " |        STORM SELECTION         |"
   echo " *--------------------------------*"
   echo " "
   set -x

   ict=1
   while [ $ict -le 15 ]
   do
      stormflag[${ict}]=3
      let ict=ict+1
   done

   dtg_current="${symd} ${CYL}00"
   stormmax=` grep "${dtg_current}" ${TRKDATA}/vitals.upd.${atcfout}.${PDY}${CYL} | wc -l`

   if [ ${stormmax} -gt 15 ]; then
      stormmax=15
   fi

   sct=1
   while [ ${sct} -le ${stormmax} ]
   do
      stormflag[${sct}]=1
      let sct=sct+1
   done

#-----------------------------------------------------------------#
#
#         ------  CUT APART INPUT GRIB FILES  -------
#
# For the selected model, cut apart the GRIB input files in order
# to pull out only the variables that we need for the tracker.  
# Put these selected variables from all forecast hours into 1 big 
# GRIB file that we'll use as input for the tracker.
# 
# The wgrib utility (/nwprod/util/exec/wgrib) is used to cut out 
# the needed parms for the GFS, MRF, GDAS, UKMET and NOGAPS files.
# The utility /nwprod/util/exec/copygb is used to interpolate the 
# NGM (polar stereographic) and Eta (Lambert Conformal) data from 
# their grids onto lat/lon grids.  Note that while the lat/lon 
# grid that I specify overlaps into areas that don't have any data 
# on the original grid, Mark Iredell wrote the copygb software so 
# that it will mask such "no-data" points with a bitmap (just be 
# sure to check the lbms in your fortran program after getgb).
#-----------------------------------------------------------------#

   set +x
   echo " "
   echo " -----------------------------------------"
   echo "   NOW CUTTING APART INPUT GRIB FILES TO "
   echo "   CREATE 1 BIG GRIB INPUT FILE "
   echo " -----------------------------------------"
   echo " "
   set -x

   gix=/nwprod/util/exec/grbindex
   cgb=/nwprod/util/exec/copygb

   regflag=`grep NHC ${TRKDATA}/vitals.upd.${atcfout}.${PDY}${CYL} | wc -l`

# ----------------------------------
#   Process Early Eta, if selected
# ----------------------------------

   if [ ${model} -eq 6 ]; then

      grid='255 0 301 141 70000 190000 128 0000 340000  500  500 0'

      if [ ${regflag} -eq 0 ]; then
         if [ ${trkrtype} = 'tracker' ]; then
            set +x
            echo " "
            echo " !!! NAM model has been selected, but there are no storms in"
            echo " !!! the TC Vitals file that can be processed.  That is, "
            echo " !!! there are no Vitals records from NHC.  The vitals "
            echo " !!! records that are in the updated vitals file must be from"
            echo " !!! another cyclone forecast center, and the Eta domain does"
            echo " !!! not extend to any region other than that covered by NHC."
            echo " !!! Exiting....."
            set -x
            exit 1
         fi
      fi

      if [ -s ${TRKDATA}/namlatlon.pgrb.${PDY}${CYL} ]; then 
         rm ${TRKDATA}/namlatlon.pgrb.${PDY}${CYL}
      fi

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

         if [ ! -s ${namdir}/${namgfile}${fhour}.tm00 ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " !!! Early NAM File missing:                            "
            echo " !!! ${namdir}/${namgfile}${fhour}.tm00                 "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            set -x
            continue
         fi

         if [ -s ${namdir}/${namifile}${fhour} ]; then
            x1=${namdir}/${namifile}${fhour}
         else
            if [ -s ${TRKDATA}/tmpnamixfile ]; then
               rm ${TRKDATA}/tmpnamixfile
            fi
            $gix ${namdir}/${namgfile}${fhour}.tm00 ${TRKDATA}/tmpnamixfile
            x1=${TRKDATA}/tmpnamixfile
         fi

         set +x
         echo " "
         echo " Extracting Early NAM GRIB data for forecast hour = $fhour"
         echo " "
         set -x
  
         g1=${namdir}/${namgfile}${fhour}.tm00
     
         $cgb -g"$grid" -k'4*-1 33 100 850' $g1 $x1 ${TRKDATA}/namllu850.grb.f${fhour};   rcc1=$?
         $cgb -g"$grid" -k'4*-1 33 100 700' $g1 $x1 ${TRKDATA}/namllu700.grb.f${fhour};   rcc2=$?
         $cgb -g"$grid" -k'4*-1 33 100 500' $g1 $x1 ${TRKDATA}/namllu500.grb.f${fhour};   rcc3=$?
         $cgb -g"$grid" -k'4*-1 33 105 10'  $g1 $x1 ${TRKDATA}/namllu10m.grb.f${fhour};   rcc4=$?
         $cgb -g"$grid" -k'4*-1 41 100 850' $g1 $x1 ${TRKDATA}/namllav850.grb.f${fhour};  rcc5=$?
         $cgb -g"$grid" -k'4*-1 41 100 700' $g1 $x1 ${TRKDATA}/namllav700.grb.f${fhour};  rcc6=$?
         $cgb -g"$grid" -k'4*-1  7 100 850' $g1 $x1 ${TRKDATA}/namllz850.grb.f${fhour};   rcc7=$?
         $cgb -g"$grid" -k'4*-1  7 100 700' $g1 $x1 ${TRKDATA}/namllz700.grb.f${fhour};   rcc8=$?
         $cgb -g"$grid" -k'4*-1  2 102   0' $g1 $x1 ${TRKDATA}/namllmslp.grb.f${fhour};   rcc9=$?

         if [ $rcc1 -eq 134 -o $rcc2 -eq 134 -o $rcc3 -eq 134 -o $rcc4 -eq 134 -o $rcc5 -eq 134 -o \
            $rcc6 -eq 134 -o $rcc7 -eq 134 -o $rcc8 -eq 134 -o $rcc9 -eq 134 ]
         then
            set +x
            echo " "
            echo "!!! ERROR using $cgb to interpolate nam data.  We will stop"
            echo "!!! execution because some variables may have been copied "
            echo "!!! okay, while some obviously have not, and that could lead"
            echo "!!! to unreliable results from the tracker.  Check to make"
            echo "!!! sure you've allocated enough memory for this job. "
            echo "!!! Exiting...."
            echo " "
            set -x
            err_exit " FAILED ${jobid} - ERROR INTERPOLATING NAM TRKDATA IN TRACKER SCRIPT - ABNORMAL EXIT"
         fi

         cat ${TRKDATA}/namllu850.grb.f${fhour} ${TRKDATA}/namllu700.grb.f${fhour} ${TRKDATA}/namllu500.grb.f${fhour} ${TRKDATA}/namllz850.grb.f${fhour} \
        ${TRKDATA}/namllz700.grb.f${fhour} ${TRKDATA}/namllmslp.grb.f${fhour} \
        ${TRKDATA}/namllav850.grb.f${fhour} ${TRKDATA}/namllav700.grb.f${fhour}  ${TRKDATA}/namllu10m.grb.f${fhour} \
        >>${TRKDATA}/namlatlon.pgrb.${PDY}${CYL}
  
      done

      $gix ${TRKDATA}/namlatlon.pgrb.${PDY}${CYL} ${TRKDATA}/namlatlon.pgrb.ix.${PDY}${CYL}
      gribfile=${TRKDATA}/namlatlon.pgrb.${PDY}${CYL}
      ixfile=${TRKDATA}/namlatlon.pgrb.ix.${PDY}${CYL}

   fi

# ----------------------------------
#   Process GFDL, if selected
# ----------------------------------

# The GFDL GRIB grid is already a lat/lon grid, however it uses a scanning 
# mode flag of 64, which means the data starts in the south and goes north.
# The tracker needs the data to start in the north and go south, so we will
# use copygb to flip the data in the grid.  The other thing that copygb 
# will do here is make the new file have wider boundaries around all 4 
# sides of the grid.  This is done because the GFDL grid is a regional grid,
# and we need to have that buffer of null, bitmapped data around a 
# regional grid.  The north-south extent of the domain is always the same,
# so we will hardwire the new latitude bounds in the "grid=" statement
# (adding an extra ~5.0 degrees both north and south), but we need to wgrib
# the 00h file to get the longitude bounds, and then modify them.  The 
# northern and southern boundaries are fixed in that "grid=" statement 
# because those boundaries do NOT change from run to run; the integration
# grid only changes in the east/west direction.

   if [ ${model} -eq 9 ]; then

      origwestlon=`$wgrib -V ${gfdldir}/${gfdlgfile}00 | grep long | head -1 | awk '{printf ("%d",$2*1000)}'`
      origeastlon=`$wgrib -V ${gfdldir}/${gfdlgfile}00 | grep long | head -1 | awk '{printf ("%d",$4*1000)}'`
      let newwestlon=origwestlon-5000
      let neweastlon=origeastlon+5000

      grid="255 0 255 255 69833 ${newwestlon} 128 -14833 ${neweastlon} 333  333 0"

      if [ ${regflag} -eq 0 ]; then
         if [ ${trkrtype} = 'tracker' ]; then
            set +x
            echo " "
            echo " !!! GFDL model has been selected, but there are no storms in"
            echo " !!! the TC Vitals file that can be processed.  That is, "
            echo " !!! there are no Vitals records from NHC.  The vitals "
            echo " !!! records that are in the updated vitals file must be from"
            echo " !!! another cyclone forecast center, and the GFDL domain "
            echo " !!! does not extend to any region other than that covered "
            echo " !!! by NHC.  Exiting....."
            set -x
            exit 0
         fi
      fi

      if [ -s ${TRKDATA}/gfdl.${stormenv}.pgrb.${PDY}${CYL} ]; then
         rm ${TRKDATA}/gfdl.${stormenv}.pgrb.${PDY}${CYL}
      fi

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

         if [ ! -s ${gfdldir}/${gfdlgfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " !!! GFDL File missing: ${gfdldir}/${gfdlgfile}${fhour}" 
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 
            set -x 
            continue
         fi
         if [ -s ${gfdldir}/${gfdlifile}${fhour} ]; then
            x1=${gfdldir}/${gfdlifile}${fhour}
         else
            if [ -s ${TRKDATA}/tmpgfdlixfile ]; then
               rm ${TRKDATA}/tmpgfdlixfile
            fi
            $gix ${gfdldir}/${gfdlgfile}${fhour} ${TRKDATA}/tmpgfdlixfile
            x1=${TRKDATA}/tmpgfdlixfile
         fi

         set +x
         echo " "
         echo " Extracting GFDL GRIB data for forecast hour = $fhour"
         echo " "
         set -x

         g1=${gfdldir}/${gfdlgfile}${fhour}

         $cgb -g"$grid" -k'4*-1 33 100 850' $g1 $x1 ${TRKDATA}/gfdlu850.grb.f${fhour};   rcc1=$?
         $cgb -g"$grid" -k'4*-1 33 100 700' $g1 $x1 ${TRKDATA}/gfdlu700.grb.f${fhour};   rcc2=$?
         $cgb -g"$grid" -k'4*-1 33 100 500' $g1 $x1 ${TRKDATA}/gfdlu500.grb.f${fhour};   rcc3=$?
         $cgb -g"$grid" -k'4*-1 33 105  35' $g1 $x1 ${TRKDATA}/gfdlu35m.grb.f${fhour};   rcc4=$?
         $cgb -g"$grid" -k'4*-1  7 100 850' $g1 $x1 ${TRKDATA}/gfdlz850.grb.f${fhour};   rcc7=$?
         $cgb -g"$grid" -k'4*-1  7 100 700' $g1 $x1 ${TRKDATA}/gfdlz700.grb.f${fhour};   rcc8=$?
         $cgb -g"$grid" -k'4*-1  2 102   0' $g1 $x1 ${TRKDATA}/gfdlmslp.grb.f${fhour};   rcc9=$?

         if [ $rcc1 -eq 134 -o $rcc2 -eq 134 -o $rcc3 -eq 134 -o $rcc4 -eq 134 -o $rcc7 -eq 134 -o $rcc8 -eq 134 -o $rcc9 -eq 134 ]; then
            set +x
            echo " "
            echo "!!! ERROR using $cgb to interpolate gfdl data.  We will stop"
            echo "!!! execution because some variables may have been copied "
            echo "!!! okay, while some obviously have not, and that could lead"
            echo "!!! to unreliable results from the tracker.  Check to make"
            echo "!!! sure you've allocated enough memory for this job."
            echo "!!! Exiting...."
            echo " "
            set -x
            err_exit " FAILED ${jobid} - ERROR INTERPOLATING GFDL TRKDATA IN TRACKER SCRIPT - ABNORMAL EXIT"
         fi

         cat ${TRKDATA}/gfdlu850.grb.f${fhour} ${TRKDATA}/gfdlu700.grb.f${fhour}  ${TRKDATA}/gfdlu500.grb.f${fhour} ${TRKDATA}/gfdlz850.grb.f${fhour} \
         ${TRKDATA}/gfdlz700.grb.f${fhour} ${TRKDATA}/gfdlmslp.grb.f${fhour} \
         ${TRKDATA}/gfdlu35m.grb.f${fhour} \
         >>${TRKDATA}/gfdl.${stormenv}.pgrb.${PDY}${CYL}
 
      done

      $gix ${TRKDATA}/gfdl.${stormenv}.pgrb.${PDY}${CYL} ${TRKDATA}/gfdl.${stormenv}.pgrb.ix.${PDY}${CYL}
      gribfile=${TRKDATA}/gfdl.${stormenv}.pgrb.${PDY}${CYL}
      ixfile=${TRKDATA}/gfdl.${stormenv}.pgrb.ix.${PDY}${CYL}

   fi

# The following ECMWF low resolution model has been replaced by a high
# resolution with twice a day frequency.
# -- Guang Ping Lou
#
# ------------------------------
#   Process ECMWF, if selected
# ------------------------------

# ECMWF is not a regional grid, however they currently (3/98) only send us the
# global belt from 35S to 35N.  Thus, it will have grid boundaries that may 
# interfere with the tracking algorithm.  It is crucial to the proper 
# functioning of the tracking program to give any regional grid dataset a 
# buffer zone around the grid boundaries, with null values in that buffer 
# zone that are bitmapped out.  That's why we use Mark Iredell's grib 
# interpolater here, to add a 5 degree buffer zone to the north and south of
# the ECMWF grid boundary; his interpolater adds the null values in the 
# bitmap surrounding the area with valid data.  If someday ECMWF begins 
# sending us data with full global coverage, then this bit of code should 
# be taken out, and the data should then be processed as the other normal
# full-coverage global models (ukmet, mrf, gfs, nogaps) currently are.
#  
#   if [ ${model} -eq 4 ]; then
#
#      if [ ! -s ${ecmwfdir}/${ecmwfgfile} ]; then
#         set +x
#         echo " "
#         echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#         echo " !!! ECMWF GRIB File missing from directory: ${ecmwfdir}"
#         echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#         echo " "
#         echo " !!! Due to missing ECMWF file, execution is ending...."
#         echo " "
#         set -x
#         err_exit " FAILED ${jobid} - MISSING ECMWF FILE IN TRACKER SCRIPT -\
# ABNORMAL EXIT"
#      fi
#
#      if [ -s ${ecmwfdir}/${ecmwfifile} ]; then
#         x1=${ecmwfdir}/${ecmwfifile}
#      else
#         $gix ${ecmwfdir}/${ecmwfgfile} ${TRKDATA}/${ecmwfgfile}.ix
#         x1=${TRKDATA}/${ecmwfgfile}.ix
#      fi
#
#      if [ -s ${TRKDATA}/ecmwf.bufzone.grb.${PDY}${CYL} ]; then 
#         rm ${TRKDATA}/ecmwf.bufzone.grb.${PDY}${CYL}
#      fi
#      if [ -s ${TRKDATA}/ecmwf.bufzone.ix.${PDY}${CYL} ]; then 
#         rm ${TRKDATA}/ecmwf.bufzone.ix.${PDY}${CYL}
#      fi
#      g1=${ecmwfdir}/${ecmwfgfile}
#      ecgrid='255 0 144 33 40000 0000 128 -40000 357500 2500 2500 0'
#      $cgb -g"$ecgrid" $g1 $x1 ${TRKDATA}/ecmwf.bufzone.grb.${PDY}${CYL}
#      $gix ${TRKDATA}/ecmwf.bufzone.grb.${PDY}${CYL} ${TRKDATA}/ecmwf.bufzone.ix.${PDY}${CYL}
#      gribfile=${TRKDATA}/ecmwf.bufzone.grb.${PDY}${CYL}
#      ixfile=${TRKDATA}/ecmwf.bufzone.ix.${PDY}${CYL}
#
#   fi

# ------------------------------
#   Process GFS, if selected
# ------------------------------
  
   if [ ${model} -eq 1 ]; then

      if [ -s ${TRKDATA}/gfsgribfile.${PDY}${CYL} ]; then 
         rm ${TRKDATA}/gfsgribfile.${PDY}${CYL}
      fi

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

         if [ ! -s ${gfsdir}/${gfsgfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " !!! GFS File missing: ${gfsdir}/${gfsgfile}${fhour}"
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " "
            set -x
            continue
         fi

         gfile=${gfsdir}/${gfsgfile}${fhour}
         $wgrib -s $gfile >gfs.ix

         for parm in ${wgrib_parmlist} 
         do
            case ${parm} in
            "SurfaceU")     
            grep "UGRD:10 m " gfs.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/gfsgribfile.${PDY}${CYL} ;;
            "SurfaceV")
            grep "VGRD:10 m " gfs.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/gfsgribfile.${PDY}${CYL} ;;
                 *) 
            grep "${parm}" gfs.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/gfsgribfile.${PDY}${CYL} ;;
            esac 

         done

      done

      $gix ${TRKDATA}/gfsgribfile.${PDY}${CYL} ${TRKDATA}/gfsixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/gfsgribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/gfsixfile.${PDY}${CYL}

   fi

# --------------------------------------------
#   Process Ensemble perturbation, if selected
# --------------------------------------------

   if [ ${model} -eq 10 ]; then

      if [ -s ${TRKDATA}/ens${pert}gribfile.${PDY}${CYL} ]; then
         rm ${TRKDATA}/ens${pert}gribfile.${PDY}${CYL}
      fi

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

         if [ ! -s ${ensdir}/${ensgfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            echo " !!! ENSEMBLE ${PERT} File missing: "
            echo " !!!                          ${ensdir}/${ensgfile}${fhour}  "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            echo " "
            set -x
            continue
         fi

         gfile=${ensdir}/${ensgfile}${fhour}
         $wgrib -s $gfile >ens.ix

         for parm in ${wgrib_parmlist}
         do
            case ${parm} in
            "SurfaceU")
            grep "UGRD:10 m " ens.ix | $wgrib -s $gfile -i -grib -append \
                                -o ${TRKDATA}/ens${pert}gribfile.${PDY}${CYL} ;;
            "SurfaceV")
            grep "VGRD:10 m " ens.ix | $wgrib -s $gfile -i -grib -append \
                                -o ${TRKDATA}/ens${pert}gribfile.${PDY}${CYL} ;;
                 *)
            grep "${parm}" ens.ix | $wgrib -s $gfile -i -grib -append \
                                -o ${TRKDATA}/ens${pert}gribfile.${PDY}${CYL} ;;
            esac

         done

      done

      $gix ${TRKDATA}/ens${pert}gribfile.${PDY}${CYL} ${TRKDATA}/ens${pert}ixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/ens${pert}gribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/ens${pert}ixfile.${PDY}${CYL}

   fi

# -------------------------------------
#   Process GDAS, if selected
# -------------------------------------

   if [ ${model} -eq 8 ]; then

      if [ -s ${TRKDATA}/gdasgribfile.${PDY}${CYL} ]; then
         rm ${TRKDATA}/gdasgribfile.${PDY}${CYL}
      fi

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

         if [ ! -s ${gdasdir}/${gdasgfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " !!! GDAS File missing: ${gdasdir}/${gdasgfile}${fhour}"
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            set -x
            continue
         fi

         gfile=${gdasdir}/${gdasgfile}${fhour}
         $wgrib -s $gfile >gdas.ix

         for parm in ${wgrib_parmlist}
         do
            case ${parm} in
            "SurfaceU")
            grep "UGRD:10 m " gdas.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/gdasgribfile.${PDY}${CYL} ;;
            "SurfaceV")
            grep "VGRD:10 m " gdas.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/gdasgribfile.${PDY}${CYL} ;;
                 *)
            grep "${parm}" gdas.ix | $wgrib -s $gfile -i -grib -append    \
                                  -o ${TRKDATA}/gdasgribfile.${PDY}${CYL} ;;
            esac
         done

      done

      $gix ${TRKDATA}/gdasgribfile.${PDY}${CYL} ${TRKDATA}/gdasixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/gdasgribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/gdasixfile.${PDY}${CYL}

   fi

# ------------------------------
#   Process UKMET, if selected
# ------------------------------
  
   if [ ${model} -eq 3 ]; then

      if [ -s ${TRKDATA}/ukmetgribfile.${PDY}${CYL} ]; then 
         rm ${TRKDATA}/ukmetgribfile.${PDY}${CYL}
      fi

      wgrib_parmlist=' HGT:850 HGT:700 UGRD:850 UGRD:700 UGRD:500 VGRD:850 VGRD:700 VGRD:500 UGRD:sfc VGRD:sfc ABSV:850 ABSV:700 PRMSL:MSL '

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

         if [ ! -s ${ukmetdir}/${ukmetgfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " !!! UKMET File missing: ${ukmetdir}/${ukmetgfile}${fhour}"
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            echo " "
            set -x
            continue
         fi

         gfile=${ukmetdir}/${ukmetgfile}${fhour}
         $wgrib -s $gfile >ukmet.ix

         for parm in ${wgrib_parmlist}
         do
            grep "${parm}" ukmet.ix | $wgrib -s $gfile -i -grib -append \
                             -o ${TRKDATA}/ukmetgribfile.${PDY}${CYL}
         done

      done

      $gix ${TRKDATA}/ukmetgribfile.${PDY}${CYL} ${TRKDATA}/ukmetixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/ukmetgribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/ukmetixfile.${PDY}${CYL}

   fi

# ------------------------------
#   Process ECMWF, if selected
# ------------------------------

# As of Summer, 2005, ECMWF is now sending us high res (1-degree) data on
# a global grid with 12-hourly resolution out to 240h.  Previously, we
# only got their data on a low res (2.5-degree) grid, from 35N-35S, with
# 24-hourly resolution out to only 168h.

if [ ${model} -eq 4 ] ; then

    if [ -s ${TRKDATA}/ecgribfile.${PDY}${CYL} ] ; then
      rm ${TRKDATA}/ecgribfile.${PDY}${CYL}
    fi

    immddhh=`echo ${PDY}${CYL}| cut -c5-`
    ict=0

    for fhour in ${fcsthrs}
    do

      if [ ${fhour} -eq 99 ]
      then
        continue
      fi

      let fhr=ict*12
      echo "fhr= $fhr"
      fmmddhh=` /nwprod/util/exec/ndate ${fhr} ${PDY}${CYL} | cut -c5- `
      ec_hires_orig=ecens_DCD${immddhh}00${fmmddhh}001

      if [ ! -s ${ecmwfdir}/${ec_hires_orig} ]
      then
        set +x
        echo " "
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " !!! ECMWF File missing: ${ecmwfdir}/${ec_hires_orig}"
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " "
        set -x
        continue
      fi

      ecfile=${ecmwfdir}/${ec_hires_orig}
      $wgrib -s $ecfile >ec.ix

      for parm in ${wgrib_ec_hires_parmlist}
      do
        grep "${parm}" ec.ix | $wgrib -s $ecfile -i -grib -append  -o ${TRKDATA}/ecgribfile.${PDY}${CYL}

      done

      let ict=ict+1

    done

  $gix ${TRKDATA}/ecgribfile.${PDY}${CYL} ${TRKDATA}/ecixfile.${PDY}${CYL}
  gribfile=${TRKDATA}/ecgribfile.${PDY}${CYL}
  ixfile=${TRKDATA}/ecixfile.${PDY}${CYL}

fi

# --------------------------------------------------
#   Process ECMWF Ensemble perturbation, if selected
# --------------------------------------------------

if [ ${model} -eq 21 ]
then

  if [ $loopnum -eq 1 ]
  then

    if [ -s ${TRKDATA}/ece${pert}gribfile.${PDY}${CYL} ]
    then
      rm ${TRKDATA}/ece${pert}gribfile.${PDY}${CYL}
    fi

    if [ ${pert_posneg} = 'n' ]; then
      posneg=2
    elif [ ${pert_posneg} = 'p' ]; then
      posneg=3
    elif [ ${pert_posneg} = 'c' ]; then
      # low-res control
      posneg=1
    else
      set +x
      echo " "
      echo "!!! ERROR: ECMWF PERT ID NOT RECOGNIZED"
      echo "!!! pert_posneg= ${pert_posneg}"
      echo " "
      set -x
      exit 8
    fi

    pnum=${pert_num}
    if [ ${pnum} -lt 10 ]; then
      pnum=` echo $pnum | cut -c2-2`
    fi

    if [ ${pnum} -eq 0 ]; then
      # low-res control
      pnum=2
    fi

    pert_grep_str=" 0 0 0 1 ${posneg} ${pnum} 1 "

    glo=${TRKDATA}/ece.lores.cut.${PDY}${CYL}
    xlo=${TRKDATA}/ece.lores.cut.${PDY}${CYL}.i

    if [ -s ${glo} ]; then rm ${glo}; fi
    if [ -s ${xlo} ]; then rm ${xlo}; fi

    grid="255 0 360 181 90000 0000 128 -90000 -1000 1000 1000 0"

    # This next part simply uses wgrib to parse out
    # the member records for each variable from each
    # respective enspost file.

    for var in u500 v500 u850 v850 mslp
    do

      ecegfile=enspost.t${CYL}z.${var}

      if [ ! -s ${ecedir}/${ecegfile} ]
      then
        set +x
        echo " "
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " !!! ECMWF ENSEMBLE POST File missing: ${ecedir}/${ecegfile}"
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " "
        set -x
        continue
      fi

      ece_orig=${ecedir}/${ecegfile}

      $wgrib -PDS10 ${ece_orig} | grep "${pert_grep_str}" |  awk -F:TR= '{print $1}' | $wgrib -i ${ece_orig} -grib -append -o ${glo}

    done

    # ECMWF data are 2.5-degree data, so for consistency
    # with the NCEP ensemble data, we now use copygb to
    # interpolate down to 1-degree.  The -g3 in the copygb
    # statement is for grid 3, a 1x1 global grid (AVN).

    ${gix} ${glo} ${xlo}
    gfile=${TRKDATA}/ece${pert}gribfile.${PDY}${CYL}
    $cgb -g"${grid}" -a ${glo} ${xlo} ${gfile}

    $gix ${TRKDATA}/ece${pert}gribfile.${PDY}${CYL} ${TRKDATA}/ece${pert}ixfile.${PDY}${CYL}

  fi

  gribfile=${TRKDATA}/ece${pert}gribfile.${PDY}${CYL}
  ixfile=${TRKDATA}/ece${pert}ixfile.${PDY}${CYL}

fi



# ------------------------------
#   Process NOGAPS, if selected
# ------------------------------

   if [ ${model} -eq 7 ]; then

      if [ -s ${TRKDATA}/ngpsgribfile.${PDY}${CYL} ]; then
         rm ${TRKDATA}/ngpsgribfile.${PDY}${CYL}
      fi

      for fhour in ${fcsthrs}
      do

         if [ ${fhour} -eq 99 ]; then
            continue
         fi

    vhour=${fhour}
      if [ ${fhour} -lt 99 ]; then
        vhour=0${fhour}
      fi

      if [ ! -s ${ngpsdir}/${ngpsgfile}${vhour} ]; then
         set +x
         echo " "
         echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
         echo " !!! NOGAPS File missing: ${ngpsdir}/${ngpsgfile}${fhour}"
         echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
         echo " "
#         err_exit " FAILED ${jobid} - MISSING NOGAPS FILE IN TRACKER SCRIPT - ABNORMAL EXIT"
         exit 1
         set -x
      fi

         if [ $fhour = '00' ]; then
            vtstring=":anl:"
         else
            vtstring="${fhour}hr"
         fi

      gfile=${ngpsdir}/${ngpsgfile}${vhour}
      $wgrib -s $gfile >ngps.ix

         for parm in ${wgrib_parmlist}
         do
            case ${parm} in
            "SurfaceU")
            grep "UGRD:10 m " ngps.ix | grep ${vtstring} | \
            $wgrib -s $gfile -i -grib -append -o ${TRKDATA}/ngpsgribfile.${PDY}${CYL} ;;
            "SurfaceV")
            grep "VGRD:10 m " ngps.ix | grep ${vtstring} | \
            $wgrib -s $gfile -i -grib -append -o ${TRKDATA}/ngpsgribfile.${PDY}${CYL} ;;
                 *)
            grep "${parm}" ngps.ix | grep ${vtstring} | \
            $wgrib -s $gfile -i -grib -append -o ${TRKDATA}/ngpsgribfile.${PDY}${CYL} ;;
            esac
         done

      done

      $gix ${TRKDATA}/ngpsgribfile.${PDY}${CYL} ${TRKDATA}/ngpsixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/ngpsgribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/ngpsixfile.${PDY}${CYL}

   fi

# ----------------------------------------------
#   *** ENSEMBLE RELOCATION ONLY FOLLOWS....***
#   Process Ensemble perturbation, if selected
# ----------------------------------------------

   if [ ${model} -eq 11 ]; then
    
      if [ -s ${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL} ]; then
         rm ${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL}
      fi
    
      for fhour in ${fcsthrs}
      do
    
         if [ ${fhour} -eq 99 ]; then
            continue
         fi
      
         if [ ! -s ${ensrdir}/${ensrgfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            echo " !!! ENSEMBLE RELOCATION ${PERT} File missing:               "
            echo " !!!                     ${ensrdir}/${ensrgfile}${fhour}     "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            echo " "
            set -x
            continue
         fi
      
         gfile=${ensrdir}/${ensrgfile}${fhour}
         $wgrib -s $gfile >ensr.ix

         for parm in ${wgrib_parmlist}
         do
            case ${parm} in
            "SurfaceU")
            grep "UGRD:10 m " ensr.ix | $wgrib -s $gfile -i -grib -append \
                               -o ${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL} ;;
            "SurfaceV")
            grep "VGRD:10 m " ensr.ix | $wgrib -s $gfile -i -grib -append \
                               -o ${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL} ;;
                 *)
            grep "${parm}" ensr.ix | $wgrib -s $gfile -i -grib -append \
                               -o ${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL} ;;
            esac

         done

      done

      $gix ${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL} ${TRKDATA}/ensr${pert}ixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/ensr${pert}gribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/ensr${pert}ixfile.${PDY}${CYL}

   fi

# ---------------------------------------------------
# * GFS ensemble control analysis (for relocation) *
# ---------------------------------------------------

   if [ ${model} -eq 12 ]; then
    
      if [ -s ${TRKDATA}/aeargribfile.${PDY}${CYL} ]; then
         rm ${TRKDATA}/aeargribfile.${PDY}${CYL}
      fi
    
      for fhour in ${fcsthrs}
      do
    
         if [ ${fhour} -eq 99 ]; then
            continue
         fi
      
         if [ ! -s ${aeardir}/${aeargfile}${fhour} ]; then
            set +x
            echo " "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            echo " !!! GFS control analysis file missing:                      "
            echo " !!!                      ${aeardir}/${aeargfile}${fhour}    "
            echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            echo " "
            set -x
            continue
         fi
      
         gfile=${aeardir}/${aeargfile}${fhour}
         $wgrib -s $gfile >aear.ix

         for parm in ${wgrib_parmlist}
         do
            case ${parm} in
            "SurfaceU")
            grep "UGRD:10 m " aear.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/aeargribfile.${PDY}${CYL} ;;
            "SurfaceV")
            grep "VGRD:10 m " aear.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/aeargribfile.${PDY}${CYL} ;;
                 *)
            grep "${parm}" aear.ix | $wgrib -s $gfile -i -grib -append \
                                  -o ${TRKDATA}/aeargribfile.${PDY}${CYL} ;;
            esac

         done

      done

      $gix ${TRKDATA}/aeargribfile.${PDY}${CYL} ${TRKDATA}/aearixfile.${PDY}${CYL}
      gribfile=${TRKDATA}/aeargribfile.${PDY}${CYL}
      ixfile=${TRKDATA}/aearixfile.${PDY}${CYL}

   fi

# --------------------------------------------------
#   Process SREF Ensemble perturbation, if selected
# --------------------------------------------------

   if [ ${model} -eq 13 ]; then

      grid='255 0 381 161 80000 160000 128 0000 350000  500  500 0'
#      grid='255 0 301 141 70000 190000 128 0000 340000  500  500 0'
#      grid='255 0 221 101 60000 200000 128 10000 310000  500  500 0'

      if [ ${regflag} -eq 0 ]; then
         if [ ${trkrtype} = 'tracker' ]; then
            set +x
            echo " "
            echo " !!! SREF ensemble has been selected, but there are no storms"
            echo " !!! in the TC Vitals file that can be processed.  That is, "
            echo " !!! there are no Vitals records from NHC.  The vitals "
            echo " !!! records that are in the updated vitals file must be from"
            echo " !!! another cyclone forecast center, and the SREF domain "
            echo " !!! does not extend to any region other than that covered "
            echo " !!! by NHC.  Exiting....."
            set -x
            exit 1
         fi
    fi

    if [ -s ${TRKDATA}/sref${pert}gribfile.${PDY}${CYL} ]; then
       rm ${TRKDATA}/sref${pert}gribfile.${PDY}${CYL}
    fi

    for fhour in ${fcsthrs}
    do

       if [ ${fhour} -eq 99 ]; then
          continue
       fi

       if [ ! -s ${srefdir}/${srefgfile}${fhour} ]; then
          set +x
          echo " "
          echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          echo " !!! SREF File missing: ${srefdir}/${srefgfile}${fhour}!"
          echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          set -x
          continue
       fi

       if [ -s ${TRKDATA}/tmpsrefixfile ]; then rm ${TRKDATA}/tmpsrefixfile; fi
          $gix ${srefdir}/${srefgfile}${fhour} ${TRKDATA}/tmpsrefixfile
          x1=${TRKDATA}/tmpsrefixfile

          set +x
          echo " "
          echo " Extracting SREF GRIB data for pert ${pert} "
          echo "                for forecast hour = $fhour"
          echo " "
          set -x

          g1=${srefdir}/${srefgfile}${fhour}

          $cgb -g"$grid" -k'4*-1 33 100 850' $g1 $x1 ${TRKDATA}/srefllu850.${pert}.grb.f${fhour};   rcc1=$?
          $cgb -g"$grid" -k'4*-1 33 100 700' $g1 $x1 ${TRKDATA}/srefllu700.${pert}.grb.f${fhour};   rcc2=$?
          $cgb -g"$grid" -k'4*-1 33 100 500' $g1 $x1 ${TRKDATA}/srefllu500.${pert}.grb.f${fhour};   rcc3=$?
          $cgb -g"$grid" -k'4*-1 33 105 10'  $g1 $x1 ${TRKDATA}/srefllu10m.${pert}.grb.f${fhour};   rcc4=$?
          $cgb -g"$grid" -k'4*-1 41 100 850' $g1 $x1 ${TRKDATA}/srefllav850.${pert}.grb.f${fhour};  rcc5=$?
          $cgb -g"$grid" -k'4*-1 41 100 700' $g1 $x1 ${TRKDATA}/srefllav700.${pert}.grb.f${fhour};  rcc6=$?
          $cgb -g"$grid" -k'4*-1  7 100 850' $g1 $x1 ${TRKDATA}/srefllz850.${pert}.grb.f${fhour};   rcc7=$?
          $cgb -g"$grid" -k'4*-1  7 100 700' $g1 $x1 ${TRKDATA}/srefllz700.${pert}.grb.f${fhour};   rcc8=$?
          $cgb -g"$grid" -k'4*-1  2 102   0' $g1 $x1 ${TRKDATA}/srefllmslp.${pert}.grb.f${fhour};   rcc9=$?

          if [ $rcc1 -eq 134 -o $rcc2 -eq 134 -o $rcc3 -eq 134 -o $rcc4 -eq 134 -o $rcc5 -eq 134 -o \
              $rcc6 -eq 134 -o $rcc7 -eq 134 -o $rcc8 -eq 134 -o $rcc9 -eq 134 ]
          then
             set +x
             echo " "
             echo "!!! ERROR using $cgb to interpolate sref data.  We will stop"
             echo "!!! execution because some variables may have been copied"
             echo "!!! okay, while some obviously have not, and that could lead"
             echo "!!! to unreliable results from the tracker.  Check to make"
             echo "!!! sure you've allocated enough memory for this job."
             echo "!!! Exiting.... "
             echo " "
             set -x
             err_exit " FAILED ${jobid} - ERROR INTERPOLATING SREF DATA IN TRACKER SCRIPT - ABNORMAL EXIT"
          fi

          cat ${TRKDATA}/srefllu850.${pert}.grb.f${fhour}   ${TRKDATA}/srefllu700.${pert}.grb.f${fhour} \
          ${TRKDATA}/srefllu500.${pert}.grb.f${fhour}   ${TRKDATA}/srefllz850.${pert}.grb.f${fhour} \
          ${TRKDATA}/srefllz700.${pert}.grb.f${fhour}   ${TRKDATA}/srefllmslp.${pert}.grb.f${fhour} \
          ${TRKDATA}/srefllav850.${pert}.grb.f${fhour}  ${TRKDATA}/srefllav700.${pert}.grb.f${fhour} \
          ${TRKDATA}/srefllu10m.${pert}.grb.f${fhour} \
          >>${TRKDATA}/sref${pert}gribfile.${PDY}${CYL}

          if [ $fhour -ne 99 ]; then
            set +x
            echo "+++ TIMING: AFTER  SREF COPYGB for fhour= $fhour  ---> `date`"
            set -x
          fi

       done

       $gix ${TRKDATA}/sref${pert}gribfile.${PDY}${CYL} ${TRKDATA}/sref${pert}ixfile.${PDY}${CYL}
       gribfile=${TRKDATA}/sref${pert}gribfile.${PDY}${CYL}
       ixfile=${TRKDATA}/sref${pert}ixfile.${PDY}${CYL}

   fi

# ------------------------------------------------------
#   Process Canadian (CMC) hi-res deterministic, if selected
# ------------------------------------------------------

   if [ ${model} -eq 15 ]; then

      if [ -s ${TRKDATA}/cmcgribfile.${PDY}${CYL} ]; then
         rm ${TRKDATA}/cmcgribfile.${PDY}${CYL}
      fi

    for fhour in ${fcsthrs}
    do

      if [ ${fhour} -lt 99 ]; then
        fhour=0${fhour}
      fi

      if [ ${fhour} -eq 99 ]
      then
        continue
      fi

      if [ ! -s ${cmcdir}/${cmcgfile}${fhour} ]
      then
        set +x
        echo " "
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " !!! CMC File missing: ${cmcdir}/${cmcgfile}${fhour}"
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " "
        echo " !!! Due to missing CMC file, execution is ending...."
        echo " "
        set -x
        exit 1
      fi

      gfile=${cmcdir}/${cmcgfile}${fhour}
      $wgrib -s $gfile >cmc.ix

         if [ $fhour = '000' ]; then
            vtstring=":anl:"
         else
        if [ ${fhour} -gt 99 ]; then
          vtstring="${fhour}hr"
        fi
        if [ ${fhour} -lt 10 ]; then
          vthour=` echo $fhour | cut -c3-3`
          vtstring="${vthour}hr"
        fi
        if [ ${fhour} -ge 10 -a ${fhour} -lt 99 ]; then
          vthour=` echo $fhour | cut -c2-3`
          vtstring="${vthour}hr"
        fi
       fi
         for parm in ${wgrib_parmlist}
         do
        case ${parm} in
          "SurfaceU")
            grep "UGRD:1.00000 (ETA level)" cmc.ix | grep ${vtstring} |  $wgrib -s $gfile -i -grib -append -o ${TRKDATA}/cmcgribfile.${PDY}${CYL} ;;
          "SurfaceV")
            grep "VGRD:1.00000 (ETA level)" cmc.ix | grep ${vtstring} |  $wgrib -s $gfile -i -grib -append -o ${TRKDATA}/cmcgribfile.${PDY}${CYL} ;;
                   *)
            grep "${parm}" cmc.ix | grep ${vtstring} |  $wgrib -s $gfile -i -grib -append -o ${TRKDATA}/cmcgribfile.${PDY}${CYL} ;;
          esac
         done

      done

      $gix ${TRKDATA}/cmcgribfile.${PDY}${CYL} ${TRKDATA}/cmcixfile.${PDY}${CYL}
      gribfile=${TRKDATA}//cmcgribfile.${PDY}${CYL}
      ixfile=${TRKDATA}//cmcixfile.${PDY}${CYL}

   fi

# ------------------------------------------------------
#   Process Canadian Ensemble perturbation, if selected
# ------------------------------------------------------

if [ ${model} -eq 16 ] ; then

    if [ -s ${TRKDATA}/cce${pert}gribfile.${PDY}${CYL} ]; then
      rm ${TRKDATA}/cce${pert}gribfile.${PDY}${CYL}
    fi

    for fhour in ${fcsthrs}
    do

      if [ ${fhour} -eq 99 ]
      then
        continue
      fi

      if [ ! -s ${ccedir}/${ccegfile}${fhour} ]
      then
        set +x
        echo " "
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " !!! CANADIAN ENSEMBLE ${PERT} File missing: ${ccedir}/${ccegfile}${fhour}"
        echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo " "
        set -x
        continue
      fi

      gfile=${ccedir}/${ccegfile}${fhour}
      $wgrib -s $gfile >ens.ix

      for parm in ${wgrib_parmlist}
      do
        case ${parm} in
          "SurfaceU")
            grep "UGRD:10 m " ens.ix | $wgrib -s $gfile -i -grib -append  -o ${TRKDATA}/cce${pert}gribfile.${PDY}${CYL} ;;
          "SurfaceV")
            grep "VGRD:10 m " ens.ix | $wgrib -s $gfile -i -grib -append  -o ${TRKDATA}/cce${pert}gribfile.${PDY}${CYL} ;;
                   *)
            grep "${parm}" ens.ix | $wgrib -s $gfile -i -grib -append  -o ${TRKDATA}/cce${pert}gribfile.${PDY}${CYL} ;;
        esac

      done

    done

    $gix ${TRKDATA}/cce${pert}gribfile.${PDY}${CYL} ${TRKDATA}/cce${pert}ixfile.${PDY}${CYL}

  gribfile=${TRKDATA}/cce${pert}gribfile.${PDY}${CYL}
  ixfile=${TRKDATA}/cce${pert}ixfile.${PDY}${CYL}

fi

# ----------------------------------------------
#------------------------------------------------------------------------#
#                         Now run the tracker                            #
#------------------------------------------------------------------------#

   ist=1
   while [ $ist -le 15 ]
   do
      if [ ${stormflag[${ist}]} -ne 1 ]; then
         set +x; echo "Storm number $ist NOT selected for processing"; set -x
      else
         set +x; echo "Storm number $ist IS selected for processing...."; set -x
      fi
      let ist=ist+1
   done

# Load the forecast hours for this particular model into an array 
# that will be passed into the executable via a namelist....

   ifh=1
   while [ $ifh -le ${maxtime} ]
   do
      fh[${ifh}]=` echo ${fcsthrs} | awk '{print $n}' n=$ifh`
      let ifh=ifh+1
   done

   namelist=${TRKDATA}/input.${atcfout}.${PDY}${CYL}
   ATCFNAME=` echo "${atcfname}" | tr '[a-z]' '[A-Z]'`

   contour_interval=200.0

  echo "&datein inp%bcc=${scc},inp%byy=${syy},inp%bmm=${smm},"      >${namelist}
  echo "        inp%bdd=${sdd},inp%bhh=${shh},inp%model=${model}/" >>${namelist}
  echo "&stormlist stswitch = ${stormflag[1]},${stormflag[2]},"    >>${namelist}
  echo "      ${stormflag[3]},${stormflag[4]},${stormflag[5]},"    >>${namelist}
  echo "      ${stormflag[6]},${stormflag[7]},${stormflag[8]},"    >>${namelist}
  echo "    ${stormflag[9]},${stormflag[10]},${stormflag[11]},"    >>${namelist}
  echo "   ${stormflag[12]},${stormflag[13]},${stormflag[14]},"    >>${namelist}
  echo "   ${stormflag[15]}/"                                      >>${namelist}
  echo "&fhlist itmphrs = ${fh[1]},${fh[2]},${fh[3]},"             >>${namelist}
  echo "      ${fh[4]},${fh[5]},${fh[6]},${fh[7]},"                >>${namelist}
  echo "      ${fh[8]},${fh[9]},${fh[10]},${fh[11]},"              >>${namelist}
  echo "      ${fh[12]},${fh[13]},${fh[14]},"                      >>${namelist}
  echo "      ${fh[15]},${fh[16]},${fh[17]},"                      >>${namelist}
  echo "      ${fh[18]},${fh[19]},${fh[20]},"                      >>${namelist}
  echo "      ${fh[21]},${fh[22]},${fh[23]},"                      >>${namelist}
  echo "      ${fh[24]},${fh[25]},${fh[26]},"                      >>${namelist}
  echo "      ${fh[27]},${fh[28]},${fh[29]},"                      >>${namelist}
  echo "      ${fh[30]},${fh[31]}/"                                >>${namelist}
  echo "&atcfinfo atcfnum=${atcfnum},atcfname='${ATCFNAME}'/"      >>${namelist}
  echo "&trackerinfo trkrinfo%westbd=${trkrwbd},"                  >>${namelist}
  echo "      trkrinfo%eastbd=${trkrebd},"                         >>${namelist}
  echo "      trkrinfo%northbd=${trkrnbd},"                        >>${namelist}
  echo "      trkrinfo%southbd=${trkrsbd},"                        >>${namelist}
  echo "      trkrinfo%type='${trkrtype}',"                        >>${namelist}
  echo "      trkrinfo%mslpthresh=${mslpthresh},"                  >>${namelist}
  echo "      trkrinfo%v850thresh=${v850thresh},"                  >>${namelist}
  echo "      trkrinfo%gridtype='${modtyp}',"                      >>${namelist}
  echo "      trkrinfo%contint=${contour_interval}/"               >>${namelist}
  
   export pgm=genesis_gettrk
   . prep_step

   ln -s -f ${gribfile}                                                  fort.11
   ln -s -f ${TRKDATA}/vitals.upd.${atcfout}.${PDY}${shh}                fort.12
   ln -s -f ${ixfile}                                                    fort.31
   ln -s -f ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL}       fort.61
   ln -s -f ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL}      fort.62
   ln -s -f ${TRKDATA}/trak.${atcfout}.radii.${regtype}.${PDY}${CYL}     fort.63
   ln -s -f ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL}  fort.64

   if [ ${atcfname} = 'aear' ]; then
      ln -s -f ${TRKDATA}/trak.${atcfout}.initvitl.${PDY}${CYL}          fort.65
   fi

   if [ ${trkrtype} = 'midlat' -o ${trkrtype} = 'tcgen' ]; then
      ln -s -f ${TRKDATA}/trkrmask.${atcfout}.${regtype}.${PDY}${CYL}    fort.72
   fi


   set +x
   echo " "
   echo " -----------------------------------------------"
   echo "           NOW EXECUTING TRACKER......"
   echo " -----------------------------------------------"
   echo " "
   set -x

   msg="$pgm start for $atcfout at ${CYL}z"
   postmsg "$jlogfile" "$msg"

#LB-----------------------------------------------------------------------
#    The bullet proofing for nam and sref has been removed because the
#    hooks in the postprocessors for each model's tracks will take the
#    regions into account.
#LB-----------------------------------------------------------------------

   if [ ${cmodel} = 'gefs' -o ${cmodel} = 'sref' ]; then

      $EXECtrkr/genesis_gettrk <${namelist} ${loopnum} 2>&1 >${TRKDATA}/trkr.${regtype}.${cmodel}.${pert}.out
   else
      $EXECtrkr/genesis_gettrk <${namelist} ${loopnum} 2>&1 >${TRKDATA}/trkr.${regtype}.${cmodel}.out

   fi
   gettrk_rcc=$?

#--------------------------------------------------------------#
# Now copy the output track files to different directories
#--------------------------------------------------------------#

   set +x
   echo " "
   echo " -----------------------------------------------"
   echo "    NOW COPYING OUTPUT TRACK FILES TO COM  "
   echo " -----------------------------------------------"
   echo " "
   set -x

   if [ ${gettrk_rcc} -eq 0 ]; then

      if [ ${PARAFLAG} = 'YES' ]; then
         cp ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} ../.
      else
         cp ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} $COM/.
      fi

      msg="$pgm end for $atcfout at ${CYL}z completed normally"
      postmsg "$jlogfile" "$msg"

# Copy atcf files to NHC archives. We'll use Steve Lord's original script,
# distatcf.sh, to do this, and that script requires the input atcf file to
# have the name "attk126", so first copy the file to that name, then call
# the distatcf.sh script.  After that's done, then copy the full 0-126-h
# track into the /com/hur/prod/global track archive file.

      if [ ${SENDCOM} = 'YES' ]; then

         gltrakarch=${gltrakarch:-${gltrkdir}/gentracks.${syy}}

         glatcfarch=${glatcfarch:-${gltrkdir}/gentracks.atcf.${syy}}

         glatuxarch=${glatuxarch:-${gltrkdir}/gentracks.atcfunix.${syy}}

         glradarch=${glradarch:-${gltrkdir}/gentracks.radii.${syy}}

#         cat ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL}   >>${gltrakarch}
#         cat ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL}   >>${tmtrakarch}

#         cat ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL}  >>${glatcfarch}
#         cat ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL}  >>${tmatcfarch}

         cat ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL}  >>${glatuxarch}
#         cat ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL}  >>${tmatuxarch}

#         cat ${TRKDATA}/trak.${atcfout}.radii.${regtype}.${PDY}${CYL} >>${glradarch}
#         cat ${TRKDATA}/trak.${atcfout}.radii.${regtype}.${PDY}${CYL} >>${tmradarch}

         if [ ${PARAFLAG} = 'YES' ]; then
            echo " "
         else
            cp ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL} attk126
          ### For testing --Julia
            #${ushtrkdir}/extrkr_distatcf.sh

            if [ ${cmodel} = 'gfdl' ]; then

               cp ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL} ${COM}/${stormenv}.${PDY}${CYL}.trackerall.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL} ${COM}/${stormenv}.${PDY}${CYL}.trackeratcf.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} ${COM}/${stormenv}.${PDY}${CYL}.trackeratcfunix.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL} ${COMOMB}/${stormenv}.${PDY}${CYL}.trackerall.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL} ${COMOMB}/${stormenv}.${PDY}${CYL}.trackeratcf.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} ${COMOMB}/${stormenv}.${PDY}${CYL}.trackeratcfunix.${regtype}

            else

               cp ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL} ${COM}/${atcfout}.t${CYL}z.cyclone.track.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL} ${COM}/${atcfout}.t${CYL}z.cyclone.trackatcf.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} ${COM}/${atcfout}.t${CYL}z.cyclone.trackatcfunix.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.radii.${regtype}.${PDY}${CYL} ${COM}/${atcfout}.t${CYL}z.cyclone.radii.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.all.${regtype}.${PDY}${CYL} ${COMOMB}/${atcfout}.t${CYL}z.cyclone.track.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcf.${regtype}.${PDY}${CYL} ${COMOMB}/${atcfout}.t${CYL}z.cyclone.trackatcf.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} ${COMOMB}/${atcfout}.t${CYL}z.cyclone.trackatcfunix.${regtype}
               cp ${TRKDATA}/trak.${atcfout}.radii.${regtype}.${PDY}${CYL} ${COMOMB}/${atcfout}.t${CYL}z.cyclone.radii.${regtype}

	       if [ $regtype = "ecml" ] ; then
                  cat ${COMOMB}/${atcfout}.t${CYL}z.cyclone.trackatcfunix.${regtype} >> ${COMOMB}/cyclone_${PDY}${CYL}.ens
		  if [ $SENDDBN = "YES" ] ; then
		     $DBNROOT/bin/dbn_alert MODEL OMBATCF $job ${COMOMB}/cyclone_${PDY}${CYL}.ens
		  fi
	       fi

            fi

            tmscrdir=/com/arch/${envir}

            tmtrakstat=${tmscrdir}/tracker.prod.status
          echo "${atcfout} trkr completed okay for ${PDY}${CYL}" >>${tmtrakstat}

# We need to parse apart the atcfunix file and distribute the forecasts to 
# the necessary directories.  To do this, first sort the atcfunix records 
# by forecast hour (k6), then sort again by ocean basin (k1), storm number (k2)
# and then quadrant radii wind threshold (k12).  Once you've got that organized 
# file, break the file up by putting all the forecast records for each storm 
# into a separate file.  Then, for each file, find the corresponding atcfunix 
# file in the /tpcprd directory and dump the atcfunix records for that storm 
# in there.  NOTE: Only do this if the model run is NOT for the ensemble.
# the reason is that we do NOT want to write out the individual member tracks
# to the atcfunix file.  We only want to write out the ensemble mean track
# to the atcfunix file, and the mean track is calculated in a separate script.

            if [ $cmodel != 'gefs' ]; then

               if [ ${cmodel} = 'gfdl' ]; then
                  auxfile=${COM}/${stormenv}.${PDY}${CYL}.trackeratcfunix
               else
                  auxfile=${TRKDATA}/trak.${atcfout}.atcfunix.${regtype}.${PDY}${CYL}
               fi

               sort -k6 ${auxfile} | sort -k1 -k2 -k12  >atcfunix.sorted

               old_string="XX, XX"

               ict=0
               while read unixrec
               do
                  storm_string=` echo "${unixrec}" | cut -c1-6`
                  if [ "${storm_string}" = "${old_string}" ]; then
                     echo "${unixrec}" >>atcfunix_file.${ict}
                  else
                     let ict=ict+1
                     echo "${unixrec}"  >atcfunix_file.${ict}
                     old_string="${storm_string}"
                  fi
               done <atcfunix.sorted


            fi

         fi

      fi

   else

      if [ ${PARAFLAG} = 'YES' ]; then
         echo " "
      else
         tmtrakstat=/com/arch/${envir}/tracker.prod.status
        echo "ERROR: ${atcfout} tracker FAILED for ${PDY}${CYL}" >>${tmtrakstat}
      fi

      set +x
      echo " "
      echo "!!! ERROR -- An error occurred while running genesis_gettrk, "
      echo "!!! which is the program that actually gets the track."
      echo "!!! Return code from genesis_gettrk = ${gettrk_rcc}"
      echo "!!! model= ${atcfout}, forecast initial time = ${PDY}${CYL}"
      echo "!!! Exiting...."
      echo " "
      set -x
      err_exit " FAILED ${jobid} - ERROR RUNNING GETTRK IN TRACKER SCRIPT- ABNORMAL EXIT"

   fi

#   Run the script that filters out storms which don't live
#   for at least 24h....

      ${ushtrkdir}/sort_tracks.sh ${PDY}${CYL} ${atcfout} ${regtype} >${TRKDATA}/sort.${regtype}.${atcfout}.${PDY}${CYL}.out

cat ${TRKDATA}/storms.${atcfout}.atcfunix.${regtype}.${PDY}${CYL} >>${savedir}/storms.${atcfout}.atcfunix.${regtype}.${syyyy}
done

#---------------------------------------------
set +x
echo " "
echo "Time at end of `basename $0` is `date`"
#-------------------------END EXTRKR_NEW.SMS.SH-------------------------
