#!/bin/sh
###############################################################################
#                                                                             #
# This script generates an archive wind field for a given time.               #
#                                                                             #
# Remarks :                                                                   #
# - This script runs in the work directory designated in the mother script in #
#   which it generates its own sub-directory 'wnd_yyyymmddhh'. If all is well #
#   the directory is removed at the end of the script.                        #
# - The time group yyyymmddhh is the first parameter passed to the script.    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                                May, 2007    #
#  Last Update : 05-29-2007                                                   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

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

  rm -rf wnd_$1
  mkdir wnd_$1
  cd wnd_$1

  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!    Make archive wind field     |'
  echo '+--------------------------------+'
  set $seton

# 0.b Check if time set

  if [ "$#" -lt '1' ]
  then
    set $setoff
    echo ' '
    echo '*******************************************'
    echo '*** TIME IN multiwavearcwind.sh NOT SET ***'
    echo '*******************************************'
    echo ' '
    set $seton
    exit 1
  else
    ymdh=$1
    time="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
    set $setoff
    echo "   Time                : $ymdh ($time)"
    echo ' '
    set $seton
  fi

# 0.c The tested variables should be exported by the calling script.

  if [ -z "$utilscript" ]
  then
    set $setoff
    echo ' '
    echo '**********************************'
    echo '*** EXPORTED VARIABLES NOT SET ***'
    echo '**********************************'
    echo ' '
    postmsg "$jlogfile" " EXPORTED VARIABLES NOT SET."
    exit 1
    set $seton
  fi

# --------------------------------------------------------------------------- #
# 1.  Run getges.sh

  $utilscript/getges.sh -q -t sigcur -r any -v $ymdh > getges.out 2> getges.err
  err=$?

  if [ "$err" != '0' ]
  then
    cat getges.out
    cat getges.err
    set $setoff
    echo ' '
    echo '********************************'
    echo '*** FATAL ERROR IN getges.sh ***'
    echo '********************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "ERROR IN getges.sh for $ymdh"
    exit 2
  fi

  spec_file=`cat getges.out | awk '{ print $1 }'`
  rm -f getges.out getges.err

# --------------------------------------------------------------------------- #
# 2.  Copy file

  set $setoff
  echo "   File for ${ymdh} : ${spec_file}"
  set $seton
  cp $spec_file swnd.$ymdh
  err=$?

  if [ "$err" != '0' ]
  then
    set $setoff
    echo ' '
    echo '********************************************'
    echo '*** FATAL ERROR IN COPYING SPECTRAL FILE ***'
    echo '********************************************'
    echo ' '
    postmsg "$jlogfile" "ERROR IN COPYING SPECTRAL FILE FOR $ymdh."
    set $seton
    exit 3
  fi

  ln -s ../sst.ww3

# --------------------------------------------------------------------------- #
# 4.  Run the wind packing program
# 4.a Defining SST grid parameters

  IDIML=720
  JDIML=361
  JCLIP=0

  echo '   Running decoder ...'

  echo "$IDIML, $JDIML,$JCLIP"                              > gfsinput
  echo "$time $time"                                       >> gfsinput

  cat gfsinput | $EXECwave/multiwavegfs
  err=$?

  if [ "$err" != '0' ]
  then
    set $setoff
    echo ' '
    echo '************************************** '
    echo "*** FATAL ERROR : ERROR IN ${modID}gfs *** "
    echo '************************************** '
    echo ' '
    set $seton
    exit 4
  fi

  if [ ! -f gfs.wind ]
  then
    set $setoff
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : gfs.wind NOT FOUND ***'
    echo '****************************************'
    echo ' '
    set $seton
    exit 5
  fi

# --------------------------------------------------------------------------- #
# 5.  Move file and clean up
# 5.a Move wind file

  echo ' '
  echo "   Moving file to ../raw/wind.$ymdh"

  mv gfs.wind ../raw/wind.$ymdh
  rm -f sst.ww3
  rm -f swnd.$ymdh

# 5.b Clean all up

  set $setoff
  echo ' '
  echo "   Removing work directory after success."
  set $seton

  cd ..
  rm -rf wnd_$ymdh

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

# End of multiwavearcwind.sh ------------------------------------------------- #
