#!/bin/sh
export member=afosairpl.sh
USAGE="usage: afosairplt.sh infile afosfile cards pass55 passcards"
DATE="$member 2000-01-21 16:15L "
VERS="Starting: ${USHutil}/${DATE}" 
#______________________________________________________________________________
# NAME     : afosairplt.sh
# Author   : Peter Henrichsen
# Purpose  : This script runs the airpltaf program to make the 
#          : apbul text file into four AFOS MAPS.
#          : They are: L0A P0D P01 P03.
# History  : 98-11-17 Peter Henrichsen
#          :1999-11-03 Peter Henrichsen modify to run on IBM.
#          :1999-12-12 Peter Henrichsen modify to pass sndfrc to script
#            from snd2forgn and to use USHutil
#          :2000-12-12 Peter Henrichsen modify to pass afosplrc to script
#            parent script. Also changed arg "job" to lower case and
#            'pillist1' to graph_pillist1.  
#
# Location : This script is found on hp36 as:
#          : /tmp_mnt/export/sgi73/peterhen/util/scripts/ibm/afosairplt.sh
#          : & on ncosp as :
#          : /nwprod/util/ush/afosairplt.sh
#          : /nfsuser/g02/wx12ph/util/ush/afosairplt.sh
#
# Usage    : This script runs in both cycles of job 314.
#
# Arguments: infile     - input file
#            afosfile   - output afos filename
#            cards      - input data cards file in PARMgraph
#            pass55     - the passed label file for next step
#            passcards  - the passed card file for next steps
#______________________________________________________________________________
#
integer istat
cd $DATA

set -x 

print "$USAGE"
print "$VERS"
    
num=$#
if test $num -ne 5
then
  echo "You need 5 input variables."
  err_exit
fi
postmsg  "$jlogfile" "$VERS"
        
infile=$1
afosfile=$2
cards=$3
pass55=$4
passcards=$5

echo infile=$infile
echo afosfile=$afosfile
echo cards=$cards
echo pass55=$pass55
echo passcards=$passcards

pgm=airpltaf

export pgm; prep_step
export XLFRTEOPTS="unit_vars=yes"
  
#   input files.
export XLFUNIT_10="$DATA/$infile"                        # the input text file data.
export XLFUNIT_11="$DATA/ncepdate"                 # production date
export XLFUNIT_13="$UPADICT/sonde.land.tbl"        # upper air dictionary
export XLFUNIT_12="$FIXutil/graph_stanlist.dat"    # list of stations needed
export XLFUNIT_14="$FIXutil/graph_precpw.normals"  # normals file
export XLFUNIT_16="$PARMutil/$cards"              # control cards
#export XLFUNIT_20="$FIXutil/graph_pillist1"        # AFOS pillist file
export XLFUNIT_20="/nwprod/util/fix/graph_pillist1"        # AFOS pillist file

#   out put files

export XLFUNIT_51="$DATA/airpltaf.data"        # work file
export XLFUNIT_52="$DATA/$passcards"           # passed card file 
export XLFUNIT_53="$DATA/airplwork"            # work file
export XLFUNIT_55="$DATA/$pass55"              # passed label file 
export XLFUNIT_61="$DATA/outable"              # work file
export XLFUNIT_64="$DATA/stntable"             # work file
export XLFUNIT_71="$DATA/$afosfile"            # the output AFOS file
export XLFUNIT_75="$DATA/airpmsg.$pid"         # the consol message file.

startmsg
 
$EXECutil/airpltaf PARM="X  AFOS" >> $pgmout  2> errfile
istat=$? 
echo ${pgm}: Ended with return code = $istat

if [ "$istat" -eq 0 ]
then

  msg=`grep -i MADE $DATA/airpmsg.$pid \
  | awk '{for(i=4;i<=NF;i++) printf("%s ", $i)}'`
   msg3="${pgm} ${msg}"   
   postmsg  "$jlogfile" "$msg3"
   
  err=$?;export err;err_chk
   

#  $pass55 (airtap55.${job}) is input for the next graphics program 
#  $passcards (airpltpass.${job}) is input for the next graphics programs 
  
  cp $DATA/$pass55      $pcom/$pass55
  cp $DATA/$passcards   $pcom/$passcards
  
   msg1=`grep -i MADE $DATA/airpmsg.$pid \
  | awk '{for(i=12;i<=NF;i++) printf("%s ", $i)}'`
  
  if test "$SENDCOM" = 'YES'
  then
    cp $afosfile $pcom/$afosfile
  fi

  if test "$SENDDBN" = 'YES'
  then
    sndfrc=0
     echo $sndfrc >$DATA/sndfrc 
     
    $USHutil/snd2forgn AFOS ${model} $job $pcom/$afosfile
       
       read sndfrc < $DATA/sndfrc
       
      if test $sndfrc -eq '0'
      then         
        msg="Afosmaps $msg1 WERE POSTED!" 
        postmsg "$jlogfile" "$msg"
        msg="$member HAS COMPLETED NORMALLY!" 
      else
        msg="ERROR AFOS MAPS $msg1 NOT POSTED!"
        postmsg "$jlogfile" "$msg"
        msg="${pgm}: ABNORMAL STOP = $sndfrc!:" 
      fi
      rm  $DATA/sndfrc
    
  else

    msg="$member testing Afosmaps: $msg1 not sent!"    

  fi
  postmsg "$jlogfile" "$msg"
else
  msg3="${pgm}: ERROR AFOS MAPS, L0A P0D P01 P03, NOT MADE!:"
  postmsg "$jlogfile" "$msg3"
  msg3="${pgm}: ABNORMAL STOP = $istat!:"
  postmsg "$jlogfile" "$msg3"
 
  afosplrc=1
  echo $afosplrc >$DATA/afosplrc
fi


echo " "
echo "Leaving shell script afosairplt.sh"
echo " "


