//======================================================= file = rate.c =====
//=  Program to convert <delta_time_stamp, num_bytes> to rate values        =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * Input is two columns of reals with no commas                   =
//=        * No comments are allowed in the input file                      =
//=    2) The BLOCK_SIZE is the number of tuples to block together          =
//=    3) The CLK_TYPE is for the output to be delta or cumulative time     =
//=    4) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=   0.01  100                                                             =
//=   0.02  125                                                             =
//=   0.01   80                                                             =
//=   0.03   90                                                             =
//=   0.01  100                                                             =
//=   0.02  200                                                             =
//=   0.01  100                                                             =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat", BLOCK_SIZE = 2, and CLK_TYPE = 2)   =
//=                                                                         =
//=   & --------------------------------------------- rate.c ----- &        =
//=   0.030000 60000.000000                                                 =
//=   0.070000 34000.000000                                                 =
//=   0.100000 80000.000000                                                 =
//=   & ---------------------------------------------------------- &        =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 rate.c                                                    =
//=-------------------------------------------------------------------------=
//=  Execute: rate < in.dat                                                 =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (05/14/02) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for exit() and ato*()
#include <string.h>                // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define BLOCK_SIZE           2     // Block size for computing rates
#define CLK_TYPE             2     // Output is 1 = delta, 2 = cumul time

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double    time_stamp;            // Delta time stamp of a tuple
  double    num_bytes;             // Number of bytes of a tuple
  double    sum_time;              // Running sum of time
  double    cumul_time;            // Running cumulative time
  double    sum_bytes;             // Running sum of bytes
  double    block_mean;            // Compute block mean
  char      instring1[1024];       // Input string #1
  char      instring2[1024];       // Input string #2
  int       i;                     // Loop counter

  // Output a banner
  printf("& --------------------------------------------- rate.c ----- & \n");

  // Compute rates until end-of-file
  cumul_time = 0.0;
  while(1)
  {
    // Compute a rate for block_size values
    sum_time = 0.0;
    sum_bytes = 0;
    for (i=0; i<BLOCK_SIZE; i++)
    {
      // Get time_stamp and num_bytes
      scanf("%s  %s \n", instring1, instring2);
      time_stamp = atof(instring1);
      num_bytes = atof(instring2);
      if (feof(stdin)) goto end;

      // Keep running sums
      sum_time = sum_time + time_stamp;
      sum_bytes = sum_bytes + num_bytes;
    }

    // Compute and output rate
    if (CLK_TYPE == 1)
    {
      printf("%f %f \n", sum_time, ((8.0 * sum_bytes) / sum_time));
    }
    else
    {
      cumul_time = cumul_time + sum_time;
      printf("%f %f \n", cumul_time, ((8.0 * sum_bytes) / sum_time));
    }
  }

  // End-of-file escape
  end:

  // Output closing banner
  printf("& ---------------------------------------------------------- & \n");
}

