//====================================================== file = block.c =====
//=  Program to block a time series X into block means                      =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * Comments are bounded by "&" characters at the beginning and    =
//=          end of the comment block                                       =
//=    2) The block size M is in the #define section                        =
//=    3) If mod(N,M) is not zero, then the last remainder values are not   =
//=       blocked
//=    4) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=   & Here is a series of 6 values to be blocked with M = 2  &            =
//=   21                                                                    =
//=   3                                                                     =
//=   55                                                                    =
//=   45                                                                    =
//=   12                                                                    =
//=   5                                                                     =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and M = 2):                          =
//=                                                                         =
//=   & -------------------------------------------- block.c ----- &        =
//=     12.000000                                                           =
//=     50.000000                                                           =
//=     8.500000                                                            =
//=   &  Output 3 block means for a block size of 2                         =
//=     ---------------------------------------------------------- &        =
//=-------------------------------------------------------------------------=
//=  Build: gcc block.c, bcc32 block.c, cl block.c                          =
//=-------------------------------------------------------------------------=
//=  Execute: block < in.dat                                                =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (10/02/98) - Genesis                                      =
//=           KJC (02/24/99) - Fixed a compile error                        =
//=           KJC (06/31/99) - Fixed error with not finishing series        =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for exit() and atof()
#include <string.h>                // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define MAX_SIZE 1000000           // Max size of time series data array
#define M             10           // Blocking size

//----- Globals -------------------------------------------------------------
double     X[MAX_SIZE];            // Time series read from "in.dat"
int        N;                      // Number of values in X[]

//----- Prototypes ----------------------------------------------------------
void   load_X_array(void);         // Load X array

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  int       count;                 // Count of number of blocks
  double    sum;                   // Temporary sum variable
  double    block_mean;            // Compute block mean
  int       i, j;                  // Loop counters

  // Load the series X
  printf("& -------------------------------------------- block.c ----- & \n");
  load_X_array();

  // Compute and output block means (aggregated blocks of size M)
  count = 0;
  for (i=0; i<N; i=i+M)
  {
    sum = 0.0;
    for (j=i; j<(i + M); j++)
    {
      if (j >= N) goto end;
      sum = sum + X[j];
    }

    count = count + 1;
    block_mean = sum / M;
    printf("%f \n", block_mean);
  }

  // End of series escape
  end:

  // Output closing message
  printf("&  Output %ld block means for a block size of %ld \n", count, M);
  printf("  ---------------------------------------------------------- & \n");
}

//===========================================================================
//=  Function to load X array from stdin and determine N                    =
//===========================================================================
void load_X_array(void)
{
  char      temp_string[1024];     // Temporary string variable

  // Read all values into X
  N = 0;
  while(1)
  {
    scanf("%s", temp_string);
    if (feof(stdin)) goto end;

    // This handles a comment bounded by "&" symbols
    while (strcmp(temp_string, "&") == 0)
    {
      do
      {
        scanf("%s", temp_string);
        if (feof(stdin)) goto end;
      } while (strcmp(temp_string, "&") != 0);
      scanf("%s", temp_string);
      if (feof(stdin)) goto end;
    }

    // Enter value in array and increment array index
    X[N] = atof(temp_string);
    N++;

    // Check if MAX_SIZE data values exceeded
    if (N >= MAX_SIZE)
    {
      printf("*** ERROR - greater than %ld data values \n", MAX_SIZE);
      exit(1);
    }
  }

  // End-of-file escape
  end:

  return;
}

