#!/bin/sh
###############################################################################
#                                                                             #
# This script generates the tabular bulletins for a single output points      #
# of  WAVEATCH MWW3 implemnatation. 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.                        #
#                                                                             #
#                                                            Feb 2008         #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA
  seton='+xa'
  setoff='+xa'
  set $seton

  rm -rf bull_$1
  mkdir bull_$1
  cd bull_$1

  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!     Make spectral bulletin     |'
  echo '+--------------------------------+'
  echo "   Model IE        : $modIE"
  set $seton

# 0.b Check if buoy location set

  if [ "$#" -lt '2' ]
  then
    set $setoff
    echo ' '
    echo '***********************************************'
    echo '*** LOCATION ID IN multiwavebullens.sh NOT SET ***'
    echo '***********************************************'
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "Error LOCATION ID IN multiwavebullens.sh NOT SET"
    exit 1
  else
    buoy=$1
    point=$2
    set $setoff
    echo "   Location ID/#   : $buoy (${point})"
    echo ' '
    set $seton
    if [ -z "$point" ]
    then
      set $setoff
      echo '*********************************************************'
      echo '*** LOCATION ID IN multiwavebullens.sh NOT RECOGNIZED ***'
      echo '*********************************************************'
      echo ' '
      set $seton
      ../postmsg "$jlogfile" "Error LOCATION ID IN multiwavebullens.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
    set $setoff
    echo ' '
    echo '******************************************************'
    echo '*** EXPORTED VARIABLES IN multiwavebullens.sh NOT SET ***'
    echo '******************************************************'
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "Error EXPORTED VARIABLES IN multiwavebullens.sh NOT SET"
    exit 3
  fi

# 0.d Starting time for output

  ymdh=`$utilexec/ndate -12 $YMDH`
  tstart="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"

  set $setoff
  echo "   Output starts at $tstart."
  echo ' '
  set $seton

# 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

  set $setoff
  echo "   Generate input file for multiwavespec"
  set $seton

  sed -e "s/TIME/$tstart/g" \
      -e "s/DT/$dtbull/g" \
      -e "s/POINT/$point/g" \
      -e "s/ITYPE/1/g" \
      -e "s/FORMAT/T/g" \
                               ../multiwavespec.inp.tmpl > multiwavespec.inp

# 2.b Run the postprocessor

  $EXECwave/multiwavespec
  err=$?

  if [ "$err" != '0' ]
  then
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : ERROR IN multiwavespec *** '
    echo '******************************************** '
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "FATAL ERROR : ERROR IN multiwavespec."
    exit 4
  fi

  outfile=ww3.`echo $tstart | cut -c3-8``echo $tstart | cut -c10-11`.spc

  if [ -f $outfile ]
  then
    mv $outfile  data
  else
    set $setoff
    echo ' '
    echo '************************************************ '
    echo '*** FATAL ERROR : OUTPUT DATA FILE NOT FOUND *** '
    echo '************************************************ '
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "FATAL ERROR : OUTPUT DATA FILE NOT FOUND"
    exit 5
  fi

# 2.c Clean up a little

  rm -f multiwavespec.inp
  rm -f mod_def.ww3 out_pnt.ww3

# --------------------------------------------------------------------------- #
# 3.  Generate bulletin
# 3.a Input file for bulletin generator

  set $setoff
  echo ' '
  echo "   Generate input file for multiwavebull"
  set $seton

  sed -e "s/BUOY/$buoy/g" \
      -e "s/DATE/$date/g" \
      -e "s/CYCLE/$cycle/g" \
      -e "s/MODEL/$modIE/g" \
                               ../multiwavebull.inp.tmpl > multiwavebull.inp

# 3.b Run the bulletin generator

  $EXECwave/multiwavebull < multiwavebull.inp
  err=$?

  if [ "$err" != '0' ]
  then
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : ERROR IN multiwavebull *** '
    echo '******************************************** '
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "FATAL ERROR : ERROR IN multiwavebull"
    exit 6
  fi

  if [ -f $modIE.$buoy.bull ] && [ -f $modIE.$buoy.cbull ] && [ -f $modIE.$buoy.csbull ]
  then
     mv $modIE.$buoy.bull ../. 
#     mv $modIE.$buoy.cbull ../. 
#     mv $modIE.$buoy.csbull ../.
  else
    set $setoff
    echo ' '
    echo '********************************************* '
    echo '*** FATAL ERROR : BULLETIN FILE NOT FOUND *** '
    echo '********************************************* '
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "FATAL ERROR : BULLETIN FILE NOT FOUND"
    exit 7
  fi

# 3.c Clean up

  rm -f data
  rm -f multiwavebull.inp

  cd ..
  rm -rf bull_$buoy

  set $setoff
  echo ' '
  echo 'End of multiwavebullens.sh at'
  date

# End of multiwavebullens.sh---------------------------------------------------- #
