#!/bin/sh
###############################################################################
#                                                                             #
# This script is the product generator ("graphics job")  for the global NWW3  #
# wave model.                                                                 #
#                                                                             #
# Remarks :                                                                   #
# - Supplemental error output is witten to the wave.log file.                 #
#                                                                             #
echo "  05 MAY 2006 Steve Lilly - Restore 2 RFAX graphics "                   #
echo "          24HR WIND/WAVE FORECAST and "                                 #
echo "          48HR WIND/WAVE FORECAST     "                                 #
#                                                                MAY,  2006   #
# Copied form nwprod 03/11/03                                                 #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

  seton='-xa'
  setoff='+xa'
  set $seton
  postmsg "$jlogfile" "HAS BEGUN on `hostname`"

  msg="Starting NWW3 PRODUCTS SCRIPT"
  postmsg "$jlogfile" "$msg"

  awipsgrib='yes'
  awipsbull='yes'
      difax='yes'
   afosbull='yes'

  if [ "$cycle" != 't00z' ] && [ "$cycle" != 't12z' ]
  then
      difax='no'
   afosbull='no'
  fi

# 0.b Date and time stuff

  export date=$PDY
  export YMDH=${PDY}${cyc}

  set $setoff
  echo ' '
  echo '                        ****************************'
  echo '                        *** NWW3 PRODUCTS SCRIPT ***'
  echo '                        ****************************'
  echo "                                       $date $cycle"
  echo ' '
  echo "Starting at : `date`"
  echo ' '
  echo "   AWIPS grib fields : $awipsgrib"
  echo "   AWIPS bulletins   : $awipsbull"
  echo "   FAX / AFOS maps   : $difax"
  echo "   AFOS bulletins    : $afosbull"
  echo ' '
  set $seton

# --------------------------------------------------------------------------- #
# 1.  Get necessary files

  set $setoff
  echo ' '
  echo 'Preparing input files :'
  echo '-----------------------'
  set $seton

# 1.a Grib file (AWIPS and FAX charts)

  if [ ! -f gribfile ]
  then
    echo "   Copying gribfile from $COMIN"
    cp $COMIN/$modID.$cycle.grib gribfile
  fi

  if [ -f gribfile ]
  then
    echo "   gribfile exists."
  else
    msg="ABNORMAL EXIT: NO GRIB FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '**************************** '
    echo '*** ERROR : NO GRIB FILE *** '
    echo '**************************** '
    echo ' '
    echo "$modID pgen $date $cycle : GRIB file missing." >> $wavelog
    echo $msg
    set $seton
    err=1;export err;err_chk
  fi

# 1.b Bulletin files ( AWIPS )

  echo "   Copying bulletins from $COMIN"
  cp $COMIN/$modID.$cycle.cbull_tar cbull.tar

  if [ ! -f cbull.tar ]
  then
    msg="ABNORMAL EXIT: NO BULLETIN TAR FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '************************************ '
    echo '*** ERROR : NO BULLETIN TAR FILE *** '
    echo '************************************ '
    echo ' '
    echo "$modID pgen $date $cycle : bulletin tar missing." >> $wavelog
    echo $msg
    set $seton
    err=2;export err;err_chk
  fi

  echo "   Untarring bulletins ..."
  tar -xf cbull.tar
  OK=$?

  if [ "$OK" = '0' ]
  then
    echo "      Unpacking successfull ..."
    rm -f cbull.tar
  else
    msg="ABNORMAL EXIT: ERROR IN BULLETIN UNTAR"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '****************************************** '
    echo '*** ERROR : ERROR IN BULLETIN TAR FILE *** '
    echo '****************************************** '
    echo ' '
    echo "$modID pgen $date $cycle : bulletin untar error." >> $wavelog
    echo $msg
    set $seton
    err=3;export err;err_chk
  fi
# 1.c Spectral files ( AFOS bulletins )

  echo "   Copying spectral data from $COMIN"
  cp $COMIN/$modID.$cycle.spec_tar spec.tar

  if [ ! -f spec.tar ]
  then
    msg="ABNORMAL EXIT: NO BULLETIN TAR FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '************************************ '
    echo '*** ERROR : NO SPECTRAL TAR FILE *** '
    echo '************************************ '
    echo ' '
    echo "$modID pgen $date $cycle : spectral tar missing." >> $wavelog
    echo $msg
    set $seton
    err=4;export err;err_chk
  fi

  echo "   Untarring spectral data ..."
  tar -xf spec.tar
  OK=$?

  if [ "$OK" = '0' ]
  then
    echo "      Unpacking successfull ..."
    rm -f spec.tar
  else
    msg="ABNORMAL EXIT: ERROR IN SPECTRAL DATA UNTAR"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : ERROR IN SPECTRAL DATA TAR FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modID pgen $date $cycle : bulletin untar error." >> $wavelog
    echo $msg
    set $seton
    err=5;export err;err_chk
  fi

# 1.d Output locations file and other raw files

  rm -f buoy.loc mod_def.ww3 out_grd.ww3

  if [ -f $FIXwave/wave_$modID.buoys ]
  then
    cp $FIXwave/wave_$modID.buoys buoy.loc.temp
    sed -n '/^\$.*/!p' buoy.loc.temp > buoy.loc
    rm -f buoy.loc.temp
  fi

  if [ -f buoy.loc ]
  then
    echo "   buoy.loc copied and processed ($FIXwave/wave_$modID.buoys)."
  else
    msg="ABNORMAL EXIT: NO BUOY LOCATION FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '************************************* '
    echo '*** ERROR : NO BUOY LOCATION FILE *** '
    echo '************************************* '
    echo ' '
    echo "$modID pgen $date $cycle : buoy location file missing." >> $wavelog
    echo $msg
    set $seton
    err=6;export err;err_chk
  fi

  if [ -f $FIXwave/wave_$modID.moddef ]
  then
    cp $FIXwave/wave_$modID.moddef mod_def.ww3
  fi

  if [ -f mod_def.ww3 ]
  then
    echo "   mod_def.ww3 copied ($FIXwave/wave_$modID.moddef)."
  else
    msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '**************************************** '
    echo '*** ERROR : NO MODEL DEFINITION FILE *** '
    echo '**************************************** '
    echo ' '
    echo "$modID pgen $date $cycle : model defintion file missing." >> $wavelog
    echo $msg
    set $seton
    err=6;export err;err_chk
  fi

  if [ -f $COMIN/$modID.$cycle.outgrd ]
  then
    cp $COMIN/$modID.$cycle.outgrd out_grd.ww3
  fi

  if [ -f out_grd.ww3 ]
  then
    echo "   out_grd.ww3 copied ($COMIN/$modID.$cycle.outgrd)."
  else
    msg="ABNORMAL EXIT: NO RAW GRID FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************** '
    echo '*** ERROR : NO RAW GRID FILE *** '
    echo '******************************** '
    echo ' '
    echo "$modID pgen $date $cycle : raw grid file missing." >> $wavelog
    echo $msg
    set $seton
    err=6;export err;err_chk
  fi

# 1.e Input template files

  if [ -f $PARMgraph/grib_awp$modID ]
  then
    cp $PARMgraph/grib_awp$modID awipsgrb.data
  fi

  if [ -f awipsgrb.data ]
  then
    echo "   awipsgrb.data copied."
  else
    msg="ABNORMAL EXIT: NO AWIPS GRIB HEADER DATA FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** ERROR : NO AWIPS GRIB DATA FILE *** '
    echo '*************************************** '
    echo ' '
    echo "$modID pgen $date $cycle : GRIB header data file  missing." >> $wavelog
    echo $msg
    set $seton
    err=7;export err;err_chk
  fi

  if [ -f $PARMgraph/bull_awp$modID ]
  then
    cp $PARMgraph/bull_awp$modID awipsbull.data
  fi

  if [ -f awipsbull.data ]
  then
    echo "   awipsbull.data copied."
  else
    msg="ABNORMAL EXIT: NO AWIPS BULLETIN HEADER DATA FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************************* '
    echo '*** ERROR : NO AWIPS BULLETIN DATA FILE *** '
    echo '******************************************* '
    echo ' '
    echo "$modID pgen $date $cycle : Bulletin header data file  missing." >> $wavelog
    echo $msg
    set $seton
    err=7;export err;err_chk
  fi

  if [ -f $FIXwave/wavegrib.inp.tmpl ]
  then
    cp $FIXwave/wavegrib.inp.tmpl wavegrib.inp.tmpl
  fi

  if [ -f wavegrib.inp.tmpl ]
  then
    echo "   wavegrib.inp.tmpl copied."
  else
    msg="ABNORMAL EXIT: NO GRIB INPUT TEMPLATE FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR GRIB INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modID pgen $date $cycle : GRIB input template file missing." >> $wavelog
    echo $msg
    set $seton
    err=7;export err;err_chk
  fi

# 1.f Fixed and PARM files for graphics products

if [ "$difax" = 'yes' ] || [ "$afosbull" = 'yes' ]
then

  if [ -f $FIXgraph/mr3001.pur ]
  then
    cp $FIXgraph/mr3001.pur .
  fi

  if [ -f $FIXutil/graph_pillist1 ]
  then
    cp $FIXutil/graph_pillist1 .
  fi

  if [ -f $PARMgraph/graph_opcwndmp.${modID}_$cyc ]
  then
    cp $PARMgraph/graph_opcwndmp.${modID}_$cyc opcwndmp.data
  fi

  if [ -f $PARMgraph/graph_afwavplt.${modID}_$cyc ]
  then
    cp $PARMgraph/graph_afwavplt.${modID}_$cyc afwavplt.data
  fi

  if [ -f $PARMgraph/graph_nhwavplt.${modID}_$cyc ]
  then
    cp $PARMgraph/graph_nhwavplt.${modID}_$cyc nhwavplt.data
  fi

  if [ -f $PARMgraph/graph_afwndplv.${modID}_$cyc ]
  then
    cp $PARMgraph/graph_afwndplv.${modID}_$cyc afwndplv.data
  fi

  if [ -f $PARMgraph/mkfosgrb.${modID} ]
  then
    cp $PARMgraph/mkfosgrb.${modID} mkfosgrb.data
  fi

  for file in mr3001.pur graph_pillist1 \
              opcwndmp.data afwavplt.data nhwavplt.data afwndplv.data mkfosgrb.data
  do
    if [ -f $file ]
    then
      echo "   $file copied."
    else
      msg="ABNORMAL EXIT: FILE $file NOT FOUND"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '**************************************** '
      echo "*** ERROR : FILE $file NOT FOUND *** "
      echo '**************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : file $file missing." >> $wavelog
      echo $msg
      set $seton
      err=8;export err;err_chk
    fi
  done

fi

# 1.g Data summary

  set $setoff
  echo ' '
  echo '   All data files accounted for.'
  echo ' '
  set $seton

# --------------------------------------------------------------------------- #
# 2.  AWIPS product generation                                Tolman / Gerald
# 2.a AWIPS GRIB file with headers

  if [ "$awipsgrib" = 'yes' ]
  then

    set $setoff
    echo ' '
    echo 'AWIPS headers to GRIB file ...'
    echo '------------------------------'
    set $seton

# ------ FIX FOR AWIPS MODEL ID, GENERATE NEW GRIB FILE -----

    set $setoff
    echo "   Make alternative GRIB file."
    set $seton

    mv gribfile gribfile.OK

    export dtgrib=21600.   # GRIB field time step
    export ngrib=31        # Number of GRIB fields
    export gribflags=' F F T F F  T F T T F  T T T T F  F F F'
    export GRIDNR=233      # Assigned NCEP number for grid
    export MODNR=10        # AWIPS assumed NCEP number for model

    SENDCOMsave=$SENDCOM
    SENDCOM=NO

    $USHwave/wavegrib.sh > grib.out 2>&1

    SENDCOM=$SENDCOMsave

    if [ -d grib ]
    then
      cat grib.out
      msg="ABNORMAL EXIT: ERROR IN wavegrib.sh"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************ '
      echo "*** ERROR : ERROR IN wavegrib.sh *** "
      echo '************************************ '
      echo ' '
      echo "$modID pgen $date $cycle : error in wavegrib.sh." >> $wavelog
      echo $msg
      set $seton
      err=19;export err;err_chk
    fi

    rm -f grib.out
    mv $modID.$cycle.grib gribfile

# -------------------- END FIX ------------------------------

# 2.a.1 Set up for tocgrib

    set $setoff
    echo "   Do set up for tocgrib."
    set $seton

    AWIPSGRB=awipsgrib

# 2.a.2 Make GRIB index

    set $setoff
    echo "   Make GRIB index for tocgrib."
    set $seton

    $utilexec/grbindex gribfile gribindex
    OK=$?

    if [ "$OK" != '0' ]
    then
      msg="ABNORMAL EXIT: ERROR IN grbindex NWW3"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '******************************************** '
      echo '*** FATAL ERROR : ERROR IN grbindex NWW3 *** '
      echo '******************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : error in grbindex." >> $wavelog
      echo $msg
      set $seton
      err=20;export err;err_chk
    fi

# 2.a.3 Run AWIPS GRIB packing program tocgrib

    set $setoff
    echo "   Run tocgrib"
    set $seton

    export pgm=tocgrib
    export pgmout=tocgrib.out
    . prep_step

    export XLFUNIT_11="gribfile"
    export XLFUNIT_31="gribindex"
    export XLFUNIT_51="$AWIPSGRB"

    $utilexec/tocgrib < awipsgrb.data parm='KWBJ' > tocgrib.out 2>&1
    OK=$?

    if [ "$OK" != '0' ]
    then
      cat tocgrib.out
      msg="ABNORMAL EXIT: ERROR IN tocgrib"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************** '
      echo '*** FATAL ERROR : ERROR IN tocgrib *** '
      echo '************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : error in tocgrib." >> $wavelog
      echo $msg
      set $seton
      err=21;export err;err_chk
    fi

# 2.a.4 Get the AWIPS grib bulletin out ...
 
    set $setoff
    echo "   Get awips GRIB bulletins out ..."
    set $seton

    if [ "$SENDCOM" = 'YES' ]
    then
      echo "      Saving $AWIPSGRB as $AWIPSGRB.$job"
      echo "          in $PCOM"
      cp $AWIPSGRB $PCOM/$AWIPSGRB.$job
    fi

    if [ "$SENDDBN" = 'YES' ]
    then
      echo "      Sending $AWIPSGRB to DBNET."
      $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $PCOM/$AWIPSGRB.$job
    fi

    rm -f $AWIPSGRB tocgrib.out

# ------ FIX FOR AWIPS MODEL ID, BACK TO OLD GRIB FILE ------

    set $setoff
    echo "   Back to previous GRIB file."
    set $seton

    mv gribfile.OK gribfile
    rm -f gribindex

# -------------------- END FIX ------------------------------

  fi

  rm -f wavegrib.inp.tmpl mod_def.ww3 out_grd.ww3

# 2.b AWIPS bulletins for output points

  if [ "$awipsbull" = 'yes' ]
  then

    set $setoff
    echo ' '
    echo 'AWIPS bulletins ...'
    echo '-------------------'
    echo '   Sourcing data file with header info ...'
    set $seton

# 2.b.1 Set up environment variables

    . awipsbull.data

# 2.b.2 Generate list of bulletins to process

    set $setoff
    echo '   Generating buoy list ...'
    set $seton

    bulls=`sed -e 's/export b//g' -e 's/=/ /' awipsbull.data | awk '{ print $1}'`

# 2.b.3 Looping over buoys running formbul

    set $setoff
    echo '   Looping over buoys ...'
    set $seton

    for bull in $bulls
    do
      bname="b$bull"
      fname="$modID.$bull.cbull"
      oname="awipsbull.$bull.$job"
      headr=`grep "b${bull}=" awipsbull.data | sed 's/=/ /g' |  awk '{ print $3}'`

      set $setoff
      echo "      Processing $bull ($headr $oname) ..."
      set $seton

      if [ -z "$headr" ] || [ ! -s $fname ]
      then
        msg="ABNORMAL EXIT: MISSING BULLETING INFO"
        postmsg "$jlogfile" "$msg"
        set $setoff
        echo ' '
        echo '******************************************** '
        echo '*** FATAL ERROR : MISSING BULLETING INFO *** '
        echo '******************************************** '
        echo ' '
        echo "$modID pgen $date $cycle : Missing bulletin data." >> $wavelog
        echo $msg
        set $seton
        err=22;export err;err_chk
      fi

      $utilities/formbul.pl -d $headr -f $fname -j $job -m $modID \
           -p $PCOM -s NO -o $oname > formbul.out 2>&1

      OK=$?

      if [ "$OK" != '0' ]
      then
        cat formbul.out
        msg="ABNORMAL EXIT: ERROR IN formbul"
        postmsg "$jlogfile" "$msg"
        set $setoff
        echo ' '
        echo '************************************** '
        echo '*** FATAL ERROR : ERROR IN formbul *** '
        echo '************************************** '
        echo ' '
        echo "$modID pgen $date $cycle : error in formbul." >> $wavelog
        echo $msg
        set $seton
        err=23;export err;err_chk
      fi

      cat $oname >> awipsbull.$job
      rm -f formbul.out $oname

    done
    if [ $SENDCOM = "YES" ] ; then
      cp  awipsbull.$job $PCOM/awipsbull.$job
      if [ $SENDDBN = "YES" ] ; then
        $utilities/make_ntc_bull.pl  WMOBH NONE KWBJ NONE $DATA/awipsbull.$job $PCOM/awipsbull.$job
      fi
    fi

  fi

# --------------------------------------------------------------------------- #
# 3.  Old graphics producs                           Chen / Caruso / Gilbert

  if [ "$difax" = 'yes' ]
  then

    export XLFRTEOPTS="unit_vars=no"

    set $setoff
    echo ' '
    echo 'Old graphics products ...'
    echo '-------------------------'

# 3.a From NWW3 grib to WAM ASCII                                       Chen
# 3.a.1 Initializations

    echo '   Convert NWW3 grib to WAM ascii.'
    echo '      Time stamp all files'
    set $seton

    hour='0 6 12 18 24 30 36 42 48 54 60 66 72'
    para='WDIR WIND UGRD VGRD HTSGW WVDIR WVPER DIRPW PERPW DIRSW PERSW'

    ymdhm=`echo $YMDH | cut -c1-10`00

    for ip in $para
    do
      echo $ymdhm > wam.T${cyc}Z.${ip}
    done

    set $setoff
    echo '      Make GRIB index file.'
    Set $seton

    $utilexec/wgrib -s gribfile > gribindex
    OK=$?

    if [ "$OK" != '0' ]
    then
      msg="ABNORMAL EXIT: ERROR IN wgrib (AFOS INDEX)"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '*************************************************** '
      echo '*** FATAL ERROR : ERROR IN wgrib FOR AFOS INDEX *** '
      echo '*************************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : error in wgrib (sec 3.a.1)." >> $wavelog
      echo $msg
      set $seton
      err=30;export err;err_chk
    fi

# 3.a.2 Loop over hours

    for ih in $hour
    do
      if [ $ih -eq 0 ] ; then
        ihr='anl'
      else
        ihr=${ih}hr
      fi

      set $setoff
      echo "      Processing for hour $ih ($ihr)."
      set $seton

# 3.a.3 Loop over pars

      for ip in $para
      do
        if [ $ip = 'WDIR' -o $ip = 'WVDIR' -o $ip = 'DIRPW' -o $ip = 'DIRSW' ]
        then
          idir='1'
        else
          idir='0'
        fi

# 3.a.4 Unpack using wgrib

#       set $setoff
#       echo "         Processing $ip with idir=$idir"
#       set $seton

        grep ":${ip}:sfc:${ihr}" gribindex | \
             $utilexec/wgrib -i gribfile -o nww3.data > wgrib.out 2>&1
        OK=$?

        if [ -f nww3.data ]
        then
          fsize=`ls -l nww3.data | awk '{ print $5 }'`
        else
          fsize=0
        fi

        if [ "$OK" != '0' ] || [ ! -f nww3.data ] || [ "$fsize" -lt '100' ]
        then
          cat wgrib.out
          msg="ABNORMAL EXIT: ERROR IN wgrib ip=$ip idir=$idir"
          postmsg "$jlogfile" "$msg"
          set $setoff
          echo ' '
          echo '*************************************************** '
          echo "*** FATAL ERROR : ERROR IN wgrib FOR ip=$ip and idir=$idir"
          echo '*************************************************** '
          echo ' '
          echo "$modID pgen $date $cycle : error in wgrib (sec 3.a.4)." >> $wavelog
          echo $msg
          set $seton
          err=31;export err;err_chk
        fi

        rm -f wgrib.out

# 3.a.5 Converting to WAM grid

#       set $setoff
#       echo "         Converting to WAM grid."
#       set $seton

        ln -sf nww3.data fort.11
        ln -sf wam.data  fort.51

        echo "$ih $idir" | $EXECwave/wave_nww2wam > nww2wam.out 2>&1
        OK=$?

        if [ "$OK" != '0' ] || [ ! -f wam.data ]
        then
          cat nww2wam.out
          msg="ABNORMAL EXIT: ERROR IN nww3wam ih=$ih idir=$idir"
          postmsg "$jlogfile" "$msg"
          set $setoff
          echo ' '
          echo '*************************************************** '
          echo "*** FATAL ERROR : ERROR IN nww2wam FOR ih=$ih and idir=$idir"
          echo '*************************************************** '
          echo ' '
          echo "$modID pgen $date $cycle : error in nww2wam." >> $wavelog
          echo $msg
          set $seton
          err=32;export err;err_chk
        fi

        cat wam.data >> wam.T${cyc}Z.${ip}

        rm -f nww2wam.out
        rm -f fort.*
        rm -f nww3.data wam.data
    
# ..... End of loops 3.a.2 and 3.a.3

      done
    done

# 3.b From WAM ASCII to WAM grib                                        Chen
# 3.b.1 GRIB file

    set $setoff
    echo '   Convert WAM ascii to grib.'
    set $seton

    ln -sf  ./wam.T${cyc}Z.WIND   fort.21
    ln -sf  ./wam.T${cyc}Z.WDIR   fort.22
    ln -sf  ./wam.T${cyc}Z.UGRD   fort.23
    ln -sf  ./wam.T${cyc}Z.VGRD   fort.24
    ln -sf  ./wam.T${cyc}Z.HTSGW  fort.25
    ln -sf  ./wam.T${cyc}Z.WVPER  fort.26
    ln -sf  ./wam.T${cyc}Z.WVDIR  fort.27
    ln -sf  ./wam.T${cyc}Z.PERPW  fort.28
    ln -sf  ./wam.T${cyc}Z.DIRPW  fort.29
    ln -sf  ./wam.T${cyc}Z.PERSW  fort.30
    ln -sf  ./wam.T${cyc}Z.DIRSW  fort.31

    ln -sf  ./wam.grib            fort.61

    $EXECwave/wave_wamgrib > wamgrib.out 2>&1
    OK=$?

    if [ "$OK" != '0' ]
    then
      cat wamgrib.out
      msg="ABNORMAL EXIT: ERROR IN wamgrib."
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '**************************************'
      echo "*** FATAL ERROR : ERROR IN wamgrib ***"
      echo '**************************************'
      echo ' '
      echo "$modID pgen $date $cycle : error in wamgrib." >> $wavelog
      echo $msg
      set $seton
      err=33;export err;err_chk
    fi 

# 3.b.2 Index file

    set $setoff
    echo '   Make WAM GRIB index file.'
    set $seton

    $utilexec/grbindex wam.grib wam.gribindex
    OK=$?

    if [ "$OK" != '0' ]
    then
      msg="ABNORMAL EXIT: ERROR IN grbindex (WAM)"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************* '
      echo '*** FATAL ERROR : ERROR IN grbindex (WAM) *** '
      echo '********************************************* '
      echo ' '
      echo "$modID pgen $date $cycle : error in grbindex (sec 3.b.2)." >> $wavelog
      echo $msg
      set $seton
      err=34;export err;err_chk
    fi

# 3.b.3 Save files

    if [ "$SENDCOM" = 'YES' ]
    then
      echo "   Saving grib file as wam.$cycle.grib"
      echo "                    in $COMOUT."
      cp wam.grib $COMOUT/wam.$cycle.grib
    fi

# 3.b.4 Cleanup

    rm -f wamgrib.out
    rm -f fort.*
    rm -f wam.T${cyc}Z.*

# 3.g Job step MKFOSGRB                                              Gilbert
#
#     Covert WAM grib to other FOS GRIB grids.

    set $setoff
    echo '   Job step MKFOSGRB ...'
    set $seton

    FOSGRB=xtrn.fosopc

    export pgm=mkfosgrb
    export pgmout=mkfosgrb.out
    . prep_step ; set $setoff

    export XLFUNIT_11="wam.grib"
    export XLFUNIT_31="wam.gribindex"

    export XLFUNIT_51="$FOSGRB"
    
    export XLFRTEOPTS="unit_vars=yes"
    
    echo '      Running mkfosgrb ...'
    $EXECgraph/mkfosgrb < ./mkfosgrb.data parm='KWBC' > mkfosgrb.out 2>&1
    OK=$? 

    if [ "$OK" != '0' ] || [ ! -f $FOSGRB ]
    then
      cat mkfosgrb.out
      msg="ABNORMAL EXIT: ERROR IN mkfosgrb"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '*************************************** '
      echo '*** FATAL ERROR : ERROR IN mkfosgrb *** '
      echo '*************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : error in mkfosgrb." >> $wavelog
      echo $msg
      set $seton
      err=39;export err;err_chk
    fi

    if [ "$SENDCOM" = 'YES' ]
    then
      echo "      Saving $FOSGRB as $FOSGRB.$job"
      echo "          in $PCOM"
      cp $FOSGRB $PCOM/$FOSGRB.$job
     if [ "$SENDDBN" = 'YES' ]
     then
       echo "      Sending $FOSGRB to DBNET."
       $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $PCOM/$FOSGRB.$job
     fi
    fi

    rm -f $FOSGRB mkfosgrb.out 

# 3.h Final clean up.

    rm -f wam.grib wam.gribindex

  fi

  rm -f mr3001.pur opcwndmp.data
  rm -f graph_pillist1 afwavplt.data nhwavplt.data  afwndplv.data mkfosgrb.data

# --------------------------------------------------------------------------- #
# 4.  AFOS bulletins                                        Balasubramaniyan

  if [ "$afosbull" = 'yes' ]
  then

    set $setoff
    echo ' '
    echo 'AFOS bulletins ...'
    echo '------------------'

# 4.a Convert input to WAM format

    echo '   Convert NWW3 files to WAM format ...'
    set $seton

    export pgm=wave_inpforopc
    export pgmout=inpforopc.out
    . prep_step ; set $setoff

    export XLFUNIT_61="specanal"
    export XLFUNIT_62="specfcst"

    $EXECwave/wave_inpforopc > inpforopc.out 2>&1
    OK=$?

    if [ "$OK" != '0' ]
    then
      cat inpforopc.out
      msg="ABNORMAL EXIT: ERROR IN inpforopc"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '**************************************** '
      echo '*** FATAL ERROR : ERROR IN inpforopc *** '
      echo '**************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : error in inpforopc." >> $wavelog
      echo $msg
      set $seton
      err=40;export err;err_chk
    fi

    rm -f inpforopc.out

# 4.b Make bulletin using opcspctx

    set $setoff
    echo '   Generate spectral bulletin using opcspctx ...'
    set $seton

    AFOSBUL=opc.afos_bull

    export pgm=wave_opcspctx
    export pgmout=opcspctx.out
   . prep_step ; set $setoff

    export XLFUNIT_17="specanal"
    export XLFUNIT_18="specfcst"
    export XLFUNIT_58="$AFOSBUL"

    echo "'"  > quot
    echo $cyc > jcdata

    $EXECwave/wave_opcspctx > opcspctx.out 2>&1
    OK=$?

    if [ "$OK" != '0' ] || [ ! -f $AFOSBUL ]
    then
      cat opcspctx.out
      msg="ABNORMAL EXIT: ERROR IN opcspctx"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '*************************************** '
      echo '*** FATAL ERROR : ERROR IN opcspctx *** '
      echo '*************************************** '
      echo ' '
      echo "$modID pgen $date $cycle : error in opcspctx." >> $wavelog
      echo $msg
      set $seton
      err=41;export err;err_chk
    fi

# 4.c Send out the bulletin

    if [ "$SENDCOM" = 'YES' ]
    then
      echo "      Saving $AFOSBUL as $AFOSBUL.$job"
      echo "          in $PCOM"
      cp $AFOSBUL $PCOM/$AFOSBUL.$job
    fi

    if [ "$SENDDBN" = 'YES' ]
    then
      echo "      Sending $AFOSBUL to DBNET."
       $USHutil/make_ntc_bull.pl  WMOBH NONE KWNO NONE $DATA/$AFOSBUL.$job  $pcom/$AFOSBUL.$job 

    fi

    rm -f quot jcdata
    rm -f specanal specfcst
    rm -f opcspctx.out
    rm -f $AFOSBUL

  fi

# --------------------------------------------------------------------------- #
# 5.  Clean up

  rm -f gribfile gribindex awipsgrb.data awipsbull.data
  rm -f $modID.*.spec
  rm -f $modID.*.cbull
  rm -f buoy.loc

# --------------------------------------------------------------------------- #
# 6.  Ending output

  set $setoff
  echo ' '
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                *** End of NWW3 product generation ***'
  echo ' '

  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"

# End of NWW3 product generation script ------------------------------------- #
