#
# NAME     : redbook_ntc.sh
# Purpose  : This script runs the redbook_ntc program to convert
#          : AFOS files into redbook graphics format. Then the graphic
#          : is posted to toc via ntc for transfer to awips.
#
# History  : Larry Sager   First implementation of this script
#

set -x    
FIXutil=${FIXutil:-/nwprod/util/fix}
EXECutil=${EXECutil:-/nwprod/util/exec}
USHutil=${USHutil:-/nwprod/util/ush}

filename=`basename ${INFILE}`
PDYHH=${PDY}${cyc}
SUB=NONE
INFILE=$1
pils=$2

# Start loop to convert the afos maps into awips format .

for PIL in $pils 
do

 name=$PIL     
  
 grep -i ^$name $FIXutil/afstoawp.controls > ${name}.card
 cat $name.card  

#  get WMO header and originator of the product.

 wmo=`awk '{print $7}' <${name}.card`
 orig=`awk '{print $3}' <${name}.card`
 NTCOUT=redbook.${wmo}.${job}

 export AWIPOUT=${name}.redbook.$wmo.$job
 export XLFRTEOPTS="unit_vars=yes"
 export XLFUNIT_21="$INFILE" # the input AFOS file to be converted.
 export XLFUNIT_51="$DATA/$AWIPOUT"  # the completed AWIPS Redbook file.
   
# startmsg
 $EXECutil/redbook_ntc < ${name}.card >> $pmgmout 2> errfile
 mstat=$? 

##############################
# Post Files to PCOM
##############################
 if test "$SENDCOM" = 'YES'
 then
   #############################
   #  Add the NTC heading
   #############################
   
   $USHutil/make_NTC_file.pl $wmo $orig $PDYHH $SUB $AWIPOUT $NTCOUT
   cp $NTCOUT  $pcom/$AWIPOUT  

   ##############################
   # Distribute Data
   ##############################
   if test "$SENDDBN" = 'YES'
   then
     $DBNROOT/bin/dbn_alert GRIB_LOW redbook awips  $pcom/$AWIPOUT   
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
 fi

  msg="redbook ntc completed normally"
  postmsg "$jlogfile" "$msg"
 
done
exit
