#!/bin/sh
###############################################################################
#                                                                             #
# This script generates ASCII data files with the wave spectral data at full  #
# resolution for a given output point of WAVEWATCH (MWW3) implementation      #
# or parallel. The location ID and position 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.                        #
#                                                                             #
#                                                            March 12, 2007   #
# Last update : 03-15-2007                                                    #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 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 '2' ]
  then
    set $setoff
    echo ' '
    echo '***********************************************'
    echo '*** LOCATION ID IN multiwavespec.sh NOT SET ***'
    echo '***********************************************'
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "LOCATION ID IN multiwavespec.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 multiwavespec.sh NOT RECOGNIZED ***'
      echo '******************************************************'
      echo ' '
      set $seton
      ../postmsg "$jlogfile" "LOCATION ID IN multiwavespec.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 multiwavespec.sh NOT SET ***'
    echo '******************************************************'
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "EXPORTED VARIABLES IN multiwavespec.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/$dtspec/g" \
      -e "s/POINT/$point/g" \
      -e "s/ITYPE/1/g" \
      -e "s/FORMAT/F/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

# --------------------------------------------------------------------------- #
# 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 multiwavespec.inp
  rm -f mod_def.ww3 out_pnt.ww3

  cd ..
  rm -rf spec_$buoy

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

# End of multiwavespec.sh ---------------------------------------------------- #
