/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2014, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"neut_tesr_fprintf_.h"

void
neut_tesr_fprintf_head (FILE * file, char *format, struct TESR Tesr)
{
  fprintf (file, "***tesr\n");
  fprintf (file, " **format\n");
  fprintf (file, "   2.0 %s\n", format);
  fprintf (file, " **general\n");
  fprintf (file, "   %d\n", Tesr.Dim);
  fprintf (file, "   ");
  ut_array_1d_int_fprintf (file, Tesr.size, Tesr.Dim, "%d");
  fprintf (file, "   ");
  ut_array_1d_fprintf (file, Tesr.vsize, Tesr.Dim, "%.12f");

  return;
}

void
neut_tesr_fprintf_data (FILE * file, char *format, struct TESR Tesr)
{
  fprintf (file, " **data\n");
  neut_tesr_fprintf_raw (file, format, Tesr);

  return;
}

void
neut_tesr_fprintf_cell (FILE * file, struct TESR Tesr)
{
  int i, j, count = 0;
  double* e = ol_e_alloc ();

  fprintf (file, " **cell\n");
  fprintf (file, "   %d\n", Tesr.CellQty);

  if (Tesr.CellId)
  {
    fprintf (file, "  *id\n   ");
    for (i = 1; i <= Tesr.CellQty; i++)
      ut_print_wnc_int (file, Tesr.CellId[i], &count, 72);
    fprintf (file, "\n");
  }

  if (Tesr.SeedCoo && Tesr.SeedWeight)
  {
    fprintf (file, "  *seed\n");
    for (i = 1; i <= Tesr.CellQty; i++)
    {
      fprintf (file, " %3d ", i);
      for (j = 0; j < 3; j++)
	fprintf (file, "%.12f ", Tesr.SeedCoo[i][j]);
      fprintf (file, "%.12f\n", Tesr.SeedWeight[i]);
    }
  }

  if (Tesr.CellOri)
  {
    fprintf (file, "  *ori\n");
    fprintf (file, "   e\n");
    for (i = 1; i <= Tesr.CellQty; i++)
    {
      fprintf (file, "   ");
      ol_q_e (Tesr.CellOri[i], e);
      ol_e_fprintf (file, e, "%17.12f");
    }
  }

  ol_e_free (e);

  return;
}

void
neut_tesr_fprintf_data_raw (FILE * file, char *rawname, char *format,
			   struct TESR Tesr)
{
  fprintf (file, " **data\n");
  fprintf (file, "  *file %s", rawname);

  FILE *file2 = ut_file_open (rawname, "w");
  neut_tesr_fprintf_raw (file2, format, Tesr);
  ut_file_close (file2, rawname, "w");

  return;
}

void
neut_tesr_fprintf_raw (FILE * file, char *format, struct TESR Tesr)
{
  int i, j, k, count;
  int* size = ut_alloc_1d_int (3);
  // char* progress = ut_alloc_1d_char (10);

  ut_array_1d_int_set (size, 3, 1);
  ut_array_1d_int_memcpy (size, Tesr.Dim, Tesr.size);

  if (!strcmp (format, "ascii"))
  {
    count = 0;
    for (k = 1; k <= size[2]; k++)
      for (j = 1; j <= size[1]; j++)
	for (i = 1; i <= size[0]; i++)
	  ut_print_wnc_int (file, Tesr.VoxCell[i][j][k], &count, 72);
  }

  else if (!strcmp (format, "binary8"))
  {
    unsigned char data;

    for (k = 1; k <= size[2]; k++)
      for (j = 1; j <= size[1]; j++)
	for (i = 1; i <= size[0]; i++)
	{
	  data = Tesr.VoxCell[i][j][k];
	  fwrite (&data, sizeof (unsigned char), 1, file);
	}
  }

  else if (!strcmp (format, "binary16"))
  {
    short data;

    for (k = 1; k <= size[2]; k++)
      for (j = 1; j <= size[1]; j++)
	for (i = 1; i <= size[0]; i++)
	{
	  data = Tesr.VoxCell[i][j][k];
	  fwrite (&data, sizeof (short), 1, file);
	}
  }

  else if (!strcmp (format, "binary32"))
  {
    int data;

    for (k = 1; k <= size[2]; k++)
      for (j = 1; j <= size[1]; j++)
	for (i = 1; i <= size[0]; i++)
	{
	  data = Tesr.VoxCell[i][j][k];
	  fwrite (&data, sizeof (int), 1, file);
	}
  }

  else
    abort ();

  // ut_free_1d_char (progress);
  ut_free_1d_int (size);

  return;
}

void
neut_tesr_fprintf_foot (FILE * file)
{
  fprintf (file, "\n***end\n");

  return;
}
