# UTILITY SCRIPT NAME : mkhmerc.sh
#              AUTHOR : David Michaud
# 
# ABSTRACT : This utility script produces the GFS mercator FAX CHARTS
#            of interest to Honolulu using grib input from hours 00-12.
#            Environment variables RUN, CYCLE, PARMgraph, pgmout, SENDDBN
#            JOB, pcom  must be exported.
#
# INPUT:    1...name of fax file
#           2...name of fax file
#           3...foreign name for fax file
#
# HISTORY: Sept. 2005 - remove snd2forgn logic
# HISTORY Oct. 2005 - Converted fax graphics to T4 format and stopped writing to the
#                     stat file.
#

set +x
hour_in=$1
out=$2
foreign=$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 FIXutil=${FIXutil:-/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 job=${job:-interactive}
   export pgmout=${pgmout:-OUTPUT.$$} 
else
   echo ""
   echo "Usage: mkhmerc.sh <out> <foreign>"
   echo ""
   exit 16
fi

set +x
echo " ------------------------------------------"
echo "  Entering Utility  mkhmerc12.sh           "
echo "  Make the VARIAN FAX CHART $out maps      "
echo " ------------------------------------------"
set -x
   
msg="Enter Make HMERC utility."
postmsg "$jlogfile" "$msg"

##############################
# Copy Input Fields to Copy to $DATA
##############################

case $hour_in in
   12)hour_list="00 06 12"
      pgbunit=12
      pgbiunit=32;;
   48)hour_list="24 30 36 42 48"
      pgbunit=16
      pgbiunit=36;;
   72)hour_list="72"
      pgbunit=21
      pgbiunit=41;;
    *)echo "Utility Set to run for hour 12/48 or 72 hours"
      exit
      ;;
esac

##############################
# Begin Processing Map
##############################

cp ${FIXgraph}/graph_gphbg/mr4004.pur mr4004.pur

export pgm=hmerc
. prep_step

for i in $hour_list
do
   if test ! -f pgrbf${i}
   then
      cp $COMIN/${RUN}.${cycle}.pgrbf${i} pgrbf${i}
   fi

   if test ! -f $COMIN/${RUN}.${cycle}.pgrbf${i}
   then
      $EXECutil/grbindex pgrbf${i} pgrbif${i}
   else
      cp $COMIN/${RUN}.${cycle}.pgrbif${i} pgrbif${i}
   fi

   eval export XLFUNIT_${pgbunit}="pgrbf${i}"
   eval export XLFUNIT_${pgbiunit}="pgrbif${i}"

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

export XLFUNIT_8="/nwprod/util/fix/graph_hmerc${hour_in}.${cycle}"
#export XLFUNIT_8="${FIXgraph}/graph_hmerc${hour_in}.${cycle}"
export XLFUNIT_9="${FIXgraph}/graph_gphbg/mr4004.pur"
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_80="f80"
export XLFUNIT_81="$out"

startmsg 
$EXECgraph/hmerc < ${PARMgraph}/graph_hmerc${hour_in}.parm.$cycle >> $pgmout 2> errfile
export err=$?;err_chk
 
msg="Leaving mkhmercb.sh"
postmsg "$jlogfile" "$msg"

exit
