#!/bin/sh
###############################################################################
#                                                                             #
# This script is the postprocessor for the multi scale MWW3 wave model. 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      #
# postprocessing is performed by the following child scripts :                #
#                                                                             #
#  multiwavegrib1.sh        : generates GRIB files.                           #
#  multiwavegrib2.sh        : generates GRIB2 files.                           #
#  multiwavegrads_fields.sh : generates output for GRADS                      #
#  multiwavespec.sh         : generates spectral data files for output        #
#                             locations.                                      #
#  multiwavebull.sh         : generates bulletins for output locations.       #
#  multiwavecopy.sh         : copies spectral and bulletin info to old        #
#                             grids for backward compatibility                #
#  multiwavetar.sh          : tars the spectral and bulletin multiple files   #
#  multiwavegrid_interp.sh  : interpolates data from new grids to old grids   #
#                                                                             #
# Remarks :                                                                   #
# - The above scripts are (mostly) run under poe in parallel.                 #
#   Each script runs in its own directory created in DATA. If all is well     #
#   this directory disappears. If this directory is still there after poe     #
#   has finished, an error has occured Only then the output of the process    #
#   is copied to the output file. Otherwise, the output is deleted.           #
# - For non-fatal errors output is witten to the wave.log file.               #
#                                                                             #
# Origination  : 12/09/2008                                                   #
# Last update  : 12/09/2008                                                   #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

  seton='+xa'
  setoff='+xa'
  set $seton
  postmsg "$jlogfile" "HAS BEGUN on `hostname`"

  msg="Starting MWW3 POSTPROCESSOR SCRIPT for $modID"
  postmsg "$jlogfile" "$msg"

  set $setoff
  echo ' '
  echo '                     *********************************'
  echo '                     *** MWW3 POSTPROCESSOR SCRIPT ***'
  echo '                     *********************************'
  echo ' '
  echo "Starting at : `date`"
  echo '-------------'
  echo ' '
  set $seton

  export MP_PGMMODEL=mpmd
  export MP_CMDFILE=cmdfile

# 0.b Date and time stuff

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

# 0.c Defining model grids

  buoy="points"

# 0.c.1 grids

  export grids='glo_30m at_15m pa_15m at_10m wc_10m ak_10m hw_10m pi_10m at_4m wc_4m ak_4m'

# 0.c.2 output grids
 
  export ogrids='glo_30m at_15m pa_15m at_10m wc_10m ak_10m at_4m wc_4m ak_4m'

# 0.c.3 interpolated grids

  export Xgrids='ep_10m'

# 0.c.4 old grids
 
  export Ogrids='nah nph'

  set $setoff
  echo ' '
  echo 'Grid information  :'
  echo '-------------------'
  echo "   wave grids    : $grids"
  echo "   output grids  : $ogrids"
  echo "   Old grids     : $Ogrids"
  echo "   output points : ${modID}_$buoy"
  echo ' '
  set $seton


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

  field_OK='yes'
  point_OK='yes'
   grib_OK='yes'
  grint_OK='yes'
  grads_OK='no'
   spec_OK='yes'
   bull_OK='yes'
  Ospec_OK='yes'
  Obull_OK='yes'

  exit_code=0

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

# 1.a Model definition files and output files (set up using poe) 

# 1.a.1 Set up the poe command 

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

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

  set $setoff
  echo ' '
  echo "   Setting up first command file for copying model definition and data files."
  echo "   Set up command file structure (type = $cmdtype)."
  echo "      Number of command files                         : $nfile"
  echo "      Number of tasks allowed per node                : $nskip"
  echo "      Number of tasks set per node (for data copying) : $nper"
  set $seton

  rm -f cmdfile
  rm -f cmdfile.*

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

  ifile=1
  istrt=1

  for grdID in $grids 
  do
  
    if [ ! -f out_grd.$grdID ]
    then
      echo "   Copying $modID.$grdID.$cycle.outgrd from $COMIN to out_grd.$grdID"

      echo  "cp $COMIN/$modID.$grdID.$cycle.outgrd out_grd.$grdID"  >> cmdfile.$ifile 

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

      iper=`expr $ifile - $istrt + 1`
      if [ "$iper" -gt "$nper" ]
      then
        ifile=`expr $istrt + $nskip`
        istrt=`expr $istrt + $nskip`
      fi

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
        istrt=1
      fi

    fi 

  done

  for grdID in $grids  
  do

    if [ ! -f mod_def.$grdID ]
    then      
      echo "   Copying mod_def.$grdID from $FIXwave/multiwave_$grdID.moddef"
      if [ -f $FIXwave/multiwave_$grdID.moddef ]
      then
        echo  "cp $FIXwave/multiwave_$grdID.moddef mod_def.$grdID"   >> cmdfile.$ifile

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

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

  done

# 1.a.2 Execute the poe command

  set $setoff
  echo ' '
  echo "   Executing the copy command file at : `date`"
  echo '   ------------------------------------'
  echo ' '
  set $seton

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

# 1.a.3 Error checks

  for grdID in $grids
  do

    if [ ! -f mod_def.$grdID ]
    then
      msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
      echo '********************************************** '
      echo ' '
      echo "$modID fcst $date $cycle : fixed file(s) missing." >> $wavelog
      echo $msg
      set $seton
      err=1;export err;err_chk
      field_OK='no'
      grib_OK='no'
    fi


    if [ ! -f out_grd.$grdID ]
    then
      set $setoff
      echo ' '
      echo '**************************************** '
      echo '*** ERROR : NO RAW FIELD OUTPUT FILE *** '
      echo '**************************************** '
      echo ' '
      echo "$modID post $grdID $date $cycle : field output missing." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR : NO RAW FIELD OUTPUT FILE"
      exit_code=2
      field_OK='no'
      grib_OK='no'
      grads_OK='no'
    fi
  done

# 1.c Model definition and Grid interpolation file for interpolated grid

  for grdID in $Xgrids
  do

    if [ ! -f mod_def.$grdID ]
    then      
      echo "   Copying mod_def.$grdID from $FIXwave/multiwave_$grdID.moddef"
      if [ -f $FIXwave/multiwave_$grdID.moddef ]
      then
        cp $FIXwave/multiwave_$grdID.moddef mod_def.$grdID
      fi
    fi

    if [ ! -f mod_def.$grdID ]
    then
      msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
      echo '********************************************** '
      echo ' '
      echo "$modID fcst $date $cycle : fixed file(s) missing." >> $wavelog
      echo $msg
      set $seton
      err=1;export err;err_chk
      grint_OK='no'
    fi

    if [ ! -f ${grdID}_interp.inp.tmpl ]
    then      
      echo "   Copying ${grdID}_interp.inp.tmpl from $FIXwave/${grdID}_interp.inp.tmpl"
      if [ -f $FIXwave/${grdID}_interp.inp.tmpl ]
      then
        cp $FIXwave/${grdID}_interp.inp.tmpl .
      fi
    fi

    if [ ! -f ${grdID}_interp.inp.tmpl ]
    then
      msg="ABNORMAL EXIT: NO GRID INTERPOLATION INPUT FILE"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '****************************************************** '
      echo '*** FATAL ERROR : NO GRID INTERPOLATION INPUT FILE *** '
      echo '****************************************************** '
      echo ' '
      echo "$modID fcst $date $cycle : fixed file(s) missing." >> $wavelog
      echo $msg
      set $seton
      err=1;export err;err_chk
      grint_OK='no'
    fi
  done

# 1.c Raw point data file and moddef file

  if [ ! -f mod_def.$buoy ]
  then 
     echo "  Copying mod_def file from $FIXwave/multiwave_${modID}_$buoy.moddef"
     cp $FIXwave/multiwave_${modID}_$buoy.moddef mod_def.$buoy
  fi 

  if [ ! -f mod_def.$buoy ]
  then
    msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '********************************************** '
    echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
    echo '********************************************** '
    echo ' '
    echo "$modID fcst $date $cycle : fixed file(s) missing." >> $wavelog
    echo $msg
    set $seton
    err=1;export err;err_chk
  fi

  if [ ! -f out_pnt.ww3 ]
  then
    echo "   Copying out_pnt.ww3 from $COMIN"
    cp $COMIN/$modID.$cycle.outpnt out_pnt.ww3
  fi

  if [ -f out_pnt.ww3 ]
  then
    echo "   out_pnt.ww3 exists."
  else
    set $setoff
    echo ' '
    echo '**************************************** '
    echo '*** ERROR : NO RAW POINT OUTPUT FILE *** '
    echo '**************************************** '
    echo ' '
    echo "$modID post $date $cycle : point output missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR NO RAW POINT OUTPUT FILE"
    exit_code=3
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
    Ospec_OK='no'
    Obull_OK='no'
  fi


# 1.d Output locations file

  rm -f buoy.loc

  if [ -f $FIXwave/wave_$modID.buoys ]
  then
    cp $FIXwave/wave_$modID.buoys buoy.loc.temp
    sed -n '/^\$.*/!p' buoy.loc.temp > buoy.loc
    rm -f buoy.loc.temp
  fi

  if [ -f buoy.loc ]
  then
    echo "   buoy.loc copied and processed ($FIXwave/wave_$modID.buoys)."
  else
    set $setoff
    echo ' '
    echo '************************************* '
    echo '*** ERROR : NO BUOY LOCATION FILE *** '
    echo '************************************* '
    echo ' '
    echo "$modID post $date $cycle : buoy location file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO BUOY LOCATION FILE"
    exit_code=4
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
    Ospec_OK='no'
    Obull_OK='no'
  fi

  for grdID in $Ogrids
  do
    rm -f buoy_$grdID.loc
    if [ -f $FIXwave/wave_$grdID.buoys ]
    then
      cp $FIXwave/wave_$grdID.buoys buoy_$grdID.tmp.loc
      sed -n '/^\$.*/!p' buoy_$grdID.tmp.loc > buoy_$grdID.loc
      rm -f buoy_$grdID.tmp.loc
    fi

    if [ -f buoy_$grdID.loc ]
    then
      echo "   wave_$grdID.buoys copied and processed for old model processing."
    else
      set $setoff
      echo ' '
      echo '************************************* '
      echo '*** ERROR : NO BUOY LOCATION FILE *** '
      echo '************************************* '
      echo ' '
      echo "$grdID post $date $cycle : buoy location file missing." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR : NO BUOY LOCATION FILE"
      exit_code=5
      Ospec_OK='no'
      Obull_OK='no'
    fi
  done

# 1.e Input template files

  if [ -f $FIXwave/multiwavegrib1.inp.tmpl ]
  then
    cp $FIXwave/multiwavegrib1.inp.tmpl multiwavegrib1.inp.tmpl
  fi

  if [ -f multiwavegrib1.inp.tmpl ]
  then
    echo "   multiwavegrib1.inp.tmpl copied."
  else
    set $setoff
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR GRIB INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modID post $date $cycle : GRIB template file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR GRIB INPUT FILE"
    exit_code=6
    grib_OK='no'
  fi

  if [ -f $FIXwave/multiwavegrib2.inp.tmpl ]
  then
    cp $FIXwave/multiwavegrib2.inp.tmpl multiwavegrib2.inp.tmpl
  fi

  if [ -f multiwavegrib2.inp.tmpl ]
  then
    echo "   multiwavegrib2.inp.tmpl copied."
  else
    set $setoff
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR GRIB INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modID post $date $cycle : GRIB2 template file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR GRIB2 INPUT FILE"
    exit_code=6
    grib_OK='no'
  fi

  if [ "$grads_OK" = 'yes' ]
  then
    if [ -f $FIXwave/multiwavegrads_fields.inp.tmpl ]
    then
      cp $FIXwave/multiwavegrads_fields.inp.tmpl multiwavegrads_fields.inp.tmpl
    fi

    if [ -f multiwavegrads_fields.inp.tmpl ]
    then
      echo "   multiwavegrads_fields.inp.tmpl copied."
    else
      set $setoff
      echo ' '
      echo '************************************************ '
      echo '*** ERROR : NO TEMPLATE FOR GRADS INPUT FILE *** '
      echo '************************************************ '
      echo ' '
      echo "$modID post $date $cycle : GRADS template file missing." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR GRADS INPUT FILE"
      exit_code=7
      grads_OK='no'
    fi
  fi

  if [ -f $FIXwave/multiwavespec.inp.tmpl ]
  then
    cp $FIXwave/multiwavespec.inp.tmpl multiwavespec.inp.tmpl
  fi

  if [ -f multiwavespec.inp.tmpl ]
  then
    echo "   multiwavespec.inp.tmpl copied."
  else
    set $setoff
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR SPEC INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modID post $date $cycle : specra template file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR SPEC INPUT FILE"
    exit_code=8
    spec_OK='no'
    bull_OK='no'
    Ospec_OK='no'
    Obull_OK='no'
  fi

  if [ -f $FIXwave/multiwavebull.inp.tmpl ]
  then
    cp $FIXwave/multiwavebull.inp.tmpl multiwavebull.inp.tmpl
  fi

  if [ -f multiwavebull.inp.tmpl ]
  then
    echo "   multiwavebull.inp.tmpl copied."
  else
    set $setoff
    echo ' '
    echo '*************************************************** '
    echo '*** ERROR : NO TEMPLATE FOR BULLETIN INPUT FILE *** '
    echo '*************************************************** '
    echo ' '
    echo "$modID post $date $cycle : bulletin template file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR BULLETIN INPUT FILE"
    exit_code=9
    bull_OK='no'
    Obull_OK='no'
  fi

# 1.f Getting buoy information for points

  if [ "$spec_OK" = 'yes' ] || [ "$bull_OK" = 'yes' ]
  then
    ymdh=`$utilexec/ndate -9 $YMDH`
    tstart="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
    dtspec=3600.            # default time step (not used here)
    sed -e "s/TIME/$tstart/g" \
        -e "s/DT/$dtspec/g" \
        -e "s/POINT/1/g" \
        -e "s/ITYPE/0/g" \
        -e "s/FORMAT/F/g" \
                               multiwavespec.inp.tmpl > multiwavespec.inp
   
    ln -s mod_def.$buoy mod_def.ww3

    $EXECwave/multiwavespec > buoy_tmp.loc 

    sed -n '11,/^$/p' buoy_tmp.loc > buoy_tmp2.loc
    sed    '$d' buoy_tmp2.loc > buoy_tmp3.loc

    buoys=`awk '{ print $1 }' buoy_tmp3.loc`
    Nb=`wc buoy_tmp3.loc | awk '{ print $1 }'`
    rm buoy_tmp.loc buoy_tmp2.loc buoy_tmp3.loc
  fi

# 1.g Data summary

  set $setoff
  echo ' '
  echo "   Input files read and processed at : `date`"
  echo ' ' 
  echo '   Data summary : '
  echo '   ---------------------------------------------'
  echo "      Sufficient data for GRIB files            : $grib_OK"
  echo "      Sufficient data for spectral files        : $spec_OK ($Nb points)"
  echo "      Sufficient data for bulletins             : $bull_OK ($Nb points)"
  echo ' '
  set $seton


# --------------------------------------------------------------------------- #
# 2.  Make second command file(s) (GRID interpolation and GRIB generation)
# 2.a Command file set-up
#     The command file points to $nfile files named cmdfile.$ifile.
#     The actual work is distributed over these files. The skip parameter
#     is used for load balancing. GRIB packing takes more time than making
#     spectral data files or bulletins.

  ifile=1
  iskip=1
  nskip=19

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

  set $setoff
  echo '   Making second command file (GRID Interpolation, GRIB, GRADS, SPEC and BULLETINS) '
  echo "   Set up command file structure (type = $cmdtype)."
  echo "      Number of command files                : $nfile"
  echo "      Number of skips for I/O intensive jobs : $nskip"
  set $seton

  rm -f cmdfile
  rm -f cmdfile.*

  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 GRID Interpolation and corresponding GRIB generation files

  gribFL=\''F F T F F  T F F F F  T T F F T  T F T F F  F F F F F  F F F F F  F'\'

  ymdh_int=`$utilexec/ndate -9 $YMDH`
  dt_int=3600
  nsteps=999

  if [ "$grint_OK" = 'yes' ]
  then
    for grdID in $Xgrids
    do

      case $grdID in
        ep_10m)  GRIDNR=14  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      esac
      echo "$USHwave/multiwavegrid_interp.sh $grdID $ymdh_int $dt_int $nsteps > grint_${grdID}_${ymdh_int}.out 2>&1;$USHwave/multiwavegrib2.sh $grdID $dtgrib $ngrib $GRIDNR $MODNR $gribFL > grib_$grdID.out 2>&1"               >> cmdfile.$ifile

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

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

# 2.c GRIB files

# GRIB field time step -- dtgrib
# Number of GRIB fields -- ngrib
# Assigned NCEP number for grid -- GRIDNR
# Assigned NCEP number for model -- MODNR

  if [ "$grib_OK" = 'yes' ]
  then
    for grdID in $ogrids
    do
      case $grdID in
      glo_30m) GRIDNR=11  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      at_15m)  GRIDNR=255  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      pa_15m)  GRIDNR=255  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      at_10m)  GRIDNR=12  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      wc_10m)  GRIDNR=13  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      ak_10m)  GRIDNR=15  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      ak_4m)   GRIDNR=16  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      wc_4m)   GRIDNR=17  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      at_4m)   GRIDNR=18  ; MODNR=255  ; dtgrib=10800. ; ngrib=61 ;;
      esac

      echo "$USHwave/multiwavegrib2.sh $grdID $dtgrib $ngrib $GRIDNR $MODNR $gribFL > grib_$grdID.out 2>&1"               >> cmdfile.$ifile

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

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


# 2.d Old grids grib files

  gribFLO=\''F F T F F  T F T T F  T T T T F  F F F F F  F F F F F  F F F F F F'\'

  for grdID in $Ogrids
  do
    case $grdID in
    nah)    Hgrid='at_15m' ; GRIDNR=244 ; MODNR=123 ; dtgrib=3600. ; ngrib=127 ;;
    nph)    Hgrid='pa_15m' ; GRIDNR=253 ; MODNR=125 ; dtgrib=3600. ; ngrib=127 ;;
    esac
    echo " ln -sf out_grd.$Hgrid out_grd.$grdID ; ln -sf mod_def.$Hgrid mod_def.$grdID ; $USHwave/multiwavegrib1.sh $grdID $dtgrib $ngrib $GRIDNR $MODNR $gribFLO > grib_$grdID.out 2>&1"               >> cmdfile.$ifile
    
    if [ "$nfile" -gt '1' ]
    then
      ifile=`expr $ifile + 1`
    fi

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

# 2.e Spectral data files

  if [ "$spec_OK" = 'yes' ]
  then
    export dtspec=10800.   # time step for spectra
    pos=1
    ifile=`expr $nskip + 1`
    for buoy in $buoys
    do
      echo "$USHwave/multiwavespec.sh $buoy $pos > spec_$buoy.out 2>&1" >> cmdfile.$ifile
      pos=`expr $pos + 1`
      if [ "$nfile" -gt '1' ]
      then
        ifile=`expr $ifile + 1`
        if [ "$ifile" -gt "$nfile" ]
        then
#          if [ "$iskip" -le "$nskip" ]
#          then
#            ifile=2
#            iskip=`expr $iskip + 1`
#          else
            ifile=`expr $nskip + 1`
#          fi
        fi
      fi
    done
  fi

# 2.f Bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    export dtbull=3600.    # time step for bulletins
    pos=1
    for buoy in $buoys
    do
      echo "$USHwave/multiwavebull.sh $buoy $pos > bull_$buoy.out 2>&1" >> cmdfile.$ifile
      pos=`expr $pos + 1`
      if [ "$nfile" -gt '1' ]
      then
        ifile=`expr $ifile + 1`
        if [ "$ifile" -gt "$nfile" ]
        then
#          if [ "$iskip" -le "$nskip" ]
#          then
#            ifile=2
#            iskip=`expr $iskip + 1`
#          else
            ifile=`expr $nskip + 1`
#          fi
        fi
      fi
    done
  fi

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

  set $setoff
  echo "   Executing second command file at : `date`"
  echo '   ----------------------------------'
  echo ' '
  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 post $date $cycle : error in $cmdtype." >> $wavelog
#    echo $msg
#    set $seton
#    err=8;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 (error output concatenated below).'
  set $seton

# 4.a Old grid interpolation

  if [ "$grint_OK" = 'yes' ]
  then
    for grdID in $Xgrids
    do
      if [ -d grint_${grdID}_${ymdh_int} ]
      then
        set $setoff
        echo "      Error in GRID interpolation for $grdID."
        postmsg "$jlogfile" "NON-FATAL ERROR in GRID interpolation for $grdID."
        set $seton
      else
        rm -f grint_${grdID}_${ymdh_int}.out
        set $setoff
        echo "      GRID interpolation successful for $grdID."
        set $seton
      fi

      if [ -d grib_$grdID ]
      then
        set $setoff
        echo "      Error in GRIB encoding for $grdID."
        postmsg "$jlogfile" "NON-FATAL ERROR in GRIB encoding for $grdID."
        set $seton
      else
        rm -f grib_$grdID.out
        set $setoff
        echo "      GRIB encoding successful for $grdID."
        touch $COMOUT/$grdID.t${cyc}z.gribdone
        set $seton
      fi
    done
  fi


# 4.b GRIB file

  if [ "$grib_OK" = 'yes' ]
  then
    for grdID in $ogrids $Ogrids
    do
      if [ -d grib_$grdID ]
      then
        set $setoff
        echo "      Error in GRIB encoding for $grdID."
        postmsg "$jlogfile" "NON-FATAL ERROR in GRIB encoding for $grdID."
        set $seton
      else
        rm -f grib_$grdID.out
        set $setoff
        echo "      GRIB encoding successful for $grdID."
        touch $COMOUT/$grdID.t${cyc}z.gribdone
        set $seton
      fi
    done
  fi

# 4.d Spectral data files and bulletins

  bullstring='Bulletins not generated'
  specstring='Spectra not generated'

  for buoy in $buoys
  do

    if [ "$spec_OK" = 'yes' ]
    then
      if [ -d spec_$buoy ]
      then
        specstring='Error in spectra.'
        postmsg "$jlogfile" "NON-FATAL ERROR in spectra."
      else
        specstring='Spectra OK.'
        rm -f spec_$buoy.out
      fi
    fi

    if [ "$bull_OK" = 'yes' ]
    then
      if [ -d bull_$buoy ]
      then
        bullstring='Error in bulletins.'
        postmsg "$jlogfile" "NON-FATAL ERROR in bulletins."
      else
        bullstring='Bulletins OK.'
        rm -f bull_$buoy.out
      fi
    fi

    set $setoff
    echo "      $buoy : $specstring   $bullstring"
    set $seton

  done

  if [ -f *.out ]
  then
    for grdID in $Xgrids
    do 
      if [ -f grint_${grdID}_${ymdh_int}.out ]
      then
        set $setoff
        echo ' '
        echo '********************************************'
        echo '*** ERROR OUTPUT multiwavegrid_interp.sh ***'
        echo '********************************************'
        echo ' '
        echo "$modID post $date $cycle : error in GRID Interpolation." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavegrid_interp.sh"
        exit_code=10
        sed "s/^/grint_${grdID}_${ymdh_int}.out : /g"  grint_${grdID}_${ymdh_int}.out
        rm -f grint_${grdID}_${ymdh_int}.out
        set $seton
	grint_OK='no'
      fi
      if [ -f grib_$grdID.out ]
      then
        set $setoff
        echo ' '
        echo '********************************************'
        echo '*** ERROR OUTPUT multiwavegrid_interp.sh ***'
        echo '********************************************'
        echo ' '
        echo "$modID post $date $cycle : error in subsequent GRIB encoding (for interpolated grids)." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavegrid_interp.sh"
        exit_code=10
        sed "s/^/grib_$grdID.out : /g"  grib_$grdID.out
        rm -f grib_$grdID.out
        set $seton
	grint_OK='no'
      fi
    done

    for grdID in $ogrids $Ogrids
    do 
      if [ -f grib_$grdID.out ]
      then
        set $setoff
        echo ' '
        echo '*************************************'
        echo '*** ERROR OUTPUT multiwavegrib.sh ***'
        echo '*************************************'
        echo ' '
        echo "$modID post $date $cycle : error in GRIB." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavegrib1.sh"
        exit_code=10
        sed "s/^/grib_$grdID.out : /g"  grib_$grdID.out
        rm -f grib_$grdID.out
        set $seton
      fi

    done

    if [ -f spec_*.out ]
    then
      set $setoff
      echo ' '
      echo '*************************************'
      echo '*** ERROR OUTPUT multiwavespec.sh ***'
      echo '*************************************'
      echo '            Possibly in multiple calls'
      echo "$modID post $date $cycle : error in spectra." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in multiwavespec.sh, possibly in multiple calls."
      exit_code=12
      for file in spec_*.out
      do
        echo ' '
        sed "s/^/$file : /g" $file
      done
      rm -f spec_*.out
      set $seton
    fi

    if [ -f bull_*.out ]
    then
      set $setoff
      echo ' '
      echo '*************************************'
      echo '*** ERROR OUTPUT multiwavebull.sh ***'
      echo '*************************************'
      echo '            Possibly in multiple calls'
      echo ' '
      echo "$modID post $date $cycle : error in bulletins." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in multiwavebull.sh, possibly in multiple calls."
      exit_code=13
      for file in bull_*.out
      do
        echo ' '
        sed "s/^/$file : /g" $file
      done
      rm -f bull_*.out
      set $seton
    fi
  fi

  rm -f cmdfile*

# --------------------------------------------------------------------------- #
# 5.  Make third command file

  set $setoff
  echo ' '
  echo '   Making third command file (copying points files to old grids).'
  set $seton

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

  ifile=1

# 5.a Spectral files

  if [ "$Ospec_OK" = 'yes' ] && [ "$spec_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      echo "$USHwave/multiwavecopy.sh $grdID spec > spec_${grdID}_copy.out 2>&1" >> cmdfile.$ifile

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

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

# 5.b Bulletins

  if [ "$Obull_OK" = 'yes' ] && [ "$bull_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      echo "$USHwave/multiwavecopy.sh $grdID bull > bull_${grdID}_copy.out 2>&1" >> cmdfile.$ifile

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

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
      fi
      echo "$USHwave/multiwavecopy.sh $grdID cbull > cbull_${grdID}_copy.out 2>&1" >> cmdfile.$ifile

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

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
      fi
      echo "$USHwave/multiwavecopy.sh $grdID csbull > csbull_${grdID}_copy.out 2>&1" >> cmdfile.$ifile

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

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

# --------------------------------------------------------------------------- #
# 6   Execute third command file


  set $setoff
  echo "   Executing point copy command file at : `date`"
  echo '   --------------------------------------'
  echo ' '
  set $seton

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

  rm -f cmdfile*

# --------------------------------------------------------------------------- #
# 7. Check for errors

# 7.a Spectral files

  if [ "$Ospec_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      if [ -d COPY_spec_$grdID ]
      then
        set $setoff
        echo ' '
        echo '*************************************'
        echo '*** ERROR OUTPUT multiwavecopy.sh ***'
        echo '*************************************'
        echo '            Possibly in multiple calls'
        echo "$modID post $date $cycle : error in copying spectral files to $grdID." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavecopy.sh, possibly in multiple calls."
        exit_code=14
        set $seton
      else
        rm -f spec_${grdID}_copy.out
        echo "  Copying of spectral files to $grdID succesful"
      fi
    done
  fi

# 7.b Bulletin files

  if [ "$Obull_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      if [ -d COPY_bull_$grdID ]
      then
        set $setoff
        echo ' '
        echo '*************************************'
        echo '*** ERROR OUTPUT multiwavecopy.sh ***'
        echo '*************************************'
        echo '            Possibly in multiple calls'
        echo "$modID post $date $cycle : error in copying bulletin files to $grdID." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavecopy.sh, possibly in multiple calls."
        exit_code=15
        set $seton
      else
        rm -f bull_${grdID}_copy.out
        echo "  Copying of bulletin files to $grdID succesful"
      fi

      if [ -d COPY_cbull_$grdID ]
      then
        set $setoff
        echo ' '
        echo '*************************************'
        echo '*** ERROR OUTPUT multiwavecopy.sh ***'
        echo '*************************************'
        echo '            Possibly in multiple calls'
        echo "$modID post $date $cycle : error in copying compressed bulletin files to $grdID." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavecopy.sh, possibly in multiple calls."
        exit_code=16
        set $seton
      else
        rm -f cbull_${grdID}_copy.out
        echo "  Copying of compressed bulletin files to $grdID succesful"
      fi

      if [ -d COPY_csbull_$grdID ]
      then
        set $setoff
        echo ' '
        echo '*************************************'
        echo '*** ERROR OUTPUT multiwavecopy.sh ***'
        echo '*************************************'
        echo '            Possibly in multiple calls'
        echo "$modID post $date $cycle : error in copying csv bulletin files to $grdID." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavecopy.sh, possibly in multiple calls."
        exit_code=17
        set $seton
      else
        rm -f csbull_${grdID}_copy.out
        echo "  Copying of csv bulletin files to $grdID succesful"
      fi
    done
  fi

# 7.c Error outputs

  if [ -f *.out ]
  then
    set $setoff
    echo ' '
    echo '*********************'
    echo '*** ERROR OUTPUTS ***'
    echo '*********************'
    echo ' '
    exit_code=18
    for file in *.out
    do
      echo ' '
      sed "s/^/$file : /g" $file
    done
    rm -f *.out
    set $seton
  fi

# --------------------------------------------------------------------------- #
# 8.  Make fourth command file


  nper=8
  nskip=16

  set $setoff
  echo ' '
  echo '   Making fourth command file for taring all point output files.'
  echo "   Number of tasks set per node     :  $nper"
  set $seton

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

  ifile=1
  istrt=1

# 8.a Spectral data files

  if [ "$spec_OK" = 'yes' ]
  then
    echo "$USHwave/multiwavetar.sh $modID spec > ${modID}_spec_tar.out 2>&1 "   >> cmdfile.$ifile

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

    iper=`expr $ifile - $istrt + 1`
    if [ "$iper" -gt "$nper" ]
    then
      ifile=`expr $istrt + $nskip`
      istrt=`expr $istrt + $nskip`
    fi

    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
      istrt=1
    fi

  fi

# 8.b Bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/multiwavetar.sh $modID bull > ${modID}_bull_tar.out 2>&1 "   >> cmdfile.$ifile

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

    iper=`expr $ifile - $istrt + 1`
    if [ "$iper" -gt "$nper" ]
    then
      ifile=`expr $istrt + $nskip`
      istrt=`expr $istrt + $nskip`
    fi

    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
      istrt=1
    fi

  fi

# 8.c Compressed bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/multiwavetar.sh $modID cbull > ${modID}_cbull_tar.out 2>&1 " >> cmdfile.$ifile

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

    iper=`expr $ifile - $istrt + 1`
    if [ "$iper" -gt "$nper" ]
    then
      ifile=`expr $istrt + $nskip`
      istrt=`expr $istrt + $nskip`
    fi

    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
      istrt=1
    fi

  fi

# 8.d CSV bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/multiwavetar.sh $modID csbull > ${modID}_csbull_tar.out 2>&1 " >> cmdfile.$ifile

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

    iper=`expr $ifile - $istrt + 1`
    if [ "$iper" -gt "$nper" ]
    then
      ifile=`expr $istrt + $nskip`
      istrt=`expr $istrt + $nskip`
    fi

    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
      istrt=1
    fi

  fi

# 8.e Old Spectral data files

  if [ "$Ospec_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      echo "$USHwave/multiwavetar.sh $grdID spec > ${grdID}_spec_tar.out 2>&1 " >> cmdfile.$ifile

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

      iper=`expr $ifile - $istrt + 1`
      if [ "$iper" -gt "$nper" ]
      then
        ifile=`expr $istrt + $nskip`
        istrt=`expr $istrt + $nskip`
      fi

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
        istrt=1
      fi
    done
  fi

# 8.f Old Bulletins

  if [ "$Obull_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      echo "$USHwave/multiwavetar.sh $grdID bull > ${grdID}_bull_tar.out 2>&1 " >> cmdfile.$ifile

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

      iper=`expr $ifile - $istrt + 1`
      if [ "$iper" -gt "$nper" ]
      then
        ifile=`expr $istrt + $nskip`
        istrt=`expr $istrt + $nskip`
      fi

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
        istrt=1
      fi

    done
  fi

# 8.g Old Compressed Bulletins

  if [ "$Obull_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      echo "$USHwave/multiwavetar.sh $grdID cbull > ${grdID}_cbull_tar.out 2>&1 "   >> cmdfile.$ifile

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

      iper=`expr $ifile - $istrt + 1`
      if [ "$iper" -gt "$nper" ]
      then
        ifile=`expr $istrt + $nskip`
        istrt=`expr $istrt + $nskip`
      fi

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
        istrt=1
      fi

    done
  fi

# 8.h Old CSV Bulletins

  if [ "$Obull_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      echo "$USHwave/multiwavetar.sh $grdID csbull > ${grdID}_csbull_tar.out 2>&1 "   >> cmdfile.$ifile

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

      iper=`expr $ifile - $istrt + 1`
      if [ "$iper" -gt "$nper" ]
      then
        ifile=`expr $istrt + $nskip`
        istrt=`expr $istrt + $nskip`
      fi

      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
        istrt=1
      fi

    done
  fi

# --------------------------------------------------------------------------- #
# 9.  Execute fourth command file

  set $setoff
  echo "   Executing tar command file at : `date`"
  echo '   -------------------------------'
  echo ' '
  set $seton

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

  rm -f cmdfile*
# --------------------------------------------------------------------------- #
# 10.  Check for errors

  set $setoff
  echo ' '
  echo '   Checking for errors (error output concatenated below).'
  set $seton

# 10.a Spectral tar file

  if [ "$spec_OK" = 'yes' ]
  then
    if [ -d TAR_spec_$modID ]
    then
      set $setoff
      echo "      Error in $modID spectral tar file."
      echo "$modID post $date $cycle : error in spectral tar." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in $modID spectral tar file."
    else
      rm -f ${modID}_spec_tar.out
      set $setoff
      echo "      $modID Spectral tar file OK."
      set $seton
    fi
  fi

# 10.b Bulletin tar files

  if [ "$bull_OK" = 'yes' ]
  then
    if [ -d TAR_bull_$modID ]
    then
      set $setoff
      echo "      Error in $modID bulletin tar file."
      echo "$modID post $date $cycle : error in bulletin tar." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in $modID bulletin tar file."
    else
      rm -f ${modID}_bull_tar.out
      set $setoff
      echo "      $modID Bulletin tar file OK."
      set $seton
    fi

    if [ -d TAR_cbull_$modID ]
    then
      set $setoff
      echo "      Error in $modID compressed bulletin tar file."
      echo "$modID post $date $cycle : error in compressed bulletin tar." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in $modID compressed bulletin tar file."
    else
      rm -f ${modID}_cbull_tar.out
      set $setoff
      echo "      $modID compressed bulletin tar file OK."
      set $seton
    fi

    if [ -d TAR_csbull_$modID ]
    then
      set $setoff
      echo "      Error in $modID csv bulletin tar file."
      echo "$modID post $date $cycle : error in csv bulletin tar." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in $modID csv bulletin tar file."
    else
      rm -f ${modID}_csbull_tar.out
      set $setoff
      echo "      $modID csv bulletin tar file OK."
      set $seton
    fi
  fi

# 10.c Old Spectral tar file

  if [ "$Ospec_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      if [ -d TAR_spec_$grdID ]
      then
        set $setoff
        echo "      Error in $grdID spectral tar file."
        echo "$grdID post $date $cycle : error in spectral tar." >> $wavelog
        set $seton
        postmsg "$jlogfile" "NON-FATAL ERROR in $grdID spectral tar file."
      else
        rm -f ${grdID}_spec_tar.out
        set $setoff
        echo "      $grdID Spectral tar file OK."
        set $seton
      fi
    done
  fi

# 10.d Old Bulletin tar file

  if [ "$Obull_OK" = 'yes' ]
  then
    for grdID in $Ogrids
    do
      if [ -d TAR_bull_$grdID ]
      then
        set $setoff
        echo "      Error in $grdID bulletin tar file."
        echo "$grdID post $date $cycle : error in bulletin tar." >> $wavelog
        set $seton
        postmsg "$jlogfile" "NON-FATAL ERROR in $grdID bulletin tar file."
      else
        rm -f ${grdID}_bull_tar.out
        set $setoff
        echo "      $grdID bulletin tar file OK."
        set $seton
      fi

      if [ -d TAR_cbull_$grdID ]
      then
        set $setoff
        echo "      Error in $grdID compressed bulletin tar file."
        echo "$grdID post $date $cycle : error in compressed bulletin tar." >> $wavelog
        set $seton
        postmsg "$jlogfile" "NON-FATAL ERROR in $grdID compressed bulletin tar file."
      else
        rm -f ${grdID}_cbull_tar.out
        set $setoff
        echo "      $grdID compressed bulletin tar file OK."
        set $seton
      fi

      if [ -d TAR_csbull_$grdID ]
      then
        set $setoff
        echo "      Error in $grdID csv bulletin tar file."
        echo "$grdID post $date $cycle : error in csv bulletin tar." >> $wavelog
        set $seton
        postmsg "$jlogfile" "NON-FATAL ERROR in $grdID csv bulletin tar file."
      else
        rm -f ${grdID}_csbull_tar.out
        set $setoff
        echo "      $grdID csv bulletin tar file OK."
        set $seton
      fi

    done
  fi

# 10.e Error outputs

  if [ -f *.out ]
  then
    set $setoff
    echo ' '
    echo '*********************'
    echo '*** ERROR OUTPUTS ***'
    echo '*********************'
    echo ' '
    exit_code=19
    for file in *.out
    do
      echo ' '
      sed "s/^/$file : /g" $file
    done
    rm -f *.out
    set $seton
  fi

# --------------------------------------------------------------------------- #
# 11.  CLean up and rename old grid files

  rm -f *.tmpl
  for ID in $modID 
  do
    rm -f $ID.*.spec
    rm -f $ID.*.bull
    rm -f $ID.*.cbull
    rm -f $ID.*.csbull
  done

  for ID in $Ogrids
  do
    mv $COMOUT/$modID.$ID.$cycle.grib     $COMOUT/$ID.$cycle.grib
  done


# --------------------------------------------------------------------------- #
# 12.  Ending output

  set $setoff
  echo ' '
  echo "Ending at : `date`"
  echo '-----------'
  echo ' '
  echo '                     *** End of MWW3 postprocessor ***'
  echo ' '
  set $seton

  if [ "$exit_code" -ne '0' ]
  then
     msg="ABNORMAL EXIT: Problem in MWW3 POST"
     postmsg "$jlogfile" "$msg"
     echo $msg
     err=exit_code ; export err ; err_chk
  fi

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

# End of MWW3 prostprocessor script ---------------------------------------- #
