//===================================================== file = shape2.c =====
//=  Program to shape a series X of size N                                  =
//=   - Shapes to exponential values (Poisson interarrival times)           =
//===========================================================================
//=  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                                               =
//=    3) The sum of the shaped series will not be exactly the same         =
//=       as the original series due to the statistical nature of using     =
//=       a probability distribution.                                       =
//=-------------------------------------------------------------------------=
//= 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")                                     =
//=                                                                         =
//=   ------------------------------------------------ shape2.c -----       =
//=     203.266316                                                          =
//=     246.990709                                                          =
//=     48.828007                                                           =
//=   ---------------------------------------------------------------       =
//=-------------------------------------------------------------------------=
//=  Build: gcc shape2.c, bcc32 shape2.c, cl shape2.c                       =
//=-------------------------------------------------------------------------=
//=  Execute: shape2 < 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 <math.h>                  // Needed for log()
#include <string.h>                // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define MAX_SIZE      1000000L     // Maximum size of time series data array

//----- 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
double expon(double x);            // Generate exponential RV with mean x

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  double   mean;                     // Computed mean value
  long int i;                        // Loop counter

  // Load the series X
  printf("------------------------------------------------ shape2.c -----\n");
  load_X_array();

  // Compute the mean
  mean = 0.0;
  for (i=0; i<N; i++)
    mean = mean + ((double) X[i] / N);

  // Output the new time series (pulling an exponential distribution)
  for (i=0; i<N; i++)
    printf("  %f \n", expon(mean));

  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;
}

//===========================================================================
//=  Function to generate exponentially distributed random variables        =
//=    - Input:  Mean value of distribution                                 =
//=    - Output: Returns with exponentially distributed random variable     =
//===========================================================================
double expon(double x)
{
  double z;                     // Uniform random number (0 < z < 1)
  double exp_value;             // Computed exponential value to be returned

  // Pull a uniform random number (0 < z < 1)
  do
  {
    z = ((double) rand() / RAND_MAX);
  }
  while ((z == 0) || (z == 1));

  // Compute exponential random variable using inversion method
  exp_value = -x * log(z);

  return(exp_value);
}

