#!/bin/sh
###############################################################################
#                                                                             #
# This script generates ASCII data files with the wave spectral data at full  #
# resolution for a given output point of a WAVEWATCH (NWW3) implementation    #
# or parallel. The location ID is passed as a shel script parameter.          #
#                                                                             #
# 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 spec_$loc which is      #
#   removed if this script exits normally.                                    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                            April 26, 1999   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

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

  rm -rf spec_$1
  mkdir spec_$1
  cd spec_$1

  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!       Make spectral file       |'
  echo '+--------------------------------+'
  echo "   Model ID        : $modID"
  set $seton

# 0.b Check if buoy location set

  if [ "$#" -lt '1' ]
  then
    set $setoff
    echo ' '
    echo '******************************************'
    echo '*** LOCATION ID IN wavespec.sh NOT SET ***'
    echo '******************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "LOCATION ID IN wavespec.sh NOT SET"
    exit 1
  else
    buoy=$1
    point=`grep $buoy ../log.ww3 | awk '{ print $1 }'`
    set $setoff
    echo "   Location ID/#   : $buoy ($point)"
    echo ' '
    set $seton
    if [ -z "$point" ]
    then
      set $setoff
      echo '*************************************************'
      echo '*** LOCATION ID IN wavespec.sh NOT RECOGNIZED ***'
      echo '*************************************************'
      echo ' '
      set $seton
      postmsg "$jlogfile" "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 "$dtspec" ] || [ -z "$EXECwave" ] || \
     [ -z "$modID" ] || [ -z "$utilexec" ]
  then
    set $setoff
    echo ' '
    echo '*************************************************'
    echo '*** EXPORTED VARIABLES IN wavespec.sh NOT SET ***'
    echo '*************************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "EXPORTED VARIABLES IN wavespec.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 wavespec."
  set $seton

  sed -e "s/TIME/$tstart/g" \
      -e "s/DT/$dtspec/g" \
      -e "s/POINT/$point/g" \
      -e "s/FORMAT/F/g" \
                               ../wavespec.inp.tmpl > wavespec.inp

# 2.b Run the postprocessor

  $EXECwave/wavespec
  err=$?

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

# --------------------------------------------------------------------------- #
# 3.  Clean up
# 3.a Move data to mother directory

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

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

# 3.b Clean up the rest

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

  cd ..
  rm -rf spec_$buoy

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

# End of wavespec.sh -------------------------------------------------------- #
