#!/bin/sh
set +xa
###############################################################################
#                                                                             #
# This script generates the tabular bulletins for a single output points of   #
# a WAVEATCH (NWW3ens) run). It is run as a child script by a postprocessor.  #
#                                                                             #
# Remarks :                                                                   #
# - The necessary files are retrieved by the mother script.                   #
# - Shell script variables controling time, directories etc. are set in the   #
#   mother script.                                                            #
# - This script runs in the work directory designated in the mother script.   #
#   Under this directory it geneates a work directory bull_$loc which is      #
#   removed if this script exits normally.                                    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                            Dec. 29 2000     #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation
#
  cd $DATA
#
  rm -rf bull_$1
  mkdir bull_$1
  cd bull_$1
#
  echo ' '
  echo '+--------------------------------+'
  echo '|         wavebullens.sh         |'
  echo '!     Make spectral bulletin     |'
  echo '+--------------------------------+'
  echo "   Model ID        : $modIE"
#
# 0.b Check if buoy location set
#
  if [ "$#" -lt '1' ]
  then
    echo ' '
    echo '******************************************'
    echo '*** LOCATION ID IN wavespecens.sh NOT SET ***'
    echo '******************************************'
    echo ' '
    postmsg "$jlogfile" "Error LOCATION ID IN wavespecens.sh NOT SET"
    exit 1
  else
    buoy=$1
    point=`grep $buoy ../log.ww3 | awk '{ print $1 }'`
    echo "   Location ID/#   : $buoy ($point)"
    echo ' '
    if [ -z "$point" ]
    then
      echo '*************************************************'
      echo '*** LOCATION ID IN wavespecens.sh NOT RECOGNIZED ***'
      echo '*************************************************'
      echo ' '
      postmsg "$jlogfile" "Error LOCATION ID IN wavespec.sh NOT RECOGNIZED"
      exit 2
    fi
  fi
#
# 0.c Define directories and the search path.
#     The tested variables should be exported by the postprocessor script.
#
  if [ -z "$YMDH" ] || [ -z "$date" ] || [ -z "$cycle" ] || \
     [ -z "$dtbull" ] || [ -z "$EXECwave" ] || [ -z "$modIE" ] || \
     [ -z "$utilexec" ]
  then
    echo ' '
    echo '*************************************************'
    echo '*** EXPORTED VARIABLES IN wavespecens.sh NOT SET ***'
    echo '*************************************************'
    echo ' '
    postmsg "$jlogfile" "Error EXPORTED VARIABLES IN wavespecens.sh NOT SET"
    exit 3
  fi
#
# 0.d Starting time for output
#
##  ymdh=`$utilexec/ndate -12 $YMDH`
  ymdh=$YMDH
  tstart="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
#
  echo "   Output starts at $tstart."
  echo ' '
#
# 0.e Links to mother directory
#
  ln -s ../mod_def.ww3 .
  ln -s ../out_pnt.ww3 .

# --------------------------------------------------------------------------- #
# 2.  Generate spectral data file
# 2.a Input file for postprocessor
#
  echo "   Generate input file for wavespec"
#
  sed -e "s/TIME/$tstart/g" \
      -e "s/DT/$dtbull/g" \
      -e "s/POINT/$point/g" \
      -e "s/FORMAT/T/g" \
                               ../wavespec.inp.tmpl > wavespec.inp
#
# 2.b Run the postprocessor
#
  $EXECwave/wavespec
  err=$?
#
  if [ "$err" != '0' ]
  then
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN wavespec *** '
    echo '*************************************** '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : ERROR IN wavespec."
    exit 4
  fi
#
  outfile=ww3.`echo $tstart | cut -c3-8``echo $tstart | cut -c10-11`.spc
#
  if [ -f $outfile ]
  then
    mv $outfile  data
  else
    echo ' '
    echo '************************************************ '
    echo '*** FATAL ERROR : OUTPUT DATA FILE NOT FOUND *** '
    echo '************************************************ '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : OUTPUT DATA FILE NOT FOUND"
    exit 5
  fi
#
# 2.c Clean up a little
#
  rm -f wavespec.inp
  rm -f mod_def.ww3 out_pnt.ww3
#
# --------------------------------------------------------------------------- #
# 3.  Generate bulletin
# 3.a Input file for bulletin generator
#
  echo ' '
  echo "   Generate input file for wavebullens"
#
  sed -e "s/BUOY/$buoy/g" \
      -e "s/DATE/$date/g" \
      -e "s/CYCLE/$cycle/g" \
      -e "s/MODEL/$modIE/g" \
                               ../wavebull.inp.tmpl > wavebull.inp
#
# 3.b Run the bulletin generator
#
  $EXECwave/wavebull < wavebull.inp
  err=$?
#
  if [ "$err" != '0' ]
  then
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN wavebullens *** '
    echo '*************************************** '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : ERROR IN wavebullens"
    exit 6
  fi
#
  if [ -f $modIE.$buoy.bull ] && [ -f $modIE.$buoy.cbull ]
  then
    mv $modIE.$buoy.bull ..
    mv $modIE.$buoy.cbull ..
  else
    echo ' '
    echo '********************************************* '
    echo '*** FATAL ERROR : BULLETIN FILE NOT FOUND *** '
    echo '********************************************* '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : BULLETIN FILE NOT FOUND"
    exit 7
  fi
#
# 3.c Clean up
#
  rm -f data
  rm -f wavebull.inp
#
  cd ..
  rm -rf bull_$buoy
#
  echo ' '
  echo "End of wavebullens.sh at"
  date
#
# End of wavebullens.sh -------------------------------------------------------- #
