#!/bin/sh
###############################################################################
#                                                                             #
# This script is the preprocessor for the GRL multi-grid wave model that runs #
# using the single grid of the stand alone NWW# 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 :                                  #
#                                                                             #
#   multiwaveice_glw.sh    : preprocess ice fields.                           #
#   multiwavenam.sh        : find and copy nam files.                         #
#                                                                             #
# Also used is the utililty script                                            #
#                                                                             #
#  multiwavestart.sh   : get time of most recent restart file.                #
#                                                                             #
# Remarks :                                                                   #
#                                                                             #
#                                                                July, 2007   #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

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

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

  set $setoff
  echo ' '
  echo '                      ********************************'
  echo '                      *** GLW 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 

  grids='grl'

# 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
#     multiwavestart.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/multiwavestart.sh

  ymdh_beg=`head multiwavestart.out | awk '{ print $1 }'`
  rm -f multiwavestart.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

  for grdID in $grids
  do
    if [ -f $FIXwave/multiwave_$grdID.moddef ]
    then
      cp $FIXwave/multiwave_$grdID.moddef mod_def.$grdID
    fi

    if [ -f mod_def.$grdID ]
    then
      echo "   mod_def.$grdID copied ($FIXwave/multiwave_$grdID.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 $grdID prep $date $cycle : fixed file(s) missing." >> $wavelog
      set $seton
      err=1;export err;err_chk
    fi

  done

# 1.b Wind preprocessor template file

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

  if [ -f multiwaveprep.nam.tmpl ]
  then
    echo "   multiwaveprep.nam.tmpl copied ($FIXwave/multiwaveprep.nam.tmpl)."
  else
    msg="ABNORMAL EXIT: NO FILE multiwaveprep.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/multiwaveprep.ice_glw.tmpl ]
  then
    cp $FIXwave/multiwaveprep.ice_glw.tmpl ./multiwaveprep.ice.tmpl
  fi
 
  if [ -f multiwaveprep.ice.tmpl ]
  then
    echo "   multiwaveprep.ice.tmpl copied ($FIXwave/multiwaveprep.ice_glw.tmpl)."
  else
    msg="ABNORMAL EXIT: NO FILE multiwaveprep.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=3;export err;err_chk
  fi
 
# 1.d copy the wave model mask file

  file=$FIXwave/multiwave_grl.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 1
  fi

# 1.e Copy the ice mask file

  file=$FIXwave/multiwaveice_glw.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 2
  fi

# 1.f NAM boost grid file 

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

  if [ -f boost.nam ]
  then
    echo "   boost.nam copied ($FIXwave/multiwave_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 : boost.nam file missing." >> $wavelog
    set $seton
    err=4;export err;err_chk
  fi

# 1.g Distance gradient files
                                                                                                                   
  if [ -f $FIXwave/multiwave_dgradX.glw ]
  then
    cp $FIXwave/multiwave_dgradX.glw dgradX.glw
  fi
                                                                                                                   
  if [ -f dgradX.glw ]
  then
    echo "   dgradX.glw copied ($FIXwave/multiwave_dgradX.glw)."
  else
    msg="ABNORMAL EXIT: NO FILE dgradX.glw"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '**************************************** '
    echo '*** FATAL ERROR : NO DGRADX.GRL FILE *** '
    echo '**************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : multiwave_dgradX.glw file missing." >> $wavelog
    set $seton
    err=5;export err;err_chk
  fi
                                                                                                                   
  if [ -f $FIXwave/multiwave_dgradY.glw ]
  then
    cp $FIXwave/multiwave_dgradY.glw dgradY.glw
  fi
                                                                                                                   
  if [ -f dgradY.glw ]
  then
    echo "   dgradY.glw copied ($FIXwave/multiwave_dgradY.glw)."
  else
    msg="ABNORMAL EXIT: NO FILE dgradY.glw"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '**************************************** '
    echo '*** FATAL ERROR : NO DGRADY.GRL FILE *** '
    echo '**************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : multiwave_dgradY.glw file missing." >> $wavelog
    set $seton
    err=6;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/multiwaveice_glw.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 multiwavenam.sh for this model ...

# 2.d Sigma files

  ymdh=$ymdh_beg

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    echo "$USHwave/multiwavenam.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=6;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 :'

  ymdh=$ymdh_beg
  nr_err=0

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    if [ -d eice_${ymdh} ]
    then
      echo "         File for $ymdh : error in multiwaveice_glw.sh"
      postmsg "$jlogfile" "    File for $ymdh : error in multiwaveice_glw.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 multiwaveice_glw.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 multiwaveice_glw.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=7;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 multiwavenam.sh"
      postmsg "$jlogfile" "    File for $ymdh : error in multiwavenam.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 multiwavenam.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 multiwavenam.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=8;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 (multiwave_glwice)

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

  echo "$time_beg $time_end $ttime13" | $EXECwave/multiwave_glwice > glwice.out
  err=$?

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

  if [ ! -f ice.new ]
  then
    msg="ABNORMAL EXIT: FILE ice.new MISSING"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    cat glwice.out
    echo ' '
    echo '***************************************'
    echo '*** FATAL ERROR : ice.new NOT FOUND ***'
    echo '***************************************'
    echo ' '
    echo "$modID $grdID prep $date $cycle : ice.new missing." >> $wavelog
    set $seton
    err=10;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" multiwaveprep.ice.tmpl > multiwaveprep.inp
  rm -f multiwaveprep.ice.tmpl
           
  for grdID in $grids
  do

    cp mod_def.$grdID mod_def.ww3 

    $EXECwave/multiwaveprep
    err=$?
            
    if [ "$err" != '0' ]
    then
      msg="ABNORMAL EXIT: ERROR IN multiwaveprep"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '******************************************** '
      echo '*** FATAL ERROR : ERROR IN multiwaveprep *** '
      echo '******************************************** '
      echo ' '
      echo "$modID $grdID prep $date $cycle : error in multiwaveprep." >> $wavelog
      set $seton
      err=11;export err;err_chk
    fi
            
    if [ ! -f ice.ww3 ]
    then
      msg="ABNORMAL EXIT: FILE ice.ww3 MISSING"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      cat multiwaveprep.out
      echo ' '
      echo '****************************************'
      echo '*** FATAL ERROR : ice.ww3 NOT FOUND ***'
      echo '****************************************'
      echo ' '
      echo "$modID $grdID prep $date $cycle : ice.ww3 missing." >> $wavelog
      set $seton
      err=12;export err;err_chk
    else
      cp ice.ww3 ice.$grdID
    fi

  done

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

# 5.c Get into single file (multiwave_glwnam)

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

  echo "$time_beg $time_end $ttime13" | $EXECwave/multiwave_glwnam > glwnam.out
  err=$?

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

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

  rm -f ewnd.*

# 5.d Run multiwaveprep

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

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

  for grdID in $grids
  do

    cp mod_def.$grdID mod_def.ww3

    $EXECwave/multiwaveprep
    err=$?

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

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


# 5.d.1 Boost NAM winds 

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

    $EXECwave/multiwave_boostnam
    err=$?

    if [ "$err" != '0' ]
    then
      msg="ABNORMAL EXIT: ERROR IN multiwave_boostnam"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************* '
      echo '*** FATAL ERROR : ERROR IN multiwave_boostnam *** '
      echo '************************************************* '
      echo ' '
      echo "$modID $grdID prep $date $cycle : error in boostnam." >> $wavelog
      set $seton
      err=17;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 $grdID prep $date $cycle : windb.ww3 missing after wave_boostnam." >> $wavelog
      set $seton
      err=18;export err;err_chk
    fi

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

    mv -f wind.ww3 wind.$grdID

  done

  rm -f nam.wind
  rm -f multiwaveprep.inp
  rm -f boost.nam

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

  if [ "$SENDCOM" = 'YES' ]
  then
    for grdID in $grids
    do
      set $setoff
      echo ' '
      echo "   Saving wind.$grdID as $COMOUT/$modID.$grdID.$cycle.wind"
      echo "   Saving ice.$grdID as $COMOUT/$modID.$grdID.$cycle.ice"
      set $seton
      cp wind.$grdID $COMOUT/$modID.$grdID.$cycle.wind
      cp ice.$grdID $COMOUT/$modID.$grdID.$cycle.ice
    done
  fi 

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

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

 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL WAVE_BINARY $job $COMOUT/$modID.$grdID.$cycle.wind
 fi
                                                                                
#     Alert the ice file
                                                                                
 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL WAVE_BINARY $job $COMOUT/$modID.$grdID.$cycle.ice
 fi

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

  set $setoff
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                     *** End of GLW preprocessor ***'
  echo ' '
  set $seton

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

# End of GLW preprocessor script -------- ----------------------------------- #
