#!/usr/bin/ksh

# This script was written by Tim Marchok

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

cd ${TRKDATA}


YMDH=$1
model=$2
regtype=$3

if [ $# -lt 3 ]; then
  set +x
  echo " "
  echo "!!! ERROR: You did not enter at least 2 arguments."
  echo "!!! "
  echo "!!! USAGE: `basename $0` yyyymmddhh model regtype"
  echo "!!!        where model is gfs, nam, etc...."
  echo "!!!        regtype = ecml, altg, wptg"
  echo "!!!           ecml: US East Coast mid lat cyclones"
  echo "!!!           altg: EPAC & Atlantic tropical cyclones"
  echo "!!!           wptg: WPAC tropical cyclones"
  echo "!!! "
  echo " "
  exit 8
fi

set -x

#vdrcc=`${datecheck} ${YMDH} >/dev/null; echo $?`
#if [ ${vdrcc} -eq 0 ]; then
#  echo " "
#  echo "Imported date there, using ymdh= $YMDH"
#  echo " "
#else
#  tmpdate=`date '+%Y%m%d'`
#  indate=${tmpdate}00
#  echo " "
#  echo "!!! IMPORTED DATE NOT THERE; USING CURRENT DAY'S 00Z CYCLE"
#  echo " "
#fi
  
ymdh=${YMDH}
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`
yyyy=`echo ${ymdh} | cut -c1-4`
ymd=${cent}${yy}${mm}${dd}
yyyymmddhh=${cent}${yy}${mm}${dd}${hh}
  
trakfile=trak.${model}.atcfunix.${regtype}.${ymdh}

# There can be up to 3 different forecast records for each time 
# level (1 each for 34-, 50- and 64-knot wind radii).  For our 
# plotting purposes, we just care about the track, so just pick
# out one of them (the records that have 34-kt winds).  The 
# lat/lon information will be the same on all 3.  Sort these 
# records by storm number.

cat ${trakfile} | grep "34, NEQ" | grep FOF | sort +0 -n >${trakfile}.sorted


# Figure out how many storms we have by getting the number of
# the last storm in this sorted list.

last_storm_num=`  tail -1 ${trakfile}.sorted | cut -c4-6`

set +x
echo "last_storm_num=  ${last_storm_num}"
set -x

# Now go through this sorted list and see how many storms we
# have in there that lived for at least 24 hours.  Storms 
# that lived for less than that are discarded in order to 
# eliminate "noise" disturbances.  The exception to this is
# if a storm exists at the end of a forecast and has lived 
# for only, say 12 or 18 hours.  Since genesis may have 
# occurred, we don't want to discard the storm simply because
# the model forecast length is not long enough to capture 
# the full 24 hours needed.  Rather, we will include this 
# storm if the mslp at the end of the forecast is at least
# as low (or lower) as it was when first detected (this is
# included in the "elif" statement below).

last_fcst_hour=`cat ${TRKDATA}/last_fcst_hour.${model}.${ymdh}`
set +x
echo " "
echo "Last forecast hour for ${model} model = --->${last_fcst_hour}<---"
echo " "
set -x

>storms.${model}.atcfunix.${regtype}.${ymdh}

ist=1
while [ $ist -le ${last_storm_num} ]
do

  if [ $ist -lt 10 ]; then
    charnum="00${ist}"
  elif [ $ist -lt 100 ]; then
    charnum="0${ist}"
  else
    charnum="${ist}"
  fi

  egrep "(ML,${charnum}|TG,${charnum})" ${trakfile}.sorted >ind.storm.temp.txt

  numtimes=` cat ind.storm.temp.txt | wc -l`
  if [ ${numtimes} -eq 0 ]; then
    set +x
    echo " "
    echo "!!! Storm $charnum has no atcfunix records...."
    echo " "
    set -x
    let ist=ist+1
    continue
  fi

  first_hour=` head -1 ind.storm.temp.txt | cut -c31-33`
  first_mslp=` head -1 ind.storm.temp.txt | cut -c54-57`
  first_mslp=` expr ${first_mslp}`

  last_hour=`  tail -1 ind.storm.temp.txt | cut -c31-33`
  last_mslp=`  tail -1 ind.storm.temp.txt | cut -c54-57`
  last_mslp=`  expr ${last_mslp}`

  let storm_length=last_hour-first_hour

  if [ ${storm_length} -ge 24 ]; then
    set +x
    echo " "
    echo "+++ Storm $charnum lived for at least 24h.  Length= $storm_length"
    echo " "
    set -x
    cat ind.storm.temp.txt >>storms.${model}.atcfunix.${regtype}.${ymdh}
#    cat ind.storm.temp.txt >>${pdir}/storms.${model}.atcfunix.${regtype}.${ymdh}
  elif [ ${storm_length} -ge 12 -a ${last_hour} -eq ${last_fcst_hour} -a \
         ${last_mslp} -le ${first_mslp} ]; then
    set +x
    echo " "
    echo "+++ Storm $charnum exists at the end of the forecast AND has "
    echo "+++ lived for at least 12h AND has a mslp that is at least as"
    echo "+++ low as (or lower than) the pressure 12h ago.  Length= $storm_length"
    echo "+++ first_hour= ${first_hour} "
    echo "+++ first_mslp= ${first_mslp} "
    echo "+++ last_hour=  ${last_hour} "
    echo "+++ last_mslp=  ${last_mslp} "
    echo " "
    set -x
    cat ind.storm.temp.txt >>storms.${model}.atcfunix.${regtype}.${ymdh}
#    cat ind.storm.temp.txt >>${pdir}/storms.${model}.atcfunix.${regtype}.${ymdh}
  else
    set +x
    echo " "
    echo "!!! Storm $charnum DID NOT LIVE at least 24h.  Length= $storm_length"
    echo " "
## GP Lou: In order to save analysis cyclone center, all records are saved:
    cat ind.storm.temp.txt >>storms.${model}.atcfunix.${regtype}.${ymdh}
    set -x
  fi

  let ist=ist+1

done


# Now add in the storms that are already being numbered and
# tracked by one of the operational forecasting centers.
# These storms have TC Vitals records issued for them and
# are identified in our track files by the "TCV" in their
# storm track records.

grep "TCV" ${trakfile} | grep "34, NEQ" | sort +0 -n >>storms.${model}.atcfunix.${regtype}.${ymdh}

