#!/bin/sh
set +xa
###############################################################################
#                                                                             #
# This script generates the GRIB file for the NWW3ens global or regional wave #
# models. It is run as a child scipt interactively by the postprocessor.      #
#                                                                             #
# Remarks :                                                                   #
# - The necessary files are retrieved by the mother script.                   #
# - This script generates it own sub-directory 'grib'.                        # 
# - See section 0.b for variables that need to be set.                        # 
#                                                                             #
#                                                           September, 1999   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation
#
  cd $DATA
  postmsg "$jlogfile" "Making GRIB Files."
#
  rm -rf grib
  mkdir grib
  cd grib
#
# 0.b Define directories and the search path.
#     The tested variables should be exported by the postprocessor script.
#
  echo ' '
  echo '+--------------------------------+'
  echo '!         Make GRIB files        |'
  echo '+--------------------------------+'
##  echo "   Model ID         : $modID"
  echo "   Model ID         : $modIE"

  if [ -z "$YMDH" ] || [ -z "$cycle" ] || [ -z "$EXECwave" ] || \
     [ -z "$COMOUT" ] || [ -z "$modIE" ] || [ -z "$SENDCOM" ] || \
     [ -z "$dtgrib" ] || [ -z "$ngrib" ] || [ -z "$gribflags" ] || \
     [ -z "$GRIDNR" ] || [ -z "$MODNR" ]
  then
    echo ' '
    echo '***************************************************'
    echo '*** EXPORTED VARIABLES IN postprocessor NOT SET ***'
    echo '***************************************************'
    echo ' '
    postmsg "$jlogfile" "EXPORTED VARIABLES IN postprocessor NOT SET"
    exit 1
  fi
#
# 0.c Starting time for output
#
  ymdh=$YMDH
  tstart="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
#
  echo "   Starting time    : $tstart"
  echo "   Time step        : $dtgrib"
  echo "   Number of times  : $ngrib"
  echo "   GRIB field flags : $gribflags"
  echo ' '
#
# 0.d Links to working directory
#
  ln -s ../mod_def.ww3 .
  ln -s ../out_grd.ww3 .
#
# --------------------------------------------------------------------------- #
# 1.  Generate GRIB file with all data
# 1.a Generate input file for wavegrib
#     Template copied in mother script ...
#
  echo "   Generate input file for wavegrib"
#
  sed -e "s/TIME/$tstart/g" \
      -e "s/DT/$dtgrib/g" \
      -e "s/NT/$ngrib/g" \
      -e "s/GRIDNR/$GRIDNR/g" \
      -e "s/MODNR/$MODNR/g" \
      -e "s/FLAGS/$gribflags/g" \
                               ../wavegrib.inp.tmpl > wavegrib.inp
#
# 1.b Run GRIB packing program
#
  echo "   Run wavegrib"
#
##  ln -s ../$modID.$cycle.grib gribfile
  ln -s ../$modIE.$cycle.grib gribfile
  $EXECwave/wavegrib
  err=$?
#
  if [ "$err" != '0' ]
  then
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN wavegrib *** '
    echo '*************************************** '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : ERROR IN wavegrib"
    exit 3
  fi
#
# 1.c CLean up
#
  rm -f wavegrib.inp
  rm -f mod_def.ww3
  rm -f out_grd.ww3
#
# 1.d Save in /com
#
  if [ "$SENDCOM" = 'YES' ]
  then
##    echo "   Saving GRIB file as $COMOUT/$modID.$cycle.grib"
    echo "   Saving GRIB file as $COMOUT/$modIE.$cycle.grib"
    cp gribfile $COMOUT/$modIE.$cycle.grib
  fi 
#
# --------------------------------------------------------------------------- #
# 3.  Clean up the directory
#
  echo "   Removing work directory after success."
#
  cd ..
  rm -rf grib
#
  echo ' '
  echo 'End of wavegribens.sh at'
  date
#
# End of wavegribens.sh ----------------------------------------------------- #
