#!/bin/sh

# UTILITY SCRIPT NAME : expltwafs.sh
#              AUTHOR : Larry Sager
#
# ABSTRACT : This utility script produces 06 12 18 24 30 and 36 hour
#            forecasts for 500, 400, 300, 250 and 200mb 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
#         3th argument - type    - Type of chart to create
#
# TYPES: a - Western Hemisphere Mercator
#       b1 - Atlantic Mercator 
#        f - Create the Pacific Mercator (500 400 300 250 200mb)
#        f1- Create the Pacific Mercator (850 700 150  70mb)
#        h - Polar Stereographic North Atlantic
#        i - Polar Stereographic North Pacific
#        j - Polar Stereographic Anarctic
#        c - Mercator Africa/Europe      
#        d - Mercator Indian Ocean        
#        e - Mercator Indian Ocean/Australia
#        g - Polar Stereographic Anarctic
#        m - Mercator North Pacific
#
# HISTORY Oct. 2005 - Converted fax graphics to T4 format and stopped writing to the
#                     stat file.

set +x
hour1=$1
hour2=$2
type_list=$3
num=$#
export job=gfs_fax_f${fhr}_${cyc}

if test "$num" -eq 3
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECgraph=${EXECgraph:-/nwprod/util/exec}
   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 job=${job:-interactive}
   export pgmout=${pgmout:-OUTPUT.$$}
else
   echo ""
   echo "Usage: expltwafs.sh hour1 hour2 \"a b1 f f1 h i j c d e g m\""
   echo ""
   exit 16
fi

set +x
echo "########################################################"
echo "#  Entering Utility  expltwafs.sh"
echo "########################################################"
set -x

##############################
# Copy Input Data from COM 
##############################

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

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

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

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

for type in $type_list
do
   case $type in
      a)executable=plotwafa
        bgrd_map=mr5101.pur;;
     b1)executable=plotwafa
        bgrd_map=mr5102.pur;;
      f)executable=plotwafa
        bgrd_map=mr5106.pur;;
     f1)executable=plotwafa
        bgrd_map=mr5106.pur;;
      h)executable=plotwafp
        bgrd_map=nh5108.pur;;
      i)executable=plotwafp
        bgrd_map=nh5109.pur;;
      j)executable=plotwafp
        bgrd_map=sh5110.pur;;
      c)executable=plotwafa
        bgrd_map=mr5103.pur;;
      d)executable=plotwafa
        bgrd_map=mr5104.pur;;
      e)executable=plotwafa
        bgrd_map=mr5105.pur;;
      g)executable=plotwafp
        bgrd_map=nh5107.pur;;
      m)executable=plotwafa
        bgrd_map=mr5113.pur;;
   esac

   if test ! -f $bgrd_map
   then
      cp $FIXgraph/graph_gphbg/$bgrd_map $bgrd_map
   fi
   echo $hour1 $hour2 $type > wafs.input
   echo wafs.input
   FAXOUT=fax.wafs${hour1}${type}
   jobn=gfs_fax_f${hour2}_${cyc}

   ##############################
   # Process PLOTWAF CHART 
   ##############################
   export pgm=$executable
   . prep_step

   export XLFUNIT_10="$FIXgraph/graph_wafs.cities.${type}"
   export XLFUNIT_13="pgrbf$hour1"
   export XLFUNIT_14="pgrbf$hour2"
   export XLFUNIT_33="pgrbif$hour1"
   export XLFUNIT_34="pgrbif$hour2"
   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_52="x6b"
   export XLFUNIT_81="fax.wafs${hour1}${type}"

   $EXECgraph/$executable < wafs.input >> $pgmout 2> errfile
   export err=$?;err_chk
   
   width=1728
   height=1950
   FMAT=fax
   HEADER=YES
   ROTATE=NO
   gif=NO
   toc=WAF
   prt=NO

   if [ $type = "b1" ]
   then
     grep $type$hour1$hour2 $FIXutil/wafsubset_wmo.tbl | cut -c8- > fax.lst
   else
     grep $type$hour1$hour2 $FIXutil/wafsubset_wmo.tbl | cut -c7- > fax.lst
   fi

   for sub in `cat fax.lst`
   do 
 
   submn=$sub
  KEYW=$type$sub

  grep $KEYW $FIXutil/wafsubset_wmo.tbl | read Keyword WMO ORIG name

  export FAXOUT Keyword width height submn FMAT WMO ORIG HEADER ROTATE gif toc prt name jobn
  $utilscript/mk_graphics.sh

  done
done
msg="expltwafs.sh completed normally"
postmsg "$jlogfile" "$msg"

exit
