# UTILITY SCRIPT NAME : mkavmerc.sh
#              AUTHOR : David Michaud
# 
# ABSTRACT : This utility script produces 
#            forecasts for 850, 200, 150, and 100MB winds and temps.
#
# INPUT: 6 arguments are input to this script.
#         1st argument - hour1   - Fcst hour of 1st grib file
#         2nd argument - hour2   - Fcst hour of 2nd grib file
#         3rd argument - faxparm - parameter input.
#         4th argument - execparm- parameter input.
#         5th argument - out     - fax chart file
#         6th argument - sendkey

echo "History: AUG     1999 - Modified for IBM SP"
#

set +x
hour1=$1
hour2=$2
faxparm=$3
execparm=$4
out=$5
sendkey=$6
num=$#

if test $num -eq 6
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECgraph=${EXECgraph:-/nwprod/util/exec}
   export PARMgraph=${PARMgraph:-/nwprod/util/parm}
   export utilities=${utilities:-/nwprod/util/ush}
   export envir=${envir:-prod}
   export jlogfile=${jlogfile:-jlogfile}
   export NET=${NET:-gfs}
   export RUN=${RUN:-gfs}
   export cyc=${cyc:-00}
   export cycle=${cycle:-t${cyc}z}
   export SENDCOM=${SENDCOM:-NO}
   export SENDDBN=${SENDDBN:-NO}
   if [ -z "$DATA" ]
   then
      export DATA=`pwd`
      cd $DATA
      /nwprod/util/ush/setup.sh
      /nwprod/util/ush/setpdy.sh
      . PDY
   fi
   export COMIN=${COMIN:-/com/$NET/$envir/$NET.$PDY}
   export pcom=${pcom:-/pcom/$NET}
   export pgmout=${pgmout:-OUTPUT.$$}
else
   echo ""
   echo "Usage: mkavmerc.sh hour1 hour2 faxparm outputname"
   echo ""
   exit 16
fi

msg="Enter Make mkavmerc.sh utility."
postmsg "$jlogfile" "$msg"

##############################
# Copy Input Field to $DATA
##############################
if test ! -f pgrbf$hour1
then
   cp $COMIN/${RUN}.${cycle}.pgrbf$hour1 pgrbf$hour1
fi
if test ! -f pgrbif$hour1
then
   $EXECutil/grbindex pgrbf$hour1 pgrbif$hour1
fi
if test ! -f pgrbf$hour2
then
   cp $COMIN/${RUN}.${cycle}.pgrbf$hour2 pgrbf$hour2
fi
if test ! -f pgrbif$hour2
then
   $EXECutil/grbindex pgrbf$hour2 pgrbif$hour2
fi

cp $FIXgraph/graph_gphbg/mr2502.pur .
cp $FIXgraph/graph_gphbg/mr4202.pur .

export pgm=avmerc
. prep_step

export XLFUNIT_8="$PARMgraph/$faxparm"
export XLFUNIT_11="pgrbf$hour1"
export XLFUNIT_12="pgrbf$hour2"
export XLFUNIT_31="pgrbif$hour1"
export XLFUNIT_32="pgrbif$hour2"
export XLFUNIT_52="x6b"
export XLFUNIT_55="putlab.55"
export XLFUNIT_60="f60"
export XLFUNIT_61="f61"
export XLFUNIT_62="f62"
export XLFUNIT_63="f63"
export XLFUNIT_71="ras"
export XLFUNIT_72="rs2"
export XLFUNIT_81="$out"
 
startmsg
$EXECgraph/avmerc < $PARMgraph/$execparm >> $pgmout 2> errfile
export err=$?;err_chk

##############################
# Post Files to PCOM
##############################
if test "$SENDCOM" = 'YES'
then
   cp $out $pcom/$out
   ##############################
   # Distribute Data
   ##############################
   if test "$SENDDBN" = 'YES'
   then
     $DBNROOT/bin/dbn_alert FAXX $sendkey $job $pcom/$out
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
fi

msg="mkavpolar hour completed normally"
postmsg "$jlogfile" "$msg"

exit
