#!/bin/sh



#########################################################################
#
#   Script:  4panel_gif.sh
#
#   This scripts creates GEMPAK .gif images of forecast fields from
#   the NWW wave model output.
#
#
#   History:   Ralph Jones     08/09/2005   JIF original version.
#
#
#########################################################################

# Run setup to initialize working directory and utility scripts
sh $utilscript/setup.sh

   msg=" Make GEMPAK TIFF utility"
   postmsg "$jlogfile" "$msg"



  set -x


# Set up month names

  imon=`echo $PDY | cut -c5-6`
  if [ $imon -lt 10 ]; then imon=`echo $PDY | cut -c6`; fi 
  MON=`$HOMEutil/month_name.sh $imon MON`

  DAY=`echo $PDY | cut -c7-8`
  YEAR=`echo $PDY | cut -c1-4`


  WMOHDR=QJAA88
  device="tiff | $WMOHDR"

  wavedev="${WMOHDR}.tiff"


# Create chart labels

  echo " " > blank.t

  echo "US DEPT OF COMMERCE" > ul_lab1.t
#  echo "                   " >> ul_lab1.t
  echo "NOAA/NWS/NCEP  WASHINGTON" >> ul_lab1.t

  echo "WAVE HEIGHTS ARE IN FEET" > ul_lab2.t

  echo "" > ur_lab3.t


# Loop through 6 forecast times

  clear=YES

  for fhr in 12 24 36 48

    do

#     Compute valid time for this forecast hour

      $HOMEutil/verfdate.sh $PDY $cyc $fhr
      cat verfdate.txt | read fy fm fd valid dayvrf monvrf

# Create date stamped labels

      echo "WWD$fhr" > bot_lab1.t
      echo "${valid}Z${fd}" >> bot_lab1.t
      echo "GMEXW" >> bot_lab1.t

echo " ${fhr}HR FCST WAVE HGT/DIR   VALID ${valid}Z $dayvrf $fd $monvrf $fy" > bot_lab2.t 


#     Set grid date and input file name

      gdattim=`echo ${PDY} | cut -c3-8`/${cyc}00F0${fhr}

      case $fhr in
        12)  panel=UL  ;;
        24)  panel=UR  ;;
        36)  panel=LL  ;;
        48)  panel=LR  ;;
      esac

      region=wna

          cp ${COMIN}/${region}_${PDY}${cyc} gem_grids${fhr}.gem
          gdfile=gem_grids${fhr}.gem

                fact=-6.0

                garea="18;-101;28;-74"
                proj="str/90;-105;0/0;0;0;0"
                gdpfun="vecr(mul($fact,sin(mul(dtr,wvdir))),mul($fact,cos(mul(dtr,wvdir))))   !  mul(3.28,htsgw)"
#                gdpfun="vecr(sin(mul(dtr,wvdir)),cos(mul(dtr,wvdir)))   !  mul(3.28,htsgw)"

                latval="1/12/1/1/5;5/-0.5;-100.25/2"
                txtloc1="#33.6;-92"
                txtloc2="#32.5;-92.35"
                txtloc3="#34.2;-98"
                txtloc4="#19.5;-89.0"
                txtloc5="0.475;0.08"
                txtsize1=8
                txtsize2=2
                txtsize5=3


# Run the GEMPAK program 

  $GEMEXE/gdplot2 << EOF

  CLEAR   = $clear
  GLEVEL  = 0
  GDFILE  = ${gdfile}
  GDATTIM = ${gdattim}
  GAREA   = ${garea}
  PROJ    = ${proj}
  PANEL   = ${panel}/1/1/1/plot
  GVCORD  = none
  GDPFUN  = ${gdpfun}
  SCALE   = 0
  CINT    = 
  CLRBAR  = 0
  TYPE    = AN                  !  P
  IJSKIP  = 2;1/2;70            !  2;3/2;72
  LINE    = 1/1/2
  MARKER  = 1/17/0.5/1/sw
  WIND    = AN1/0.6/1/0123/0.60
  DEVICE  = $device
  TITLE   =
  TEXT    = S/21//1/hw
  LATLON  = 0
  l
  r

  CLEAR   = no
  TYPE    =
  TEXT    = M/21/hw
  LATLON  = 1/1/1//5;5//2
  l
  r

  exit
EOF



#  Draw a box around the plot

gpbox<< GPBOX_FLAG
  REGION  = plot
  DEVICE  = $device
  LINE    = 1/1/2/1
r

exit
GPBOX_FLAG


#  Plot chart labels

gptext << GPTEXT_FLAG

  CLEAR   = NO
  PANEL   = ${panel}/1/1/1/plot
  COLORS  = 1
  TEXT    = S/21/1/1//c/hw
  DEVICE  = $device
  TXTFIL  = ul_lab1.t
  TXTLOC  = ${txtloc1}
  COLUMN  = 1
  l
  r

  TEXT    = M/21/1/1//c/hw
  TXTFIL  = ul_lab2.t
  TXTLOC  = ${txtloc2}
  r

  TEXT    = M/21//1//c/hw
  TXTFIL  = LOGO
  TXTLOC  = ${txtloc3}
  r

  TEXT    = S/21//1//c/hw
  TXTFIL  = bot_lab1.t
  TXTLOC  = ${txtloc4}
  r

  TEXT    = M/21//121//c/hw
  TXTFIL  = bot_lab2.t
  TXTLOC  = ${txtloc5}
  r

  exit
GPTEXT_FLAG


  clear=NO

done


$GEMEXE/gpend


   ORIG=KWBC
   PDYHH=${PDY}${cyc}

   if [ $SENDDBN = YES ]; then

     INPATH=${DATA}/${WMOHDR}.tiff
     OUTPATH=${DATA}/chart.out
     SUB=IMAG2000

     $HOMEutil/make_NTC_file.pl $WMOHDR $ORIG $PDYHH $SUB $INPATH $OUTPATH

# Send graphic to TOC

     cp $OUTPATH ${pcom}/gulfwave_${cyc}.tif

     $DBNROOT/bin/dbn_alert GRIB_LOW ${NET} ${job} ${pcom}/gulfwave_${cyc}.tif

   fi

     


   msg=" GULFWAVE_TIFF ${fhr} hour completed normally"
   postmsg "$jlogfile" "$msg"

   exit

