//===================================================== file = spread.c =====
//=  Program to spread-out a series X of size N                             =
//=   - Value greater than a specific MAX_VALUE are clipped and then the    =
//=     sum of excess value is evenly added to all values in the series     =
//===========================================================================
//=  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) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=    & Sample series of data which can be integers or reals.              =
//=      There are 3 values in this file. &                                 =
//=    51                                                                   =
//=    38                                                                   =
//=    45                                                                   =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and MAX_VALUE = 42.0)                =
//=                                                                         =
//=   ------------------------------------------------ spread.c -----       =
//=     Smoothing for MAX_VALUE = 42.000000 (clip value)                    =
//=     46.000000                                                           =
//=     42.000000                                                           =
//=     46.000000                                                           =
//=   ---------------------------------------------------------------       =
//=-------------------------------------------------------------------------=
//=  Build: gcc spread.c, bcc32 spread.c, cl spread.c                       =
//=-------------------------------------------------------------------------=
//=  Execute: spread < 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/11/00) - Genesis                                      =
//===========================================================================

//----- 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      1000000L     // Maximum size of time series data array
#define MAX_VALUE         42.0     // Maximum value for clipping

//----- Globals -------------------------------------------------------------
double     X[MAX_SIZE];            // Time series read from "in.dat"
long int   N;                      // Number of values in "in.dat"

//----- Function prototypes -------------------------------------------------
void   load_X_array(void);         // Load X array

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double   excess_sum;               // Computed excess sum value
  double   excess_fraction;          // Computed excess fraction value
  long int i;                        // Loop counter

  // Load the series X
  printf("------------------------------------------------ spread.c -----\n");
  load_X_array();

  // Output MAX_VALUE
  printf("  Spreading for MAX_VALUE = %f (clip value) \n",
    MAX_VALUE);

  // Compute the excess sum
  excess_sum = 0;
  for (i=0; i<N; i++)
    if (X[i] > MAX_VALUE)
      excess_sum = excess_sum + (X[i] - MAX_VALUE);

  // Add the excess fraction to each value and output
  excess_fraction = excess_sum / N;
  for (i=0; i<N; i++)
    if (X[i] <= MAX_VALUE)
      printf("  %f \n", X[i] + excess_fraction);
    else
      printf("  %f \n", MAX_VALUE + excess_fraction);

  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;
}


