/* nag_tsa_diff (g13aac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2002.
 */

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

int main(void)
{
  /* Scalars */
  Integer  exit_status, i, d, ds, s, nx, nxd;
  NagError fail;

  /* Arrays */
  double   *x = 0, *xd = 0;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_tsa_diff (g13aac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%ld%ld%*[^\n] ", &nx, &d,
         &ds, &s);

  if (nx > 0)
    {
      /* Allocate memory */
      if (!(x = NAG_ALLOC(nx, double)) ||
          !(xd = NAG_ALLOC(nx, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }

      for (i = 1; i <= nx; ++i)
        scanf("%lf", &x[i-1]);
      scanf("%*[^\n] ");

      printf("\n");
      printf("Non-seasonal differencing of order %ld "
              "and seasonal differencing\nof order %ld "
              "with seasonality %ld are applied\n", d, ds, s);

      /* nag_tsa_diff (g13aac).
       * Univariate time series, seasonal and non-seasonal
       * differencing
       */
      nag_tsa_diff(x, nx, d, ds, s, xd, &nxd, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_tsa_diff (g13aac).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("\n");
      printf("The output array holds %2ld values, of which the "
              "first %2ld are differenced values\n\n", nx, nxd);

      for (i = 1; i <= nx; ++i)
        {
          printf("%10.1f", xd[i-1]);
          if (i % 5 == 0 || i == nx)
            printf("\n");
        }
    }

 END:
  NAG_FREE(x);
  NAG_FREE(xd);

  return exit_status;
}