/* nag_tsa_diff (g13aac) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#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("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\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 %" NAG_IFMT " "
           "and seasonal differencing\nof order %" NAG_IFMT " "
           "with seasonality %" NAG_IFMT " 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 %2" NAG_IFMT " values, of which the "
           "first %2" NAG_IFMT " 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;
}