#!/bin/sh
###############################################################################
#                                                                             #
# This script generates the interpolated data for the older grids             #
# in 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 'grint_*'.                     # 
# - See section 0.b for variables that need to be set.                        # 
# - The script is designed to generate interpolated files for a single step   #
#                                                                             #
#                                                             July 10, 2009   #
# Last update : 07-10-2009                                                    #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA
  seton='+xa'
  setoff='+xa'
  set $seton
  postmsg "$jlogfile" "Making GRID Interpolation Files."

  grdID=$1  
  ymdh=$2
  dt=$3
  nst=$4
  rm -rf grint_${grdID}_${ymdh}
  mkdir grint_${grdID}_${ymdh}
  cd grint_${grdID}_${ymdh}

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

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

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

# 0.c Links to files

  rm -f ../out_grd.$grdID
  
  if [ ! -f ../${grdID}_interp.inp.tmpl ]; then
    cp $FIXwave/${grdID}_interp.inp.tmpl ../.
  fi
  ln -sf ../${grdID}_interp.inp.tmpl . 

  for ID in $grids
  do
    ln -sf ../out_grd.$ID .
  done

  for ID in $grids $grdID 
  do
    ln -sf ../mod_def.$ID .
  done

# --------------------------------------------------------------------------- #
# 1.  Generate GRID file with all data
# 1.a Generate Input file

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

  sed -e "s/TIME/$time/g" \
      -e "s/DT/$dt/g" \
      -e "s/NSTEPS/$nst/g" ${grdID}_interp.inp.tmpl > multiwavegrid_interp.inp

# 1.b Run interpolation code

  set $setoff
  echo "   Run multiwavegrid_interp"
  set $seton

  $EXECwave/multiwavegrid_interp
  err=$?

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

# 1.b Clean up

#  rm -f grid_interp.inp
#  rm -f mod_def.*
  cp out_grd.$grdID ../out_grd.$grdID

# 1.c Save in /com

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo "   Saving GRID file as $COMOUT/$modID.$grdID.$cycle.outgrd"
    set $seton
    cp out_grd.$grdID $COMOUT/$modID.$grdID.$cycle.outgrd

#    if [ "$SENDDBN" = 'YES' ]
#    then
#      set $setoff
#      echo "   Alerting GRID file as $COMOUT/$modID.$grdID.$cycle.outgrd"
#      set $seton

#
# PUT DBNET ALERT HERE ....
#

#    fi
  fi 

# --------------------------------------------------------------------------- #
# 2.  Clean up the directory

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

  cd ..
  rm -rf grint_${grdID}_${ymdh}

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

# End of multiwavegrid_interp.sh -------------------------------------------- #
