/* nag_tsa_inhom_iema (g13mec) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 24, 2013.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg13.h>

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

  /* NAG structures and types */
  NagError fail;
  Nag_TS_Interpolation inter[2];

  /* Double scalar and array declarations */
  double tau;
  double *iema = 0, *rcomm = 0, *sinit = 0, *t = 0;

  /* Character scalar and array declarations */
  char cinter[40];

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

  printf("nag_tsa_inhom_iema (g13mec) Example Program Results\n\n");

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

  /* Read in the number of iterations required */
  scanf("%ld%*[^\n] ",&m);

  /* Read in the interpolation method to use */
  scanf("%39s",cinter);
  inter[0] = (Nag_TS_Interpolation) nag_enum_name_to_value(cinter); 
  scanf("%39s",cinter);
  inter[1] = (Nag_TS_Interpolation) nag_enum_name_to_value(cinter);

  /* Read in the decay parameter */
  scanf("%lf%*[^\n] ", &tau);

  /* Read in the initial values */
  if (!(sinit = NAG_ALLOC(m + 2, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 0; i < m + 2; i++)
    {
      scanf("%lf", &sinit[i]);
    }
  scanf("%*[^\n] ");

  /* Print some titles */
  printf("                         Iterated\n");
  printf("              Time          EMA\n");
  printf(" --------------------------------\n");

  if (!(rcomm = NAG_ALLOC(m + 20, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  
  /* 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 IEMA and T to the required size */
      NAG_FREE(iema);
      NAG_FREE(t);
      if (!(iema = NAG_ALLOC(nb, double)) ||
          !(t = 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%lf",&t[i], &iema[i]);
        }
      scanf("%*[^\n] ");

      /* Call nag_tsa_inhom_iema (g13mec) to update the iterated EMA for
         this block of data. The routine overwrites the input data with 
         the iterated EMA */
      nag_tsa_inhom_iema(nb,iema,t,tau,m,sinit,inter,&pn,rcomm,&fail);
      if (fail.code != NE_NOERROR) 
        {
          printf("Error from nag_tsa_inhom_iema (g13mec).\n%s\n",
                 fail.message);
          exit_status = -1;
          goto END;
        }

      /* Display the results for this block of data */
      for (i = 0; i < nb; i++)
        {
          printf(" %3ld    %10.1f    %10.3f\n",pn-nb+i+1,t[i],
                 iema[i]);
        }
      printf("\n");
    }

 END:
  NAG_FREE(iema);
  NAG_FREE(t);
  NAG_FREE(sinit);
  NAG_FREE(rcomm);

  return(exit_status);
}