# UTILITY SCRIPT NAME : mkfax.sh
#              AUTHOR : Mary Jacobs
# 
# ABSTRACT : This utility script produces FAX CHARTS
#
#            *Note:  This utility expects a parm fild 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 - faxparm - parameter input.
#         3rd argument - out     - fax chart file
#         4th argument - sendkey for snd2forgntbl.sh.
#         5th argument - [optional] old plot vpap tape55,
#
echo "History: AUG     1999 - Modified for IBM SP"
#

set +x
hour_list=$1
faxparm=$2
out=$3
sendkey=$4
num=$#

if test $num -eq 4 -o $num -eq 5
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: mkfax.sh "hour_list" faxparm outputname sendkey [plottape55]"
   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 FAX GRAPHICS utility."
postmsg "$jlogfile" "$msg"

##############################
# Process FAX Chart
##############################
cp $FIXgraph/graph_gphbg/nh4004.pur nh4004.pur
cp $FIXgraph/graph_gphbg/nh2005.pur nh2005.pur
cp $FIXgraph/graph_gphbg/nh4006.pur nh4006.pur
cp $FIXgraph/graph_gphbg/nh2007.pur nh2007.pur
cp $FIXgraph/graph_gphbg/nh4005.pur nh4005.pur
cp $FIXgraph/graph_gphbg/sh4001.pur sh4001.pur

if test $sendkey = 'fax.brknel' -o $sendkey = 'fax.bcmrfmp1'
then
  export pgm=faxmakrx
else
  export pgm=faxmakr
fi
. prep_step

echo sendkey=$sendkey
echo pgm=$pgm
read any
##############################
# 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) if [ "$CGRID" = "YES" ] ; then
                      grbext=grb_fm${i}.tm00
                      grbiext=grb_fmi${i}
                    else
                      grbext=grb5fm${i}.tm00
                      grbiext=grb5fmi${i}
                    fi
                   ;;
               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
            ;;
          *)grbext=$i
            grbiext=${i}i
            ;;
   esac

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

   if test ! -f $grbiext
   then
      $EXECutil/grbindex $grbext $grbiext
   fi

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

   pgbunit=`expr $pgbunit + 1`
   pgbiunit=`expr $pgbiunit + 1`
   if [ $pgbunit -eq 15 ] ; then
     pgbunit=`expr $pgbunit + 1`
     pgbiunit=`expr $pgbiunit + 1`
   fi
done

export XLFUNIT_15="$PARMgraph/$faxparm"
export XLFUNIT_48="$FIXgraph/graph_awpseed"
export XLFUNIT_49="ncepdate"
if test $num -eq 5
then
   export XLFUNIT_54="$5"
fi
export XLFUNIT_55="label55"
export XLFUNIT_60="label60"
export XLFUNIT_61="label61"
export XLFUNIT_62="label62"
export XLFUNIT_63="label63"
export XLFUNIT_71="$out"    # fax chart file
export XLFUNIT_76="outdgn1" # output design file
export XLFUNIT_77="outdgn2" # output design file
export XLFUNIT_78="outdgn3" # output design file
export XLFUNIT_79="outdgn4" # output design file
export XLFUNIT_80="faxext8" # temp output file
export XLFUNIT_89="pureras"
 
startmsg
$EXECgraph/$pgm $newparm >> $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
     $utilities/snd2forgntbl.sh $sendkey $out $pcom
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
fi

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

exit
