# UTILITY SCRIPT NAME : mkafos.sh
#              AUTHOR : David Michaud
# 
# ABSTRACT : This utility script produces AFOS CHARTS
#
#            *Note:  This utility expects a parm field for
#                    the label on the map.  If you don't
#                    have a parm fild, the calling script
#                    will input NONE.
#
# INPUT: 4 arguments are input to this script.
#         1st argument - hour1   - Fcst hour of 1st grib file
#         2nd argument - afosparm - parameter input.
#         3rd argument - out     - afos chart file
#
echo "History: AUG     1999 - Modified for IBM SP"
#

set +x
hour_list=$1
afosparm=$2
out=$3
num=$#

if test $num -eq 3
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECutil=${EXECutil:-/nwprod/util/exec}
   export PARMutil=${PARMutil:-/nwprod/util/parm}
   export EXECgraph=${EXECgraph:-/nwprod/util/exec}
   export PARMgraph=${PARMgraph:-/nwprod/util/parm}
   export FIXgraph=${FIXgraph:-/nwprod/util/fix}
   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.$$}
   export do_label=${do_label:-NO}
else
   echo ""
   echo "Usage: mkafos.sh "hour_list" afosparm outputname"
   echo ""
   exit 16
fi

if test "$do_label" = "YES"
then
   read parm < parm
   newparm="PARM='$parm'"
   echo "newparm = $newparm"
else
   unset newparm
fi

set +x
echo "########################################################"
echo "#  Begin making the VARIAN FAX CHART $out maps"
echo "########################################################"
set -x

msg="Enter Make AFOS GRAPHICS utility."
postmsg "$jlogfile" "$msg"

export pgm=afosmakr
. prep_step

##############################
# Copy Input Field to $DATA
##############################
pgbunit=11
pgbiunit=31

for i in $hour_list
do
   echo $i
   case $i in
         [0-9][0-9]|[0-9][0-9][0-9])case $NET in
               gfs|mrf)grbext=pgrbf${i}
                       grbiext=pgrbif${i}
                       ;;
               nam)grbext=grb5fm${i}.tm00
                   grbiext=grb5fmi${i}
                   ;;
               ngm)grbext=pgrb.f${i}
                   grbiext=pgrbif${i}
                   ;;
                 *)grbext=pgrbf${i}
                   grbiext=pgrbif${i}
                   ;;
            esac
            ;;
        anl)grbext=pgrbanl
            grbiext=pgrbianl
            ;;
        D+3)grbext=D+3.z500.grib
            grbiext=D+3.z500.gribi
            pgbunit=`expr $pgbunit + 1`
            pgbiunit=`expr $pgbiunit + 1`
            ;;
          *)grbext=$i
            grbiext=${i}i
            ;;
   esac

   if test ! -f $grbext
   then
      cp $COMIN/${RUN}.${cycle}.$grbext $grbext
   fi

   if test ! -f $COMIN/${RUN}.${cycle}.$grbiext
   then
      $EXECutil/grbindex $grbext $grbiext
   else
      cp $COMIN/${RUN}.${cycle}.$grbiext $grbiext
   fi

   eval export XLFUNIT_${pgbunit}="$grbext"
   eval export XLFUNIT_${pgbiunit}="$grbiext"

   pgbunit=`expr $pgbunit + 1`
   pgbiunit=`expr $pgbiunit + 1`
done

export XLFUNIT_30="$PARMgraph/$afosparm"

if test ! -f ncepdate
then
   cp /com/date/t${cyc}z ncepdate
fi
export XLFUNIT_47="ncepdate"
export XLFUNIT_48="$FIXgraph/graph_pillist1"
export XLFUNIT_49="$FIXgraph/graph_awpseed"
export XLFUNIT_51="scratch"    # afos chart file
export XLFUNIT_61="$out"    # afos chart file
 
startmsg
$EXECgraph/afosmakr >> $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 AFOS $NET $job $pcom/$out
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
fi

msg="mkafosmakr ${hour} hour completed normally"
postmsg "$jlogfile" "$msg"

exit
