#!/bin/sh
###############################################################################
#                                                                             #
# This script generates archive data for output locations for any of the      #
# WAVEWATCH (MWW3) implementations or parallels.                              #
#                                                                             #
# 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 generates a work directory arc_$loc which is      #
#   removed if this script exits normally.                                    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                                 May, 2007   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

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

  rm -rf arc_$1
  mkdir arc_$1
  cd arc_$1

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

# 0.b Check if location set

  if [ "$#" -lt '1' ]
  then
    set $setoff
    echo ' '
    echo '**************************************************'
    echo '*** LOCATION ID IN multiwavearcbuoy.sh NOT SET ***'
    echo '**************************************************'
    echo ' '
    set $seton
    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 multiwavearcbuoy.sh NOT RECOGNIZED ***'
      echo '*********************************************************'
      echo ' '
      set $seton
      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 "$EXECwave" ] || \
     [ -z "$modID" ] || [ -z "$utilexec" ] || \
     [ -z "$ombutil" ] || [ -z "$dttab" ]
  then
    set $setoff
    echo ' '
    echo '*********************************************************'
    echo '*** EXPORTED VARIABLES IN multiwavearcbuoy.sh NOT SET ***'
    echo '*********************************************************'
    echo ' '
    set $seton
    exit 3
  fi

# 0.d Starting time for output

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

  set $setoff
  echo "   Output starts at $tstart."
  echo "   Output interval ${dttab}s."
  echo ' '
  set $seton

# 0.e Links to mother directory

  ln -s ../mod_def.points mod_def.ww3 
  ln -s ../out_pnt.ww3 .

# --------------------------------------------------------------------------- #
# 2.  Generate table using multiwavespec
# 2.a Input file for postprocessor

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

  sed -e "s/OUTPUT_TIME/$tstart/g" \
      -e "s/DT/$dttab/g" \
      -e "s/POINT/$point/g" \
      -e "s/ITYPE/2/g" \
                               ../multiwavespec.arc.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
    exit 4
  fi

  if [ ! -f tab51.ww3 ]
  then
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : tab51.ww3 FILE MISSING *** '
    echo '******************************************** '
    echo ' '
    set $seton
    exit 5
  fi

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

# --------------------------------------------------------------------------- #
# 3.  Move file to raw data directory and clean up
# 3.a Move file

  echo ' '
  echo "   Moving file to ../raw/$buoy.arc"

  mv tab51.ww3 ../raw/$buoy.arc

# 4.b Clean up the rest

  cd ..
  rm -rf arc_$buoy

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

# End of multiwavearcbuoy.sh ------------------------------------------------ #
