/* nag_cwt_1d_real (c09bac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */
#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("%ld%ld", &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("%ld%*[^\n]", &wparam);
  printf("Parameters read from file ::\n");
  printf("    Wavelet : %20s,  wparam : %ld\n", wavnam, wparam);
  printf("          n : %20ld,   nscal : %ld\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("%9ld", 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 :%9ld\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 %3ld", 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;
}