#!/bin/sh
###############################################################################
#                                                                             #
# This script is the postprocessor for the regional GRL 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 :                #
#                                                                             #
#  wavegrib.sh    : generates GRIB files.                                     #
#  wavespec.sh    : generates spectral data files for output locations.       #
#  wavebull_grl.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.               #
#                                                                             #
#                                                               July, 2004    #
# Copied from nwprod 03/12/03                                                 #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

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

  msg="Starting GRL POSTPROCESSOR SCRIPT"
  postmsg "$jlogfile" "$msg"

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

  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

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

# 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"
    set $setoff
    echo ' '
    echo '********************************************** '
    echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
    echo '********************************************** '
    echo ' '
    echo "$modID post $date $cycle : fixed file(s) missing." >> $wavelog
    echo $msg
    set $seton
    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/$modID.$cycle.outgrd out_grd.ww3
  fi

  if [ -f out_grd.ww3 ]
  then
    echo "   out_grd.ww3 exists."
  else
    set $setoff
    echo ' '
    echo '**************************************** '
    echo '*** ERROR : NO RAW FIELD OUTPUT FILE *** '
    echo '**************************************** '
    echo ' '
    echo "$modID post $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'
  fi

# 1.c Raw point data file

  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'
  fi

# 1.d Log file

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

  if [ -f log.ww3 ]
  then
    echo "   log.ww3 exists."
  else
    set $setoff
    echo ' '
    echo '*************************** '
    echo '*** ERROR : NO LOG FILE *** '
    echo '*************************** '
    echo ' '
    echo "$modID post $date $cycle : log file missing." >> $wavelog
    set $seton
    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
    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=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
    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/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
    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=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
    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=7
    bull_OK='no'
  fi

# 1.g Data summary

  set $setoff
  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 ' '
  set $seton

# --------------------------------------------------------------------------- #
# 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
    nfile=1
    cmdtype='sh'
    nskip='-'
  fi

  set $setoff
  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"
  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 GRIB file

  if [ "$grib_OK" = 'yes' ]
  then
    export dtgrib=10800.   # GRIB field time step
    export ngrib=30        # 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=176      # Assigned NCEP number for grid
    export MODNR=131       # Assigned NCEP number for model

    echo "$USHwave/wavegrib.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/wavespec.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/wavebull_grl.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

  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 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 GRIB file

  if [ "$grib_OK" = 'yes' ]
  then
    if [ -d grib ]
    then
      set $setoff
      echo '      Error in GRIB encoding.'
      postmsg "$jlogfile" "NON-FATAL ERROR in GRIB encoding."
      set $seton
      sed "s/^/grib.out : /g"  grib.out
    else
      sed "s/^/grib.out : /g"  grib.out
      rm -f grib.out
      set $setoff
      echo '      GRIB encoding successful.'
      set $seton
    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

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

  done

  if [ -f *.out ]
  then

    if [ -f grib.out ]
    then
      set $setoff
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT wavegrib.sh ***'
      echo '********************************'
      echo ' '
      echo "$modID post $date $cycle : error in GRIB." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavegrib.sh"
      exit_code=9
#      sed "s/^/grib.out : /g"  grib.out
      rm -f grib.out
      set $seton
    fi

    if [ -f spec_*.out ]
    then
      set $setoff
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT wavespec.sh ***'
      echo '********************************'
      echo '            Possibly in multiple calls'
      echo "$modID post $date $cycle : error in spectra." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavespec.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
      set $seton
    fi

    if [ -f bull_*.out ]
    then
      set $setoff
      echo ' '
      echo '********************************'
      echo '*** ERROR OUTPUT wavebull_grl.sh ***'
      echo '********************************'
      echo '            Possibly in multiple calls'
      echo ' '
      echo "$modID post $date $cycle : error in bulletins." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in wavebull_grl.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
      set $seton
    fi
  fi

  rm -f cmdfile*

# --------------------------------------------------------------------------- #
# 5.  Make second command file (tar).

  set $setoff
  echo ' '
  echo '   Making second command file (tar).'
  set $seton
  rm -f commandfile

# 5.a Spectral data files

  if [ "$spec_OK" = 'yes' ]
  then
    echo "$USHwave/wavetar.sh spec > spec_tar.out 2>&1 "   >> commandfile
  fi

# 5.b Bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/wavetar.sh bull > bull_tar.out 2>&1 "   >> commandfile
  fi

# 5.c Compressed bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/wavetar.sh cbull > cbull_tar.out 2>&1 " >> commandfile
  fi
# 5.d CSV bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    echo "$USHwave/wavetar.sh csbull > csbull_tar.out 2>&1 " >> commandfile
  fi

# --------------------------------------------------------------------------- #
# 6.  Execute second command file

  set $setoff
  echo '   Executing command file.'
  set $seton

  sh ./commandfile
  rm -f commandfile

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

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

# 7.a Spectral tar file

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

# 7.b Bulletin tar file

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

    if [ -d tar_cbull ]
    then
      set $setoff
      echo '      Error in compressed bulletin tar file.'
      echo "$modID post $date $cycle : error in compressed bulletin tar." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in compressed bulletin tar file."
    else
      rm -f cbull_tar.out
      set $setoff
      echo '      Compressed bulletin tar file OK.'
      set $seton
    fi
    if [ -d tar_csbull ]
    then
      set $setoff
      echo '      Error in csv bulletin tar file.'
      echo "$modID post $date $cycle : error in csv bulletin tar." >> $wavelog
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in csv bulletin tar file."
                                                                                                          
    else
      rm -f csbull_tar.out
      set $setoff
      echo '      CSV bulletin tar file OK.'
      set $seton
    fi
                                                                                                          
  fi

# 7.c Error outputs

  if [ -f *.out ]
  then
    set $setoff
    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
    set $seton
  fi

# --------------------------------------------------------------------------- #
# 8.  CLean up

  rm -f *.tmpl
  rm -f $modID.*.spec
  rm -f $modID.*.bull
  rm -f $modID.*.cbull
  rm -f $modID.*.csbull

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

# --------------------------------------------------------------------------- #
# 9.  Ending output

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

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

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

# End of GRL prostprocessor script ----------------------------------------- #
