#!/bin/sh
###############################################################################
#                                                                             #
# This script is the product generator ("graphics job")  for the regional NAH #
# wave model.                                                                 #
#                                                                             #
# Remarks :                                                                   #
# - Supplemental error output is witten to the wave.log file.                 #
#                                                                             #
#                                                                June 2002    #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

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

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

  awipsgrib='yes'
  awipsbull='yes'

# 0.b Date and time stuff

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

  set $setoff
  echo ' '
  echo '                         ***************************'
  echo '                         *** NAH PRODUCTS SCRIPT ***'
  echo '                         ***************************'
  echo "                                       $date $cycle"
  echo ' '
  echo "Starting at : `date`"
  echo ' '
  echo "   AWIPS grib fields : $awipsgrib"
  echo "   AWIPS bulletins   : $awipsbull"
  echo ' '
  set $seton

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

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

# 1.a Grib file ( AWIPS )

  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 Output locations file

  rm -f buoy.loc

  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=4;export err;err_chk
  fi

# 1.d 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 template 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

# 1.d Data summary

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

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

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

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

# 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 NAH"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '******************************************** '
      echo '*** FATAL ERROR : ERROR IN grbindex NAH *** '
      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

  fi

# 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 KWBC NONE $DATA/awipsbull.$job $PCOM/awipsbull.$job
      fi
    fi

  fi

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

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

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

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

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

# End of NAH product generation script -------------------------------------- #
