#!/bin/sh
# set -nv

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.
#          04 Jun 2010 - new hysplit_volcplot output filename on command line -ovolcplot_rN -pps
#                         this newly named file written to $COMOUT
#                         legacy name kept for subsequent post-processing
#                           moved section from hysplit_conc.sh and hysplit_traj.sh 
#                             after run hymodelc/hymodelt to here to
#                             have all post-processing in one file
#                           ngrd defined based on cdump/cdump2 existence
#                           effective area souce (plot symbol)
#          23 Mar 2011 - force -m1 for hysplit_concplot for RSMC (ARL)
#          16 May 2011 - error checking
#             May 2011 - Internal NOAA Request; 
#                        RSMC sent to ARL web with post_rsmc; send to ARL for runtypes volcano and hls
#                        take out automatic email to ARL
#########################################################################
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 [ $PARAFLAG = "YES" ];then   
#ARL  export COMIN=/ptmp/$USER/hysplit.${PDY}	
#EMC Input dir
      export COMIN=${usrdir}/com/hysplit/${envir}/hysplit.${PDY}	
      export COMOUT=$COMIN
   else
     export COMIN=${usrdir}/com/hysplit/${envir}/hysplit.${PDY}	
     export COMOUT=${usrdir}/com/hysplit/${envir}/hysplit.${PDY}
   fi    

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

####################################################################
# Copy files to working directory (assume fcst run was with 4 tasks
####################################################################

#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}.MESSAGE.001 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.001 MESSAGE.001
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.002 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.002 MESSAGE.002
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.003 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.003 MESSAGE.003
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.004 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.004 MESSAGE.004
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.005 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.005 MESSAGE.005
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.006 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.006 MESSAGE.006
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.007 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.007 MESSAGE.007
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.008 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MESSAGE.008 MESSAGE.008
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
   let ngrd=1
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.cdump2 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.cdump2 cdump2
   let ngrd=2
fi

#if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP ]
#then
#   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP PARDUMP
#fi

if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.001 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.001 PARDUMP.001
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.002 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.002 PARDUMP.002
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.003 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.003 PARDUMP.003
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.004 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.004 PARDUMP.004
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.005 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.005 PARDUMP.005
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.006 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.006 PARDUMP.006
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.007 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.007 PARDUMP.007
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.008 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARDUMP.008 PARDUMP.008
fi

#if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MAPTEXT.conc ]
#then
#   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.MAPTEXT.conc MAPTEXT.conc
#fi

# parinit.001 and parshift.001 are used by volcplot for labeling, only parshift, parinit not copied to $COMOUT

#if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARINIT.001 ]
#then
#   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARINIT.001 PARINIT.001
#fi

if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARSHIFT.001 ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.PARSHIFT.001 PARSHIFT.001
fi
if [ -s ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.GFS.ftpdone ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.GFS.ftpdone GFS.ftpdone
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}.for_post.txt ]
then
   cp ${COMIN}/hysplit.${cycle}.${site}.${runtyp}${seq}.for_post.txt for_post.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 

#####################################################
# read info saved by fcst job in file for_post.txt and US_DATE.TXT
#####################################################
# read meteorology DATlabel
  DATlabel=`grep DATlabel for_post.txt | cut -f2 -dX`
# read traj start heights
  traj_hgts=`grep traj_hgts for_post.txt | cut -f2 -dX`
# read day month
  iday=`grep create_day_month for_post.txt | cut -f2 -dX`
  imonth=`grep create_day_month for_post.txt | cut -f3 -dX`
# read created DATE label
  cdt=`tail -1 US_DATE.TXT`
  iyr=`echo ${cdt} | cut -c1-4`
  imo=`echo ${cdt} | cut -c5-6`
  ida=`echo ${cdt} | cut -c7-8`
  ihr=`echo ${cdt} | cut -c9-10`
  imn=`echo ${cdt} | cut -c11-12`
  iyr=`echo ${cdt} | cut -c1-4`

######################################################################################
######################################################################################
## this is from hysplit_traj.sh single-processor version after run hysplit_hymodelt
## (copied to hysplit_post.sh and slightly modified)

#############################################################################
# Create File to Label Graphics Maps - RSMC and back traj 
#############################################################################
rm -f hyout.ps
case $typenum in 
1|4) rm -f MAPTEXT.traj 

   echo $title | read tmp tmp1
   case $typenum in
   1) case $tmp in
        Test)      label="EXERCISE     EXERCISE      EXERCISE";;
        Requested) label="REQUESTED SERVICES";; 
        IAEA)      label="IAEA NOTIFIED EMERGENCY";;
        Internal)  label="Internal NOAA Request";;
        Special)   label=$title;;
        New)       label="EXERCISE     EXERCISE      NEW VERSION";;
      esac
      ;;
   4) case $tmp in
        Test)      label="EXERCISE       EXERCISE        EXERCISE";;
        Requested) label="REQUESTED SERVICES";;
        New)       label="EXERCISE       EXERCISE        NEW VERSION";;
      esac
      ;;
   esac

   if [ $nhrs -gt 0 ]
   then
      direction="forward"
      duration=$nhrs
   else
      direction="backward"
      duration=`echo "${nhrs}*-1" | bc` 
   fi

   total=`echo "${qrat}*${qhrs}" | bc` 
   export total=`printf %3.2f $total`		# needed in hysplit_conc

   rm -f MAPTEXT.traj 
   echo "Created: $ihr${imn}UTC $ida/$imo/$iyr (day/month/year) RSMC Washington - NOAA ARL / NCEP" >>MAPTEXT.traj
   echo "-----------------------------------------------------------" >>MAPTEXT.traj
  #echo "Source: ${site}  lat:${olat}  lon:${olon}  height:${olvlt1} ${olvlt2} ${olvlt3} m AGL" >>MAPTEXT.traj
   echo "Source: ${site}  lat:${olat}  lon:${olon}  height:${traj_hgts} m AGL" >>MAPTEXT.traj
   echo "-----------------------------------------------------------" >>MAPTEXT.traj
   echo "Trajectory    Direction:$direction    Duration:${duration} hr" >> MAPTEXT.traj
   echo "Trajectories: ${traj_hgts} m AGL" >>MAPTEXT.traj
   echo "Release Start (YYYY MM DD HH MM):${dsyr} ${dsmo} ${dsda} ${dshr} ${dsmn}    " >>MAPTEXT.traj
   echo "Meteorology:  $DATlabel                                    " >>MAPTEXT.traj
   echo "Vertical Motion Calculation Method:   Model Vertical Velocity">>MAPTEXT.traj
   echo "Release ID:${ident}   Rate:${qrat}/hr  Duration:${qhrs} hr" >>MAPTEXT.traj
   echo "Distribution: Uniform between 0 - $olvl2 m agl             " >>MAPTEXT.traj
   echo "Deposition:   Wet and Dry ($dryvl m/s)             " >>MAPTEXT.traj
   echo "Notes:        Contour values may change from chart to chart" >>MAPTEXT.traj
   case $typenum in
   1) if [ ${icmt} -eq 1 ]; then
        echo "              RESULTS BASED ON DEFAULT INITIAL VALUES" >>MAPTEXT.traj
      else
        echo "  " >> MAPTEXT.traj
      fi
      ;;
   4) echo "  " >> MAPTEXT.traj;;
   esac
   echo "Response:     $label                                       " >>MAPTEXT.traj
   echo "-----------------------------------------------------------" >>MAPTEXT.traj

   if [ ! -f tdump ]
   then
      msg='WARNING hysplit_hymodelt output file tdump not created; continue anyway'
     #errexit "$msg"

   else

   ####################
   # Output graphics
   ####################
      cp $FIXhysplit/hysplit_arlmap arlmap		
      echo "`wc -c tdump`" | read fsize tmp

      if [ $fsize -gt 512 ]
      then
      # options
      #  -f = frames (1 = 1 frame/file)
      #  -i = input file
      #  -j = map background file (arlmap)
      #  -k = color (1) or B&W (0)
      #  -l = label interval (hours)
      #  -m = map projection
      #  -o = output filename (traplot.ps)
      #  -v = vertical (1=agl)
      #  -z = zoom (0-100)
        pgm=hysplit_trajplot
        export pgm
        . prep_step
        startmsg
        if [ ${nhrs} -gt 144 ]
        then
          $EXEChysplit/hysplit_trajplot -i./tdump -f1 -k1 -l24 -ptraj -v1 -z80 >> $pgmout 2>errfile
        elif [ ${nhrs} -gt 84 ]; then
           $EXEChysplit/hysplit_trajplot -i./tdump -f1 -k1 -l12 -ptraj -v1 -z80 >> $pgmout 2>errfile
        else
           $EXEChysplit/hysplit_trajplot -i./tdump -f1 -k1 -l6 -ptraj -v1 -z80 >> $pgmout 2>errfile
        fi
        export err=$?;err_chk
      else
        msg='WARNING trajectory graphic (trajplot.ps) not available - problem with hysplit_trajplot run, continue with dispersion'
        echo $msg
        postmsg "$jlogfile" "$msg"
      fi
      if [ -s trajplot.traj ]
      then
          cp -p trajplot.traj trajplot.ps 
      fi

      if [ "$SENDCOM" = "YES" ]
      then
        if [ -s MAPTEXT.traj ]
        then
            cp MAPTEXT.traj $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.MAPTEXT.traj
        fi
        if [ -s trajplot.ps ]
        then
            cp trajplot.ps $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.trajplot.ps
        fi
      fi

   fi

   ;;
esac

## end from hysplit_traj.sh single-processor version after run hysplit_hymodelt
######################################################################################
######################################################################################

######################################################################################
######################################################################################
## this is from hysplit_conc.sh single-processor version after run hysplit_hymodelc
## (copied to hysplit_post.sh and modified slightly)

##################################################################
# if duration is zero skip this section (e.g. tracking ash from previuos eruption)
##################################################################
if [ ${qhrs} -ne 0 ]
then

##################################################################
# read timestep from MESSAGE file (1 to 60 minutes)
##################################################################
# this is written to MESSAGE in hymodelc.F as: 
#    WRITE(30,*)' NOTICE   main: Initial time step (min) ',INT(DT)
##################################################################
  STEP=`grep "Initial time step (min)" MESSAGE.001 | tail -c4`
# convert emission duration in hours to minutes
  qhrsm=`echo "$qhrs*60" | bc`
				 	
##################################################################
# if release duration chosen in setup_hysplit is less than the
#   initial timestep, then duration was actually the timestep instead 
# need to force the duration for the output plot labels
#   by re-making MAPTEXT.CFG 
##################################################################
if [ $STEP -gt $qhrsm ]
then
 # convert from minutes to hours
   STEP=`echo "scale=4\n $STEP/60" | bc`
   qhrs=$STEP 			# qhrs input to volcplot for labeling
   w=`echo $qhrs | cut -d. -f1`			# whole number portion
   dot=`expr index $qhrs .`			# decimal portion
   if [ dot -gt 0 ]; then

   # truncate any unnecessary zeros .2500 ==> .25
      frac=`echo $qhrs | cut -d. -f2`
      flen=`expr length $frac`			
      d1=0
      d2=0
      d3=0
      d4=0
      if [ $flen -ge 1 ]; then
         d1=`echo $frac | cut -c1,1`
         if [ $flen -ge 2 ]; then
            d2=`echo $frac | cut -c2,2`
            if [ $flen -ge 3 ]; then
               d3=`echo $frac | cut -c3,3`
               if [ $flen -ge 4 ]; then
                  d4=`echo $frac | cut -c4,4`
               fi
            fi
         fi
      fi
      if [ $d4 -eq 0 ]; then
         qhrs=${w}.${d1}${d2}${d3}
         if [ $d3 -eq 0 ]; then
            qhrs=${w}.${d1}${d2}
            if [ $d2 -eq 0 ]; then
               qhrs=${w}.${d1}
               if [ $d1 -eq 0 ]; then
                  qhrs=${w}.
               fi 
            fi 
         fi 
      fi
   fi
   msg='NOTICE duration is set to initial time step'
   postmsg "$jlogfile" "$msg"
fi

fi	 #### end if duration is zero

#############################################################################
# Create File to Label Graphics Maps - RSMC conc, Homeland conc
#   note that concplot uses lines 3,5,8-15 as long as they don't start "Traj"
#        lines not used for concplot are used in the RSMC coversheet
#############################################################################
rm -f hyout.ps
case $typenum in 
1|3|4) rm -f MAPTEXT.conc 

   echo $title | read tmp tmp1
   case $typenum in 
   1) case $tmp in
        Test)      label="EXERCISE       EXERCISE        EXERCISE";;
        Requested) label="REQUESTED SERVICES";; 
        IAEA)      label="IAEA NOTIFIED EMERGENCY";;
        Internal)  label="Internal NOAA Request";;
        Special)   label=$title;;
        New)       label="EXERCISE       EXERCISE        NEW VERSION";;
      esac
      ;;
   3) case $tmp in
        Test) label="TEST         TEST          TEST";;
        Real) label="As Requested     ";; 
        New)  label="TEST         TEST          NEW VERSION";;
      esac
      ;;
   4) case $tmp in
        Test)      label="EXERCISE       EXERCISE        EXERCISE";;
        Requested) label="REQUESTED SERVICES";; 
        New)       label="EXERCISE       EXERCISE        NEW VERSION";;
      esac 
      ;;
   esac

 # total=`echo "${qrat}*${qhrs}" | bc` 
 # export total=`printf %3.2f $total`		

   rm -f MAPTEXT.conc 
   case $typenum in
   1|4) echo "RSMC Washington  - NOAA  ARL / NCEP"                  >MAPTEXT.conc;;
   3) echo "NOAA NWS NCEP NCO                  "                  >MAPTEXT.conc;;
   esac
   echo "-----------------------------------------------------------" >>MAPTEXT.conc
   case $typenum in

   1|4) echo "Created: $ihr${imn}UTC $ida/$imo/$iyr (day/month/year) RSMC Washington - NOAA ARL / NCEP" >>MAPTEXT.conc;;
   3) echo "Created:$ihr${imn}UTC $imo/$ida/$iyr NOAA NWS NCEP NCO" >>MAPTEXT.conc;;
   esac
   echo "-----------------------------------------------------------" >>MAPTEXT.conc
   echo "Source:${site}  lat:${olat}  lon:${olon}  hgt:${olvl1} to ${olvl2} m" >>MAPTEXT.conc
   echo "Location:${site}  lat:${olat}  lon:${olon}                            " >>MAPTEXT.conc
   echo "Release Start (YYYY MM DD HH MM):${dsyr} ${dsmo} ${dsda} ${dshr} ${dsmn}    " >>MAPTEXT.conc
   case $typenum in
   1) echo "Release ID:${ident}   Rate: ${qrat} Bq/hr  Duration: ${qhrs} hr  Particles: ${numpar}" >>MAPTEXT.conc;;
   *) echo "Release ID:${ident}   Rate: ${qrat}/hr  Duration: ${qhrs} hr  Particles: ${numpar}" >>MAPTEXT.conc;;
   esac
   echo "Traj created $iday $imonth $ida ${ihr}:${imn} UTC ${iyr}  " >> MAPTEXT.conc
   echo "Distribution: Uniform between ${olvl1} and ${olvl2} m AGL" >>MAPTEXT.conc
   case $typenum in
   1|3|4) echo "Dry Deposition Rate:${dryvl} m/s  Wet Removal (below/in-cloud):${wetlo}/${wetin}" >>MAPTEXT.conc;;
   esac
   echo "Meteorology:  $DATlabel                                    " >>MAPTEXT.conc
   echo "Note: Contour values may change from chart to chart" >>MAPTEXT.conc
   case $typenum in
   1) if [ ${icmt} -eq 1 ]; then
         echo "Note: RESULTS BASED ON DEFAULT INITIAL VALUES" >>MAPTEXT.conc
      else
         echo "  " >> MAPTEXT.conc
      fi
      ;; 
   3|4) echo "                                                     " >>MAPTEXT.conc;;
   esac
   echo "Response: $label                                       " >>MAPTEXT.conc
   echo "-----------------------------------------------------------" >>MAPTEXT.conc
   ;;
esac

  ############################################################
  # Output graphics
  ##################################################################

  ##################################################################
  # Setup plot program filename, options and post-processing specs
  #    GEMPAK processing is later 
  ##################################################################
  case $typenum in
  1) pgm=hysplit_concplot		# output filename concplot.ps
     PGM=hysplit_concplot
     map[1]=arlmap
     map[2]=arlmap
     gis=0				# GIS?        0=no 1=yes
     gfil[1]=cdump
     gfil[2]=cdump2
     suffix[1]=conc
     suffix[2]=fine
     options[1]="-e1 -r3 -uBq -z${zoom1} -m1"	# see below (3-23-11 ARL add -m1 for RSMC)
    #options[2]="-e1 -r3 -uBq -z${zoom2} -n${N1}:${N2}"  # current time --> +18 h
     options[2]="-e1 -r3 -uBq -z${zoom2} -m1 "    # rlse time --> current time +18 h (3-23-11 ARL add -m1 for RSMC)
     cp MAPTEXT.conc MAPTEXT.${suffix[2]}
     ;;
  2) pgm=hysplit_volcplot	
     PGM=hysplit_volcplot	

     wetdepflg="-W1"
   # if wetin or wetlo are exponential (e.g. 3.5E+5) will get syntax error, but ignore)
     if [ ${wetin} -eq 0 -a ${wetlo} -eq 0 ]
     then
        wetdepflg="-W0"
     fi
     for imap in 1 2 3 4 5 6 7 8 9; do
       map[$imap]=arlmap
       gfil[$imap]=cdump      		# force this to plot all reductions
     done
     gis=0		

                                #  6-h plots are for internal use
                                # 12-h plots are for dissemination (wafs)
     N=0

     initflg=
     echo pinpf: $pinpf
     if [ "${pinpf}" != '' ]
     then

        klen=`expr "${pinpf}" : ".*"`

      # was PARDUMP or PARSHIFT chosen?
        let klenm7=${klen}-7
        let klenm6=${klen}-6
        read_dump=`echo $pinpf  | cut -c${klenm6}-${klen}`
        read_shft=`echo $pinpf  | cut -c${klenm7}-${klen}`
        echo read_dump: $read_dump
        echo read_shft: $read_shft

      # date of single time period in PARSHIFT file is in filename delimited by plus signs
        if [ "${read_shft}" = "PARSHIFT" ]
        then
           initflg="-F2"
        elif [ "${read_dump}" = "PARDUMP" ]
        then
           initflg="-F1"
        else
           echo "Not using PARDUMP nor PARSHIFT."
        fi

     fi

     for tint in 06 12; do
       numb=
       kolor=1		# color
       zoom=${zoom1}	# was 80 but now keep it same as +12-h plots
       if [ $tint -eq 12 ]
       then
         numb=-n-2		
         kolor=0	# B&W (want better resolution in g3-format graphic)
         zoom=${zoom1}	# was 90
       fi
       for red in 0 1 2 3; do
         let N=$N+1 
         if [ ${olat2} -eq -99 -o ${olon2} -eq -99 -o ${olat3} -eq -99 -o ${olon3} -eq -99 ]
         then
         # this test is also in hysplit_conc.sh to specify number of source locations
         options[$N]="-1${alert} -2${atyp} -3${red} -4${icmt} -5${site} -6${qhrs} -z${zoom} -k${kolor} -ovolcplot${tint}_r${red} $numb ${initflg} ${wetdepflg}"
         else
           # -l75 for open symbol on plot
         options[$N]="-1${alert} -2${atyp} -3${red} -4${icmt} -5${site} -6${qhrs} -z${zoom} -k${kolor} -ovolcplot${tint}_r${red} $numb ${initflg} ${wetdepflg} -l75"
         fi
       done
      done
    
    # alert-ended run for dissemination (force alert=1 atyp=2 reduction=0)
      tint=ae
      alert=0	
      atyp=2
      red=0
      options[9]="-1${alert} -2${atyp} -3${red} -4${icmt} -5${site} -6${qhrs} -z80 -ovolcplot${tint}_r${red} -n-2"
      ;;

  3) pgm=hysplit_concplot
     PGM=hysplit_concplot
     map[1]=countymap		# fine grid
     map[2]=arlmap		# coarse grid
     gfil[1]=cdump
     gfil[2]=cdump2
     gis=1
     suffix[1]=fine
     suffix[2]=coarse
     options[1]="         -d2     -s1 -z${zoom1} -c51"
     if [ ${relcen} -eq 1 ]; then
        options[1]="${options[1]} -g0"
     fi
     options[2]="         -d2     -s1 -z${zoom2} -c1"
     cp MAPTEXT.conc MAPTEXT.${suffix[1]}
     cp MAPTEXT.conc MAPTEXT.${suffix[2]}
     ;;
  4) pgm=hysplit_concplot		# output filename concplot.ps
     PGM=hysplit_concplot
     map[1]=arlmap
     gfil[1]=cdump
     gis=0				# GIS?        0=no 1=yes
     suffix[1]=conc
     options[1]="-u -z${zoom1}"		# see below, -u turns off "mass" label
    #cp MAPTEXT.conc MAPTEXT.${suffix[2]}
     ;;
  esac 

  case $typenum in
  1) lgrd=$ngrd;;		# only set for ngrd=1
  2) lgrd=9;;  			# to do all cases
  3) lgrd=$ngrd;;		# only set for ngrd=2
  4) lgrd=$ngrd;;		# only set for ngrd=1
  esac

# test for GDAS-GFS run to modify volcplot met init time labeling
  rm -f GDAS-GFS.date
# wc -l means count number of lines with a match
  if [ `grep GDAS MESSAGE.001 | wc -l` -ne 0 ]
  then
     if [ `grep GFS MESSAGE.001 | wc -l` -ne 0 ]
     then
      # both GDAS and GFS used since both in MESSAGE file
        FTPDONE=GFS.ftpdone
        if [ -s ${FTPDONE} ]
        then
           YR=`cat ${FTPDONE} | cut -c1-2`
           if [ ${YR} -lt 10 ]
           then
              YR=`cat ${FTPDONE} | cut -c2-2`
              YR="0"${YR}
           fi
           MN=`cat ${FTPDONE} | cut -c3-4`
           if [ ${MN} -lt 10 ]
           then
              MN=`cat ${FTPDONE} | cut -c4-4`
              MN="0"${MN}
           fi
           DY=`cat ${FTPDONE} | cut -c5-6`
           #XXW if [ ${DA} -lt 10 ]
           if [ ${DY} -lt 10 ]
           then
              DY=`cat ${FTPDONE} | cut -c6-6`
              DY="0"${DY}
           fi
           HR=`cat ${FTPDONE} | cut -c7-8`
           if [ ${HR} -lt 10 ]
           then
              HR=`cat ${FTPDONE} | cut -c8-8`
              HR="0"${HR}
           fi
           echo "GFS ${YR} ${MN} ${DY} ${HR}" > GDAS-GFS.date
        else
           echo "WARNING: GFS.ftpdone file not found, plots will _not_ contain GFS labels"
        fi
     fi
  fi

  grd=1
  while [ $grd -le $lgrd ]
  do

  # copy appropriate map boundary file
    cp $FIXhysplit/hysplit_${map[$grd]} arlmap
  
  # do each grid or each volcanic ash reduction level (not both)
  #   -p... is ignored by volcplot (no longer, 6-4-10)

  # concplot options:
  #  -a = write GIS text file (one file per time period)
  #  -c = contours: (0)-dynamic, 1-fixed, Val-10^(val)
  #  -d = concentration average between levels
  #  -e = exposure
  #  -f = one frame per output file
  #  -o = output filename
  #  -p = output file identifier
  #  -n = number of time periods: (0)-all, numb, min:max, -incr
  #  -r = removal (3=total)
  #  -s = number of species
  #  -u = units
  #  -z = zoom (0-100)

   # dummy in DHOUR for RSMC and hls (input file to hysplit_concrop for volc ash)
     case $typenum in
     1|3|4) echo "20" > DHOUR ;;
     esac
     TMP=${gfil[$grd]}
     export pgm=hysplit_concrop;. prep_step
     startmsg
     case $typenum in
      1|3|4) $EXEChysplit/hysplit_concrop -i${gfil[$grd]} -occrop -f0;;
          2) $EXEChysplit/hysplit_concrop -i${gfil[$grd]} -occrop -f1;;
     esac 
     err=$?; export err; err_chk
     gfil[$grd]=ccrop

   # force in ps for volcano filename extension
     case $typenum in
      2) suffix[$grd]=ps;;
     esac

    export pgm=${PGM};. prep_step
    startmsg
    $EXEChysplit/${pgm} -i${gfil[$grd]} -p${suffix[$grd]} ${options[$grd]} >> $pgmout 2>errfile_${grd}
    err=$?; export err; err_chk
    gfil[$grd]=$TMP

    let grd=$grd+1
  done 
  if [ -s concplot.conc ]
  then
     cp concplot.conc concplot.ps
  fi
# add this copy concplot.fine 4-30-07
  if [ -s concplot.fine ]
  then
     cp concplot.fine concplot.fine.ps
  fi
  case $typenum in
  3) if [ -s concplot.fine ]
     then
        cp concplot.fine concplot_loop.fine
     fi
     if [ -s concplot.coarse ]
     then
        cp concplot.coarse concplot_loop.coarse
     fi
  esac

  #######################
  # Google Earth output
  #######################
  case $typenum in
  1|3|4)
     grd=1
     while [ $grd -le $lgrd ]
     do

     case $typenum in
     1) options[1]="-e1 -r3 -uBq -z10 -a3 -m1" 
        options[2]="-e1 -r3 -uBq -z10 -a3 -m1"
      # both of above ARL add -m1 3-23-11 (force map projection)
        ;;
     3) options[1]="-a3 -d2 -s1 -z10 -c50"
        options[2]="-a3 -d2 -s1 -z10";;
     4) options[1]="-u -z10 -a3" 
        options[2]="-u -z10 -a3";;
     esac

     # copy appropriate map boundary file
     #       - don't need map for GIS but get default arlmap so no WARNING msg
       cp $FIXhysplit/hysplit_arlmap arlmap

       export pgm=${PGM};. prep_step
       startmsg
       $EXEChysplit/${pgm} -i${gfil[$grd]} -p${suffix[$grd]} ${options[$grd]} >> $pgmout 2>errfile_${grd}_a3
       err=$?; export err; err_chk

     # don't need postscript file
       rm concplot.${suffix[$grd]}

     if [ -f GELABEL_${suffix[$grd]}.txt ];then

        export pgm=hysplit_gelabel;. prep_step
        startmsg
        $EXEChysplit/${pgm} -p${suffix[$grd]} >> $pgmout 2>errfile_gelabel
        err=$?; export err; err_chk
        if [ -f GELABEL_01_${suffix[$grd]}.ps ];then
          let frame=1
          fnum=`printf %2.2d $frame`
          while  [ -f GELABEL_${fnum}_${suffix[$grd]}.ps ]; do
            export pgm=gs;. prep_step; startmsg
            ${GSPATH}/gs -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER -sDEVICE=pnmraw -q -sOutputFile=- GELABEL_${fnum}_${suffix[$grd]}.ps  -c quit | convert +adjoin - -crop 0x0 GIF:GELABEL_${fnum}_${suffix[$grd]}.gif
            err=$?; export err; err_chk
          # rm GELABEL_${fnum}_${suffix[$grd]}.ps
            let frame=$frame+1
            fnum=`printf %2.2d $frame`
          done
          cp ${FIXhysplit}/hysplit_logocon.gif logocon.gif
          export pgm=zip;. prep_step; startmsg
         /usr/bin/zip HYSPLIT_${suffix[$grd]}.kmz HYSPLIT_${suffix[$grd]}.kml logocon.gif GELABEL*${suffix[$grd]}.gif
          err=$?; export err; err_chk
         #rm -f HYSPLIT_${suffix[$grd]}.kml
         #rm -f GELABEL*${suffix[$grd]}.gif
        fi
     fi

       let grd=$grd+1
     done

  ;;
  esac

  ###########################################################
  # For hls, loops made above, ie plots with same concentration contours
  #  now make individual plots with contours set separately for each plot
  ###########################################################
  case $typenum in
  3)
 # run barebones concplot to count number of maps to make individual frames
     grd=1
     while [ $grd -le $lgrd ]
     do
       optionstmp[$grd]="-d2 -f1 -s1"
       export pgm=${PGM};. prep_step
       startmsg
       $EXEChysplit/${pgm} -i${gfil[$grd]} -p${suffix[$grd]} ${optionstmp[$grd]} >> $pgmout 2>errfile_${grd}
       err=$?; export err; err_chk

     NMAPS=0
     ls concplot00??.${suffix[$grd]} | while read tmp
     do
        let NMAPS=${NMAPS}+1
     done
     rm concplot00??.${suffix[$grd]}

    # set concplot options
     options[1]="-d2 -s1 -z${zoom1} -c50"
     options[2]="-d2 -s1 -z${zoom2}" 
      if [ ${relcen} -eq 1 ]; then
        options[1]="${options[1]} -g0"
      fi

     # copy appropriate map boundary file
       cp $FIXhysplit/hysplit_${map[$grd]} arlmap

 # loop through maps, plotting each separately
     N=1
     while [ $N -le $NMAPS ]
     do

     #######################
     # GIS output
     #######################
       export pgm=${PGM};. prep_step
       startmsg
       $EXEChysplit/${pgm} -i${gfil[$grd]} -p${suffix[$grd]}  -n${N}:${N} -a${gis} -d2 -s1 -z10 >> $pgmout 2>errfile_${grd}
       err=$?; export err; err_chk

     # remove postscript file
       rm -f concplot.${suffix[$grd]}

     #######################
     # postscript output
     #######################
   # dummy in DHOUR for RSMC and hls (input file to hysplit_concrop for volc ash)
     case $typenum in
     1|3|4) echo "20" > DHOUR ;;
     esac
     TMP=${gfil[$grd]}
     export pgm=hysplit_concrop;. prep_step
     startmsg
     $EXEChysplit/hysplit_concrop -i${gfil[$grd]} -occrop -f0
     err=$?; export err; err_chk
     gfil[$grd]=ccrop

       export pgm=${PGM};. prep_step
       startmsg
       $EXEChysplit/${pgm} -i${gfil[$grd]} -p${suffix[$grd]} -n${N}:${N} ${options[$grd]} >> $pgmout 2>errfile_${grd}
       err=$?; export err; err_chk
       gfil[$grd]=$TMP
       if [ $N -lt 10 ]; then
          n="000${N}"
       elif [ $N -lt 100 ]; then
          n="00${N}"
       elif [ $N -lt 1000 ]; then
          n="0${N}"
       fi
       mv concplot.${suffix[$grd]} concplot${n}.${suffix[$grd]}

     let N=$N+1
     done

       let grd=$grd+1
     done

     ;;
  esac

  ###########################################################
  # particle plot and inventory
  ###########################################################
  if [ -s PARDUMP.001 ]
  then
  # merge PARDUMPs 
    export pgm=hysplit_parmerge;. prep_step
    startmsg
    ${EXEChysplit}/${pgm} -iPARDUMP -oPARDUMP.tmp
    export err=$?; err_chk  
  # plot, assuming arlmap already in working directory
    zoomp=75
    export pgm=hysplit_parhplot;. prep_step
    startmsg
   ${EXEChysplit}/${pgm} -iPARDUMP.tmp -z${zoomp}
    export err=$?
   #err_chk  					# not-fatal
  # inventory PARDUMP
    export pgm=hysplit_par2asc;. prep_step
    startmsg
   ${EXEChysplit}/${pgm} -iPARDUMP.tmp -oDUMMY -v
    export err=$?; err_chk  
    rm DUMMY
  fi

# these 2 lines added to prod in response to hysplit_concplot coredump on 3-11-11 for RSMC run
# copied here to para 3-35-11
[ -f concplot.conc ] && rm concplot.conc   # dcs -keep these from getting copied to $COMOUT
[ -f concplot.fine ] && rm concplot.fine   # dcs -keep these from getting copied to $COMOUT

###########################################################
# output to /com  
###########################################################
if [ "$SENDCOM" = "YES" ]
then

  if [ -s MAPTEXT.conc ]
  then
      cp MAPTEXT.conc $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.MAPTEXT.conc
  fi

 #if [ -s concplot.ps ]
 #then
 #   cp concplot.ps $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.concplot.ps
 #fi

  if [ -s parhplot.ps ]
  then
     cp parhplot.ps $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.parhplot.ps
     cp PARINV      $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.PARINV
  fi

  case $typenum in
  2)
     ls volcplot* | while read tmp
     do
        cp $tmp $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.$tmp
     done
     ;;
  esac

  case $typenum in
  1|3|4) 
     ls concplot* | while read tmp
     do
        cp $tmp $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.$tmp
     done
     ;;
  esac

  case $typenum in
  3)
    ls GIS* | while read tmp
    do
       cp $tmp $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.$tmp
    done
    ;;
  esac

  case $typenum in
  1|3|4)
    ls *.kmz | while read tmp
    do
       tmpfile=`echo $tmp | cut -f2 -d_`
       cp $tmp $COMOUT/hysplit.${cycle}.${site}.${TYP}${seq}.$tmpfile
    done
    ;;
  esac
 
fi

## end from hysplit_conc.sh single-processor version after run hysplit_hymodelc
######################################################################################
######################################################################################

####################################################
#    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}.ps
     if [ -s $file ]
     then
      # legacy filename
        cp ${file} 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 [ $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

      if [ $SENDDBN = YES ]; then
         $DBNROOT/bin/dbn_alert MODEL VAFTAD_GRIB $job ${COMOUT}/${VNAME}.e${EPDY}${ECYC}.r${PDY}${cyc}.grib
      fi

      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 volcanic ash hourly files from hymodelm for NAWIPS - hysplit_post.sh"
      postmsg "$jlogfile" "$msg"
      export err=3; err_chk
   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)
  export pgm=hysplit_concplot
  . prep_step
  startmsg
                                               # force -m1 3-23-11 by ARL
  $EXEChysplit/${pgm} -icdump -pconc -e1 -r2 -uBq -z80 -m1 >> $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)
  export pgm=gs; . prep_step; startmsg
  $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- rsmc.ps -c quit | \
   convert - PDF:rsmc.pdf
  export err=$?; err_chk

  ;;
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) 
  export pgm=gs; . prep_step; startmsg
  $GSPATH/gs -r96 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- rsmc.ps -c quit | \
   convert - PDF:rsmc.pdf
  export err=$?; err_chk
  ;;
esac

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

 # split into frames temp00.gif temp01.gif ...
   export pgm=convert; . prep_step; startmsg
   convert nocrop.gif temp%02d.gif
   export err=$?; err_chk
 # 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
      ########################################
      export pgm=convert; . prep_step; startmsg
      convert -crop 0x0 temp${ii}.gif rsmc_web${ii}.gif
      export err=$?; err_chk
      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
      export pgm=gs; . prep_step; startmsg
      $GSPATH/gs -r85 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
      -sDEVICE=pnmraw -q -sOutputFile=- rsmc_togif.ps -c quit | \
      convert - GIF:nocrop.gif
      export err=$?; err_chk
   else
      export pgm=gs; . prep_step; startmsg
      $GSPATH/gs -r105 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
      -sDEVICE=pnmraw -q -sOutputFile=- rsmc_togif.ps -c quit | \
      convert - GIF:nocrop.gif
      export err=$?; err_chk
   fi

 # split into frames temp00.gif temp01.gif ...
   export pgm=convert; . prep_step; startmsg
   convert nocrop.gif temp%02d.gif
   export err=$?; err_chk
   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
      ########################################
      export pgm=convert; . prep_step; startmsg
      convert -crop 0x0 temp${ii}.gif rsmc_web${ii}.gif
      export err=$?; err_chk
      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
   ########################################
   export pgm=gs; . prep_step; startmsg
   $GSPATH/gs -r72 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER  \
   -sDEVICE=pnmraw -q -sOutputFile=- cover.ps -c quit | \
   convert - GIF:cover.gif
   export err=$?; err_chk
  
   # no need to crop coversheet 
   mv cover.gif CVRUS.gif

   ;;
3) 
   for sfx in fine coarse
   do 

   ########################################
   # convert to pdf (all frames one file)
   ########################################
   export pgm=gs; . prep_step; startmsg
   $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
   export err=$?; err_chk

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

  rm -f temp*.gif			

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

 # split into frames temp00.gif temp01.gif ...
   export pgm=convert; . prep_step; startmsg
   convert nocrop.gif temp%02d.gif
   export err=$?; err_chk
   
   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)
      export pgm=gs; . prep_step; startmsg
      $GSPATH/gs -r48 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
      -sDEVICE=pnmraw -q -sOutputFile=- concplot_loop.${sfx} -c quit | \
      convert - GIF:nocrop.gif
      export err=$?; err_chk

    # split into frames temp00.gif temp01.gif ...
      export pgm=convert; . prep_step; startmsg
      convert nocrop.gif temp%02d.gif
      export err=$?; err_chk
   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
      export pgm=convert; . prep_step; startmsg
      convert -crop 0x0 temp${ii}.gif temp_loop${ii}.gif
      export err=$?; err_chk
      rm temp${ii}.gif
      let i=i+1
      ii=`printf %.2d $i`
   done

       ########################################
       # Create Animated GIFs
       ########################################
       export pgm=convert; . prep_step; startmsg
       convert -adjoin -loop 100 -delay 100 \
          temp_loop*.gif ${site}_t${cyc}z_${sfx}_loop.gif
       export err=$?; err_chk

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

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

        ########################################
        # crop white space - this is not required, but looks better in browser
        #   uses George Vandenberghe's patch in /nfsuser/g02/ibmapp/im_beta
        ########################################
        export pgm=convert; . prep_step; startmsg
        convert -crop 0x0 nocrop.gif plot.gif
        export err=$?; err_chk

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

# rename postscript file for web and copy to COMOUTHLS
        cp concplot00${fnum}.${sfx} ${site}_t${cyc}z_${sfx}_${fnum}.ps
        if [ "${SENDCOM}" = "YES" ]
        then
           cp ${site}_t${cyc}z_${sfx}_${fnum}.ps ${COMOUTHLS}
        fi
#
        let frame=$frame+1
        fnum=`printf %2.2d $frame`
     done
   done
   ;;
4)
   ########################################
   # convert ps to gif (small, Yx300)
   ########################################
   export pgm=gs; . prep_step; startmsg
   $GSPATH/gs -r39.5 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- back_togif.ps -c quit | \
   convert - GIF:nocrop.gif
   export err=$?; err_chk

 # split into frames temp00.gif temp01.gif ...
   export pgm=convert; . prep_step; startmsg
   convert nocrop.gif temp%02d.gif
   export err=$?; err_chk
 # 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
      ########################################
      export pgm=convert; . prep_step; startmsg
      convert -crop 0x0 temp${ii}.gif back_web${ii}.gif
      export err=$?; err_chk
      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)
   ########################################
   export pgm=gs; . prep_step; startmsg
   $GSPATH/gs -r105 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER \
   -sDEVICE=pnmraw -q -sOutputFile=- back_togif.ps -c quit | \
   convert - GIF:nocrop.gif
   export err=$?; err_chk

 # split into frames temp00.gif temp01.gif ...
   export pgm=convert; . prep_step; startmsg
   convert nocrop.gif temp%02d.gif
   export err=$?; err_chk
   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
      ########################################
      export pgm=convert; . prep_step; startmsg
      convert -crop 0x0 temp${ii}.gif back_web${ii}.gif
      export err=$?; err_chk
      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
   ########################################
   export pgm=gs; . prep_step; startmsg
   $GSPATH/gs -r72 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dSAFER  \
   -sDEVICE=pnmraw -q -sOutputFile=- cover.ps -c quit | \
   convert - GIF:cover.gif
   export err=$?; err_chk
  
   # 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
       export pgm=ascii2shp		
       . prep_step
       startmsg
       /nwprod/util/exec/${pgm} ${site}_t${cyc}z_${sfx}_${fnum} polygons \
          < hysplit.${cycle}.${site}.${runtyp}${seq}.GIS_*_${sfx}_${fnum}.txt
       export err=$?; err_chk
       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
     ;;
  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.
  ###########################################################################

  if [ "$typenum" -eq '3' ]; then
     if [ "$run_type" = "on_demand" ]
     then
        export org=${org:-sdm}
        export run_id=`${USHhysplit}/hiresw_get_hls_nest.sh | awk '{print $4}'`
        export MODEL=`${USHhysplit}/hiresw_get_hls_nest.sh | awk '{print $5}'`
        export COMOUT=${COMOUTHLS}
        export RUN=hls
     else
        export run_id="*"
     fi

     ###############################################################
     # Make html index files
     ##############################################################
     ${USHhysplit}/hysplit_web_index_hls.sh $run_type
  fi

  ${USHhysplit}/hysplit_ondemand_send_web.sh $MACHINE_SSMC &
  ssmc_pid=$!
  ${USHhysplit}/hysplit_ondemand_send_web.sh $MACHINE_BLDR &
  bldr_pid=$!
  wait $ssmc_pid
  export errsum_ssmc=$?
  wait $bldr_pid
  export errsum_bldr=$?
 
  if [ "$errsum_bldr" -ne '0' ]
  then
    set +x
    echo "*******************************************************************"
    echo "************************* FATAL ERROR! ****************************"
    echo "*HYSPLIT output DID NOT mirror successfully to BOULDER ($MACHINE_BLDR)*"
    echo "*                   Set this job to complete and                  *"
    echo "*  Run '/utility/jhyspt_recover' when the server is back online   *"
    echo "*   Contact the WOC for server availability problems/questions    *"
    echo "*******************************************************************"
    set -x
  else
    echo "On Demand HYSPLIT successfully mirrored to BLDR ($MACHINE_BLDR)"
    if [ "$SENDSMS" == "YES" ]
    then
       $SMSBIN/setev bldr
    fi
  fi

  if [ "$errsum_ssmc" -ne '0' ]
  then
    set +x
    echo "*******************************************************************"
    echo "************************* FATAL ERROR! ****************************"
    echo "*HYSPLIT output DID NOT mirror successfully to SSMC ($MACHINE_SSMC)   *"
    echo "*                   Set this job to complete and                  *"
    echo "*  Run '/utility/jhyspt_recover' when the server is back online   *"
    echo "*   Contact the WOC for server availability problems/questions    *"
    echo "*******************************************************************"
    set -x
  else
    echo "On Demand HYSPLIT successfully mirrored to SSMC ($MACHINE_SSMC)"
    if [ "$SENDSMS" == "YES" ]
    then
       $SMSBIN/setev ssmc
    fi
  fi
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

# 4-11-11 only send to arl for volcanic ash and WFO/hls
if [ ${typenum} -eq 2 -o ${typenum} -eq 3 ]
then

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
#   ARL=205.156.4.190			# alert.arl.noaa.gov
  ARL=140.90.78.254
  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.001   ${PDY}${cyc}+${cdt}+${site}+${runtyp}${seq}+MESSAGE.001 
  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 HYSPLIT_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 HYSPLIT_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.ps
         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

fi

if [ "$errsum_ssmc" -ne '0' ] || [ "$errsum_bldr" -ne '0' ]; then
   exit 1
fi
