#!/bin/ksh

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

set +x
##############################################################################
echo " "
echo "-----------------------------------------------------"
echo " +++ - Compute ensemble mean cyclone forecast track  "
echo "-----------------------------------------------------"
echo "History: Dec 2015 - multiple model ensemble mean-----"
echo " "
echo "Current time is: `date`"
echo " "
##############################################################################
set -x

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

# This script computes ensemble mean cyclone tracks.  It dumps all
# the cyclone tracks for a given ensemble forecast into one file,
# then runs a fortran program to compute the ensemble mean, spread
# and mode.  In addition, strike probabilities are produced for
# any storms in the Atlantic, EastPac and WestPac basins. 
#
# Environmental variable inputs needed for this scripts:
#  PDY   -- The date for data being processed, in YYYYMMDD format
#  cycle -- The cycle for data being processed (t00z, t06z, t12z, t18z)
#  cmodel -- Model being processed: ens (ncep ensemble), cens (CMC ensemble),
#                                   ece (ECMWF ensemble).
#  envir -- 'prod' or 'test'
#  SENDCOM -- 'YES' or 'NO'
#
# For testing script interactively in non-production, set following vars:
#     gltrkdir   - Directory for output tracks
#
#-------J.Peng---2010-09-27-------------------------
export PDY=$1
export CYL=$2
export CYCLE=t${CYL}z
export cmodel=$3
export DATA=$4

export jlogfile=${DATA}/log_file_mean
export pgmout=${DATA}/pgm_out_mean
export ROOT=/gpfs/dell2/emc/modeling/save/Jiayi.Peng


#export CYL=${CYL:-` echo ${cycle} | cut -c2-3`}
ymdh=${PDY}${CYL}

cent=`  echo ${ymdh} | cut -c1-2`
yy=`    echo ${ymdh} | cut -c3-4`
mm=`    echo ${ymdh} | cut -c5-6`
dd=`    echo ${ymdh} | cut -c7-8`
hh=`    echo ${ymdh} | cut -c9-10`
syyyy=` echo ${ymdh} | cut -c1-4`
ymd=${yy}${mm}${dd}
yyyymmdd=${cent}${yy}${mm}${dd}
export cyc=${hh}

#ndate=/nwprod/util/exec/ndate

#-------J.Peng---2010-09-27-------------------------
export envir=${envir:-test}
export SENDCOM=${SENDCOM:-NO}

#export exectrkdir=${exectrkdir:-/nwprod/util/exec}

#export gltrkdir=${gltrkdir:-/com/hur/${envir}/ensemble}
export gltrkdir=${gltrkdir:-$ROOT/${envir}/track}

#export ATCFdir=${ATCFdir:-/com/tpc/prod/atcf}
export ATCFdir=$ROOT/${envir}/atcf

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

#/nwprod/util/ush/setup.sh

case ${cmodel} in

   ref) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ Tom ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="GRMN"                                          ;
        amodel="grmn"                                          ;
#        achar="a"                                         ;
COM=${COM:-/ensemble/save/Jiayi.Peng/refor/data/ens.${PDY}${CYL}} ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

  ukes) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ UKMET ensemble tracks will be averaged...."    ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="UKMN"                                          ;
        amodel="ukmn"                                          ;
#        achar="u"                                              ;
#        COM=${COM:-/com/gens/prod/cmce.${PDY}/${CYL}}          ;
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/cmce}  ;
#--J.Peng------2010-11-09-------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/cmce}  ;
#COM=${COM:-/ensemble/save/Jiayi.Peng/track/ukes.${PDY}${CYL}} ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

  fres) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ FRANCE ensemble tracks will be averaged...."    ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="FRMN"                                          ;
        amodel="frmn"                                          ;
#        achar="u"                                              ;
#        COM=${COM:-/com/gens/prod/cmce.${PDY}/${CYL}}          ;
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/cmce}  ;
#--J.Peng------2010-11-09-------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/cmce}  ;
        COM=${COM:-/ensemble/save/Jiayi.Peng/track/fres.${PDY}${CYL}} ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

#--J.Peng--------2010-10-29-------------------------------------------
   g2c) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ NCEP+CMC ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="2EMN"                                          ;
        amodel="2emn"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-09--------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/gefs2cmce}        ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2010-11-18-------------------------------------------
   gce4) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP+CMC+FNMOC ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="4EMN"                                          ;
        amodel="4emn"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2011-08-02-------------------------------------------
   gce5) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP+CMC+FNMOC+UK ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="5EMN"                                          ;
        amodel="5emn"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2011-08-02-------------------------------------------
   gce6) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP+CMC+FNMOC+UK+HFIP ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="MIX6"                                          ;
        amodel="mix6"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

   neu3) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP+UK ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="3NEU"                                          ;
        amodel="3neu"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2011-01-07-------------------------------------------
   ncf3) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ NCEP+CMC+FNMOC ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="3NCF"                                          ;
        amodel="3ncf"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2016-10-18-------------------------------------------
   ens) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ GEFS-legacy ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="AEMN"                                          ;
        amodel="aemn"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;


#--J.Peng--------2012-04-27-------------------------------------------
   naf2) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ NCEP+FNMOC ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="2NAF"                                          ;
        amodel="2naf"                                          ;
#        achar="A"                                         ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2010-11-02-------------------------------------------
   gce3) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP+CMC ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="3EMN"                                          ;
        amodel="3emn"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-09--------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/gce3}        ;
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/gce3}        ;
        COM=${DATA}; 

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2011-10-19-------------------------------------------
   nae2) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="2NAE"                                          ;
        amodel="2nae"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-09--------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/gce3}        ;
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/gce3}        ;
        COM=${DATA};

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2014-05-21-------------------------------------------
   inae) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ ECMWF+NCEP ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="NAEI"                                          ;
        amodel="naei"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-09--------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/gce3}        ;
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/gce3}        ;
        COM=${DATA};

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2010-11-08-------------------------------------------
   gce3a) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ eemn+2EMN ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="SEMN"                                          ;
        amodel="semn"                                          ;
#        achar="A"                                         ;
        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/gce3a}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

#--J.Peng--------2010-11-15-------------------------------------------
   gce3b) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ eemn+cemn+aemn ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="PEMN"                                          ;
        amodel="pemn"                                          ;
#        achar="A"                                         ;
        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/gce3b}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                         ;;

   sing) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ AVNO+EMX+CMC tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="3DET"                                          ;
        amodel="3det"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-10----------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/single}        ;
        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/single}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

  tot6) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ AVNO+EMX+CMC+AEMN+CEMN+EEMN tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="6EMN"                                          ;
        amodel="6emn"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-10----------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/6emn}        ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

  six6) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ EEMN+AEMN+CEMN+FEMN+UKMN+HPMN tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="SIXM"                                          ;
        amodel="sixm"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-10----------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/6emn}        ;
        COM=${DATA}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

  totw) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ AVNO+EMX+CMC+AEMN+CEMN+EEMN tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="6EMW"                                          ;
        amodel="6emw"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-10----------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/6emn}        ;
        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/6emn_weight/atcfunix}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

  tot4) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ AVNO+EMX+CMC+3EMN tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="T4MN"                                          ;
        amodel="t4mn"                                          ;
#        achar="A"                                         ;
        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/t4mn}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

  tot2) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ 3EMN+3DET tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="S6MN"                                          ;
        amodel="s6mn"                                          ;
#        achar="A"                                         ;
#--J.Peng--------2010-11-15----------------------------------------
#        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2009/6emn}        ;
        COM=${COM:-/hfip/noscrub/Jiayi.Peng/2010/s6mn}        ;

        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;


  sref) set +x                                                 ;
        echo " "                                               ;
        echo " ++ Input cmodel parameter = ${cmodel}...."      ;
        echo " ++ SREF ensemble tracks will be averaged...."   ;
        echo " "                                               ;
        set -x                                                 ;
        AMODEL="SFMN"                                          ;
        amodel="sfmn"                                          ;
        achar="s"                                              ;
#        COM=${COM:-/com/sref/${envir}/sref.${PDY}/${CYL}/track};
COM=${COM:-/ensemble/save/Jiayi.Peng/track/sref.${PDY}${CYL}} ;
        SENDDBN=NO                                             ;
        SENDTRACKER=NO                                        ;;

     *) set +x                                                 ;
        echo " "                                               ;
        echo " !! INPUT CMODEL PARAMETER IS NOT RECOGNIZED."   ;
        echo " !! Input cmodel parameter = ${cmodel}...."      ;
        echo " "                                               ;
        set -x                                                 ;
        err_exit " FAILED ${jobid} -- UNKNOWN cmodel IN TRACK-AVERAGING SCRIPT - ABNORMAL EXIT";;

esac

#---------------------------------------------------
# Run the program that calculates the ensemble mean
# track and generates the probability files....
#---------------------------------------------------

echo "TIMING: Time before any of the track-averaging stuff is `date`"

>trak.allperts.atcfunix.${amodel}.${ymdh}

# for tfile in `ls -1 ${COM}/${achar}[np]*.t${cyc}z.cyclone.trackatcfunix`
#------J.Peng----2010-10-28-----------------------
#for tfile in `ls -1 ${COM}/${achar}[np]*.t${cyc}z.cyclone.trackatcfunix`
#for tfile in `ls -1 ${COM}/trak.${achar}P??.atcfunix.${PDY}${cyc}`

#----J.Peng-----2010-10-29----and 2010-11-02 ---------------------
if [ ${cmodel} = "g2c" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done
elif [ ${cmodel} = "gce4" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "gce5" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "gce6" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc} ${COM}/track.${PDY}${cyc}.GE??`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "neu3" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "ncf3" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

#----J.Peng-----2016-10-18--------------------------------
elif [ ${cmodel} = "ens" ]; then
  for tfile in `ls -1 ${COM}/trak.ap*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

#----J.Peng-----2012-04-27--------------------------------
elif [ ${cmodel} = "naf2" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "gce3a" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "gce3b" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "sing" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done
elif [ ${cmodel} = "tot6" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "six6" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "totw" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "tot4" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "tot2" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "ref" ]; then
#  for tfile in `ls -1 ${COM}/trak.gr*.atcfunix.${PDY}${cyc}`
#  do
#    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
#  done
cat ${COM}/*gr01* ${COM}/*gr02* ${COM}/*gr03* ${COM}/*gr04* ${COM}/*gr05* ${COM}/*gr06* ${COM}/*gr07* ${COM}/*gr08* ${COM}/*gr09* ${COM}/*gr10* >trak.allperts.atcfunix.${amodel}.${ymdh} 

elif [ ${cmodel} = "ukes" ]; then
  for tfile in `ls -1 ${COM}/trak.uk[0123]*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "fres" ]; then
  for tfile in `ls -1 ${COM}/trak.fr[0123]*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "sref" ]; then
#  for tfile in `ls -1 ${COM}/trak.${achar}[aenr]*.atcfunix.${PDY}${cyc}`
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "gce3" ]; then
  for tfile in `ls -1 ${COM}/trak.[ace][np]*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "nae2" ]; then
  for tfile in `ls -1 ${COM}/trak.[ae][np]*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

elif [ ${cmodel} = "inae" ]; then
  for tfile in `ls -1 ${COM}/trak.*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done

else
  for tfile in `ls -1 ${COM}/trak.${achar}[NP]*.atcfunix.${PDY}${cyc}`
  do
    cat $tfile >>trak.allperts.atcfunix.${amodel}.${ymdh}
  done
fi

numrecs=` cat trak.allperts.atcfunix.${amodel}.${ymdh} | wc -l`
if [ ${numrecs} -gt 0 ]; then
  echo
else
  echo " "
  echo "+++ NOTE: NO MEMBER TRACKS EXIST FOR ${ymdh}"
  echo "+++ EXITING...."
  exit 0
fi

for dt in 65
do

  set +x
  echo "TIMING: Time just before call to trakave for dt= $dt is  `date`"
  set -x
  export pgm=ens_trak_ave
  . prep_step

  rm -rf fort.11 fort.51 fort.53 fort.54 fort.55 fort.56 fort.57

  ln -s -f ${DATA}/trak.allperts.atcfunix.${amodel}.${ymdh}     fort.11
  ln -s -f ${DATA}/${amodel}.trkprob.${ymdh}.${dt}.ctlinfo.txt  fort.51
#   Unit 52: Created internally, may be 1 to 15 files...

  ln -s -f ${DATA}/trak.${amodel}.atcfunix.${ymdh}              fort.53
  ln -s -f ${DATA}/trak.${amodel}.atcf.${ymdh}                  fort.54
  ln -s -f ${DATA}/trak.${amodel}.all.${ymdh}                   fort.55
  ln -s -f ${DATA}/trak.${amodel}.spread.${ymdh}                fort.56
  ln -s -f ${DATA}/trak.${amodel}.mode.${ymdh}                  fort.57
#----J.Peng----2011-03-01-------------------------------------------
#  ln -s -f /hfip/noscrub/Jiayi.Peng/2010/super_new/weight/weight.dat fort.92 

  namelist=${DATA}/input.${ymdh}.nlist
  echo "&datain dthresh=${dt}.0,cmodel='${cmodel}'/" >${namelist}

#  /ptmpp1/wx20tm/trakout/2009082600/ens_trak_ave <${namelist} >${DATA}/ens_trak_ave.${ymdh}.${dt}.fout
#  ${exectrkdir}/ens_trak_ave <${namelist} >${DATA}/ens_trak_ave.${ymdh}.${dt}.fout
#  /ensemble/save/Jiayi.Peng/meantker/ens_trak_ave.fd_new/ens_trak_ave <${namelist} >${DATA}/ens_trak_ave.${ymdh}.${dt}.fout
  $ROOT/meantkerN/ens_trak_ave.fd/ens_trak_ave <${namelist} >${DATA}/ens_trak_ave.${ymdh}.${dt}.fout
#/ensemble/save/Jiayi.Peng/ens_tracker.v1.0.0/sorc/ens_trak_ave.fd/ens_trak_ave <${namelist} >${DATA}/ens_trak_ave.${ymdh}.${dt}.fout

  ens_trak_ave_rcc=$?

  set +x
  echo "TIMING: Time just after call to trakave for dt= $dt is  `date`"
  set -x

done

set +x
echo "TIMING: Time after loop to get mean & probabilities for $ymdh is `date`"
set -x

if [ ${ens_trak_ave_rcc} -ne 0 ]
then
  set +x
  echo " "
  echo "!!! ERROR -- An error occurred while running ens_trak_ave.x, "
  echo "!!! which is the program that computes the mean track."
  echo "!!! Return code from ens_trak_ave.x = ${ens_trak_ave_rcc}"
  echo "!!! model= ${amodel}, forecast initial time = ${PDY}${CYL}"
  echo "!!! Exiting...."
  echo " "
  set -x
  err_exit " FAILED ${jobid} - ERROR RUNNING ENS_TRAK_AVE.X - ABNORMAL EXIT"
fi

#-----------------------------------------------------
# Parse out the atcfunix records and send them to the
# correct tpcprd directory and file.

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

  glatuxarch=${glatuxarch:-${gltrkdir}/tracks.atcfunix.${yy}}
  cat ${DATA}/trak.${amodel}.atcfunix.${ymdh}           >>${glatuxarch}

  glmodearch=${glmodearch:-${gltrkdir}/tracks.ens_mode.atcfunix.${yy}}
  cat ${DATA}/trak.${amodel}.mode.${ymdh}               >>${glmodearch}

  cp ${DATA}/trak.${amodel}.atcfunix.${ymdh} ${COM}/${amodel}.t${CYL}z.cyclone.trackatcfunix
  cp ${DATA}/trak.${amodel}.spread.${ymdh}   ${COM}/${amodel}.t${CYL}z.cyclone.trackspread
  cp ${DATA}/trak.${amodel}.mode.${ymdh}     ${COM}/${amodel}.t${CYL}z.cyclone.trackmode
  cp ${DATA}/${amodel}.trkprob.*${ymdh}*.ieee             ${COM}/.
  cp ${DATA}/${amodel}.trkprob.${ymdh}.${dt}.ctlinfo.txt  ${COM}/.

  export SENDDBN=${SENDDBN:-NO}
  export SENDTRACKER=${SENDTRACKER:-NO}
  if [ ${SENDDBN} = 'YES' -o ${SENDTRACKER} = 'YES' ]
  then
    if [ $cmodel != 'ece' ]; then
      $DBNROOT/bin/dbn_alert ATCFUNIX GFS_NAVY $job ${COM}/${amodel}.t${CYL}z.cyclone.trackatcfunix
    fi
  fi  

  # 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.

  auxfile=${DATA}/trak.${amodel}.atcfunix.${PDY}${cyc}
  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

  nhcct=0
  if [ $ict -gt 0 ]
  then
    mct=0
    while [ $mct -lt $ict ]
    do
      let mct=mct+1
      at=` head -1 atcfunix_file.$mct | cut -c1-2 | tr '[A-Z]' '[a-z]'`
      NO=` head -1 atcfunix_file.$mct | cut -c5-6`
      if [ -d /tpcprd/atcf_unix/${at}${NO}${syyyy} ]
      then
        cat atcfunix_file.$mct >>/tpcprd/atcf_unix/${at}${NO}${syyyy}/a${at}${NO}${syyyy}.dat
        set +x
        echo " "
        echo "+++ Adding records to  TPC ATCFUNIX directory: /tpcprd/atcf_unix/${at}${NO}${syyyy}"
        echo " "
        set -x
      else
        set +x
        echo " "
        echo "There is no TPC ATCFUNIX directory for: /tpcprd/atcf_unix/${at}${NO}${syyyy}"
        set -x
      fi
      if [ $at = 'al' -o $at = 'ep' ]; then
        let nhcct=nhcct+1
      fi
    done
  fi

  cat ${DATA}/ens_trak_ave.${ymdh}.${dt}.fout

fi