#!/bin/sh
###############################################################################
#                                                                             #
# This script is the postprocessor for the multi-grid great lakes 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 :                #
#                                                                             #
#  multiwavegrib2.sh        : generates GRIB2 files.                          #
#  multiwavegrads_fields.sh : generates output for GRADS                      #
#  multiwavespec.sh         : generates spectral data files for output        #
#                             locations.                                      #
#  multiwavebull_glw.sh     : generates bulletins for output locations.       #
#  multiwavetar.sh          : tars the spectral and bulletin multiple files   #
#                                                                             #
# 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  : 07/25/2007                                                   #
# Last update  : 07/26/2007                                                   #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

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

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

  set $setoff
  echo ' '
  echo '                     ********************************'
  echo '                     *** GLW 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"
  grids='grl'


  set $setoff
  echo ' '
  echo 'Grid information  :'
  echo '-------------------'
  echo "   wave grids    : $grids"
  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'
  grads_OK='no'
   spec_OK='yes'
   bull_OK='yes'

  exit_code=0

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

# 1.a Model definition files and output files 

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

    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 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=1
      field_OK='no'
      grib_OK='no'
      grads_OK='no'
    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
      field_OK='no'
      grib_OK='no'
    fi

  done

# 1.b Raw point moddef and data file

  if [ ! -f mod_def.$buoy ]
  then
     echo "  Copying mod_def.$buoy 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=2;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=2
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
  fi


# 1.c 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=3
    point_OK='no'
    spec_OK='no'
    bull_OK='no'
  fi

# 1.d Input template files

  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=4
    grib_OK='no'
  fi

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

  if [ "$grads_OK" = 'yes' ]
  then

    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=5
      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=6
    spec_OK='no'
    bull_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=7
    bull_OK='no'
  fi

# 1.e Getting buoy information for points

  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

# 1.f 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 first command file(s) 
# 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=1

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

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

  set $setoff
  echo '   Making second command file (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 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
    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
        grl) GRIDNR=176  ; MODNR=131  ; dtgrib=3600. ; ngrib=85 ;;
      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.c GRADS files

# GRADS field time step -- dtgrads
# Number of GRADS fields -- ngrads

  if [ "$grads_OK" = 'yes' ]
  then
    export gradsflags='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
        grl) dtgrads=3600. ; ngrads=999 ;;
      esac

      echo "$USHwave/multiwavegrads_fields.sh $grdID $dtgrads $ngrads > grads_$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 Spectral data files

  if [ "$spec_OK" = 'yes' ]
  then
    export dtspec=3600.   # 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.e Bulletins

  if [ "$bull_OK" = 'yes' ]
  then
    export dtbull=3600.    # time step for bulletins
    pos=1
    for buoy in $buoys
    do
      echo "$USHwave/multiwavebull_glw.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 command file
# 3.a Execution

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

  if [ "$grib_OK" = 'yes' ]
  then
    for grdID in $grids
    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.b GRADS file

  if [ "$grads_OK" = 'yes' ]
  then
    for grdID in $grids
    do
      if [ -d grads_$grdID ]
      then
        set $setoff
        echo "      Error in GRADS output for $grdID."
        postmsg "$jlogfile" "NON-FATAL ERROR in GRADS output for $grdID."
        set $seton
      else
        rm -f grads_$grdID.out
        set $setoff
        echo "      GRADS output successful for $grdID."
        set $seton
      fi
    done
  fi

# 4.c 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 $grids
    do 
      if [ -f grib_$grdID.out ]
      then
        set $setoff
        echo ' '
        echo '**************************************'
        echo '*** ERROR OUTPUT multiwavegrib2.sh ***'
        echo '**************************************'
        echo ' '
        echo "$modID post $date $cycle : error in GRIB." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavegrib1.sh"
        exit_code=8
        sed "s/^/grib_$grdID.out : /g"  grib_$grdID.out
        rm -f grib_$grdID.out
        set $seton
      fi

      if [ -f grads_$grdID.out ]
      then
        set $setoff
        echo ' '
        echo '*********************************************'
        echo '*** ERROR OUTPUT multiwavegrads_fields.sh ***'
        echo '*********************************************'
        echo ' '
        echo "$modID post $date $cycle : error in GRADS." >> $wavelog
        postmsg "$jlogfile" "NON-FATAL ERROR in multiwavegrads_fields.sh"
        exit_code=9
        sed "s/^/grads_$grdID.out : /g"  grads_$grdID.out
        rm -f grads_$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=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 multiwavebull_glw.sh ***'
      echo '*****************************************'
      echo '            Possibly in multiple calls'
      echo ' '
      echo "$modID post $date $cycle : error in bulletins." >> $wavelog
      postmsg "$jlogfile" "NON-FATAL ERROR in multiwavebull_glw.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


  nper=8
  nskip=16

  set $setoff
  echo ' '
  echo '   Making second 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

# 5.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

# 5.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

# 5.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

# 5.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

# --------------------------------------------------------------------------- #
# 6.  Execute second 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*
# --------------------------------------------------------------------------- #
# 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_$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

# 7.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

# 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
  for ID in $modID
  do
    rm -f $ID.*.spec
    rm -f $ID.*.bull
    rm -f $ID.*.cbull
    rm -f $ID.*.csbull
  done

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

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

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

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

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

# End of GLW prostprocessor script ------------------------------------------ #
