/* nag_moving_average (g01wac) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer i, ierr, lrcomm, m, nb, offset, pn, nsummaries;
  Integer exit_status = 0;

  /* NAG structures and types */
  NagError fail;
  Nag_Weightstype iwt;
  Nag_Boolean want_sd;

  /* Double scalar and array declarations */
  double *rcomm = 0, *rmean = 0, *rsd = 0, *x = 0, *wt = 0;

  /* Character scalar and array declarations */
  char ciwt[40], cwant_sd[40];

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

  printf("nag_moving_average (g01wac) Example Program Results\n\n");

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

  /* Read in the problem size */
  scanf("%39s%" NAG_IFMT "%*[^\n] ", ciwt, &m);
  iwt = (Nag_Weightstype) nag_enum_name_to_value(ciwt);

  /* Read in a flag indicating whether we want the standard deviations */
  scanf("%39s%*[^\n] ", cwant_sd);
  want_sd = (Nag_Boolean) nag_enum_name_to_value(cwant_sd);

  /* Initial handling of weights */
  if (iwt == Nag_WeightWindow) {
    /* Each observation in the rolling window has its own weight */
    if (!(wt = NAG_ALLOC(m, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    for (i = 0; i < m; i++) {
      scanf("%lf", &wt[i]);
    }
    scanf("%*[^\n] ");
  }

  /* Allocate memory for the communication array */
  lrcomm = 2 * m + 20;
  if (!(rcomm = NAG_ALLOC(lrcomm, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Print some titles */
  if (want_sd) {
    printf("                               Standard\n");
    printf("  Interval         Mean        Deviation\n");
    printf(" ---------------------------------------\n");
  }
  else {
    printf("  Interval         Mean  \n");
    printf(" ------------------------\n");
  }

  /* Loop over each block of data */
  for (pn = 0;;) {
    /* Read in the number of observations in this block */
    ierr = scanf("%" NAG_IFMT, &nb);
    if (ierr == EOF || ierr < 1)
      break;
    scanf("%*[^\n] ");

    /* 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 */
    for (i = 0; i < nb; i++) {
      scanf("%lf", &x[i]);
    }
    scanf("%*[^\n] ");

    if (iwt == Nag_WeightObs) {
      /* User supplied weights are present */

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

      /* Read in the weights for this block */
      for (i = 0; i < nb; i++) {
        scanf("%lf", &wt[i]);
      }
      scanf("%*[^\n] ");
    }

    /* Calculate the number of summaries we can produce */
    nsummaries = MAX(0, nb + MIN(0, pn - m + 1));

    /* Reallocate the output arrays */
    NAG_FREE(rmean);
    if (!(rmean = NAG_ALLOC(nsummaries, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    if (want_sd) {
      NAG_FREE(rsd);
      if (!(rsd = NAG_ALLOC(nsummaries, double)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
    }

    /* nag_moving_average (g01wac):
       Calculate the moving average (and optionally the standard deviation)
       for this block of data
     */
    nag_moving_average(m, nb, x, iwt, wt, &pn, rmean, rsd, rcomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_moving_average (g01wac).\n%s\n", fail.message);
      exit_status = -1;
      if (fail.code != NW_POTENTIAL_PROBLEM)
        goto END;
    }

    /* Number of results printed so far */
    offset = MAX(1, pn - nb - m + 2);

    /* Display the results for this block of data */
    if (want_sd) {
      for (i = 0; i < nsummaries; i++) {
        printf(" [%3" NAG_IFMT ",%3" NAG_IFMT "]    "
               "%10.1f    %10.1f\n",
               i + offset, i + m + offset - 1, rmean[i], rsd[i]);
      }
    }
    else {
      for (i = 0; i < nsummaries; i++) {
        printf(" [%3" NAG_IFMT ",%3" NAG_IFMT "]    %10.1f\n",
               i + offset, i + m + offset - 1, rmean[i]);
      }
    }
  }

  printf("\n");
  printf(" Total number of observations : %3" NAG_IFMT "\n", pn);
  printf(" Length of window             : %3" NAG_IFMT "\n", m);

END:
  NAG_FREE(x);
  NAG_FREE(wt);
  NAG_FREE(rmean);
  NAG_FREE(rsd);
  NAG_FREE(rcomm);

  return (exit_status);
}