#!/bin/sh
USAGE="usage: cacafos.sh infile afosout pilist "
VERS="${USHutil}/${member} 2000-03-17 06:40L"
#______________________________________________________________________________
# NAME     : cacafos.sh
# Author   : Peter Henrichsen
# Purpose  : This script runs program cacafos which
#               makes two AFOS maps,
#               6KA, "DROUGHT/WET INDEX" and
#               6KB, "CROP MOIST INDEX",
#               using the data from infile.
#
# History  : 98-04-21 Peter Henrichsen
#          : 1999-06-30 Peter Henrichsen modify to run on IBM ncosp
#          : 2000-03-16 Peter Henrichsen modify to use USHutil.        
#
# Location : This script is found on hp36 as:
#          : /tmp_mnt/export/sgi73/peterhen/util/scripts/ibm/cacafos.sh
#          : & on ncosp as          
#          : /nwprod/util/ush/cacafos.sh or
#          : /nfsuser/g02/wx12ph/util/ush/cacafos.sh           
#
#                Arg1 is         Arg2          Arg1    
# cacafos.sh    data card file   name of AFOS  list of afos     
#               fullpath         output file   pills         
#
#  AN EXAMPLE:
#
# cacafos.sh  /tmp/foreign/data/afos.data 
#       where:
#	      "/tmp/foreign/data/afos.data " is the full-path of input data.
#  AN EXAMPLE:
#
# cacafos.sh  /pcom/foreign/afos/afos.data  AFOSCAC '6ka 6kb' 
#       where:
#	      "/pcom/foreign/afos/afos.data" is the full-path of input data.
#
#             "AFOSCAC" is the name of the file to hold the afos maps.
#
#             "6ka 6kb" is the name of the two AFOS to make from the
#             input data file.
#
#_______________________________________________________________________________
#
cd $DATA
print "$USAGE"
print "$VERS"    
msg="$member HAS STARTED!"
postmsg "$jlogfile" "$msg"

integer   istat      
echo Number of arguements passed in is $#
for arg in "$@"
do  
 echo $arg
done
export XLFRTEOPTS="unit_vars=yes"
INFILE="$1"
export filename=`basename ${INFILE}`
export OUTFILE="$2"
export pils="$3"
export LOGMSG=${NAME}msg.$pid
 
pgm=cacafos

export pgm; prep_step
set -x
   
#input files

export XLFUNIT_10="$INFILE"                 # the input text file.
export XLFUNIT_20="$FIXutil/graph_pillist1" # the pilfile.

# output files


export XLFUNIT_71="$DATA/$OUTFILE.$pid"  # completed afos file.

export XLFUNIT_75="$DATA/$LOGMSG" #the consol message file. 
 

# make messages

for PILL in $pils 
do

#  get PILL in upper case.

  pilupper=`echo  $PILL | tr '[a-z]' '[A-Z]' `
  msg0=`egrep -h 1.*$pilupper $FIXutil/afostitles.cntl  \
  | awk '{for(i=2;i<=NF;i++) printf("%s ", $i)}'`
     
  msgs="${member} WILL MAKE $msg0!"
  postmsg "$jlogfile" "$msgs" 

done
#____________________________________________________________________
#
#    now run the cacafos program !
#
#____________________________________________________________________
startmsg

if test "$envir" = 'test'
then
  P=C  
  T=X
else
  P=X
  T=X
fi
export P T

$EXECutil/cacafos PARM="${P}${T}"  2> errfile


istat=$err
 
if test "$istat" = 0
then
 err=$?;export err; err_chk
 
  cp $DATA/$LOGMSG $DATA/sav.$pid
 
  PILS=`echo  $pils | tr '[a-z]' '[A-Z]' ` 

  msg0=`grep -i  MADE $DATA/sav.$pid \
  | awk '{for(i=4;i<=NF;i++) printf("%s ", $i)}'`
    postmsg "$jlogfile" "$msg0"

  rm $DATA/$LOGMSG

# build messages

  msg2="Afosmaps ${PILS} WERE POSTED!"
  msg="${member} testing ${PILS} not sent!" 
    

   if test "$envir" = 'prod'
   then

     if test "$SENDCOM" = 'YES' 
     then
       SENDNAME=afos.${PIL}.$USR
#
#      convert SENDNAME to "lower case".
#
       export sendname=`echo  $SENDNAME | tr '[A-Z]' '[a-z]' `
     
       path=$pcom/afos 
       mkdir -m 775 -p  $path >/dev/null 2>&1
       
       cp $DATA/$OUTFILE.$pid $path/$sendname  
#______________________________________________________________
#     start the procedure to post the  AFOS product.
#______________________________________________________________
    
       if test "$SENDDBN" = 'YES'    
       then    
         export KEY=AFOS
         export MODEL=$model
        sndfrc=0
        echo $sndfrc >$DATA/sndfrc 
         $USHutil/snd2forgn $KEY $MODEL $job $path/$sendname
                  
         read sndfrc < $DATA/sndfrc
          if test $sndfrc -eq '0'
          then  
           postmsg "$jlogfile" "$msg2"      
            msg="${member} HAS COMPLETED NORMALLY!"
          else
            msg="ERROR AFOS $sendnam NOT POSTED!"
            postmsg "$jlogfile" "$msg"
            msg="${pgm}: ABNORMAL STOP = $sndfrc!:" 
            postmsg "$jlogfile" "$msg"
          fi
         rm  $DATA/sndfrc
       else
   
        echo "${member} testing SENDDBN = ${SENDDBN}, $msg1 not sent!"
       fi

     else
       
      echo  "${member} testing SENDCOM = ${SENDCOM}, $msg1 not sent!" 
     fi

   else
   
    echo  "${member} testing envir = ${envir}, $msg1 not sent!"   
   fi
else
   msg="${pgm}: ABNORMAL STOP MAKING ${PILS}, STOP = $istat!"
  
fi
postmsg "$jlogfile" "$msg"
rm $DATA/sav.$pid
 

