#!/bin/sh

set -x
#####################################################################
echo "----------------------------------------------------------"
echo "exhysplit_post.sh - Script To Post Hysplit Model "
echo "----------------------------------------------------------"
echo "History: JUL 2003 - Original Script Submitted by Luke Lin"
echo "         JUN 2010 - additional variables in hysplit.ini"
echo "                     multi-processor version"
echo "      26 May 2011 - error checking; Internal NOAA request"
#####################################################################

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

cd $DATA

#############################################################
# Set Model Simulation Variables - Read In SDM Input
#############################################################
cp ${COMIN}/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 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;;	 	# added 10-10-08
      58) export input_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 ###
     export TYP=rsmc
  elif [ "$word1" = "volcanic" ]
  then
     export typenum=2					# Volcano ###
     export TYP=volc
     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 ###
     export TYP=hls 
  elif [ "$title" = "Test of the Back-tracking System" -o \
         "$title" = "Requested Services" ]
     then
     export typenum=4 					# Source Attribution
     export TYP=back
  else
       msg='ERROR - title does not match setup_hysplit - job terminated'
       postmsg "$jlogfile" "$msg"
       export err=2; err_exit			
  fi


#############################################################
# Run HYSPLIT post-processing
#############################################################
$USHhysplit/hysplit_post.sh		
export err=$?

mv ${COMIN}/hysplit.ini ${COMIN}/hysplit.${cycle}.${site}.${TYP}${seq}.hysplit.ini
########################################################
err_chk

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

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

