#!/bin/sh
###############################################################################
#                                                                             #
# This script generates the GRIB 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.                        # 
#                                                                             #
#                                                               March, 2007   #
# Last update : 03-15-2007                                                    #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA
  seton='-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 multiwavegrib1
#     Template copied in mother script ...

  set $setoff
  echo "   Generate input file for multiwavegrib1"
  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" \
                               ../multiwavegrib1.inp.tmpl > multiwavegrib1.inp

# 1.b Run GRIB packing program

  set $setoff
  echo "   Run multiwavegrib1"
  set $seton

  ln -s ../$modID.$grdID.$cycle.grib gribfile
  $EXECwave/multiwavegrib1
  err=$?

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

# 1.c Clean up

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

# 1.d Save in /com

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo "   Saving GRIB file as $COMOUT/$modID.$grdID.$cycle.grib"
    set $seton
    cp gribfile $COMOUT/$grdID.$cycle.grib
    $utilexec/cnvgrib -g12 -p40 gribfile grib2file
    $utilexec/wgrib2 -s grib2file >grib2file.idx
    mv grib2file $COMOUT/$grdID.$cycle.grib.grib2
    mv grib2file.idx $COMOUT/$grdID.$cycle.grib.grib2.idx

    if [ "$SENDDBN" = 'YES' ]
    then
      set $setoff
      echo "   Alerting GRIB2 file as $COMOUT/$grdID.$cycle.grib.grib2"
      set $seton
      if [ "$envir" = 'prod' ]
      then
        $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2 $job $COMOUT/$grdID.$cycle.grib.grib2
        $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2_WIDX $job $COMOUT/$grdID.$cycle.grib.grib2.idx
      else
        $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2_PARA $job $COMOUT/$grdID.$cycle.grib.grib2
        $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2_WIDX_PARA $job $COMOUT/$grdID.$cycle.grib.grib2.idx
      fi
    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 multiwavegrib1.sh at"
  date

# End of multiwavegrib1.sh -------------------------------------------------- #
