#!/bin/sh
###############################################################################
#                                                                             #
# This is the master script to generates GRIB2 file for the MWW3 forecast     #
#  model as the output is generated. The script is called from the master     #
#  script that starts the model run and is completed when the grib2 file      #
#  for the last step is generated or when the abort signal from the master    #
#  script is sent out. It runs a child script to generate grib2 files for     #
#  individual steps                                                           #
#                                                                             #
# Remarks :                                                                   #
# - The necessary files are retrieved by the mother script.                   #
# - See section 0.b for variables that need to be set.                        # 
#                                                                             #
#                                                                Jan, 2009    #
# Last update : 01-12-2009                                                    #
#                                                                             #
###############################################################################


#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  MODNR=11

  grdID=$1
  ymdh_end=$2

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

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

  if [ -z "$YMDH" ] || [ -z "$cycle" ] || [ -z "$EXECwave" ] || \
     [ -z "$COMOUT" ] || [ -z "$modID" ] || [ -z "$SENDCOM" ] || \
     [ -z "$dtgrib" ] || [ -z "$GRIBFLAGS" ] || [ -z "$grdID" ] || \
     [ -z "$SENDDBN" ] || [ -z "$INDRUN" ]
  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"
  tend="`echo $ymdh_end | cut -c1-8` `echo $ymdh_end | cut -c9-10`0000"

  dtgribh=`expr $dtgrib / 3600`
  set $setoff
  echo "   Starting time           : $tstart"
  echo "   Ending time             : $tend"
  echo "   Time step (hours)       : $dtgribh"
  echo "   GRIB field flags        : $GRIBFLAGS"
  echo "   Independent Run ?       : $INDRUN"
  echo ' '
  set $seton


# --------------------------------------------------------------------------- #
# 1.  Set up control loop

  while [ -f "grib2.$grdID.lock" ]
  do

    gribdone='yes'
    ymdh=$YMDH

    while [ "$ymdh" -le "$ymdh_end" ]
    do

      if [ ! -f "gribdone.$ymdh.$grdID" ]
      then

        case $grdID in
           glo_30mext ) par_grids='glo_30m ao_30m' ;;
        esac
	
	int_ready='yes'

        if [ "$INDRUN" = 'no' ]
        then
          for pgrdID in $par_grids
          do  
            if [ ! -f "Field_done.$ymdh.$pgrdID" ]
            then
              int_ready='no'
            fi
          done
        fi

	if [ "$int_ready" = 'yes' ]
        then
          set $setoff
	  echo "  Interpolating data for $grdID at $ymdh" 
          set $seton

          $USHwave/multiwavegrid_interp_single.sh $grdID $ymdh > grint_${grdID}_${ymdh}.out
 
          if [ -d "grint_${grdID}_${ymdh}" ]
          then
            set $setoff
            echo ' '
            echo '***********************************'
            echo '***ERROR IN INTERPOLATING GRIDS ***'
            echo '***********************************'
            postmsg "$jlogfile" " Interpolated GRID Files not generated for $grdID and $ymdh"
            sed "s/^/grint_${grdID}_${ymdh}.out :/g" grint_${grdID}_${ymdh}.out
            rm -f grint_${grdID}_${ymdh}.out
            gribdone='no'
            set $seton
          else
            rm -f grint_${grdID}_${ymdh}.out
          fi 

          if [ -f "out_grd.$grdID.$ymdh" ]
          then
            set $setoff
            echo "  Generating grib file for $grdID at $ymdh"
            set $seton
            
            case $grdID in
              glo_30mext ) GRIDNR=11 ;;
            esac

            rm -f out_grd.$grdID
            ln -sf out_grd.$grdID.$ymdh out_grd.$grdID

            $USHwave/multiwavegrib2_single.sh $grdID $ymdh $YMDH $GRIDNR $MODNR > grib2out_${grdID}_${ymdh}.out

            if [ -d "grib_${grdID}_${ymdh}" ]
            then
              set $setoff
              echo ' '
              echo '************************************'
              echo '***ERROR IN GENERATING GRIB FILES***'
              echo '************************************'
              postmsg "$jlogfile" "GRIB2 Files not generated for $grdID and $ymdh"
              sed "s/^/grib2out_${grdID}_${ymdh}.out :/g" grib2out_${grdID}_${ymdh}.out
              rm -f grib2out_${grdID}_${ymdh}.out
              gribdone='no'
              rm -f gribdone.$ymdh.$grdID 
              set $seton
            else
              rm -f grib2out_${grdID}_${ymdh}.out
            fi 

            typeset -Z3 hr=`$utilexec/nhour $ymdh $YMDH`

            #if [ ! -f "$modID.$grdID.$cycle.fh${hr}.grib2" ]
            if [ ! -f "$modID.$grdID.$cycle.f${hr}.grib2" ]
            then
              set $setoff
              echo ' '
              echo '************************************'
              echo '***ERROR IN GENERATING GRIB FILES***'
              echo '************************************'
              postmsg "$jlogfile" "GRIB2 Files not generated for $grdID and forecast hour $hr"
              rm -f gribdone.$ymdh.$grdID 
              gribdone='no'
            fi
          fi
        else
          gribdone='no'
        fi

      fi

      ymdh=`$utilexec/ndate $dtgribh $ymdh`

    done

    if [ "$gribdone" = 'yes' ]
    then
      rm -f grib2.$grdID.lock
    else
      sleep 30
    fi

  done 

# --------------------------------------------------------------------------- #
# 2.  Check to see if script completed cleanly

  if [ "$gribdone" = 'yes' ]
  then
    set $setoff
    echo ' '
    echo " Grib2 files for $grdID generated cleanly "
    set $seton
    touch grib2.$grdID.complete
  else 
    set $setoff
    echo ' '
    echo '************************************************************ '
    echo '*** FATAL ERROR : multiwavegrib2_sbs did not end cleanly *** '
    echo '************************************************************ '
    echo ' '
    set $seton
    postmsg "$jlogfile" "FATAL ERROR : ERROR IN multiwavegrib2"
  fi

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

# End of multiwavegrib2_sbs.sh -------------------------------------------------- #
