#!/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"
echo "         JUN 2002 - Generalized hysplit                  "
echo "         DEC 2005 - Add NAM Alaska"
echo "         APR 2006 - Add AQM, later comment it out to hold 'slot'"
echo "         DEC 2006 - Add zoom, map centering"
echo "         MAY 2007 - met init time labels; per updated volcplot"
echo "         APR 2008 - Add back-tracking (source attribution) capability"
echo "                       replace gfsa with gdas"
echo "         MAY 2008 - load-leveler"
echo "         JUNE 2010 - multi-processor"
echo "      26 May 2011 - error checking; Internal NOAA Request"

# this script reads output from setup script
#             copies already-made ARL-format meteorology files
#             calls script to run HYSPLIT

#####################################################################
set -x

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

###########################
# typenum=1 RSMC
#         2 volcanic ash
#         3 hls/WFO
#         4 back-tracking
###########################
# meteonum=1 HRW
#          2 NAM
#          3 NAM-Alaska
#          4 GFS
#          5 GDAS
###########################

#############################################################
# Set Model Simulation Variables - Read In SDM Input
#############################################################
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 olvl1=$tmp;;
      7)  export olvl2=$tmp;;
      8)  export nhrs=$tmp;;
      9)  export mtype=$tmp;;
      10) export dir1=$tmp;;
      11) export meteo1=$tmp;;
      12) export dir2=$tmp;;
      13) export meteo2=$tmp;;
      14) export dir3=$tmp;;
      15) export meteo3=$tmp;;
      16) export ident=$tmp;;
      17) export qrat=$tmp;;
      18) export qhrs=$tmp;;
      19) export dsyr=$tmp;;
      20) export dsmo=$tmp;;
      21) export dsda=$tmp;;
      22) export dshr=$tmp;;
      23) export dsmn=$tmp;;
      24) export cnlat=$tmp;;
      25) export cnlon=$tmp;;
      26) export dlat=$tmp;;
      27) export dlon=$tmp;;
      28) export splat=$tmp;;
      29) export splon=$tmp;;
      30) export height=$tmp;;
      31) export csyr=$tmp;;
      32) export csmo=$tmp;;
      33) export csda=$tmp;;
      34) export cshr=$tmp;;
      35) export dhr=$tmp;;
      36) export delhr=$tmp;;
      37) export cnlat2=$tmp;;
      38) export cnlon2=$tmp;;
      39) export dlat2=$tmp;;
      40) export dlon2=$tmp;;
      41) export splat2=$tmp;;
      42) export splon2=$tmp;;
      43) export height2=$tmp;;
      44) export dhr2=$tmp;;
      45) export dryvl=$tmp;;
      46) export wetin=$tmp;;
      47) export wetlo=$tmp;;
      48) export rhalf=$tmp;;
      49) export icmt=$tmp;;
      50) export initd=$tmp;;
      51) export khmax=$tmp;;
      52) export numpar=$tmp;;
      53) export isot=$tmp;;
      54) export ndump=$tmp;;
      55) export kmsl=$tmp;;
      56) export seq=$tmp;;
      57) export qunits=$tmp;;	 	
      58) export org=$tmp;;
      59) export zoom1=$tmp;;
      60) export zoom2=$tmp;;
      61) export relcen=$tmp;;
      62) export ncycl=$tmp;;
      63) export pinpf=$tmp;;
      64) export olat2=$tmp;;
      65) export olon2=$tmp;;
      66) export olat3=$tmp;;
      67) export olon3=$tmp;;
      68) export dum4=$tmp;;
      69) export dum3=$tmp;;
      70) export dum2=$tmp;;
      71) export dum1=$tmp;;
    esac
 done
else
   msg='hysplit.ini file not found -- job terminated'
   postmsg "$jlogfile" "$msg"			
   export err=1; err_exit		
fi

#############################################################
# Set run type per setup_hysplit
#############################################################
# read words of $title to identify volcanic ash run
  echo $title | read word1 word2 word3 word4

  if [ "$title" = "Test of the RSMC Response System" -o \
       "$title" = "Requested Services" -o \
       "$title" = "Internal NOAA Request" -o \
       "$title" = "IAEA Notified Emergency" ]
  then
     export typenum=1                                   # RSMC ###
  elif [ "$word1" = "volcanic" ]
  then
     export typenum=2                                   # Volcano ###
     if [ "$word4" = "test" ]
     then
        export alert=0
        export atyp=-1
     elif [ "$word4" = "alert" ]
     then
        export alert=0
        export atyp=0
     elif [ "$word4" = "alert watch" ]
     then
        export alert=0
        export atyp=3
     elif [ "$word4" = "alert update" ]
     then
        export alert=0
        export atyp=1
     fi
  elif [ "$title" = "Test run of the on-demand HYSPLIT Model" -o \
         "$title" = "Real event run of the HYSPLIT Model" ]
     then
     export typenum=3                                   # Homeland Security ###
  elif [ "$title" = "Test of the Back-tracking System" -o \
         "$title" = "Requested Services" ]
     then
     export typenum=4                                   # back-tracking
  else
       msg='ERROR - title does not match setup_hysplit - job terminated'
       postmsg "$jlogfile" "$msg"
       export err=2; err_exit
  fi

##############################################################
# define TYP for filename
##############################################################
case $typenum in
1) export TYP=rsmc;;
2) export TYP=volc;;
3) export TYP=hls;;
4) export TYP=back;;
esac

#############################################################
# Run HYSPLIT fcst output script
#############################################################
$USHhysplit/hysplit_fcst_out.sh
export err=$?; err_chk

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

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

