#!/bin/sh
set +x
#####################################################################
echo "----------------------------------------------------------"
echo "exhysplit.sh - Script To Setup Hysplit Model Run."
echo "----------------------------------------------------------"
echo "History:     1997 - Original Script Submitted by Roland Draxler"
echo "         FEB 1998 - First Implementation of this script   "
echo "                    by David Michaud"
echo "         JUN 1999 - Converted to run on the IBM-SP system"
#####################################################################
set -x

msg="HAS BEGUN"
postmsg "$jlogfile" "$msg"

cd $DATA

#############################################################
# Set Model Simulation Variables - Read In SDM Input
#############################################################
cp ${GESDIR}/hysplit.ini hysplit.ini

if [ -s hysplit.ini ]
then
  k=0
  cat hysplit.ini | while read tmp
  do
    let k=$k+1
    case $k in
      1)  export title=$tmp;;
      2)  export meteo=$tmp;;
      3)  export site=$tmp;;
      4)  export olat=$tmp;;
      5)  export olon=$tmp;;
      6)  export nfile=$tmp;;
      7)  export dir1=$tmp;;
      8)  export met1=$tmp;;
      9)  export dir2=$tmp;;
      10) export met2=$tmp;;
      11) export year=$tmp;;
      12) export month=$tmp;;
      13) export day=$tmp;;
      14) export hour=$tmp;;
      15) export run=$tmp;;
      16) export dur=$tmp;;
      17) export rate=$tmp;;
      18) export rbot=$tmp;;
      19) export rtop=$tmp;;
      20) export poll=$tmp;;
      21) export syr=$tmp;;
      22) export smo=$tmp;;
      23) export sda=$tmp;;
      24) export ctop=$tmp;;
    esac
 done
else
   msg='hysplit.ini file not found -- job terminated'
   postmsg "$jlogfile" "$msg"
   export err=1; err_exit
fi

#############################################################
# Define IPDY and Icyc as the NAM model cycle time less than
# or equal to the Incident time.
#############################################################
export IPDY=$year$month$day

Icyc=$hour
let Icyc=Icyc/6*6
export Icyc

#############################################################
# Run Trajectory and Concentration Models (HYSPLIT)
#############################################################
if test $meteo = "NAM" -o $meteo = "HIRESW"
then
   $USHhysplit/hysplit_trajconc_hls.sh
fi

#
# Send output from this special run to the SDM printer and phone-fax PCs
#
lpr -P${SDM_PRNTR} ${site}_t${cyc}z.ps
for remote_pc in 140.90.194.71 140.90.194.113
do
  ping -c3 $remote_pc
  pingerr=$?
  if [ $pingerr -eq 0 ] ; then
    ftp $remote_pc << ftpEOF
      put ${site}_t${cyc}z.ps hysplit_spec.${site}.${PDY}${cyc}.ps
      bye
ftpEOF
  else
    postmsg "$jlogfile" "SDMPC UNREACHABLE.  NO DATA SENT TO SDMPC"
  fi
done

########################################################

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

