#!/bin/sh

set +x
#########################################################################
echo "----------------------------------------------------------"
echo "hysplit_post.sh - Script To Run Hysplit Post-processing   "
echo "----------------------------------------------------------"

# NOTE for volcano - 12-h and alert-ended graphics are copied to
#      $COMOUT for possible WAFS dissemination
# 
# REVISED: 19 May 2005 - Add cdump2 for volcano hourly output
#             (cdump2 also exists for hls)
#          14 Jun 2005 - Change ARL email notification
#          28 Jun 2006 - Add 2nd grid (RSMC)
#          01 May 2007 - Google Earth
#          15 Feb 2008 - Source Attribution, hls output to ARL
#          13 Mar 2008 - change ARL email notification
#          14 Mar 2008 - add PDF outputs
#          28 Mar 2008 - change cdump1 to cdump 
#          31 Mar 2008 - createdate (cdt) filename, up to 12 time periods
#          02 Mar 2010 - sftp to arl
#          19 May 2010 - if SENDPRINT=YES, always print to the SDM color
#                        printer.  during COOP drills, also print to 
#                        /u/wx12ss/printer/sdmprint.
#########################################################################
set -x

cd $DATA

##################################
# typenum=1 RSMC
# typenum=2 Volcano
# typenum=3 Homeland Security
# typenum=4 Source Attribution 
##################################
case $typenum in
2) export EPDY=${dsyr}${dsmo}${dsda}
   export ECYC=${dshr}
   export VNAME=${site}
   ;;
esac

##############################################################
# define runtype label for filename and set directories
##############################################################
case $typenum in
1|2|4) 
   if [ "$USER" = "mgc0bs" ]
   then
      export COMIN=/ptmp/mgc0bs/hysplit			# for ARL
      export COMOUT=$COMIN				
   else
     export COMIN=/com/hysplit/${envir}/hysplit.${PDY}	
     export COMOUT=/com/hysplit/${envir}/hysplit.${PDY}
   fi

   case $typenum in 
   1) runtyp=rsmc;;
   2) runtyp=volc;;
   4) runtyp=back;;
   esac
   ;;
3) 
   #export COMIN=/com/hls/${envir}/hls.${PDY}
   #export COMOUT=/com/hysplit/${envir}/hls.${PDY}
   if [ "$USER" = "mgc0bs" ]
   then
      COMIN=/ptmp/mgc0bs/hysplit	# for ARL
      COMOUT=$COMIN	
      COMOUTHLS=$COMIN
   else
      export COMIN=/com/hysplit/${envir}/hysplit.${PDY}	
      export COMOUT=/com/hysplit/${envir}/hysplit.${PDY}
      export COMOUTHLS=/com/hysplit/${envir}/hls.${PDY}	
   fi
   mkdir -m 775 -p $COMOUTHLS
   runtyp=hls;;
esac

####################################################
#    setup email notification to ARL - moved below
####################################################
#if [ "$envir" = "prod" ]
#then
#  case $typenum in
#  1|3|4) ARLemail=arl.emerg@noaa.gov;;
#  2) ARLemail=barbara.stunder@noaa.gov;;
#  esac
#else
#  ARLemail=barbara.stunder@noaa.gov		
#fi
#if [ "$USER" = "mgc0bs" ]; then
## override above for testing
#  ARLemail=barbara.stunder@noaa.gov		
#fi

#################################################
#    Copy volcano graphics to working directory
#################################################
case $typenum in
2) for sfx in r0 r1 r2 r3
   do
     file1=${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot06.${sfx}	#6-hourly plots for sending to SDM,SAB,Alaska
     file2=${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot12.${sfx}	#12-hourly plots for sending to SDM,SAB,Alaska
     if [ -s $file1 ]
     then
        cp $file1 hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot06.${sfx}
     fi
     if [ -s $file2 ]
     then
        cp $file2 hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot12.${sfx}
        cp $file2 volcplot12.${sfx}
     fi
   done
   if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplotae.r0 ]
   then
      cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplotae.r0 hysplit.${cycle}.${site}.${runtyp}${seq}.volcplotae.r0 
      cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplotae.r0 volcplotae.r0 
   fi
   ;;
esac

#######################################################
# Copy control message and setup file to working directory
######################################################
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.conc ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.conc MESSAGE.conc
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.CONTROL.conc ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.CONTROL.conc CONTROL.conc
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.SETUP.conc ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.SETUP.conc SETUP.conc
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.DHOUR ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.DHOUR DHOUR
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.cdump ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.cdump cdump
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.cdump2 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.cdump2 cdump2
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MAPTEXT.conc ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MAPTEXT.conc MAPTEXT.conc
fi

if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.traj ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.traj MESSAGE.traj
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.CONTROL.traj ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.CONTROL.traj CONTROL.traj
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.SETUP.traj ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.SETUP.traj SETUP.traj
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.tdump ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.tdump tdump
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MAPTEXT.traj ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MAPTEXT.traj MAPTEXT.traj
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.US_DATE.TXT ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.US_DATE.TXT US_DATE.TXT
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.conc.kmz ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.conc.kmz conc.kmz
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.fine.kmz ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.fine.kmz fine.kmz
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.coarse.kmz ]
then
  cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.coarse.kmz coarse.kmz
fi 


####################################################
#    email notification to ARL 
####################################################
if [ "$envir" = "prod" ]
then
  case $typenum in
  1|3|4) ARLemail=arl.emerg@noaa.gov;;
  2) ARLemail=barbara.stunder@noaa.gov;;
  esac
else
  ARLemail=barbara.stunder@noaa.gov		
fi
if [ "$USER" = "mgc0bs" ]; then
# override above for testing
  ARLemail=barbara.stunder@noaa.gov		
fi

# make a temp file of CONTROL, SETUP.CFG, and hysplit.ini to send ARL
  echo "--------------- CONTROL.conc --------------------" >> mailfile
  cat CONTROL.conc >> mailfile
  echo "--------------- SETUP.conc   --------------------" >> mailfile
  cat SETUP.conc >> mailfile
  echo "--------------- hysplit.ini  --------------------" >> mailfile
  cat hysplit.ini >> mailfile
  mail -s "NCEP hysplit_post.sh" $ARLemail < mailfile

####################################################
#    make RSMC coversheet for fax and web (cover.ps)
####################################################
case $typenum in 
1|4)

 # make file RSMC.TXT for info to be written to RSMC fax coversheet
   head -2              MAPTEXT.conc 		   > RSMC.TXT
   grep "Response"      MAPTEXT.conc | cut -c1-60 >> RSMC.TXT
   tail -n1             MAPTEXT.conc 		  >> RSMC.TXT
   grep "Location"      MAPTEXT.conc 	 	  >> RSMC.TXT
   grep "Release Start" MAPTEXT.conc  		  >> RSMC.TXT
   grep "Meteorology"   MAPTEXT.conc  		  >> RSMC.TXT
   grep "Trajectories"  MAPTEXT.traj 	 	  >> RSMC.TXT
   grep "Duration"      MAPTEXT.conc  		  >> RSMC.TXT
   grep "Distribution"  MAPTEXT.conc  		  >> RSMC.TXT
   grep "Deposition"    MAPTEXT.conc  		  >> RSMC.TXT
   grep "Note"          MAPTEXT.conc  		  >> RSMC.TXT
   
 # make file COVER.TXT with date timestamp to be written to RSMC fax coversheet
   grep "Traj "         MAPTEXT.conc | cut -f2 -dj > COVER.TXT

 # make RSMC fax coversheet
   pgm=hysplit_coversheet
   export pgm;. prep_step
   startmsg
   case $typenum in
     1) $EXEChysplit/${pgm} >> $pgmout 2>errfile ;;
     4) $EXEChysplit/${pgm} BACK >> $pgmout 2>errfile ;;
   esac
   err=$?; export err; err_chk

;;
esac

####################################################################
# Merge All (trajectory and concentration) Graphics to Single File
#   hyout.ps is printed for SDM, ftp'd to SDM, etc.
####################################################################
case $typenum in

1|4) cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.trajplot.ps trajplot.ps
   cp cover.ps hyout.ps
   cat trajplot.ps >> hyout.ps
   cat ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot.ps >> hyout.ps
   cp  ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot.ps concplot.ps
   if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot.fine.ps ]
   then
      cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot.fine.ps \
             concplot.fine.ps
   fi
   ;;
    
2) for sfx in r0 r1 r2 r3
   do
     # 6-hourly plots for sending to SDM,SAB,Alaska
     file=${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot06.${sfx}	
     if [ -s $file ]
     then
        cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.volcplot06.${sfx} volcplot06.${sfx}
     fi
   done
   ;;

3) for sfx in fine coarse
   do
     rm -f temp.${sfx}.ps
   # copy file for loop
     cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot_loop.${sfx} \
                                                          concplot_loop.${sfx}
     cp concplot_loop.${sfx} concplot_loop_${sfx}.ps
   
   # copy files for individual plots
     let frame=1
     fnum=`printf %2.2d $frame`
     file=${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot00${fnum}.${sfx}
     while [ -s $file ]
     do
        cp $file concplot00${fnum}.${sfx}
        cat $file >> ${site}_t${cyc}z.ps
        cat $file >> ${site}_t${cyc}z_${sfx}.ps
        let frame=$frame+1
        fnum=`printf %2.2d $frame`
        file=${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot00${fnum}.${sfx}
     done
   done
   cp ${site}_t${cyc}z.ps hyout.ps

   ls ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.GIS* | while read tmp
   do
      cp $tmp .
   done
   ;;
#4) 
 # SDM doesn't need to print, but get these files
  #cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.trajplot.ps trajplot.ps
  #cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.concplot.ps concplot.ps
  #;;

esac

if [ $SENDFTP = "YES" ]
then
  ####################################################
  #    ftp postscript file to SDM-PC for review (for phone-fax?)
  ####################################################
  case $typenum in                # 140.90.194.57 as in exvfcst?
#  1) for remote_pc in 140.90.194.71 140.90.194.113
#  ^^^^ uncomment the line above and delete this one wx11rp -- 20070331
  1|4) for remote_pc in 140.90.194.71 # wx11rp -- 20070331 <- delete this line
       do
         ftp $remote_pc << ftpEOF
         put hyout.ps $PDY$cyc.$site.${runtyp}$seq.ps
         bye
ftpEOF
     done
     ;;
#  2) for remote_pc in 140.90.194.71 140.90.194.113
#  ^^^^ uncomment the line above and delete this one wx11rp -- 20070331
  2) for remote_pc in 140.90.194.71 # wx11rp -- 20070331 <- delete this line
     do
         ftp $remote_pc << ftpEOF
         put volcplot06.r0 $PDY$cyc.$site.${runtyp}$seq.volcplot06.r0.ps
         put volcplot06.r1 $PDY$cyc.$site.${runtyp}$seq.volcplot06.r1.ps
         put volcplot06.r2 $PDY$cyc.$site.${runtyp}$seq.volcplot06.r2.ps
         put volcplot06.r3 $PDY$cyc.$site.${runtyp}$seq.volcplot06.r3.ps
         put volcplot12.r0 $PDY$cyc.$site.${runtyp}$seq.volcplot12.r0.ps
         put volcplot12.r1 $PDY$cyc.$site.${runtyp}$seq.volcplot12.r1.ps
         put volcplot12.r2 $PDY$cyc.$site.${runtyp}$seq.volcplot12.r2.ps
         put volcplot12.r3 $PDY$cyc.$site.${runtyp}$seq.volcplot12.r3.ps
         put volcplotae.r0 $PDY$cyc.$site.${runtyp}$seq.volcplotae.r0.ps
         bye
ftpEOF
     done
     ;;
  esac
  ####################################################
  #    ftp postscript file to SAB (volcano)
  ####################################################
  if [ "${input_org}" != 'alaska' ]
 then
    case $typenum in
    2)
        ftp 140.90.195.198 << ftpEOF
            cd /data/ftp/pub/volcano/VAFTAD
            bin
            put volcplot06.r0 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r0.ps
            put volcplot06.r1 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r1.ps
            put volcplot06.r2 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r2.ps
            put volcplot06.r3 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r3.ps
            put volcplot12.r0 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r0.ps
            put volcplot12.r1 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r1.ps
            put volcplot12.r2 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r2.ps
            put volcplot12.r3 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r3.ps
            put volcplotae.r0 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplotae.r0.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r0.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r1.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r2.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot06.r3.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r0.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r1.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r2.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplot12.r3.ps
            site chmod 664 ${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.volcplotae.r0.ps
            bye
ftpEOF
  ;;
   esac
fi

fi

if [ $SENDPRINT = "YES" ]
then
if [ "${input_org}" != "arl" ]
then

  #######################################
  #    Print at sdm's desk 
  #    also print to sdm coop printer if needed
  #######################################
  case $typenum in
  1|4) lpr -P${SDM_PRNTR} hyout.ps
       if [ "${SDM_PRNTR2}" != "hplj4sdm" -o "${SDM_PRNTR2}" != "hp26_auto" ]
       then
          lpr -P${SDM_PRNTR2} hyout.ps
       fi
       ;;

  3) lpr -P${SDM_PRNTR} concplot_loop_fine.ps
     lpr -P${SDM_PRNTR} concplot_loop_coarse.ps
     if [ "${SDM_PRNTR2}" != "hplj4sdm" -o "${SDM_PRNTR2}" != "hp26_auto" ]
     then
        lpr -P${SDM_PRNTR2} concplot_loop_fine.ps
        lpr -P${SDM_PRNTR2} concplot_loop_coarse.ps
     fi
     ;;
  esac

fi
fi

####################################################
#    convert to GEMPAK
####################################################
if [ $typenum -eq 2 ]
then
   # ----------------------------------------------------------------------
   # Crop conc grid by excluding rows/columns of zeros.  Resulting grid
   # should be less than limits set by GEMPAK processing.
   #                            note input filename cdump
   #                            filename defined in hysplit_conc.sh
   # ----------------------------------------------------------------------
   if [ -s cdump2 ]
   then
      pgm=hysplit_concrop
      export pgm;. prep_step
      startmsg
      $EXEChysplit/${pgm} -icdump2 -occrop -f1 >> $pgmout 2>errfile_z
      err=$?; export err; err_chk
      cat FCSTHR.TXT

      # ----------------------------------------------------------------------
      # make grib file for volcano run 
      # ----------------------------------------------------------------------
      # 3 levels individually, then the layer-composite
      #   for the 1st, 2nd, and 3rd time periods of file (6, 12 and 18-h forecast)
      # ----------------------------------------------------------------------
      rm -f grib1
      pgm=hysplit_con2grib
      export pgm;. prep_step
      startmsg
      $EXEChysplit/${pgm} -i./ccrop -o./GCON -z1 -s0 -d1 -b0 -t16800 -n1:1 >> $pgmout 2>errfile_z
      err=$?; export err; err_chk

      $EXEChysplit/${pgm} -i./ccrop -o./grib1 -z1 -s0 -d2 -b0 -t16800 -n1:1 >> $pgmout 2>errfile_y
      err=$?; export err; err_chk
      cat grib1 >> GCON 
      rm -f grib1
     
      let time1=2
      cat DHOUR | read dhour
      while [ ${time1} -le ${dhour} ]; do
        for kavg in 1 2; do

         $EXEChysplit/${pgm} -i./ccrop -o./grib1 -z1 -s0 -d${kavg} -b0 -t16800 -n${time1}:${time1} >> $pgmout 2>errfile_x
         err=$?; export err; err_chk
         cat grib1 >> GCON 
         rm -f grib1

         done
         let time1=${time1}+1
      done
      rm -f grib1

      # --------------------------
      # make VISUAL_ASH_TABLE file
      # --------------------------
      pgm=hysplit_visthrsh
      export pgm;. prep_step
      startmsg
      $EXEChysplit/${pgm} -8${olvl1} -9${olvl2} -a0  >> $pgmout 2>errfile_w
      err=$?; export err; err_chk

      cp GCON ${COMOUT}/${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.grib
      cp VISUAL_ASH_TABLE ${COMOUT}/${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.visual_ash_table
 
      if [ "${input_org}" != "arl" ]
      then

        # --------------------------
        # GEMPAK
        # --------------------------
        export fend=00
        export finc=1
        export fstart=00
        export DBN_ALERT_TYPE=VAFTAD_GEMPAK
        export GRIB=none
        export model=hysplit

        . /nwprod/gempak/.gempak

        /nwprod/scripts/exnawips.sh.sms

        #
        # Create VG, PS, and META files from the GEMPAK grid
        #
        #/nwprod/ush/vaftad_nawips.sh
        #NOTE - hysplit_nawips.sh forces loop through forecast hours
        #    6,12,and 18 from eruption.  For update run, ie eruption was 
        #    12 hours ago, to get GEMPAK plots, need to change loop to 
        #    18, 24 and 30 hours 
        ${USHhysplit}/hysplit_nawips.sh
      fi	
   else
      msg="No hourly files for NAWIPS - hysplit_post.sh"
      postmsg "$jlogfile" "$msg"
   fi

fi

####################################################
#    make RSMC plots for web
#
#    concplot... -r2 ==> each time period total deposition, output all plots
#       RSMC plot for fax has -r3 ==> as -r2 but only output last deposition plot
#       note hardwire concplot parameters here
####################################################
case $typenum in 
1)

# copy map boundary file
  cp $FIXhysplit/hysplit_arlmap arlmap

# run plotting program (-r2 ==> each time period total deposition, output all plots)
  pgm=hysplit_concplot
  startmsg
  $EXEChysplit/${pgm} -icdump -pconc -e1 -r2 -uBq -z80 >> $pgmout 2>errfile
  err=$?; export err; err_chk
  if [ -s concplot.conc ]
  then
     cp concplot.conc concplot_web.ps
  fi

# concatenate postscript files for web - to be converted to gif
  cp trajplot.ps rsmc_togif.ps
  cat concplot_web.ps >> rsmc_togif.ps

# concatenate postscript files for web - to remain postscript
  cp cover.ps rsmc.ps
  cat trajplot.ps >> rsmc.ps
  cat concplot_web.ps >> rsmc.ps

# make pdf for web (one file)
  $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- rsmc.ps -c quit | \
   convert - PDF:rsmc.pdf

  ;;
esac

####################################################
#    make Source Attribution plots for web
####################################################
case $typenum in 
4)
# concatenate postscript files for web - to be converted to gif
  cp trajplot.ps back_togif.ps
  cat concplot.ps >> back_togif.ps

# concatenate postscript files for web - to remain postscript
  cp cover.ps rsmc.ps
  cat trajplot.ps >> rsmc.ps
  cat concplot.ps >> rsmc.ps

# make pdf for web (one file) 
  $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- rsmc.ps -c quit | \
   convert - PDF:rsmc.pdf
  ;;
esac

####################################################
#    convert to GIF (for hls, plots are individual frames)
####################################################
case $typenum in
1)
   ########################################
   # convert ps to gif (small, Yx300)
   ########################################
   $GSPATH/gs -r39.5 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- rsmc_togif.ps -c quit | \
   convert - GIF:nocrop.gif

 # split into frames temp00.gif temp01.gif ...
   convert nocrop.gif temp%02d.gif
 # crop frames
   let i=0
   ii=`printf %.2d $i`
   while [ -f temp${ii}.gif ]; do
      ########################################
      # crop white space - this is not required, but looks better in browser
      #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
      ########################################
      convert -crop 0x0 temp${ii}.gif rsmc_web${ii}.gif
      rm temp${ii}.gif
      let i=i+1
      ii=`printf %.2d $i`
      if [ -s rsmc_web00.gif ]; then
         mv rsmc_web00.gif STJUS.gif		# trajecjtory
      elif [ -s rsmc_web01.gif ]; then
         mv rsmc_web01.gif SICUS_01.gif		# integrated concentrtion 1
      elif [ -s rsmc_web02.gif ]; then
         mv rsmc_web02.gif STDUS_01.gif		# total deposition 1
      elif [ -s rsmc_web03.gif ]; then
         mv rsmc_web03.gif SICUS_02.gif
      elif [ -s rsmc_web04.gif ]; then
         mv rsmc_web04.gif STDUS_02.gif
      elif [ -s rsmc_web05.gif ]; then
         mv rsmc_web05.gif SICUS_03.gif
      elif [ -s rsmc_web06.gif ]; then
         mv rsmc_web06.gif STDUS_03.gif
      elif [ -s rsmc_web07.gif ]; then
         mv rsmc_web07.gif SICUS_04.gif
      elif [ -s rsmc_web08.gif ]; then
         mv rsmc_web08.gif STDUS_04.gif
      elif [ -s rsmc_web09.gif ]; then
         mv rsmc_web09.gif SICUS_05.gif
      elif [ -s rsmc_web10.gif ]; then
         mv rsmc_web10.gif STDUS_05.gif
      elif [ -s rsmc_web11.gif ]; then
         mv rsmc_web11.gif SICUS_06.gif
      elif [ -s rsmc_web12.gif ]; then
         mv rsmc_web12.gif STDUS_06.gif
      elif [ -s rsmc_web13.gif ]; then
         mv rsmc_web13.gif SICUS_07.gif
      elif [ -s rsmc_web14.gif ]; then
         mv rsmc_web14.gif STDUS_07.gif
      elif [ -s rsmc_web15.gif ]; then
         mv rsmc_web15.gif SICUS_08.gif
      elif [ -s rsmc_web16.gif ]; then
         mv rsmc_web16.gif STDUS_08.gif
      elif [ -s rsmc_web17.gif ]; then
         mv rsmc_web17.gif SICUS_09.gif
      elif [ -s rsmc_web18.gif ]; then
         mv rsmc_web18.gif STDUS_09.gif
      elif [ -s rsmc_web19.gif ]; then
         mv rsmc_web19.gif SICUS_10.gif
      elif [ -s rsmc_web20.gif ]; then
         mv rsmc_web20.gif STDUS_10.gif
      elif [ -s rsmc_web21.gif ]; then
         mv rsmc_web21.gif SICUS_11.gif
      elif [ -s rsmc_web22.gif ]; then
         mv rsmc_web22.gif STDUS_11.gif
      elif [ -s rsmc_web23.gif ]; then
         mv rsmc_web23.gif SICUS_12.gif
      elif [ -s rsmc_web24.gif ]; then
         mv rsmc_web24.gif STDUS_12.gif
      elif [ -s rsmc_web25.gif ]; then
         mv rsmc_web25.gif SICUS_13.gif
      elif [ -s rsmc_web26.gif ]; then
         mv rsmc_web26.gif STDUS_13.gif
      elif [ -s rsmc_web27.gif ]; then
         mv rsmc_web27.gif SICUS_14.gif
      elif [ -s rsmc_web28.gif ]; then
         mv rsmc_web28.gif STDUS_14.gif
      elif [ -s rsmc_web29.gif ]; then
         mv rsmc_web29.gif SICUS_15.gif
      elif [ -s rsmc_web30.gif ]; then
         mv rsmc_web30.gif STDUS_15.gif
      else
         msg="NOTICE - Did not account for this many output plots - rsmc_web??.gif"
         postmsg "$jlogfile" "$msg"
      fi
   done

   ########################################
   # convert ps to gif (large Zx800)
   ########################################
   # -r105 and 10-day run get error -  convert: Memory allocation failed (GIF:nocrop.gif)
   if [ -s SICUS_06.gif ]; then
    # make smaller file for long-range run so don't get memory error
      $GSPATH/gs -r85 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
      -sDEVICE=pnmraw -q -sOutputFile=- rsmc_togif.ps -c quit | \
      convert - GIF:nocrop.gif
   else
      $GSPATH/gs -r105 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
      -sDEVICE=pnmraw -q -sOutputFile=- rsmc_togif.ps -c quit | \
      convert - GIF:nocrop.gif
   fi

 # split into frames temp00.gif temp01.gif ...
   convert nocrop.gif temp%02d.gif
   rm -f temp.gif
   let i=0
   ii=`printf %.2d $i`
   while [ -f temp${ii}.gif ]; do
      ########################################
      # crop white space - this is not required, but looks better in browser
      #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
      ########################################
      convert -crop 0x0 temp${ii}.gif rsmc_web${ii}.gif
      rm temp${ii}.gif
      let i=i+1
      ii=`printf %.2d $i`
      if [ -s rsmc_web00.gif ]; then
         mv rsmc_web00.gif LTJUS.gif		# trajecjtory
      elif [ -s rsmc_web01.gif ]; then
         mv rsmc_web01.gif LICUS_01.gif		# integrated concentrtion 1
      elif [ -s rsmc_web02.gif ]; then
         mv rsmc_web02.gif LTDUS_01.gif		# total deposition 1
      elif [ -s rsmc_web03.gif ]; then
         mv rsmc_web03.gif LICUS_02.gif
      elif [ -s rsmc_web04.gif ]; then
         mv rsmc_web04.gif LTDUS_02.gif
      elif [ -s rsmc_web05.gif ]; then
         mv rsmc_web05.gif LICUS_03.gif
      elif [ -s rsmc_web06.gif ]; then
         mv rsmc_web06.gif LTDUS_03.gif
      elif [ -s rsmc_web07.gif ]; then
         mv rsmc_web07.gif LICUS_04.gif
      elif [ -s rsmc_web08.gif ]; then
         mv rsmc_web08.gif LTDUS_04.gif
      elif [ -s rsmc_web09.gif ]; then
         mv rsmc_web09.gif LICUS_05.gif
      elif [ -s rsmc_web10.gif ]; then
         mv rsmc_web10.gif LTDUS_05.gif
      elif [ -s rsmc_web11.gif ]; then
         mv rsmc_web11.gif LICUS_06.gif
      elif [ -s rsmc_web12.gif ]; then
         mv rsmc_web12.gif LTDUS_06.gif
      elif [ -s rsmc_web13.gif ]; then
         mv rsmc_web13.gif LICUS_07.gif
      elif [ -s rsmc_web14.gif ]; then
         mv rsmc_web14.gif LTDUS_07.gif
      elif [ -s rsmc_web15.gif ]; then
         mv rsmc_web15.gif LICUS_08.gif
      elif [ -s rsmc_web16.gif ]; then
         mv rsmc_web16.gif LTDUS_08.gif
      elif [ -s rsmc_web17.gif ]; then
         mv rsmc_web17.gif LICUS_09.gif
      elif [ -s rsmc_web18.gif ]; then
         mv rsmc_web18.gif LTDUS_09.gif
      elif [ -s rsmc_web19.gif ]; then
         mv rsmc_web19.gif LICUS_10.gif
      elif [ -s rsmc_web20.gif ]; then
         mv rsmc_web20.gif LTDUS_10.gif
      elif [ -s rsmc_web21.gif ]; then
         mv rsmc_web21.gif LICUS_11.gif
      elif [ -s rsmc_web22.gif ]; then
         mv rsmc_web22.gif LTDUS_11.gif
      elif [ -s rsmc_web23.gif ]; then
         mv rsmc_web23.gif LICUS_12.gif
      elif [ -s rsmc_web24.gif ]; then
         mv rsmc_web24.gif LTDUS_12.gif
      elif [ -s rsmc_web25.gif ]; then
         mv rsmc_web25.gif LICUS_13.gif
      elif [ -s rsmc_web26.gif ]; then
         mv rsmc_web26.gif LTDUS_13.gif
      elif [ -s rsmc_web27.gif ]; then
         mv rsmc_web27.gif LICUS_14.gif
      elif [ -s rsmc_web28.gif ]; then
         mv rsmc_web28.gif LTDUS_14.gif
      elif [ -s rsmc_web29.gif ]; then
         mv rsmc_web29.gif LICUS_15.gif
      elif [ -s rsmc_web30.gif ]; then
         mv rsmc_web30.gif LTDUS_15.gif
      fi
   done

   ########################################
   # convert ps to gif - cover sheet
   ########################################
   $GSPATH/gs -r72 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER  \
   -sDEVICE=pnmraw -q -sOutputFile=- cover.ps -c quit | \
   convert - GIF:cover.gif
  
   # no need to crop coversheet 
   mv cover.gif CVRUS.gif

   ;;
3) 
   for sfx in fine coarse
   do 

   ########################################
   # convert to pdf (all frames one file)
   ########################################
   $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- concplot_loop.${sfx} -c quit | \
   convert - PDF:${site}_t${cyc}z_${sfx}.pdf

  ########################################
  # rename kmz file for web (same naming convention as for pdf files)
  ########################################
  cp ${sfx}.kmz ${site}_t${cyc}z_${sfx}.kmz

  rm -f temp*.gif			

   ########################################
   # convert ps to gif (for loops)
   ########################################
   $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- concplot_loop.${sfx} -c quit | \
   convert - GIF:nocrop.gif

 # split into frames temp00.gif temp01.gif ...
   convert nocrop.gif temp%02d.gif
   
   if [ ! -f temp00.gif ]; then
    # assume above gs -r96 convert to gif failed
    # try smaller size, may be Memory error (e.g. release was yesterday, many frames)
      $GSPATH/gs -r48 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
      -sDEVICE=pnmraw -q -sOutputFile=- concplot_loop.${sfx} -c quit | \
      convert - GIF:nocrop.gif

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

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

       ########################################
       # Create Animated GIFs
       ########################################
       convert -adjoin -loop 100 -delay 100 \
          temp_loop*.gif ${site}_t${cyc}z_${sfx}_loop.gif

       ########################################
       # Process files for individual plots
       ########################################
     let frame=1
     fnum=`printf %2.2d $frame`
     while  [ -f concplot00${fnum}.$sfx ]
     do

        ########################################
        # convert ps to gif
        ########################################
        $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER  \
        -sDEVICE=pnmraw -q -sOutputFile=- concplot00${fnum}.$sfx -c quit | \
        convert - GIF:nocrop.gif

        ########################################
        # crop white space - this is not required, but looks better in browser
        #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
        ########################################
        convert -crop 0x0 nocrop.gif plot.gif

        mv plot.gif ${site}_t${cyc}z_${sfx}_${fnum}.gif

        let frame=$frame+1
        fnum=`printf %2.2d $frame`
     done
   done
   ;;
4)
   ########################################
   # convert ps to gif (small, Yx300)
   ########################################
   $GSPATH/gs -r39.5 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- back_togif.ps -c quit | \
   convert - GIF:nocrop.gif

 # split into frames temp00.gif temp01.gif ...
   convert nocrop.gif temp%02d.gif
 # crop frames
   let i=0
   ii=`printf %.2d $i`
   while [ -f temp${ii}.gif ]; do
      ########################################
      # crop white space - this is not required, but looks better in browser
      #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
      ########################################
      convert -crop 0x0 temp${ii}.gif back_web${ii}.gif
      rm temp${ii}.gif
      let i=i+1
      ii=`printf %.2d $i`
      if [ -s back_web00.gif ]; then
         mv back_web00.gif STJUS.gif		# trajecjtory
      elif [ -s back_web01.gif ]; then
         mv back_web01.gif SICUS_01.gif		# integrated concentrtion 1
      elif [ -s back_web02.gif ]; then
         mv back_web02.gif SICUS_02.gif
      elif [ -s back_web03.gif ]; then
         mv back_web03.gif SICUS_03.gif
      elif [ -s back_web04.gif ]; then
         mv back_web04.gif SICUS_04.gif
      elif [ -s back_web05.gif ]; then
         mv back_web05.gif SICUS_05.gif
      else
         msg="NOTICE - Did not account for this many output plots - back_web??.gif"
         postmsg "$jlogfile" "$msg"
      fi
   done

   ########################################
   # convert ps to gif (large Zx800)
   ########################################
   $GSPATH/gs -r105 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- back_togif.ps -c quit | \
   convert - GIF:nocrop.gif

 # split into frames temp00.gif temp01.gif ...
   convert nocrop.gif temp%02d.gif
   rm -f temp.gif
   let i=0
   ii=`printf %.2d $i`
   while [ -f temp${ii}.gif ]; do
      ########################################
      # crop white space - this is not required, but looks better in browser
      #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
      ########################################
      convert -crop 0x0 temp${ii}.gif back_web${ii}.gif
      rm temp${ii}.gif
      let i=i+1
      ii=`printf %.2d $i`
      if [ -s back_web00.gif ]; then
         mv back_web00.gif LTJUS.gif		# trajecjtory
      elif [ -s back_web01.gif ]; then
         mv back_web01.gif LICUS_01.gif		# integrated concentrtion 1
      elif [ -s back_web02.gif ]; then
         mv back_web02.gif LICUS_02.gif
      elif [ -s back_web03.gif ]; then
         mv back_web03.gif LICUS_03.gif
      elif [ -s back_web04.gif ]; then
         mv back_web04.gif LICUS_04.gif
      elif [ -s back_web05.gif ]; then
         mv back_web05.gif LICUS_05.gif
      fi
   done

   ########################################
   # convert ps to gif - cover sheet
   ########################################
   $GSPATH/gs -r72 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER  \
   -sDEVICE=pnmraw -q -sOutputFile=- cover.ps -c quit | \
   convert - GIF:cover.gif
  
   # no need to crop coversheet 
   mv cover.gif CVRUS.gif

   ;;
esac

####################################################
#    process GIS files
####################################################
                     #### kept same filenames since could impact web processing
case $typenum in 
3) for sfx in fine coarse
   do
     let frame=1
     fnum=`printf %2.2d $frame`
     while  [ -f concplot00${fnum}.${sfx} ]
     do
       /nwprod/util/exec/ascii2shp ${site}_t${cyc}z_${sfx}_${fnum} polygons \
          < hysplit.${cycle}.${site}.${runtyp}${seq}.GIS_*_${sfx}_${fnum}.txt
       cp hysplit.${cycle}.${site}.${runtyp}${seq}.GIS_*_${sfx}_${fnum}.txt ${site}_t${cyc}z_${sfx}_${fnum}.txt
       cp ${site}_t${cyc}z_${sfx}_${fnum}.txt ${COMOUTHLS}/${site}_t${cyc}z_${sfx}_${fnum}.txt
       let frame=$frame+1
       fnum=`printf %2.2d $frame`
     done
   done
   ;;
esac

####################################################
#    send to /com
####################################################
if [ $SENDCOM = 'YES' ]
then
  case $typenum in
  1) cp rsmc.ps   ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.rsmc.ps
     cp rsmc.pdf  ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.rsmc.pdf
     cp hyout.ps  ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.hyout.ps 
     cp CVRUS.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.CVRUS.gif
     cp STJUS.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.STJUS.gif
     cp LTJUS.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.LTJUS.gif
     ls SICUS_*.gif | while read tmp
     do
        cp ${tmp} ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${tmp}
     done
     ls LICUS_*.gif | while read tmp
     do
        cp ${tmp} ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${tmp}
     done
     ls STDUS_*.gif | while read tmp
     do
        cp ${tmp} ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${tmp}
     done
     ls LTDUS_*.gif | while read tmp
     do
        cp ${tmp} ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${tmp}
     done
    # the above is more general (if works, delete this)
    #for plotnum in 01 02 03 04 05 06 07 08 09 10 11 12
    #do
    #  if [ -s SICUS_${plotnum}.gif ]; then
    #   cp SICUS_${plotnum}.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.SICUS_${plotnum}.gif
    #  fi 
    #  if [ -s LICUS_${plotnum}.gif ]; then
    #   cp LICUS_${plotnum}.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.LICUS_${plotnum}.gif
    #  fi
    #  if [ -s STDUS_${plotnum}.gif ]; then
    #   cp STDUS_${plotnum}.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.STDUS_${plotnum}.gif
    #  fi
    #  if [ -s LTDUS_${plotnum}.gif ]; then
    #   cp LTDUS_${plotnum}.gif ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.LTDUS_${plotnum}.gif
    #  fi
    #done
     ;;
  #2) if [ ${ngrd} -eq 2 ]; then 
  2) if [ -s GCON ]; then 
        cp GCON ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.GCON
        cp VISUAL_ASH_TABLE \
          ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.VISUAL_ASH_TABLE
     fi
     ;;
  3)
     for sfx in fine coarse
     do 
       ########################################
       # copy full postscript file to $COMOUT
       ########################################
       cp ${site}_t${cyc}z_${sfx}.ps \
                ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${sfx}.ps
       cp ${site}_t${cyc}z_${sfx}.ps ${COMOUTHLS}/${site}_t${cyc}z_${sfx}.ps

       ########################################
       # copy full PDF file to $COMOUT
       ########################################
       cp ${site}_t${cyc}z_${sfx}.pdf \
                ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${sfx}.pdf
       cp ${site}_t${cyc}z_${sfx}.pdf ${COMOUTHLS}/${site}_t${cyc}z_${sfx}.pdf

       ########################################
       # copy full GOOGLE EARTH file to $COMOUT
       ########################################
       cp ${site}_t${cyc}z_${sfx}.kmz \
                ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${sfx}.kmz
       cp ${site}_t${cyc}z_${sfx}.kmz ${COMOUTHLS}/${site}_t${cyc}z_${sfx}.kmz

       ########################################
       # create GIS tar ball in $COMOUT (4-6-06 create here, copy to $COMOUT)
       ########################################
       tar -cvf hysplit.${cycle}.${site}.${runtyp}${seq}_${sfx}.gis.tar \
         ${site}_t${cyc}z_${sfx}_*.shp \
         ${site}_t${cyc}z_${sfx}_*.shx \
         ${site}_t${cyc}z_${sfx}_*.dbf \
         ${site}_t${cyc}z_${sfx}_*.txt
       cp hysplit.${cycle}.${site}.${runtyp}${seq}_${sfx}.gis.tar ${COMOUT}
       cp hysplit.${cycle}.${site}.${runtyp}${seq}_${sfx}.gis.tar \
                     ${COMOUTHLS}/${site}_t${cyc}z_${sfx}.gis.tar

#      ########################################
#      # Copy Animated GIFs
#      ########################################
       cp ${site}_t${cyc}z_${sfx}_loop.gif \
             ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${sfx}_loop.gif
       cp ${site}_t${cyc}z_${sfx}_loop.gif \
             ${COMOUTHLS}/${site}_${cycle}_${sfx}_loop.gif

       ########################################
       # copy frame-by-frame
       ########################################
       let frame=1
       fnum=`printf %2.2d $frame`
       while  [ -f concplot00${fnum}.$sfx ]
       do
          ####################
          # gif file
          ####################
          cp ${site}_t${cyc}z_${sfx}_${fnum}.gif \
           ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${sfx}_${fnum}.gif
          cp ${site}_t${cyc}z_${sfx}_${fnum}.gif \
           ${COMOUTHLS}/${site}_t${cyc}z_${sfx}_${fnum}.gif 

          let frame=$frame+1
          fnum=`printf %2.2d $frame`
       done
     done
     ;;

  4) 
   # use same filenames as RSMC so can go on web in same place as regular RSMC run
     cp rsmc.ps   ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.rsmc.ps
     cp rsmc.pdf  ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.rsmc.pdf
     ls SICUS_*.gif | while read tmp
     do
        cp ${tmp} ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${tmp}
     done
     ls LICUS_*.gif | while read tmp
     do
        cp ${tmp} ${COMOUT}/hysplit.${cycle}.${site}.${runtyp}${seq}.${tmp}
     done
     ;;

  esac
fi

if [ $SENDWEB = 'YES' ]
then

  ###########################################################################
  #    send to NCEP - GIF images to dedicated web server with rsync via ssh.
  ###########################################################################

  case $typenum in
  1) 
     ssh rzdm -l nwprod "mkdir ~/hysplit/www/htdocs/sdm/${PDY}"
     /usrx/local/bin/rsync -v -e ssh hyout.ps nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.ps
     ;;

  2)
     ssh rzdm -l nwprod "mkdir ~/hysplit/www/htdocs/sdm/${PDY}"
     /usrx/local/bin/rsync -v -e ssh  volcplot06.r0 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot06.r0.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot06.r1 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot06.r1.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot06.r2 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot06.r2.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot06.r3 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot06.r3.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot12.r0 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot12.r0.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot12.r1 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot12.r1.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot12.r2 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot12.r2.ps
     /usrx/local/bin/rsync -v -e ssh  volcplot12.r3 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplot12.r3.ps
     /usrx/local/bin/rsync -v -e ssh  volcplotae.r0 nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.volcplotae.r0.ps
    ;;


 
  #3) /usrx/local/bin/ssh 172.16.19.5 -l nwprod "mkdir ~/hysplit/htdocs/${PDY}"
  3)
     if [ "$run_type" = "on_demand" ]
     then
        export org=${org:-sdm}
        #export PDY=`/nw${envir}/ush/hiresw_get_hls_nest.sh | awk '{print $2}'`
        #export cyc=`/nw${envir}/ush/hiresw_get_hls_nest.sh | awk '{print $3}'`
        export run_id=`/nw${envir}/ush/hiresw_get_hls_nest.sh | awk '{print $4}'`
        export MODEL=`/nw${envir}/ush/hiresw_get_hls_nest.sh | awk '{print $5}'`
        export COMOUT=${COMOUTHLS}
        export RUN=hls
     else
        run_id="*"
     fi

     ##############################################################
     # Make html index files
     ##############################################################
     /nw${envir}/ush/hysplit_web_index_hls.sh $run_type

     ssh rzdm -l nwprod "mkdir ~/hysplit/www/htdocs/hysplit.${PDY}"
     ssh rzdm -l nwprod "cd ~/hysplit/www/htdocs/;ln -sf hysplit.${PDY} ${PDY}"

    /usrx/local/bin/rsync -v -e ssh $COMOUT/${run_id}_t${cyc}z_*pdf nwprod@rzdm:~/hysplit/www/htdocs/hysplit.${PDY}/
    /usrx/local/bin/rsync -v -e ssh $COMOUT/${run_id}_t${cyc}z_*kmz nwprod@rzdm:~/hysplit/www/htdocs/hysplit.${PDY}/
     /usrx/local/bin/rsync -v -e ssh $COMOUT/${run_id}_t${cyc}z_*gif nwprod@rzdm:~/hysplit/www/htdocs/hysplit.${PDY}/
     /usrx/local/bin/rsync -v -e ssh $COMOUT/${run_id}_t${cyc}z_*tar nwprod@rzdm:~/hysplit/www/htdocs/hysplit.${PDY}/
     /usrx/local/bin/rsync -v -e ssh $COMOUT/${run_id}_t${cyc}z_*ps  nwprod@rzdm:~/hysplit/www/htdocs/hysplit.${PDY}/
     /usrx/local/bin/rsync -v -e ssh $COMOUT/${run_id}_t${cyc}z*.html nwprod@rzdm:~/hysplit/www/htdocs/hysplit.${PDY}/
     /usrx/local/bin/rsync -v -e ssh $COMOUT/index.html nwprod@rzdm:~/hysplit/www/htdocs/
     /usrx/local/bin/rsync -v -e ssh $COMOUT/*_site.html nwprod@rzdm:~/hysplit/www/htdocs/

   # Remove data more than 180 days old
     PDYm180=`$utilexec/ndate -4320 ${PDY}00 | cut -c1-8`

     ssh rzdm -l nwprod "~/rm_datadir.sh ${PDYm180}"
     ssh rzdm -l nwprod "~/rm_datadir.sh hysplit.${PDYm180}"
     #ssh rzdm -l nwprod "~/rm_datadir_para.sh ${PDYm180}"
     #ssh rzdm -l nwprod "~/rm_datadir_para.sh hysplit.${PDYm180}"

     ;;
  4) 
     ssh rzdm -l nwprod "mkdir ~/hysplit/www/htdocs/sdm/${PDY}"
     /usrx/local/bin/rsync -v -e ssh hyout.ps nwprod@rzdm:~/hysplit/www/htdocs/sdm/${PDY}/$PDY$cyc.$site.${runtyp}$seq.ps
     ;;
  esac
fi

if [ "${input_org}" != "arl" ]
then

  ####################################################
  #    ftp to Alaska - volcano - 
  #           move to near end since sometimes this takes a long time
  ####################################################

  case $typenum in
  2) 
    if [ $SENDALASKA = 'YES' ]
    then
      msg="Starting ftp of volcanic ash files to Alaska"
      postmsg "$jlogfile" "$msg"

      # --------------------------
      # ftp postscript files
      # --------------------------
      ftp 140.90.246.3 << ftpEOF
        cd /incoming/vaftad
        put volcplot06.r0 $site.E${EPDY}${ECYC}.R${PDY}${cyc}.r0.ps
        put volcplot06.r1 $site.E${EPDY}${ECYC}.R${PDY}${cyc}.r1.ps
        put volcplot06.r2 $site.E${EPDY}${ECYC}.R${PDY}${cyc}.r2.ps
        put volcplot06.r3 $site.E${EPDY}${ECYC}.R${PDY}${cyc}.r3.ps
        bye
ftpEOF

    fi
  ;;
  esac

fi

if [ $SENDARL = 'YES' ]
then
  ####################################################
  #    send to ARL (was 140.90.81.226 gus user 'sdm')
  ####################################################
#  (sftp by $USERID to $WEB:$DIR/$FILE)
  ARL=205.156.4.185			# tulip.arl.noaa.gov
  ARLdir=/home/prod/ncep_hysplit
  USERID=ncepdev
  PORT=57103

# note: $site includes characters " . -  _ , ' "
#   so need something else as delimeter (ie, @ # + :) for processing at ARL

# created date-time
  cdt=`tail -1 US_DATE.TXT`

# files common to all run types, except hysplit.ini, which is sent last
  sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
  cd ${ARLdir}
  put cdump         ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+cdump
  put MESSAGE.conc  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MESSAGE.conc
  put CONTROL.conc  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+CONTROL.conc
  put SETUP.conc    ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+SETUP.conc
  quit
arlEOF

  case $typenum in
  1|4) 
         sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
         cd ${ARLdir}
         put hyout.ps      ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+ps	
         put MAPTEXT.conc  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MAPTEXT.conc
         put tdump         ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+tdump
         put MESSAGE.traj  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MESSAGE.traj
         put CONTROL.traj  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+CONTROL.traj
         put MAPTEXT.traj  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MAPTEXT.traj
         put conc.kmz      ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+conc.kmz

         put US_DATE.TXT
         put CVRUS.gif  
         put LTJUS.gif  
         put STJUS.gif  
         put rsmc.ps       
         put rsmc.pdf
         put tdump traj.txt
         quit
arlEOF

       # variable number of output files - integrated concentrations (IC)
       #   use LICUS as base.  List LICUS_XX.gif files.  
       #   loop through XX and ftp the set of files.
         ls LICUS_*.gif | while read tmp
         do
           num=`echo ${tmp} | cut -c7-8`
           sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
           cd ${ARLdir}
           put LICUS_${num}.gif
           put SICUS_${num}.gif
           quit
arlEOF
         done

         case $typenum in
         1)

       # variable number of output files - deposition (D) (not for back-tracking)
       #   use LICUS as base.  List LICUS_XX.gif files.  
       #   loop through XX and ftp the set of files.
         ls LTDUS_*.gif | while read tmp
         do
           num=`echo ${tmp} | cut -c7-8`
           sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
           cd ${ARLdir}
           put LTDUS_${num}.gif
           put STDUS_${num}.gif
           quit
arlEOF
         done
         esac

       # in case cdump2 was turned off do this separately
         if [ -s cdump2 ]; then
           sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
           cd ${ARLdir}
           put cdump2           ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+cdump2
           put concplot.fine.ps ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+concplot.fine.ps
           put fine.kmz         ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+fine.kmz
           quit
arlEOF
         fi

         if [ -s cdump.txt ]; then
           sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
           cd ${ARLdir}
           put cdump.txt
           quit
arlEOF
         fi
     ;;
  2) 
         sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
         cd ${ARLdir}
         put volcplot06.r0 ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+volcplot06.r0
         put cdump2        ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+cdump2
         quit
arlEOF
     ;;
  3) 
         sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
         cd ${ARLdir}
         put cdump2        ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+cdump2
         put MAPTEXT.conc  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MAPTEXT.conc
         put concplot_loop_fine.ps \
                            ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+concplot_loop_fine+ps
         put concplot_loop_coarse.ps \
                            ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+concplot_loop_coarse+ps
         quit
arlEOF
     ;;

# this was 4 until made 1|4 above.  If 1|4 is ok, delete this
  *) 
         sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
         cd ${ARLdir}
         put MAPTEXT.conc  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MAPTEXT.conc
         put tdump         ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+tdump
         put MESSAGE.traj  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MESSAGE.traj
         put CONTROL.traj  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+CONTROL.traj
         put MAPTEXT.traj  ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MAPTEXT.traj

         put US_DATE.TXT
         put LTJUS.gif  
         put STJUS.gif  
         put tdump traj.txt
         put rsmc.ps       
         put rsmc.pdf
         quit
arlEOF
       # variable number of output files
       #   use LICUS as base.  List LICUS_XX.gif files.  
       #   loop through XX and ftp the set of files.
         ls LICUS_*.gif | while read tmp
         do
           num=`echo ${tmp} | cut -c7-8`
           sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
           cd ${ARLdir}
           put LICUS_${num}.gif
           put LTDUS_${num}.gif
           put SICUS_${num}.gif
           put STDUS_${num}.gif
           quit
arlEOF
         done

         if [ -s cdump.txt ]; then
           sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
           cd ${ARLdir}
           put cdump.txt
           quit
arlEOF
         fi
     ;;
  esac 

# hysplit.ini is last file for ARL processing script
  sftp -o Port=${PORT} ${USERID}@${ARL} << arlEOF
  cd ${ARLdir}
  put hysplit.ini   ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+hysplit.ini
  quit
arlEOF

fi