#----------------------------------------------------------------
# Name    : afsfcplt.sh   author:  Larry Sager
# Purpose : Script to prepare the AFOS surface plotfile
#           Script runs in all RW1 to prepare NA sfc plotfile type=NA
#           Script runs in 00, o6, 12, and 18Z RW4 to prepare NH sfc
#           plotfile type=NA.
#           Script runs in 00, o6, 12, and 18Z RW4 to prepare  North 
#           African sfc plotfile type=AF.
#
# usage   : afsfcplt.sh  hr type 
#----------------------------------------------------------------
set -xa

cd $DATA
                                                        
HOUR="$1"
type=`echo $2 | tr '[a-z]' '[A-Z]'`

filename=afos.sfplt$2${HOUR}.$job

# prepare control card for standard input. 
cat <<EOH > cardec
${PDY}${HOUR}
${type}
EOH

#
#     DUMP THE SYNOPTIC DATA
#

export FORM=$PDY$HOUR
DUMP=/nwprod/ush/dumpjb

ix=0
for TYPE in synop metar ships dbuoy mbuoy lcman
do
   if test -s  ${TYPE}.${PDY}${HOUR} 
   then
      ix=`expr $ix + 1`
      echo " $TYPE" > dumplogfile
   else
      $DUMP ${PDY}${HOUR} .5 $TYPE
      err=$?
      if [ "$err" -eq 0 ] 
      then
         ix=`expr $ix + 1`
         echo " $TYPE" > dumplogfile
      else
         msg="No $TYPE data for ${PDY}${HOUR}!"
         postmsg  "$jlogfile" "$msg"
      fi
   fi
done
cat dumplogfile

if [ "$ix" -eq '0' ] 
then
   msg="ERROR NO DATA TO MAKE DUMPED - ABORTING script exafsfcplt.sh"
   postmsg "$jlogfile" "$msg"
   export pgm="dumping"
   err_exit 
fi

export pgm=afsfcplt
. prep_step

export XLFUNIT_10="synop.$PDY$HOUR"
export XLFUNIT_11="metar.$PDY$HOUR"
export XLFUNIT_12="ships.$PDY$HOUR"
export XLFUNIT_13="lcman.$PDY$HOUR"
export XLFUNIT_14="mbuoy.$PDY$HOUR"
export XLFUNIT_15="dbuoy.$PDY$HOUR"
export XLFUNIT_17="$FIXgraph/graph_pillist1"
export XLFUNIT_22="$FIXhrly/hourly_naafos.table"
export XLFUNIT_52="$filename"

startmsg
$EXEChrly/afsfcplt >> $pgmout <cardec 2> errfile
export err=$?; err_chk
    
if test "$SENDCOM" = 'YES'
then
   cp $filename $pcom/$filename

   if test "$SENDDBN" = 'YES'
   then
      $DBNROOT/bin/dbn_alert AFOS hrly $job $pcom/$filename
   fi
fi

set +x
msg="Script exafsfcplt.sh completed successfully."
postmsg "$jlogfile" "$msg"

##################### END OF EXAFSFCPLT.script ####################
