#!/bin/sh
###############################################################################
#                                                                             #
# This script is the product generator ("graphics job")  for the regional WNA #
# wave model.                                                                 #
#                                                                             #
# Remarks :                                                                   #
# - Supplemental error output is witten to the wave.log file.                 #
echo "  31 AUG 2005 Steve Lilly - Converted graphics from 6bit format"        #
echo "           to T4 format and rmoved snd2forgn processing."               #
#                                                                             #
#                                                                 May 2001    #
# Copied from nwprod 03/12/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 WNA PRODUCTS SCRIPT"
  postmsg "$jlogfile" "$msg"

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

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

# 0.b Date and time stuff

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

  set $setoff
  echo ' '
  echo '                         ***************************'
  echo '                         *** WNA PRODUCTS SCRIPT ***'
  echo '                         ***************************'
  echo "                                       $date $cycle"
  echo ' '
  echo "Starting at : `date`"
  echo ' '
  echo "   AWIPS grib fields : $awipsgrib"
  echo "   AWIPS bulletins   : $awipsbull"
  echo "   4-panel tiff map  : GEMPAK"
  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 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=5;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.e Fixed and PARM files for graphics products

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

  if [ -f $FIXutil/graph_gmbathy.dat ]
  then
    cp $FIXutil/graph_gmbathy.dat gmbathy.dat
  fi

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

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

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

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

  for file in gmbathy.dat graph_pillist1 pn1302.pur afwavplt.data gmexwvfx.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=6;export err;err_chk
    fi
  done

fi

# 1.f 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 WNA"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '******************************************** '
      echo '*** FATAL ERROR : ERROR IN grbindex WNA *** '
      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
      else
        if [ ${envir} = "para" ]
        then
           echo "Making NTC bulletin for parallel environment"
           $utilities/make_ntc_bull.pl  WMOBH NONE KWBC NONE $DATA/awipsbull.$job $PCOM/awipsbull.$job
        fi
      fi
    fi
    
  fi

# --------------------------------------------------------------------------- #
# 3.  Old graphics products


  if [ $difax = "yes" ]
  then

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

# 3.a Convert NWW3 data to AFOS data file.
# 3.a.1 Set-up

    echo "   Make AFOS data file from NWW3 data file ..."
    set $seton

# 3.a.2 Make GRIB index if necessary

    if [ ! -f gribindex ]
    then
      set $setoff
      echo "   Make WNA GRIB index for FAX charts."
      set $seton

      $utilexec/grbindex gribfile gribindex
      OK=$?

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

# 3.a.3 Running code to generate AFOS files

    set $setoff
    echo "      Generate AFOS data files ..."
    set $seton

    export pgm=wave_gmwvfax
    export pgmout=gmwvfax.out
    . prep_step ; set $setoff

    export XLFUNIT_10="gmbathy.dat"
    export XLFUNIT_11="gribfile"
    export XLFUNIT_12="gribindex"
    export XLFUNIT_51="afosfax1"
    export XLFUNIT_52="afosfax2"

    echo "$date $cyc" | $EXECgraph/graph_gmwvfax > gmwvfax.out 2>&1
    OK=$?

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

    rm -f gmwvfax.out

# 3.b Convert AFOS data into GMEX GRIB
# 3.b.1 AOFS to GRIB

    set $setoff
    echo "      Convert GMEX data into GRIB ..."
    set $seton

    echo "$cyc" > clk
    echo '10'   > inp
    echo '11'   > inp1
    echo "      $YMDH" > ncepdate
#   cp /com/date/$cycle  ncepdate

    export pgm=wave_gmexgrib
    export pgmout=gmexgrib.out
    . prep_step ; set $setoff

    export XLFUNIT_14="afosfax1"
    export XLFUNIT_8="inp"
    export XLFUNIT_9="clk"
    export XLFUNIT_10="ncepdate"
    export XLFUNIT_58="gmex.grib"

    $EXECgraph/graph_gmexgrib > gmexgrib.out 2>&1
    OK=$?

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

    rm -f gmexgrib.out

    . prep_step ; set $setoff

    export XLFUNIT_14="afosfax2"
    export XLFUNIT_8="inp1"
    export XLFUNIT_9="clk"
    export XLFUNIT_10="ncepdate"
    export XLFUNIT_58="gmex.moregrib"

    $EXECgraph/graph_gmexgrib > gmexgrib.out 2>&1
    OK=$?

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

    cat gmex.moregrib >> gmex.grib
    rm -f gmex.moregrib
    rm -f gmexgrib.out


# 3.b.2 Index file

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

    $utilexec/grbindex gmex.grib gmex.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 (GMEX) *** '
      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 gmex.$cycle.grib"
      echo "                    in $COMOUT."
      cp gmex.grib $COMOUT/gmex.$cycle.grib
    fi

# 3.b.4 Cleanup

    rm -f inp inp1 clk ncepdate
    rm -f afosfax1 afosfax2

  fi

# --------------------------------------------------------------------------- #
# 4.  Clean up

  rm -f gribfile gribindex awipsgrb.data awipsbull.data
  rm -f gmex.grib gmex.gribindex
  rm -f gmbathy.dat graph_pillist1 pn1302.pur afwavplt.data gmexwvfx.data
  rm -f $modID.*.cbull
  rm -f buoy.loc
  rm -f fort.*

# --------------------------------------------------------------------------- #
# 5.  Ending output

  set $setoff
  echo ' '
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                 *** End of WNA product generation ***'
  echo ' '
pwd
ls -l

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

# End of WNA product generation script -------------------------------------- #
