####################################################################
# Name:  exsfcanl.sh          Author:  Ralph Jones
# Abstract: This script makes either the North American or Northern Hemisphere
#           Hemisphere surface plot for afos.
# 
# History: MAR 1997 - First implementation of this new script.
#####################################################################
 
cd $DATA
 
##########################################
#
# START FLOW OF CONTROL
#
# 1) Process ncepdate and decide to make either the North American (na)
#    or Northern Hemisphere (nh) surface plot.
#
# 2) Dump bufr observational data and execute rdbufr to put it into an 
#    ascii file. 
#
# 3) Execute rdgrib to make first guess. 
#
# 4) Execute dcopy to merge the observational data with the 1st guess.
#
# 5) Execute allspline to perform the sfc analysis.
#
# 6) Execute script snd2forgn to send analysis and plotfile to the 
#    intergraph.
# 7) Execute nasfcplt or nhplot to process the data.
#
#########################################

set -xa

########################################
msg="Script exsfcanl.sh starting."
postmsg "$jlogfile" "$msg"
########################################
 
zhour=$1

typeset -Z2 cychr
if test $krun -lt 5
then 
  cychr=`expr \( $zhour / 3 \) \* 3` 
elif test $krun -eq 5
then
  cychr=`expr \( $zhour / 6 \) \* 6` 
else
  msg="Invalid variable krun=$krun."  
  errexit
fi

echo "$PDY$cychr" > daycyc
  
###############################################
# Make the Northern Hemisphere afos surface plotfile.
###############################################
AFOSFILE=nhplot.afos$krun$cychr

export pgm=nhplot
. prep_step

export XLFUNIT_10="synop.$PDY$cychr"
export XLFUNIT_11="metar.$PDY$cychr"
export XLFUNIT_12="ships.$PDY$cychr"
export XLFUNIT_13="lcman.$PDY$cychr"
export XLFUNIT_14="mbuoy.$PDY$cychr"
export XLFUNIT_15="dbuoy.$PDY$cychr"
export XLFUNIT_17="$FIXgraph/graph_pillist1"
export XLFUNIT_52="$AFOSFILE"
 
startmsg
$EXEChrly/nhplot >> $pgmout < daycyc  2> errfile
export err=$?;err_chk
  
SNDKEY=ipsa2_nh${krun}${cychr}

###############################################
# copy files to /pcom and call snd2forgn
# FTYPE and FOREIGN required for snd2forgn 
###############################################
if test "$SENDCOM" = 'YES'
then
   cp $AFOSFILE $pcom/$AFOSFILE.$job
 
   if test "$SENDDBN" = 'YES'
   then
      FTABLE=$FIXgraph/graph_snd2forgn.names
      grep -i $SNDKEY $FTABLE >>mgot
      FTYPE=` awk '{print $2}' mgot`
      FOREIGN=` awk '{print $3}' mgot`
      rm  mgot
      $USHutil/snd2forgn $FTYPE $FOREIGN $job $pcom/$AFOSFILE.$job
   fi
fi

###############################################
msg='Script exsfcanl.sh completed Normally.'
postmsg "$jlogfile" "$msg"
###############################################
