//=================================================== file = peakmean.c =====
//=  Program to compute peak-mean ratios for a series X of size N           =
//===========================================================================
//=  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) Must manually set INT_TIME                                        =
//=    4) Computes mean of interval divided by minimum value in interval    =
//=    5) Ends an interval with the first value to "cross" INT_TIME, and    =
//=       then also starts the next interval at this value as zero          =
//=       carefully study the example below!)                               =
//=    6) If an interval only contains one value, no peak-mean value is     =
//=       computed (carefully study the example below!)                     =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=    & Sample series of data which can be integers or reals.  There are   =
//=      17 values in this file. &                                          =
//=     0.01                                                                =
//=     0.07                                                                =
//=     0.04                                                                =
//=     0.02                                                                =
//=     0.02                                                                =
//=     0.05                                                                =
//=     0.06                                                                =
//=     0.01                                                                =
//=     0.02                                                                =
//=     0.02                                                                =
//=     0.02                                                                =
//=     0.02                                                                =
//=     0.02                                                                =
//=     0.23                                                                =
//=     0.05                                                                =
//=     0.06                                                                =
//=     0.03                                                                =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and INT_TIME = 0.10)                 =
//=                                                                         =
//=   ---------------------------------------------- peakmean.c -----       =
//=   1   4.000000                                                          =
//=   2   1.875000                                                          =
//=   3   1.833333                                                          =
//=   4   1.100000                                                          =
//=   &  Output 4 peak-mean values for an interval size of 0.100000         =
//=   ---------------------------------------------------------------       =
//=-------------------------------------------------------------------------=
//=  Build: gcc peakmean.c, bcc32 peakmean.c, cl peakmean.c                 =
//=-------------------------------------------------------------------------=
//=  Execute: peakmean < 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/07/00) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf()
#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 INT_TIME     0.10          // Interval time

//----- 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   pm_value;                 // Peak-mean values
  long int pm_count;                 // Count of peak-mean values
  long int v_count;                  // Count of values for an interval
  double   sum;                      // Sum for an interval
  double   min_val;                  // Minimum value for an interval
  long int i, j;                     // Loop counters

  // Load the series X
  printf("---------------------------------------------- peakmean.c -----\n");
  load_X_array();

  // Compute peak-mean ratios for X for INT_TIME intervals
  sum = 0.0;
  v_count = pm_count = 0;
  min_val = X[0];
  for (i=0; i<N; i++)
  {
    sum = sum + X[i];
    v_count++;
    if (X[i] < min_val) min_val = X[i];
    if (sum >= INT_TIME)
    {
      if (v_count > 1)
      {
        pm_value = (sum / v_count) / min_val;
        pm_count++;
        printf("%d   %f \n", pm_count, pm_value);
      }
      sum = 0.0;
      v_count = 0;
      if ((i+1) < N) min_val = X[i+1];
    }
  }

  // Output closing message
  printf("&  Output %ld peak-mean values for an interval size of %f \n",
    pm_count, INT_TIME);
  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;
}

