#!/bin/sh
###############################################################################
#                                                                             #
# This script is the preprocessor for the GRL wave model that runs as a stand #
# alone model within the NWW3 suite. It sets some shell script variables for  #
# export to child scripts and copies some generally used files to the work    #
# directory. After this the actual preprocessing is performed by the          #
# following child scripts :                                                   #
#                                                                             #
#   grlice.sh     : preprocess ice fields.  *** NOT IMPLEMENTED ***           #
#   grlsst.sh     : preprocess sst fields.  *** NOT IMPLEMENTED ***           #
#   grlnam.sh     : find and copy nam files. *** NOT IMPLEMENTED ***          #
#                                                                             #
# Also used is the utililty script                                            #
#                                                                             #
#  wavestart.sh   : get time of most recent restart file.                     #
#                                                                             #
# Remarks :                                                                   #
# - The sst script generates files that are needed in the wind script.        #
# - For non-fatal errors output is witten to the wave.log file.               #
# - No ice for this model.                                                    #
#                                                                             #
#                                                                July, 2004   #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

  seton='-xa'
  setoff='+xa'
#  set $seton
  set -x

  msg="HAS BEGUN on `hostname`"
  postmsg "$jlogfile" "$msg"
  msg="Starting GRL PREPROCESSOR SCRIPT"
  postmsg "$jlogfile" "$msg"

#  set $setoff
  echo ' '
  echo '                      *******************************'
  echo '                      *** GRL PREPROCESSOR SCRIPT ***'
  echo '                      *******************************'
  echo ' '
  echo "Starting at : `date`"
#  set $seton

# Grid limits for ice data
  export GRID="267.8  284.1  327   40.91  49.1  235"             ###NCEP-ice domain

  export WGRD="235.0620 292.9370 464  25.0630   52.9380  224"  #Grid limits for NAM grid 110 

  export MP_PGMMODEL=mpmd
  export MP_CMDFILE=cmdfile

  lsth=84 
  ttime13=36 

# 0.b Date and time stuff
#     The ending time of the run always is the $lsth hour forecast. The starting
#     time depends on availablility of restart files, and is obtained with
#     wavestart.sh
#
#     Make sure nback is set identically in the forecast script !!!
#     nback is the number of cycles (12-hour cycles) to look back.

  export date=$PDY
  export YMDH=${PDY}${cyc}

  export nback=10

  $USHwave/wavestart.sh

  ymdh_beg=`head wavestart.out | awk '{ print $1 }'`
  rm -f wavestart.out
  time_beg="`echo $ymdh_beg | cut -c1-8` `echo $ymdh_beg | cut -c9-10`0000"

# NAM winds change from 1 to 3 hourly at this date stamp
  ymdh_newtres=`$utilexec/ndate $ttime13 $YMDH`

  ymdh_end=`$utilexec/ndate $lsth $YMDH`
  time_end="`echo $ymdh_end | cut -c1-8` `echo $ymdh_end | cut -c9-10`0000"

#  set $setoff
  echo ' '
  echo 'Times in wave model format :'
  echo '----------------------------'
  echo "   date / cycle  : $date $cycle"
  echo "   starting time : $time_beg"
  echo "   ending time   : $time_end"
  echo ' '

# --------------------------------------------------------------------------- #
# 1.  Get files that are used by most child scripts

  echo 'Preparing input files :'
  echo '-----------------------'
#  set $seton

# 1.a Model definition file

  if [ -f $FIXwave/wave_$modID.moddef ]
  then
    cp $FIXwave/wave_$modID.moddef mod_def.ww3
  fi

  if [ -f mod_def.ww3 ]
  then
    echo "   mod_def.ww3 copied ($FIXwave/wave_$modID.moddef)."
  else
    msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '********************************************** '
    echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
    echo '********************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : fixed file(s) missing." >> $wavelog
#    set $seton
    err=1;export err;err_chk
  fi

# 1.b Wind preprocessor template file

  if [ -f $FIXwave/waveprep.nam.tmpl ]
  then
    cp $FIXwave/waveprep.nam.tmpl .
  fi

  if [ -f waveprep.nam.tmpl ]
  then
    echo "   waveprep.nam.tmpl copied ($FIXwave/waveprep.nam.tmpl)."
  else
    msg="ABNORMAL EXIT: NO FILE waveprep.nam.tmpl"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : NO TEMPLATE FILE *** '
    echo '************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : template file missing." >> $wavelog
#    set $seton
    err=2;export err;err_chk
  fi

# 1.c Ice preprocessor template file
  if [ -f $FIXwave/waveprep.ice_grl.tmpl ]
  then
    cp $FIXwave/waveprep.ice_grl.tmpl ./waveprep.ice.tmpl
  fi
 
  if [ -f waveprep.ice.tmpl ]
  then
    echo "   waveprep.ice.tmpl copied ($FIXwave/waveprep.ice_grl.tmpl)."
  else
    msg="ABNORMAL EXIT: NO FILE waveprep.ice.tmpl"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : NO TEMPLATE FILE *** '
    echo '************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : template file missing." >> $wavelog
#    set $seton
    err=2;export err;err_chk
  fi
 
# 1.d ccopy the wave model mask file

  file=$FIXwave/wave_$modID.mask

  if [ -f $file ]
  then
    cp $file mask.ww3
  fi

  if [ -f mask.ww3 ]
  then
#    set $setoff
    echo "   mask.ww3 copied ($file)."
#    set $seton
  else
#    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** ERROR : NO WAVE MODEL MASK FILE *** '
    echo '*************************************** '
    echo ' '
#    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR - NO WAVE MODEL MASK FILE"
    exit 0
  fi

# 1.e Copy the ice mask file

  file=$FIXwave/waveice_grl.mask

  if [ -f $file ]
  then
    cp $file mask.ice
  fi

  if [ -f mask.ice ]
  then
#    set $setoff
    echo "   mask.ice copied ($file)."
#    set $seton
  else
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** ERROR : NO ICE FIELD MASK FILE *** '
    echo '************************************** '
    echo ' '
#    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR - NO ICE FIELD MASK FILE"
    exit 0
  fi

# 1.f NAM boost grid file 

  if [ -f $FIXwave/wave_boost.nam ]
  then
    cp $FIXwave/wave_boost.nam boost.nam 
  fi

  if [ -f boost.nam ]
  then
    echo "   boost.nam copied ($FIXwave/wave_boost.nam)."
  else
    msg="ABNORMAL EXIT: NO FILE boost.nam"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : NO BOOST.NAM FILE *** '
    echo '************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : wave_boost.nam file missing." >> $wavelog
#    set $seton
    err=2;export err;err_chk
  fi
# 1.g Distance gradient files
                                                                                                                   
  if [ -f $FIXwave/wave_dgradX.grl ]
  then
    cp $FIXwave/wave_dgradX.grl dgradX.grl
  fi
                                                                                                                   
  if [ -f dgradX.grl ]
  then
    echo "   dgradX.grl copied ($FIXwave/wave_dgradX.grl)."
  else
    msg="ABNORMAL EXIT: NO FILE dgradX.grl"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : NO DGRADX.GRL FILE *** '
    echo '************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : wave_dgradX.grl file missing." >> $wavelog
#    set $seton
    err=2;export err;err_chk
  fi
                                                                                                                   
  if [ -f $FIXwave/wave_dgradY.grl ]
  then
    cp $FIXwave/wave_dgradY.grl dgradY.grl
  fi
                                                                                                                   
  if [ -f dgradY.grl ]
  then
    echo "   dgradY.grl copied ($FIXwave/wave_dgradY.grl)."
  else
    msg="ABNORMAL EXIT: NO FILE wave_dgradY.grl"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : NO DGRADY.GRL FILE *** '
    echo '************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : wave_dgradY.grl file missing." >> $wavelog
#    set $seton
    err=2;export err;err_chk
  fi

# --------------------------------------------------------------------------- #
# 2.  Make command file(s) (ice, SST, coefficient files)
# 2.a Command file set-up
#     The command file points to $nfile files named cmdfile.$ifile.
#     The actual work is distributed over these files.

  ifile=1
  nfile=`echo $LOADL_PROCESSOR_LIST | wc -w | awk '{ print $1}'`
  iskip=1

  if [ "$nfile" -gt '1' ]
  then
    cmdtype='poe'
  else
    nfile=1
    cmdtype='sh'
    nskip='-'
  fi

#  set $setoff
  echo '   Making command file(s)'
  echo "   Set up command file structure (type = $cmdtype)."
  echo "      Number of command files  : $nfile"
#  set $seton

  while [ "$ifile" -le "$nfile" ]
  do
    touch cmdfile.$ifile
    chmod 700 cmdfile.$ifile
    echo "cmdfile.$ifile" >> cmdfile
    ifile=`expr $ifile + 1`
  done

  ifile=1

# 2.b Ice preprocessing

  echo "Ice preprocessing"
  ymdh=$ymdh_beg

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    echo "$USHwave/waveice_grl.sh $ymdh > ice_$ymdh.out 2>&1"     >> cmdfile.$ifile

    tinc=1
    if [ $ymdh -ge $ymdh_newtres -o $ymdh -lt $YMDH ]
    then
      tinc=3
    fi

    ymdh=`$utilexec/ndate $tinc $ymdh`

    ifile=`expr $ifile + 1`
    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
    fi

  done

# 2.c SST preprocessing
#     integrated into wavenam.sh for this model ...

# 2.d Sigma files

  ymdh=$ymdh_beg

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    echo "$USHwave/wavenam.sh $ymdh > nam_$ymdh.out 2>&1"     >> cmdfile.$ifile

    tinc=1
    if [ $ymdh -ge $ymdh_newtres -o $ymdh -lt $YMDH ]
    then
      tinc=3
    fi

    ymdh=`$utilexec/ndate $tinc $ymdh`

    ifile=`expr $ifile + 1`
    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
    fi
  done

# --------------------------------------------------------------------------- #
# 3   Execute command file
# 3.a Execution

#  set $setoff
  echo "   Executing command file."
#  set $seton

  if [ "$nfile" -gt '1' ]
  then
    poe
    exit=$?
  else
    cmdfile.1
    exit=$?
  fi

# 3.b Error trap on poe or shell

   if [ "$exit" != '0' ]
   then
     msg="ABNORMAL EXIT: ERROR IN $cmdtype"
     postmsg "$jlogfile" "$msg"
#     set $setoff
     echo ' '
     echo '**************************************** '
     echo '*** FATAL ERROR : ERROR IN poe OR sh *** '
     echo '**************************************** '
     echo ' '
     echo "$modID prep $date $cycle : error in $cmdtype." >> $wavelog
     echo $msg
#     set $seton
     err=3;export err;err_chk
   fi

# 3.c Timing info on steps

#  set $setoff
  echo ' '
  echo "   Ending times for sepate processors :"

  for file in `cat cmdfile`
  do
    if [ "`wc $file | awk '{ print $1 }'`" = '0' ]
    then
      echo "      $file : no commands in this file."
    else
      words="`tail -1 $file | wc | awk '{ print $2 }'`"
      wrdnr=`expr $words - 1`
      echo "{ print "'$'"$wrdnr }" > awkfile
      outfile="`tail -1 $file | awk -f awkfile`"
      rm -f awkfile
      echo "      $file : `tail -1 $outfile`"
    fi
  done
#  set $seton

# --------------------------------------------------------------------------- #
# 4.  Check for errors

#  set $setoff
  echo ' '
  echo '   Checking for errors.'
#  set $seton

# 4.a Ice field
#     We will go on if the number of errors in files is less
#     than err_max

  err_max=1
  echo '      Sources of nam-ice files :'
#  set $seton

  ymdh=$ymdh_beg
  nr_err=0

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    if [ -d nam_${ymdh} ]
    then
      echo "         File for $ymdh : error in waveice_grl.sh"
      postmsg "$jlogfile" "    File for $ymdh : error in waveice_grl.sh"
      nr_err=`expr $nr_err + 1`
      rm -f eice.$ymdh
    else
      namfile=`grep 'File for' ice_${ymdh}.out`
      if [ -z "$namfile" ]
      then
        echo "         File for $ymdh : cannot identify source"
        nr_err=`expr $nr_err + 1`
        rm -f eice.$ymdh
      else
        if [ ! -f eice.$ymdh ]
        then
          echo "         File for $ymdh : file not found"
          nr_err=`expr $nr_err + 1`
        else
          echo "      $namfile"
          rm -f ice_${ymdh}.out
        fi
      fi
    fi
    tinc=1
    if [ $ymdh -ge $ymdh_newtres -o $ymdh -lt $YMDH ]
    then
      tinc=3
    fi

    ymdh=`$utilexec/ndate $tinc $ymdh`
  done

  if [ -f ice_*.out ]
  then
#    set $setoff
    echo ' '
    echo '*******************************'
    echo '*** ERROR OUTPUT waveice_grl.sh ***'
    echo '*******************************'
    echo '            Possibly in multiple calls'
    echo "$modID prep $date $cycle : error in ice   files." >> $wavelog
    for file in ice_*.out
    do
      echo ' '
      sed "s/^/$file : /g" $file
    done
     rm -f ice_*.out
#    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR in waveice_grl.sh, possibly in multiple calls."
  fi

  if [ "$nr_err" -gt "$err_max" ]
  then
    msg="ABNORMAL EXIT: ERROR(S) IN NAM-ICE FILES"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '********************************************* '
    echo '*** FATAL ERROR : ERROR(S) IN NAM-ICE FILES *** '
    echo '********************************************* '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : fatal error in nam   files." >> $wavelog
#    set $seton
    err=5;export err;err_chk
  fi

#  rm -f cmdfile*

# 4.b SST field
# Integrated into wavenam.sh for this version

# 4.c Sigma files
#     We will go on if the number of errors in files is less
#     than err_max

  err_max=1

  echo '      Sources of nam-wind files :'
#  set $seton

  ymdh=$ymdh_beg
  nr_err=0

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    if [ -d nam_${ymdh} ]
    then
      echo "         File for $ymdh : error in wavenam.sh"
      postmsg "$jlogfile" "    File for $ymdh : error in wavenam.sh"
      nr_err=`expr $nr_err + 1`
      rm -f ewnd.$ymdh
    else
      namfile=`grep 'File for' nam_${ymdh}.out`
      if [ -z "$namfile" ]
      then
        echo "         File for $ymdh : cannot identify source"
        nr_err=`expr $nr_err + 1`
        rm -f ewnd.$ymdh
      else
        if [ ! -f ewnd.$ymdh ]
        then
          echo "         File for $ymdh : file not found"
          nr_err=`expr $nr_err + 1`
        else
          echo "      $namfile"
          rm -f nam_${ymdh}.out
        fi
      fi
    fi
    tinc=1
    if [ $ymdh -ge $ymdh_newtres -o $ymdh -lt $YMDH ]
    then
      tinc=3
    fi
                                                                                           
    ymdh=`$utilexec/ndate $tinc $ymdh`
  done

  if [ -f nam_*.out ]
  then
#    set $setoff
    echo ' '
    echo '*******************************'
    echo '*** ERROR OUTPUT wavenam.sh ***'
    echo '*******************************'
    echo '            Possibly in multiple calls'
    echo "$modID prep $date $cycle : error in nam   files." >> $wavelog
    for file in nam_*.out
    do
      echo ' '
      sed "s/^/$file : /g" $file
    done
    rm -f nam_*.out
#    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR in wavenam.sh, possibly in multiple calls."
  fi

  if [ "$nr_err" -gt "$err_max" ]
  then
    msg="ABNORMAL EXIT: ERROR(S) IN NAM-WIND FILES"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************************ '
    echo '*** FATAL ERROR : ERROR(S) IN NAM-WIND FILES *** '
    echo '************************************************ '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : fatal error in nam-wind files." >> $wavelog
#    set $seton
    err=5;export err;err_chk
  fi

  rm -f cmdfile*

# --------------------------------------------------------------------------- #
# 5.  Convert nam   files to final wind input file

# 5.a Get ice field into single file (wave_${modID}ice)

#  set $setoff
  echo ' '
  echo '   Extract ice  fields from nam files ...'
#  set $seton

  echo "$time_beg $time_end $ttime13" | $EXECwave/wave_${modID}ice
  err=$?

  if [ "$err" != '0' ]
  then
    msg="ABNORMAL EXIT: ERROR IN wave_${modID}ice"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '***************************************** '
    echo '*** FATAL ERROR : ERROR IN wave_${modID}ice *** '
    echo '***************************************** '
    echo ' '
    echo "$modID prep $date $cycle : error in wave_${modID}ice." >> $wavelog
#    set $seton
    err=6;export err;err_chk
  fi

  if [ ! -f ice.new ]
  then
    msg="ABNORMAL EXIT: FILE ice.new MISSING"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    cat ${modID}ice.out
    echo ' '
    echo '***************************************'
    echo '*** FATAL ERROR : ice.new NOT FOUND ***'
    echo '***************************************'
    echo ' '
    echo "$modID prep $date $cycle : ice.new missing." >> $wavelog
#    set $seton
    err=7;export err;err_chk
  fi

#5.b  Run waveprep for NAM/ice field

#  set $setoff
  echo ' '
  echo '   Running ice field through preprocessor.'
#  set $seton

  sed "s/GRIDLAYOUT/$GRID/g" waveprep.ice.tmpl > waveprep.inp
  rm -f waveprep.ice.tmpl
            
  $EXECwave/waveprep
  err=$?
            
  if [ "$err" != '0' ]
  then
    msg="ABNORMAL EXIT: ERROR IN waveprep"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN waveprep *** '
    echo '*************************************** '
    echo ' '
    echo "$modID prep $date $cycle : error in waveprep." >> $wavelog
#    set $seton
    err=8;export err;err_chk
  fi
            
  if [ ! -f ice.ww3 ]
  then
    msg="ABNORMAL EXIT: FILE ice.ww3 MISSING"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    cat waveprep.out
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : ice.ww3 NOT FOUND ***'
    echo '****************************************'
    echo ' '
    echo "$modID prep $date $cycle : ice.ww3 missing." >> $wavelog
#    set $seton
    err=9;export err;err_chk
  fi

  rm -f eice.*
  rm -f ice.new
  rm -f waveprep.inp

# 5.c Get into single file (wave_${modID}nam)

#   set $setoff
  echo ' '
  echo '   Extract wind and temperature fields from nam files ...'
#   set $seton

  echo "$time_beg $time_end $ttime13" | $EXECwave/wave_${modID}nam
  err=$?

  if [ "$err" != '0' ]
  then
    msg="ABNORMAL EXIT: ERROR IN wave_${modID}nam"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : ERROR IN wave_${modID}nam *** '
    echo '************************************** '
    echo ' '
    echo "$modID prep $date $cycle : error in wave_${modID}nam." >> $wavelog
#    set $seton
    err=6;export err;err_chk
  fi 

  if [ ! -f nam.wind ]
  then
    msg="ABNORMAL EXIT: FILE nam.wind MISSING"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    cat ${modID}nam.out
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : nam.wind NOT FOUND ***'
    echo '****************************************'
    echo ' '
    echo "$modID prep $date $cycle : nam.wind missing." >> $wavelog
#    set $seton
    err=7;export err;err_chk
  fi

  rm -f ewnd.*

# 5.d Run waveprep

#  set $setoff
  echo ' '
  echo '   Running wind fields through preprocessor.'
#  set $seton

  sed "s/GRIDLAYOUT/$WGRD/g" waveprep.nam.tmpl > waveprep.inp
  rm -f waveprep.nam.tmpl

  $EXECwave/waveprep
  err=$?

  if [ "$err" != '0' ]
  then
    msg="ABNORMAL EXIT: ERROR IN waveprep"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN waveprep *** '
    echo '*************************************** '
    echo ' '
    echo "$modID prep $date $cycle : error in waveprep." >> $wavelog
#    set $seton
    err=8;export err;err_chk
  fi

  if [ ! -f wind.ww3 ]
  then
    msg="ABNORMAL EXIT: FILE wind.ww3 MISSING"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    cat waveprep.out
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : wind.ww3 NOT FOUND ***'
    echo '****************************************'
    echo ' '
    echo "$modID prep $date $cycle : wind.ww3 missing." >> $wavelog
#    set $seton
    err=9;export err;err_chk
  fi

  rm -f nam.wind
  rm -f waveprep.inp

# 5.e Boost NAM winds 

#  set $setoff
  echo ' '
  echo '   Boosting NAM winds '
#  set $seton

  $EXECwave/wave_boostnam
  err=$?

  if [ "$err" != '0' ]
  then
    msg="ABNORMAL EXIT: ERROR IN wave_boostnam"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN wave_boostnam *** '
    echo '*************************************** '
    echo ' '
    echo "$modID prep $date $cycle : error in boostnam." >> $wavelog
#    set $seton
    err=8;export err;err_chk
  fi

  if [ ! -f windb.ww3 ]
  then
    msg="ABNORMAL EXIT: FILE wind.ww3 MISSING"
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo ' '
    echo '*******************************************************'
    echo '*** FATAL ERROR : windb.ww3 NOT FOUND after boostnam ***'
    echo '*******************************************************'
    echo ' '
    echo "$modID prep $date $cycle : windb.ww3 missing after wave_boostnam." >> $wavelog
#    set $seton
    err=9;export err;err_chk
  fi

# Replace wind with boosted wind
  mv -f windb.ww3 wind.ww3

  rm -f wave_boost.nam

# --------------------------------------------------------------------------- #
# 6.  Output to /com

  if [ "$SENDCOM" = 'YES' ]
  then
#    set $setoff
    echo ' '
    echo "   Saving wind.ww3 as $COMOUT/$modID.$cycle.wind"
    echo "   Saving ice.ww3 as $COMOUT/$modID.$cycle.ice"
#    set $seton
    cp wind.ww3 $COMOUT/$modID.$cycle.wind
    cp ice.ww3 $COMOUT/$modID.$cycle.ice
  fi 

  chmod 664 $COMOUT/$modID.$cycle.*

# --------------------------------------------------------------------------- #
# 7.  Alert the wind file

 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL WAVE_BINARY $job $COMOUT/$modID.$cycle.wind
 fi

#     Alert the ice file

 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL WAVE_BINARY $job $COMOUT/$modID.$cycle.ice
 fi

# --------------------------------------------------------------------------- #
# 8.  Ending output

#  set $setoff
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                     *** End of GRL preprocessor ***'
  echo ' '

  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"

# End of GRL preprocessor script -------- ----------------------------------- #
