#!/bin/sh
set +xa
###############################################################################
#                                                                             #
# This script generates ASCII data files with the wave spectral data at full  #
# resolution for a given output point of a WAVEWATCH (NWW3ens) 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
#
  rm -rf spec_$1
  mkdir spec_$1
  cd spec_$1
#
  echo ' '
  echo '+--------------------------------+'
  echo '!       Make spectral file       |'
  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" "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" "LOCATION ID IN wavespecens.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 "$modIE" ] || [ -z "$utilexec" ]
  then
    echo ' '
    echo '*************************************************'
    echo '*** EXPORTED VARIABLES IN wavespecens.sh NOT SET ***'
    echo '*************************************************'
    echo ' '
    postmsg "$jlogfile" "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${ENS}."
#
  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
    echo ' '
    echo '*************************************** '
    echo "*** FATAL ERROR : ERROR IN wavespecens *** "
    echo '*************************************** '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : ERROR IN wavespecens"
    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  ../$modIE.$buoy.spec
  else
    echo ' '
    echo '***************************************************************** '
    echo '*** FATAL ERROR : OUTPUT DATA FILE FOR BOUY $bouy NOT FOUND *** '
    echo '***************************************************************** '
    echo ' '
    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
#
  echo ' '
  echo 'End of wavespecens.sh at'
  date
#
# End of wavespecens.sh -------------------------------------------------------- #
