#!/bin/sh
###############################################################################
#                                                                             #
# This script generates the GRIB2 file for the MWW3 forecast model            #
# 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.                        # 
#                                                                             #
#                                                                July, 2007   #
# Last update : 07-06-2007                                                    #
#                                                                             #
###############################################################################

set -xa
#
# ... Define directories
#
export UTILexec=${UTILexec:-/nwprod/util/exec}
export EXECwave=${EXECwave:-/nwprod/exec}
######################################################################
# Binary aliases
######################################################################
export EXECutil_grib2index=${EXECutil_grib2index:-$UTILexec/grb2index}
export EXECwavesteepgrb2=${EXECwavesteepgrb2:-$EXECwave/wavesteepgrb2}
echo "exec = $EXECwavesteepgrb2"
export EXECmultiwavegrib2=${EXECmultiwavegrib2:-$EXECwave/multiwavegrib2}
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA
  seton='-xa'
#  setoff='+xa'
  setoff='-xa'
  set $seton
  postmsg "$jlogfile" "Making GRIB Files."

  grdID=$1  
  rm -rf grib_$grdID
  mkdir grib_$grdID
  cd grib_$grdID

# 0.b Define directories and the search path.
#     The tested variables should be exported by the postprocessor script.

  dtgrib=$2
  ngrib=$3
  GRIDNR=$4
  MODNR=$5
  gribflags=$6

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

  if [ -z "$YMDH" ] || [ -z "$cycle" ] || [ -z "$EXECwave" ] || \
     [ -z "$COMOUT" ] || [ -z "$modID" ] || [ -z "$SENDCOM" ] || \
     [ -z "$dtgrib" ] || [ -z "$ngrib" ] || [ -z "$gribflags" ] || \
     [ -z "$GRIDNR" ] || [ -z "$MODNR" ] || [ -z "$SENDDBN" ]
  then
    set $setoff
    echo ' '
    echo '***************************************************'
    echo '*** EXPORTED VARIABLES IN postprocessor NOT SET ***'
    echo '***************************************************'
    echo ' '
    ../postmsg "$jlogfile" "EXPORTED VARIABLES IN postprocessor NOT SET"
    exit 1
    set $seton
  fi

# 0.c Starting time for output

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

  set $setoff
  echo "   Starting time    : $tstart"
  echo "   Time step        : $dtgrib"
  echo "   Number of times  : $ngrib"
  echo "   GRIB field flags : $gribflags"
  echo ' '
  set $seton

# 0.d Links to working directory

  ln -s ../mod_def.$grdID mod_def.ww3
  ln -s ../out_grd.$grdID  out_grd.ww3 

# --------------------------------------------------------------------------- #
# 1.  Generate GRIB file with all data
# 1.a Generate input file for multiwavegrib2
#     Template copied in mother script ...

  set $setoff
  echo "   Generate input file for multiwavegrib2"
  set $seton

  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" \
                               ../multiwavegrib2.inp.tmpl > multiwavegrib2.inp

# 1.b Run GRIB packing program

  set $setoff
  echo "   Run multiwavegrib2"
  set $seton

  ln -sf ../$modID.$grdID.$cycle.grib2 gribfile
  $EXECmultiwavegrib2
  err=$?

  if [ "$err" != '0' ]
  then
    set $setoff
    echo ' '
    echo '********************************************* '
    echo '*** FATAL ERROR : ERROR IN multiwavegrib2 *** '
    echo '********************************************* '
    echo ' '
    set $seton
    ../postmsg "$jlogfile" "FATAL ERROR : ERROR IN multiwavegrib2"
    exit 3
  fi

# 1.c Clean up

  rm -f multiwavegrib2.inp
  rm -f mod_def.ww3
  rm -f out_grd.ww3

# 1.d Create a wave steepness grib2 file

# 1.d.i Create grib2 index file

  $EXECutil_grib2index gribfile gribfile_indx
  err=$?
  echo "err from grib2index = $err"

# 1.d.ii Run wave steepness executable

  ls -l gribfile
  export XLFUNIT_10="gribfile"
  export XLFUNIT_11="gribfile_indx"
  export XLFUNIT_20="wsteep.grib2"

  timex $EXECwavesteepgrb2 1> ft06 2> errfile
  err=$?
  echo "err from steep = $err"
  cp ft06 sav.ft06

# 1.e Save in /com

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo "   Saving GRIB file as $COMOUT/$modID.$grdID.$cycle.grib2"
    echo "   Saving wave steepness file as $COMOUT/$modID.wstp.$grdID.$cycle.grib2"
    set $seton
    cp gribfile $COMOUT/$modID.$grdID.$cycle.grib2
    cp wsteep.grib2 $COMOUT/$modID.wstp.$grdID.$cycle.grib2
    echo "   Creating wgrib index of $COMOUT/$modID.$grdID.$cycle.grib2"
    /nwprod/util/exec/wgrib2 -s $COMOUT/$modID.$grdID.$cycle.grib2 > $COMOUT/$modID.$grdID.$cycle.grib2.idx
    echo "   Creating wgrib index of $COMOUT/$modID.wstp.$grdID.$cycle.grib2"
    /nwprod/util/exec/wgrib2 -s $COMOUT/$modID.wstp.$grdID.$cycle.grib2 > $COMOUT/$modID.wstp.$grdID.$cycle.grib2.idx

    if [ "$SENDDBN" = 'YES' ]
    then
      set $setoff
      echo "   Alerting GRIB file as $COMOUT/$modID.$grdID.$cycle.grib2"
      echo "   Alerting GRIB index file as $COMOUT/$modID.$grdID.$cycle.grib2.idx"
      echo "   Alerting GRIB steepness file as $COMOUT/$modID.wstp.$grdID.$cycle.grib2"
      echo "   Alerting GRIB steepness index file as $COMOUT/$modID.wstp.$grdID.$cycle.grib2.idx"
      set $seton
      $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2 $job $COMOUT/$modID.$grdID.$cycle.grib2
      $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2_WIDX $job $COMOUT/$modID.$grdID.$cycle.grib2.idx
      $DBNROOT/bin/dbn_alert MODEL WAVE_STEEPNESS_GRIB_GB2 $job $COMOUT/$modID.wstp.$grdID.$cycle.grib2
      $DBNROOT/bin/dbn_alert MODEL WAVE_STEEPNESS_GRIB_GB2_WIDX $job $COMOUT/$modID.wstp.$grdID.$cycle.grib2.idx
    fi
  fi 

# --------------------------------------------------------------------------- #
# 3.  Clean up the directory

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

  cd ..
  rm -rf grib_$grdID

  set $setoff
  echo ' '
  echo "End of multiwavegrib2.sh at"
  date

# End of multiwavegrib2.sh -------------------------------------------------- #
