#!/bin/sh

###############################################################################
set +x
echo "------------------------------------------------------------------------"
echo "exhysplit_wafs.sh.sms - Hysplit Volcano Dissemination After QA"
echo "   "
echo " Rev. 24 Mar 2008 - Back-tracking"
echo "      02 Mar 2010 - sftp to ARL"
echo "      19 May 2010 - During COOP drills, print to both default color"
echo "                    printer AND COOP printer."
echo "      04 Jun 2010 - new volcplot file name from /com"
echo "                    additional variables in hysplit.ini"
echo "------------------------------------------------------------------------"

# INPUT FILES - $GESDIR/wafs.ini 
#               $GESDIR/hysplit.ini 
###############################################################################
set -x

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 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;;	 	# added 01-16-04
      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

export EPDY=${dsyr}${dsmo}${dsda}
export ECYC=${dshr}
export VNAME=${site}

#############################################################
# 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" = "IAEA Notified Emergency" ]; then
     export typenum=1                                   # RSMC ###
  elif [ "$word1" = "volcanic" ]
  then
     export typenum=2                                   # Volcano ###
  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                                   # Source Attribution
  else
       msg='ERROR - title does not match setup_hysplit - job terminated'
       postmsg "$jlogfile" "$msg"
       export err=1; err_exit
  fi


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

##############################################################
# define TYP for filename
##############################################################
case $typenum in
1) exit 0;;
2) runtyp=volc;;
3) exit 0;;
4) exit 0;;
esac

####################################################
#  Read SDM Input
####################################################
cp ${GESDIR}/wafs.ini ./
if [ -s wafs.ini ];
then
  #cat wafs.ini | read red

#---
  k=0
  cat wafs.ini | while read tmp
  do
    let k=$k+1
    case $k in
      1)  red=$tmp;;
      2)  FILE=$tmp;;
    esac
  done
#---

else
   msg='wafs.ini file not found -- job terminated'
   postmsg "$jlogfile" "$msg"
   export err=1; err_exit
fi

######################################################
#  Set volcplot filename values
#      (hysplit_volcplot filenames all end with ".rN"
######################################################
#tint=12			# time interval
#if [ $red -eq 4 ]	# alert ended
#then
#  tint=ae
#  red=0
#fi
 
########################################################################
# Get appropriate graphic for dissemination       
#	12-hourly, given reduction, or alert-ended
########################################################################
#cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot${tint}_r${red}.ps volcplot.ps
#cp ${FILE}_r${red}.ps volcplot.ps
cp ${FILE} volcplot.ps

##############################################################
#    send to SDM (print, fax)   
#    also print to sdm coop printer if needed
##############################################################
 if [ "${input_org}" != 'arl' ]
 then
    lpr -P${SDM_PRNTR} volcplot.ps

    if [ "${SDM_PRNTR2}" != "hplj4sdm" -o "${SDM_PRNTR2}" != "hp26_auto" ] 
     then
     lpr -P${SDM_PRNTR2} volcplot.ps
    fi
 fi

##############################################################
#    convert to GIF
##############################################################
  gif1=${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.part1.gif
  gif2=${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.part2.gif


# from hysplit_post.sh
   ########################################
   # convert ps to gif (for loops)
   ########################################
   gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- volcplot.ps -c quit | \
   convert - GIF:nocrop.gif

 # split into frames temp00.gif temp01.gif ...
   convert nocrop.gif temp%1d.gif

   ########################################
   # crop white space - this is not required, but looks better in browser
   #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
   ########################################
   let ii=0
   while [ -f temp${ii}.gif ]; do
      convert -crop 0x0 temp${ii}.gif volcplot.gif.${ii}
      rm temp${ii}.gif
      let ii=${ii}+1
   done
# end from hysplit_post.sh

  if [ -s volcplot.gif.0 ] 
  then
     mv volcplot.gif.0 $gif1
  fi
  if [ -s volcplot.gif.1 ] 
  then
     mv volcplot.gif.1 $gif2
  fi

####################################################
#  Create volcanic ash images in g3 format for OOS 
####################################################

g31=${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.part1.g3
g32=${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.part2.g3

###################################
#  Convert postscript to g3
#     output: volcplot1.g3 (page 1, 12-h and 24-h fcst)
#             volcplot2.g3 (page 2, 36-h and 48-h fcst)
###################################
gs -sDEVICE=faxg3 -dNOPAUSE -dBATCH -sOutputFile=volcplot%d.g3 volcplot.ps

###################################
#  Create the header needed for OOS
###################################
da=`date -u +%d`				
hr=`date -u +%H`			
echo ${da}${hr} > DAYHR
wc -m volcplot1.g3 | cut -c1-8 >> DAYHR		
wc -m volcplot2.g3 | cut -c1-8 >> DAYHR


if test "$SENDCOM" = 'YES'
then
   cp $gif1  $pcom/$gif1
   cp $gif2  $pcom/$gif2

   SUB=DFAX1064
   $USHutil/make_ntc_file.pl PHBE10 KWBC ${PDY}${cyc} $SUB volcplot1.g3 $pcom/$g31
   $USHutil/make_ntc_file.pl PHBI10 KWBC ${PDY}${cyc} $SUB volcplot2.g3 $pcom/$g32


   if test "$SENDDBN" = 'YES'
   then
     $DBNROOT/bin/dbn_alert MODEL VAFTAD_GIF $job $pcom/$gif1
     $DBNROOT/bin/dbn_alert MODEL VAFTAD_GIF $job $pcom/$gif2
     $DBNROOT/bin/dbn_alert GRIB_LOW  HYSPLIT $job $pcom/$g31           
     $DBNROOT/bin/dbn_alert GRIB_LOW  HYSPLIT $job $pcom/$g32           
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
fi


##############################################################
#    send to ARL
##############################################################
if [ $SENDARL = 'YES' ]
then
 #ARL=205.156.4.185                     # tulip.arl.noaa.gov 
#  ARL=205.156.4.190                     # alert.arl.noaa.gov (new)
  ARL=140.90.78.254                     # alert.arl.noaa.gov (NCWCP)
  ARLdir=/home/prod/ncep_hysplit
  USERID=ncepdev
  PORT=57103
  ARLemail=barbara.stunder@noaa.gov		

  echo "This email is from the NCEP HYSPLIT WAFS run for $site" | \
              mail -s "NCEP exhysplit_wafs.sh.sms" $ARLemail       

  # note: $site includes characters " . -  _ , ' "
  #   so need something else as delimeter (ie, @ # + :) for processing at ARL
  
  sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
  cd ${ARLdir}
  put $gif1  ${site}@gif1
  put $gif2  ${site}@gif2
  quit
arlEOF

fi
