#!/bin/sh
set -xa
###############################################################################
#                                                                             #
# This script is the postprocessor for the global NWW3ens 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 occure.d 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.               #
#                                                                             #
#                                                                Feb., 2008   #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation
#
  cd $DATA
#
  postmsg "$jlogfile" "HAS BEGUN on `hostname`"
#
  msg="Starting NMWW3 ENSEMBLE POSTPROCESSOR SCRIPT"
  postmsg "$jlogfile" "$msg"
#
  echo ' '
  echo '                     *********************************'
  echo '                     *** NMWW3 ENS POSTPROCESSOR SCRIPT ***'
  echo '                     *********************************'
  echo ' '
  echo "Starting at : `date`"
#
  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.d model grids

  grids='glo_60m '

#
# --------------------------------------------------------------------------- #
# 1.  Get files that are used by most child scripts
#
  field_OK='yes'
  point_OK='yes'
  grib_OK='yes'
  grint_OK='no'
  grads_OK='no'
  spec_OK='yes'
  bull_OK='yes'

  exit_code=0
#
  echo ' '
  echo 'Preparing input files :'
  echo '-----------------------'

# 
#
# 1.a Model definition files  
#
#### 1.a.1 model modefinition

  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
             cp $FIXwave/multiwave_$grdID.moddef mod_def.$grdID
           fi
       fi
#
        if [ -f mod_def.$grdID ] ; then
           echo "   mod_def.$grdID exists."
        else
           msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
           postmsg "$jlogfile" "$msg"
           echo ' '
           echo '********************************************** '
           echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
           echo '********************************************** '
           echo ' '
           echo "$modIE post $date $cycle : fixed file(s) missing." >> $wavelog
           echo $msg
           err=1;export err;err_chk
        fi

  done

# 1.a.2
  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

#
# 1.b Raw field data file
#

  for grdID in $grids
  do

    if [ ! -f out_grd.$grdID ]
    then
      echo "   Copying $modIE.$cycle.outgrd from $COMIN to out_grd.$grdID"
      cp $COMIN/$modIE.$cycle.outgrd out_grd.$grdID
    fi

#
     if [ -f out_grd.$grdID ] ; then
       echo "   out_grd.$grdID exists."
     else
       echo ' '
       echo '**************************************** '
       echo '*** ERROR : NO RAW FIELD OUTPUT FILE *** '
       echo '**************************************** '
       echo ' '
       echo "$modIE post $date $cycle : field output missing." >> $wavelog
       postmsg "$jlogfile" "NON-FATAL ERROR : NO RAW FIELD OUTPUT FILE"
       exit_code=2
       field_OK='no'
       grib_OK='no'
     fi

   done
#
# 1.c Raw point data file
#
  for grdID in $grids
  do

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

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

  done
#
# 1.d Log file
#
  if [ ! -f log.mww3 ] ; then
    echo "   Copying log.mww3 from $COMIN"
    cp ${COMIN}/$modIE.$cycle.log log.mww3
  fi
#
  if [ -f log.mww3 ] ; then
    echo "   log.mww3 exists."
  else
    echo ' '
    echo '*************************** '
    echo '*** ERROR : NO LOG FILE *** '
    echo '*************************** '
    echo ' '
    echo "$modIE post $date $cycle : log file missing." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR : NO LOG FILE"
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
    exit_code=4
  fi
#
# 1.e 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
    echo ' '
    echo '************************************* '
    echo '*** ERROR : NO BUOY LOCATION FILE *** '
    echo '************************************* '
    echo ' '
    echo "$modIE post $date $cycle : buoy location file missing." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR : NO BUOY LOCATION FILE"
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
    exit_code=5
  fi
#
# 1.f 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
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR GRIB INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modIE post $date $cycle : GRIB template file missing." >> $wavelog
    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 multiwavegrib1.inp.tmpl ] ; then
    echo "   multiwavegrib1.inp.tmpl  copied."
  else
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR GRIB INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modIE post $date $cycle : GRIB template file missing." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR GRIB INPUT FILE"
    exit_code=7
    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
      echo ' '
      echo '************************************************ '
      echo '*** ERROR : NO TEMPLATE FOR GRADS INPUT FILE *** '
      echo '************************************************ '
      echo ' '
      echo "$modIE post $date $cycle : GRADS template file missing." >> $wavelog
      exit_code=8
      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 "$modIE post $date $cycle : specra template file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR SPEC INPUT FILE"
    spec_OK='no'
    bull_OK='no'
    exit_code=9
  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 "$modIE post $date $cycle : bulletin template file missing." >> $wavelog
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR BULLETIN INPUT FILE"
    bull_OK='no'
    exit_code=10
  fi

#
# 1.g Data summary
#
  echo ' '
  echo '   Data summary : '
  echo '   ---------------------------------------------'
  echo "      Sufficient data for GRIB files     : $grib_OK"
  echo "      Sufficient data for spectral files : $spec_OK"
  echo "      Sufficient data for bulletins      : $bull_OK"
  echo ' '
#
# --------------------------------------------------------------------------- #
# 2.  Make first command file(s) (GRIB, spectral data, bulletins)
# 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
  nfile=`echo $LOADL_PROCESSOR_LIST | wc -w | awk '{ print $1}'`
  iskip=1
  nskip=7
#
  if [ "$nfile" -gt '1' ] ; then
    cmdtype='poe'
  else
    cmdtype='sh'
    #nskip='-'
    nskip='0'
  fi
#
  echo '   Making first command file (GRIB, spectral data, bulletins).'
  echo "   Set up command file structure (type = $cmdtype)."
  echo "      Number of command files  : $nfile"
  echo "      Number of skips for GRIB : $nskip"
#
  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 GRIB file
#    dtgrib:  GRIB field time step
#    ngrib:   Number of GRIB fields
#    GRIDNR   Assigned NCEP number for grid
#    MODNR    Assigned NCEP number for model
#
  if [ "$grib_OK" = 'yes' ]
  then
    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'\'
    for grdID in $grids
    do
      case $grdID in
      glo_60m) GRIDNR=255  ; MODNR=255  ; dtgrib=21600. ; ngrib=41 ;;
      esac

      echo "$USHwave/multiwavegrib2ens.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 Spectral data files
#
#  buoys=`sed "s/\'/ /g" buoy.loc | awk '{ print $3 }'`
#
  if [ "$spec_OK" = 'yes' ] || [ "$bull_OK" = 'yes' ]
  then
    ymdh=`$utilexec/ndate -12 $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
  
  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/multiwavespecens.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.d Bulletins
#
   
  if [ "$bull_OK" = 'yes' ]
  then
    export dtbull=3600.    # time step for bulletins
    pos=1
    for buoy in $buoys
    do
      echo "$USHwave/multiwavebullens.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 first command file
# 3.a Execution
#
  echo "   Executing command file."
#
  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"
#    echo ' '
#    echo '**************************************** '
#    echo '*** FATAL ERROR : ERROR IN poe OR sh *** '
#    echo '**************************************** '
#    echo ' '
#    echo "$modIE post $date $cycle : error in $cmdtype." >> $wavelog
#    echo $msg
#    err=8;export err;err_chk
#  fi
#
# 3.c Timing info on steps
#
  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
#
# --------------------------------------------------------------------------- #
# 4.  Check for errors
#
  echo ' '
  echo '   Checking for errors (error output concatenated below).'
#
# 4.a GRIB file
#
  if [ "$grib_OK" = 'yes' ]
  then
    for grdID in $grids
    do
    if [ -d grib_$grdID  ]
    then
      echo '      Error in GRIB encoding.'
      postmsg "$jlogfile" "NON-FATAL ERROR in GRIB encoding."
      sed "s/^/grib_$grdID.out : /g"  grib_$grdID.out
    else
      sed "s/^/grib_$grdID.out : /g"  grib_$grdID.out
      rm -f grib_$grdID.out
      echo '      GRIB encoding successful.'
    fi
   done
  fi
#
# 4.b 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
#
    echo "      $buoy : $specstring   $bullstring"
#
  done
#
  if [ -f *.out ]
  then
#
    for grdID in $grids
    do

    if [ -f grib_$grdID.out ]
    then
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT multiwavegrib2ens.sh ***'
      echo '********************************'
      echo ' '
      echo "$modIE post $date $cycle : error in GRIB." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavegribens.sh"
#      sed "s/^/grib_$grdID.out : /g"  grib_$grdID.out
       rm -f grib_$grdID.out
    fi
#
    if [ -f spec_*.out ]
    then
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT wavespecens.sh ***'
      echo '********************************'
      echo '            Possibly in multiple calls'
      echo "$modIE post $date $cycle : error in spectra." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavespecens.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
    fi
#
    if [ -f bull_*.out ]
    then
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT wavebullens.sh ***'
      echo '********************************'
      echo '            Possibly in multiple calls'
      echo ' '
      echo "$modIE post $date $cycle : error in bulletins." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavebullens.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
    fi

   done
  fi
#
  rm -f cmdfile*
#
# --------------------------------------------------------------------------- #
# 5.  Make second command file
#
  echo ' '
  echo '   Making second command file (tar).'
  rm -f commandfile
#
# 5.a Spectral data files
#
  if [ "$spec_OK" = 'yes' ]
  then
    echo "$USHwave/multiwavetarens.sh $modIE spec > ${modIE}_spec_tar.out 2>&1 "   >> commandfile 
  fi
#
# 5.b Bulletins
#
  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/multiwavetarens.sh $modIE bull > ${modIE}_bull_tar.out 2>&1 "   >> commandfile 
  fi
#
# 5.c Compressed bulletins
#
#  if [ "$bull_OK" = 'yes' ]
#  then
#    echo "$USHwave/multiwavetarens.sh $modIE cbull > ${modIE}_cbull_tar.out 2>&1 " >> commandfile 
#  fi
# --------------------------------------------------------------------------- #
# 6.  Execute second command file
#
  echo '   Executing command file.'
#
  sh ./commandfile
  rm -f commandfile
#
# --------------------------------------------------------------------------- #
# 7.  Check for errors
#
  echo ' '
  echo '   Checking for errors (error output concatenated below).'
#
# 7.a Spectral tar file
#
  if [ "$spec_OK" = 'yes' ]
  then
    if [ -d TAR_spec_$modIE ]
    then
      echo "      Error in $modIE spectral tar file."
      echo "$modIE post $date $cycle : error in spectral tar." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in $modIE spectral tar file."
    else
      rm -f ${modIE}_spec_tar.out
      echo "      $modIE Spectral tar file OK."
    fi
  fi

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

  fi
  
#
# 7.c Error outputs
#
  if [ -f *.out ]
  then
    echo ' '
    echo '*********************'
    echo '*** ERROR OUTPUTS ***'
    echo '*********************'
    echo ' '
    exit_code=14
    for file in *.out
    do
      echo ' '
      sed "s/^/$file : /g" $file
    done
    rm -f *.out
  fi
#
# --------------------------------------------------------------------------- #
# 8.  CLean up
#
  rm -f *.tmpl
  rm -f $modIE.*.spec
  rm -f $modIE.*.bull
  rm -f $modIE.*.cbull
#
  chmod 664 $COMOUT/$modIE.$cycle.*
#
# --------------------------------------------------------------------------- #
# 9.  Ending output
#
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                     *** End of NMWW3 ENS postprocessor ***'
  echo ' '
#
#
  if [ "$exit_code" -ne '0' ]
  then
     msg="ABNORMAL EXIT: Problem in NMWW3 ENS POST"
     postmsg "$jlogfile" "$msg"
     echo $msg
     err=exit_code ; export err ; err_chk
  fi
#
  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"
#
# End of NMWW3 ENS prostprocessor script ----------------------------------- #
