#!/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 :                #
#                                                                             #
#  wavegribens.sh    : generates GRIB files.                                  #
#  wavespecens.sh    : generates spectral data files for output locations.    #
#  wavebullens.sh    : generates bulletins for output locations.              #
#                                                                             #
# 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.               #
#                                                                             #
#                                                                June, 2002   #
# Copied from nwprod 06/18/02                                                 #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation
#
  cd $DATA
#
  postmsg "$jlogfile" "HAS BEGUN on `hostname`"
#
  msg="Starting NWW3ens POSTPROCESSOR SCRIPT"
  postmsg "$jlogfile" "$msg"
#
  echo ' '
  echo '                     *********************************'
  echo '                     *** NWW3ens 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}
#
# --------------------------------------------------------------------------- #
# 1.  Get files that are used by most child scripts
#
  field_OK='yes'
  point_OK='yes'
  grib_OK='yes'
  spec_OK='yes'
  bull_OK='yes'
  exit_code=0
#
  echo ' '
  echo 'Preparing input files :'
  echo '-----------------------'
#
# 1.a Model definition file
#
  if [ ! -f mod_def.ww3 ] ; then
    echo "   Copying mod_def.ww3 from $FIXwave"
    cp $FIXwave/wave_$modID.moddef mod_def.ww3
  fi
#
  if [ -f mod_def.ww3 ] ; then
    echo "   mod_def.ww3 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
#
# 1.b Raw field data file
#
  if [ ! -f out_grd.ww3 ] ; then
    echo "   Copying out_grd.ww3 from $COMIN"
    cp ${COMIN}/$modIE.$cycle.outgrd out_grd.ww3
  fi
#
  if [ -f out_grd.ww3 ] ; then
    echo "   out_grd.ww3 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
#
# 1.c Raw point data file
#
  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"
    exit_code=3
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
  fi
#
# 1.d Log file
#
  if [ ! -f log.ww3 ] ; then
    echo "   Copying log.ww3 from $COMIN"
    cp ${COMIN}/$modIE.$cycle.log log.ww3
  fi
#
  if [ -f log.ww3 ] ; then
    echo "   log.ww3 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"
    exit_code=4
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
  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"
    exit_code=5
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
  fi
#
# 1.f Input template files
#
  if [ -f $FIXwave/wavegrib.inp.tmpl ] ; then
    cp $FIXwave/wavegrib.inp.tmpl wavegrib.inp.tmpl
  fi
#
  if [ -f wavegrib.inp.tmpl ] ; then
    echo "   wavegrib.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/wavespec.inp.tmpl ] ; then
    cp $FIXwave/wavespec.inp.tmpl wavespec.inp.tmpl
  fi
#
  if [ -f wavespec.inp.tmpl ] ; then
    echo "   wavespec.inp.tmpl copied."
  else
    echo ' '
    echo '*********************************************** '
    echo '*** ERROR : NO TEMPLATE FOR SPEC INPUT FILE *** '
    echo '*********************************************** '
    echo ' '
    echo "$modIE post $date $cycle : specra template file missing." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR SPEC INPUT FILE"
    exit_code=7
    spec_OK='no'
    bull_OK='no'
  fi
#
  if [ -f $FIXwave/wavebull.inp.tmpl ] ; then
    cp $FIXwave/wavebull.inp.tmpl wavebull.inp.tmpl
  fi
#
  if [ -f wavebull.inp.tmpl ] ; then
    echo "   wavebull.inp.tmpl copied."
  else
    echo ' '
    echo '*************************************************** '
    echo '*** ERROR : NO TEMPLATE FOR BULLETIN INPUT FILE *** '
    echo '*************************************************** '
    echo ' '
    echo "$modIE post $date $cycle : bulletin template file missing." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR : NO TEMPLATE FOR BULLETIN INPUT FILE"
    exit_code=7
    bull_OK='no'
  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='-'
  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
#
  if [ "$grib_OK" = 'yes' ]
  then
    export dtgrib=10800.   # GRIB field time step
    export ngrib=47        # Number of GRIB fields
    export gribflags=' F F T F F  T F T T F  T T T T F  F F F'
    export GRIDNR=233      # Assigned NCEP number for grid
    export MODNR=88        # Assigned NCEP number for model
    echo "$USHwave/wavegribens.sh > grib.out 2>&1"               >> cmdfile.$ifile
  fi
#
  if [ "$nfile" -gt '1' ]
  then
    ifile=`expr $ifile + 1`
  fi
#
# 2.c Spectral data files
#
  buoys=`sed "s/\'/ /g" buoy.loc | awk '{ print $3 }'`
#
  if [ "$spec_OK" = 'yes' ]
  then
    export dtspec=10800.   # time step for spectra
    for buoy in $buoys
    do
      echo "$USHwave/wavespecens.sh $buoy > spec_$buoy.out 2>&1" >> cmdfile.$ifile
      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=1
          fi
        fi
      fi
    done
  fi
#
# 2.d Bulletins
#
  if [ "$bull_OK" = 'yes' ]
  then
    export dtbull=3600.    # time step for bulletins
    for buoy in $buoys
    do
      echo "$USHwave/wavebullens.sh $buoy > bull_$buoy.out 2>&1" >> cmdfile.$ifile
      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=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
    if [ -d grib ]
    then
      echo '      Error in GRIB encoding.'
      postmsg "$jlogfile" "NON-FATAL ERROR in GRIB encoding."
      sed "s/^/grib.out : /g"  grib.out
    else
      sed "s/^/grib.out : /g"  grib.out
      rm -f grib.out
      echo '      GRIB encoding successful.'
    fi
  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
#
    if [ -f grib.out ]
    then
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT wavegribens.sh ***'
      echo '********************************'
      echo ' '
      echo "$modIE post $date $cycle : error in GRIB." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavegribens.sh"
      exit_code=9
#      sed "s/^/grib.out : /g"  grib.out
      rm -f grib.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=10
      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=11
      for file in bull_*.out
      do
        echo ' '
        sed "s/^/$file : /g" $file
      done
      rm -f bull_*.out
    fi
  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/wavetarens.sh spec > spec_tar.out 2>&1 "   >> commandfile
  fi
#
# 5.b Bulletins
#
  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/wavetarens.sh bull > bull_tar.out 2>&1 "   >> commandfile
  fi
#
# 5.c Compressed bulletins
#
  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/wavetarens.sh cbull > 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 ]
    then
      echo '      Error in spectral tar file.'
      echo "$modIE post $date $cycle : error in spectral tar." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in spectral tar file."
    else
      rm -f spec_tar.out
      echo '      Spectral tar file OK.'
    fi
  fi
#
# 7.b Bulletin tar files
#
  if [ "$bull_OK" = 'yes' ]
  then
    if [ -d tar_bull ]
    then
      echo '      Error in bulletin tar file.'
      echo "$modIE post $date $cycle : error in bulletin tar." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in bulletin tar file."
    else
      rm -f bull_tar.out
      echo '      Bulletin tar file OK.'
    fi

    if [ -d tar_cbull ]
    then
      echo '      Error in compressed bulletin tar file.'
      echo "$modIE post $date $cycle : error in compressed bulletin tar." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in compressed bulletin tar file."
    else
      rm -f cbull_tar.out
      echo '      Compressed bulletin tar file OK.'
    fi
  fi
#
# 7.c Error outputs
#
  if [ -f *.out ]
  then
    echo ' '
    echo '*********************'
    echo '*** ERROR OUTPUTS ***'
    echo '*********************'
    echo ' '
    exit_code=12
    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 NWW3ens postprocessor ***'
  echo ' '
#
#
  if [ "$exit_code" -ne '0' ]
  then
     msg="ABNORMAL EXIT: Problem in NWW3ens POST"
     postmsg "$jlogfile" "$msg"
     echo $msg
     err=exit_code ; export err ; err_chk
  fi
#
  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"
#
# End of NWW3ens prostprocessor script ---------------------------------------- #
