#!/bin/sh

set +x
#########################################################################
echo "------------------------------------------------------------------"
echo "hysplit_trajectory.sh - Script To Run Hysplit Trajectory and Plot."
echo "------------------------------------------------------------------"

# revised June 2006 - Add possible extra meteo
#      31 Mar 2008 - trajectory symbols depend on traj length 
#                    issued to created
#      06 Jul 2010 - multiprocessor version 
#                       move hymodelt to separate step of fcst job
#                       move post-processing to post job
#########################################################################
set -x

cd $DATA

##################################
# typenum=1 RSMC
# typenum=2 Volcano
# typenum=3 Homeland Security
# typenum=4 Back-tracking
##################################

case $typenum in
1|4) ntrj=3
   olvlt1=500.0
   olvlt2=1500.0
   olvlt3=3000.0
   echo "traj_hgts X ${olvlt1} ${olvlt2} ${olvlt3}" >> for_post.txt	# X is delimiter
   if [ "$SENDCOM" = "YES" ]
   then
     cp for_post.txt $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.for_post.txt
   fi
;;
2|3) msg='WARNING - hysplit_traj.sh not expected to be run for this runtype - trajectory heights not set - skip trajectories and continue'
     postmsg "$jlogfile" "$msg"
     exit
;;
esac

############################################################
# Set Up Control File for Trajectory Run
############################################################
echo "$ibyr $ibmo $ibda $ibhr" >CONTROL.traj  
echo "$ntrj                  ">>CONTROL.traj
echo "$olat $olon $olvlt1    ">>CONTROL.traj
echo "$olat $olon $olvlt2    ">>CONTROL.traj
echo "$olat $olon $olvlt3    ">>CONTROL.traj
echo "$nhrs                  ">>CONTROL.traj
echo "0                      ">>CONTROL.traj
echo "$zdata                 ">>CONTROL.traj
echo "$nfile                 ">>CONTROL.traj
                             # meteo files may appear in any order
                             # hysplit will choose appropriate files in order
                               # see MESSAGE file
                             # meteo files must exist
ifile=0
if [ ! "$met1" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/                 ">>CONTROL.traj	# file #1 directory
   echo "${met1}              ">>CONTROL.traj	# file #1 filename
fi
if [ ! "$met2" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #2 directory
   echo "${met2}           ">>CONTROL.traj	# file #2 filename
fi
if [ ! "$met3" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met3}           ">>CONTROL.traj	# file #3 filename
fi
if [ ! "$met4" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met4}           ">>CONTROL.traj	# file #3 filename
fi
if [ ! "$met5" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met5}           ">>CONTROL.traj	# file #3 filename
fi
if [ ! "$met6" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met6}           ">>CONTROL.traj	# file #3 filename
fi
if [ ! "$met7" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met7}           ">>CONTROL.traj	# file #3 filename
fi
if [ ! "$met8" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met8}           ">>CONTROL.traj	# file #3 filename
fi
if [ ! "$met9" = "undefined" ]
then
   let ifile=$ifile+1
   echo "$DATA/              ">>CONTROL.traj	# file #3 directory
   echo "${met9}           ">>CONTROL.traj	# file #3 filename
fi

if [ $ifile -ne $nfile ]
then
   msg='ERROR - Unexpected number of meteorology files written to CONTROL.traj - hysplit_hymodelt will fail'
   postmsg "$jlogfile" "$msg"
   export err=1; err_exit 
fi
echo "${DATA}/               ">>CONTROL.traj
echo "tdump                  ">>CONTROL.traj

###############################################################################
# Set Up Namelist parameters for each type of trajectory run
#   only those different from defaults must be given
# This is a placeholder.  Currently not used for trajectories.
###############################################################################
#rm -f SETUP.traj
#case $typenum in
#2) echo "&SETUP"             > SETUP.traj
#   echo "KMSL = 1"          >> SETUP.traj    # source ht, conc grid ht wrt msl
#   echo "/"                 >> SETUP.traj
#   ;;
#esac

exit
