/* nag_summary_stats_onevar (g01atc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 24, 2013.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer  b, i, ierr, iwt, nb, pn;
  Integer  exit_status = 0;

  /* NAG structures and types */
  NagError fail;

  /* Double scalar and array declarations */
  double   xkurt, xmax, xmean, xmin, xsd, xskew;
  double   rcomm[20];
  double   *wt = 0, *x = 0;

  /* Initialise the error structure */
  INIT_FAIL(fail);

  printf("nag_summary_stats_onevar (g01atc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Initialise the number of valid observations processed so far */
  pn = 0;

  /* Loop over each block of data */
  for (b = 0;; )
    {
      /* Read in the number of observations in this block and a flag indicating
         whether weights have been supplied (iwt = 1) or not (iwt = 0) */
      ierr = scanf("%ld%ld", &nb, &iwt);
      if (ierr == EOF || ierr < 2) break;
      scanf("%*[^\n] ");

      /* Keep a running total of the number of blocks of data */
      b++;

      /* Reallocate X to the required size */
      NAG_FREE(x);
      if (!(x = NAG_ALLOC(nb, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }

      /* Read in the data for this block */
      if (iwt)
        {
          /* Weights supplied, so reallocate WT to the required size */
          NAG_FREE(wt);
          if (!(wt = NAG_ALLOC(nb, double)))
            {
              printf("Allocation failure\n");
              exit_status = -2;
              goto END;
            }
          for (i = 0; i < nb; i++)
            scanf("%lf%lf", &x[i], &wt[i]);
        }
      else
        {
          /* No weights */
          NAG_FREE(wt);
          wt = 0;

          for (i = 0; i < nb; i++)
            scanf("%lf", &x[i]);
        }
      scanf("%*[^\n] ");

      /* Call nag_summary_stats_onevar (g01atc) to update the summaries for
         this block of data */
      nag_summary_stats_onevar(nb, x, wt, &pn, &xmean, &xsd, &xskew, &xkurt,
                               &xmin, &xmax, rcomm, &fail);
      if (fail.code != NE_NOERROR && fail.code != NE_CASES_ONE &&
          fail.code != NE_ZERO_VARIANCE && fail.code != NE_CASES_ZERO)
        {
          printf("Error from nag_summary_stats_onevar (g01atc).\n%s\n",
                 fail.message);
          exit_status = 1;
          goto END;
        }
    }

  /* Display the results */
  printf(" Data supplied in %ld blocks\n", b);
  if (fail.code == NE_CASES_ZERO)
    printf(" No valid observations supplied. All weights are zero.\n");
  else
    {
      printf(" %ld valid observations\n", pn);
      printf(" Mean          %13.2f\n", xmean);
      if (fail.code == NE_CASES_ONE)
        {
          printf("   Unable to calculate the standard deviation,");
          printf(" skewness or kurtosis\n");
        }
      else
        {
          printf(" Std devn      %13.2f\n", xsd);
          if (fail.code == NE_ZERO_VARIANCE)
            printf("   Unable to calculate the skewness and kurtosis\n");
          else
            {
              printf(" Skewness      %13.2f\n", xskew);
              printf(" Kurtosis      %13.2f\n", xkurt);
            }
        }
      printf(" Minimum       %13.2f\n", xmin);
      printf(" Maximum       %13.2f\n", xmax);
    }

 END:
  NAG_FREE(x);
  NAG_FREE(wt);

  return(exit_status);
}