#!/bin/sh
USAGE="usage: afstoawp.sh filename pils "
VERS="${USHutil}/${member} 2000-01-21 1625L"
#______________________________________________________________________________
# NAME     : afstoawp.sh
# Author   : Peter Henrichsen
# Purpose  : This script runs the formawip programs to convert
#          : AFOS files into AWIPS graphics format. Then the AWIPS graphic
#          : file is POSTED.
#
# History  : 98-03-05 Peter Henrichsen
#           :1999-06-30 Peter Henrichsen modify to run on IBM.
#           :1999-12-02 Peter Henrichsen modify to use USHutil.
#           :1999-12-14 Peter Henrichsen modify to check sndfrc from 
#            snd2forgn
#           :2000-01-20 Peter Henrichsen modify check for lower case job.
#            and to remove some un-need logic.
#           :2000-01-21 Peter Henrichsen modify change arg "job" to
#            to lower case. Also modified to remove envir test.
#
# Location : This script is found on hp36 as:
#          : /tmp_mnt/export/sgi73/peterhen/util/scripts/ibm/afstoawp.sh
#          : & on ncosp as          
#          : /nwprod/util/ush/afstoawp.sh or 
#          : /nfsuser/g02/wx12ph/util/ush/afstoawp.sh
#            
# Usage    : This script is called to convert AFOS products created on
#            any machine into AWIPS format.
#
#            The AFOS file is passed as the first argument (full path). 
#            
#                Arg1 is afosfile fullpath                   
# AN EXAMPLE:
#
#     afstoawp.sh  /tmp/fnafos/l8p2900.utf 'list of afos pils' 
#     where:
#            "/tmp/fnafos/l8p2900.utf" is the afos file full-path.
#            "'list of afos pils'" is the list of pils in the file to
#             converted into awips format. The list will fave the form of
#              '93p 94p 95p'  where each pil is seperated by a blank.
#
#  Remarks : This script assumes that the following values have been exported
#            by the parent shell:
#            DATA EXECutil envir FIXutil job SENDCOM SENDDBN
#            jlogfile model pcom pgmout pid utilities 
#             
#
#  Note    : If you ARE NOT running as dbnprd and are testing
#            this script: Then set envir=test and SENDCOM=NO 
#            in the parent job.
#            If you ARE running as dbnprd and are testing
#            this script: Then set envir=prod and SENDCOM=NO
#            in the parent job.                     
# 
#_______________________________________________________________________________

cd $DATA 
integer   istat
integer   mstat
istat=0
mstat=0
print "$USAGE"
print "$VERS"
set -x    
msg="${member} HAS STARTED!"
postmsg "$jlogfile" "$msg"

echo Number of arguements passed in is $#

for arg in "$@"
do  
   echo $arg
done
INFILE="$1"
export filename=`basename ${INFILE}`
export afosfile=`echo ${filename} | awk 'BEGIN{FS="."} {print $1$2}'` 

pils="$2"
export pils

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

for PIL in $pils 
do

# get PIL in upper case.

 pilupper=`echo  $PIL | tr '[a-z]' '[A-Z]' `

 PILNUM=$pilupper
 
 msgs="${member} HAS STARTED processing $PIL!"
 postmsg "$jlogfile" "$msgs" 

 pgm=afstoawp
 export pgm; prep_step

            
    name=$pilupper
 
 # name is now in upper
 
 export name
  
 # get name in lower case.

 pillower=`echo  $name | tr '[A-Z]' '[a-z]' `

#  a sample control card is:

#  90X 0101700025 KWBC U00 HPXAI105CN MAXT3D PTWO33     AWPS_RB 000 YES 


#  get the control card for program "afstoawp" ! 

egrep -h ^$name $FIXutil/afstoawp.controls >$DATA/${name}.card
    
istat=$? 
if test "$istat" = 0
then
  
#  get WMO name of the product.

   wmonam=`awk '{print $7}' <$DATA/${name}.card`

   msg1=$wmonam
   
#   check to see if $job is missing

    if [ -z "$job" ] ; then 
       export job=missgn
    fi

  export AWIPOUT=afos.$name.toawips.$wmonam.$job
  export LOGMSG=${PIL}msg.${reqid}
  export awipout=`echo  $AWIPOUT | tr '[A-Z]' '[a-z]' ` 
  export XLFRTEOPTS="unit_vars=yes"
  
#   input files.
  
  export XLFUNIT_21="$INFILE" # the input AFOS file to be converted.
  
#   output files.
 
 export XLFUNIT_51="$DATA/$AWIPOUT"  # the completed AWIPS Redbook file.
   
 export XLFUNIT_75="$DATA/$LOGMSG"   #the consol message file.

          
#____________________________________________________________________
#
#    now run the afstoawp program !
#
#____________________________________________________________________

 startmsg

 $EXECutil/afstoawp < $DATA/${name}.card >> $pgmout 2> errfile
 mstat=$? 
 echo afstoawp: Ended with return code = $mstat

 if [ "$mstat" -eq 0 ]
 then
  err=$?;export err; err_chk
 
  cp $LOGMSG save.$LOGMSG 
  chmod go+rw save.$LOGMSG
  rm $LOGMSG
   
  export msg0=`egrep -h 1.*$PILNUM $FIXutil/afostitles.cntl  \
       | awk '{for(i=3;i<=NF;i++) {if(i<NF) printf("%s ", $i);\
        else printf("%s",$i);}}'` 

 export msg1

  grep -i SUCCESSFULLY $DATA/save.$LOGMSG >>whatis
   istat=$?
   if test "$istat" = 0
   then
     msg=`grep SUCCESSFULL  $DATA/save.$LOGMSG  \
     | awk '{for(i=4;i<=NF;i++) printf("%s ", $i)}'`

     postmsg "$jlogfile" "$msg" 
#   Write to the file that indicates we had a good conversion to awips!
    awiprc=0
    echo $awiprc >$DATA/$jobid.awiprc
 
#    build messages

     msg2="Awipsmap, $msg1 ${msg0}, WAS POSTED!"
     msg="${member} testing $msg1 ${msg0} not sent!"   

      if test "$SENDCOM" = 'YES' 
      then
       iname=`basename ${pcom}`
        if test "$iname" = 'foreign'
        then
          path=$pcom/awips 
          mkdir -m 775 -p  $path >/dev/null 2>&1       
        else
          path=$pcom             
        fi
       export path
     
       cp $DATA/$AWIPOUT $path/$awipout  
#______________________________________________________________
#    start the procedure to post the  AWIP product.
#______________________________________________________________
    
        if test "$SENDDBN" = 'YES'    
           then     
#           check to see if $model is missing

             if [ -z "$model" ] ; then
               export model=unkown
             fi    
#            get TYPE from 8th arg of card

             export TYPE=`awk '{print $8}' <$DATA/${name}.card`
             export MODEL=$model      
             rm $DATA/${name}.card 
              
             sndfrc=0
             echo $sndfrc >$DATA/sndfrc 
      
             $USHutil/snd2forgn \
             $TYPE $MODEL $job $path/$awipout 
             
             read sndfrc < $DATA/sndfrc
       
             if test $sndfrc -eq '0'
             then
             
              msg2="Awipsmap $msg1 ${msg0}WAS POSTED!" 
               postmsg "$jlogfile" "$msg2"      
               msg="${member} HAS COMPLETED NORMALLY!"
             else
              msg="ERROR AWIPS MAP $msg1 NOT POSTED!"
              postmsg "$jlogfile" "$msg"
              msg="${pgm}: ABNORMAL STOP = $sndfrc!:" 
            fi
            rm  $DATA/sndfrc
           else   
             echo "${member} testing SENDDBN = ${SENDDBN}, $msg1 not sent!"
           fi
          rm $DATA/$AWIPOUT
         else       
         echo  "${member} testing SENDCOM = ${SENDCOM}, $msg1 not sent!" 
        fi
    else
     msg1=`grep ERROR  $DATA/save.$LOGMSG  \
     | awk '{for(i=4;i<=NF;i++) printf("%s ", $i)}'`
     
#    postmsg "$jlogfile" "$msg1" 
     
     msg="ERROR $name not found in $INFILE !"

   fi
 else
  msg3="${pgm}: ERROR $name NOT CONVERTED INTO AWIPS.!:"
  postmsg "$jlogfile" "$msg3"
  msg3="${pgm}: ABNORMAL STOP CONVERTING $name, STOP = $mstat!:"
  postmsg "$jlogfile" "$msg3"
 fi
else
   msg="$name NOT found in $FIXutil/afstoawp.controls!:"
  
fi
 postmsg "$jlogfile" "$msg"
 rm save.$LOGMSG 
 rm $DATA/whatis
 rm $DATA/${name}.card

done
 

