/* nag_cwt_1d_real (c09bac) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <nag.h>
#include <nag_stdlib.h>
#include <nagc09.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  Integer i, n, nscal, wparam;
  /* Arrays */
  Integer *scales = 0;
  double *c = 0, *x = 0;
  char *clabs = 0, **clabsc = 0;
  char wavnam[20];
  /* NAG types */
  NagError fail;
  Nag_Wavelet wavnamenum;

  INIT_FAIL(fail);

  printf("nag_cwt_1d_real (c09bac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  /* Read problem parameters */
  scanf("%" NAG_IFMT "%" NAG_IFMT "", &n, &nscal);
  scanf("%*[^\n]");
  if (!(c = NAG_ALLOC((nscal) * (n), double)) ||
      !(scales = NAG_ALLOC((nscal), Integer)) ||
      !(x = NAG_ALLOC((n), double)) ||
      !(clabs = NAG_ALLOC(10 * 10, char)) || !(clabsc = NAG_ALLOC(10, char *))
         )
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  scanf("%19s", wavnam);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  wavnamenum = (Nag_Wavelet) nag_enum_name_to_value(wavnam);
  scanf("%" NAG_IFMT "%*[^\n]", &wparam);
  printf("Parameters read from file ::\n");
  printf("    Wavelet : %20s,  wparam : %" NAG_IFMT "\n", wavnam, wparam);
  printf("          n : %20" NAG_IFMT ",   nscal : %" NAG_IFMT "\n", n,
         nscal);

  /* Read data array and write it out */
  for (i = 0; i < nscal; i++)
    scanf("%" NAG_IFMT "", &scales[i]);
  scanf("%*[^\n] ");
  for (i = 0; i < n; i++)
    scanf("%lf", &x[i]);
  scanf("%*[^\n] ");
  printf("Input Data ::\n");
  printf("     Scales :");
  for (i = 0; i < nscal; i++)
    printf("%9" NAG_IFMT "", scales[i]);

  printf("\n          x :");
  for (i = 0; i < n; i++)
    printf("%9.3f%s", x[i], (i + 1) % 5 ? "" : "\n             ");
  printf("\n");
  /* nag_cwt_1d_real (c09bac).
   * One-dimensional real continuous wavelet transform
   */
  nag_cwt_1d_real(wavnamenum, wparam, n, x, nscal, scales, c, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_cwt_1d_real (c09bac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Number of Scales :%9" NAG_IFMT "\n\n", nscal);
  fflush(stdout);

  /* print coefficients for each scale as columns of a matrix. This requires
   * printing c as a row major matrix with principal dimension nscal using
   * nag_gen_real_mat_print_comp (x04cbc).
   */
  for (i = 0; i < nscal; i++)
    sprintf(&clabs[10 * i], "scale %3" NAG_IFMT "", scales[i]);
  for (i = 0; i < nscal; i++)
    clabsc[i] = &clabs[i * 10];
  nag_gen_real_mat_print_comp(Nag_RowMajor, Nag_GeneralMatrix,
                              Nag_NonUnitDiag, n, nscal, c, nscal, "%13.4e",
                              "Wavelet coefficients C ::", Nag_NoLabels, 0,
                              Nag_CharacterLabels, (const char **) clabsc, 80,
                              0, NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 6;
    goto END;
  }

END:
  NAG_FREE(c);
  NAG_FREE(x);
  NAG_FREE(scales);
  NAG_FREE(clabs);
  NAG_FREE(clabsc);
  return exit_status;
}