# UTILITY SCRIPT NAME : mkavpolar.sh
#              AUTHOR : David Michaud
# 
# ABSTRACT : This utility script produces charts of plotted winds
#            and temperatures at specified latitudinal and
#            longitundinal position for the following flight levels:
#            j631:FL450(150MB);  FL390(200MB); FL340(250MB);
#                 FL300(300MB):FL240(400); FL180(500MB)
#
# INPUT: 4 arguments are input to this script.
#         1st argument - hour1      - Fcst hour1 grib file
#         2nd argument - hour2      - Fcst hour2 grib file
#         3rd argument - faxparm    - parameter input.
#         4th argument - out        - fax chart file
#
echo "History: AUG     1999 - Modified for IBM SP"
#

set +x
hour1=$1
hour2=$2
faxparm=$3
out=$4
num=$#

if test $num -eq 4
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECgraph=${EXECgraph:-/nwprod/util/exec}
   export PARMgraph=${PARMgraph:-/nwprod/util/parm}
   export FIXgraph=${FIXgraph:-/nwprod/util/fix}
   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: mkavpolar.sh hour1 hour2 faxparm outputname"
   echo ""
   exit 16
fi

msg="Enter Make mkavpolar.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

export pgm=avpolar
. prep_step

#cp $FIXgraph/graph_gphbg/nh2500x.pur .
cp /nwprod/util/fix/graph_gphbg/nh2500x.pur .

export XLFUNIT_12="pgrbf$hour1"
export XLFUNIT_13="pgrbf$hour2"
export XLFUNIT_15="$PARMgraph/$faxparm"
export XLFUNIT_18="$FIXgraph/graph_avpolar.$cycle"
export XLFUNIT_32="pgrbif$hour1"
export XLFUNIT_33="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/avpolar >> $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 fax.avpolar $out $pcom
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
fi

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

exit
