/* 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 "net_stat_.h"

void
net_stat_tess (FILE * file, char* entity, char *format, struct TESS Tess)
{
  int qty;
  int i, j, status, invalqty;
  double val;
  char **invar = NULL, *valstring = NULL, *type = NULL;
  double **data = NULL;
  char** datatype = NULL;

  neut_tess_entity_qty (Tess, entity, &qty);

  ut_string_separate (format, NEUT_SEP_NODEP, &invar, &invalqty);

  data = ut_alloc_2d (invalqty, qty + 1);
  datatype = ut_alloc_1d_pchar (invalqty);

  for (j = 0; j < invalqty; j++)
    if (!strstr (invar[j], "list"))
    {
      status = neut_tess_var_val_all (Tess, NULL, NULL, NULL,
				      entity, invar[j], data[j],
				      &(datatype[j]));
      if (status != 0)
	ut_print_message (2, 0, "Expression `%s' could not be processed.\n", invar[j]);
    }

  for (i = 1; i <= qty; i++)
    for (j = 0; j < invalqty; j++)
    {
      val = 0;
      if (!strstr (invar[j], "list"))
      {
	val = data[j][i];
	ut_string_string (datatype[j], &type);
	status = 0;
      }
      else
	status =
	  neut_tess_var_val_string (Tess, entity, i, invar[j], &valstring,
				    &type);

      if (status == 0)
      {
	if (!strcmp (type, "%d"))
	  fprintf (file, "%d", ut_num_d2ri (val));
	else if (!strcmp (type, "%f"))
	  fprintf (file, "%.12f", val);
	else if (!strcmp (type, "%s"))
	  fprintf (file, "%s", valstring);
	else
	  ut_error_reportbug ();
      }
      else
	ut_print_message (2, 0, "Expression `%s' could not be processed.\n", invar[j]);

      fprintf (file, (j < invalqty - 1) ? " " : "\n");
    }

  ut_free_2d_char (invar, invalqty);
  ut_free_1d_char (type);
  ut_free_1d_char (valstring);
  ut_free_2d (data, invalqty);
  ut_free_2d_char (datatype, invalqty);

  return;
}

void
net_stat_point (FILE * file, char *format, struct POINT Point,
                struct TESS Tess)
{
  int i, j, status, invalqty, var_qty;
  double val;
  char **invar = NULL, *type = NULL, **vars = NULL;
  struct NODES Nodes;
  struct MESH Mesh;

  neut_nodes_set_zero (&Nodes);
  neut_mesh_set_zero (&Mesh);

  ut_string_separate (format, NEUT_SEP_NODEP, &invar, &invalqty);

  neut_point_var_list (&vars, &var_qty);

  for (i = 1; i <= Point.PointQty; i++)
    for (j = 0; j < invalqty; j++)
    {
      status = neut_point_var_val (Point, i, Tess, Nodes, Mesh, invar[j], &val, &type);

      if (status == 0)
      {
	if (!strcmp (type, "%d"))
	  fprintf (file, "%d", ut_num_d2ri (val));
	else if (!strcmp (type, "%f"))
	  fprintf (file, "%.12f", val);
	else
	  ut_error_reportbug ();
      }
      else
	ut_print_message (2, 0, "Expression `%s' could not be processed.\n", invar[j]);

      fprintf (file, (j < invalqty - 1) ? " " : "\n");
    }

  ut_free_2d_char (invar, invalqty);
  ut_free_2d_char (vars, var_qty);
  ut_free_1d_char (type);

  neut_nodes_free (&Nodes);
  neut_mesh_free (&Mesh);

  return;
}

void
net_stat_tesr (FILE * file, char* entity, char *format, struct TESR Tesr)
{
  int qty;
  int i, j, status, invalqty;
  double val;
  char **invar = NULL, *type = NULL;
  double** coo = NULL;
  double* size = NULL;

  neut_tesr_entity_qty (Tesr, entity, &qty);

  ut_string_separate (format, NEUT_SEP_NODEP, &invar, &invalqty);

  if (!strcmp (entity, "cell") &&
     (ut_string_inlist (format, NEUT_SEP_NODEP, "x")
   || ut_string_inlist (format, NEUT_SEP_NODEP, "y")
   || ut_string_inlist (format, NEUT_SEP_NODEP, "z")
   || ut_string_inlist (format, NEUT_SEP_NODEP, "size")))
  {
    coo = ut_alloc_2d (Tesr.CellQty + 1, 3);
    size = ut_alloc_1d (Tesr.CellQty + 1);
    neut_tesr_cellprop (Tesr, coo, size);
  }

  for (i = 1; i <= qty; i++)
    for (j = 0; j < invalqty; j++)
    {
      if (!strcmp (entity, "cell"))
      {
	if (!strcmp (invar[j], "x"))
	{
	  status = 0;
	  val = coo[i][0];
	  ut_string_string ("%f", &type);
	}
	else if (!strcmp (invar[j], "y"))
	{
	  status = 0;
	  val = coo[i][1];
	  ut_string_string ("%f", &type);
	}
	else if (!strcmp (invar[j], "z"))
	{
	  status = 0;
	  val = coo[i][2];
	  ut_string_string ("%f", &type);
	}
	else if (!strcmp (invar[j], "size"))
	{
	  status = 0;
	  val = size[i];
	  ut_string_string ("%f", &type);
	}
	else
	  status = neut_tesr_var_val (Tesr, entity, i, invar[j], &val, &type);
      }

      else
	status = neut_tesr_var_val (Tesr, entity, i, invar[j], &val, &type);

      if (status == 0)
      {
	if (!strcmp (type, "%d"))
	  fprintf (file, "%d", ut_num_d2ri (val));
	else if (!strcmp (type, "%f"))
	  fprintf (file, "%.12f", val);
	else
	  ut_error_reportbug ();
      }
      else
	ut_print_message (2, 0, "Expression `%s' could not be processed.\n", invar[j]);

      fprintf (file, (j < invalqty - 1) ? " " : "\n");
    }

  ut_free_2d_char (invar, invalqty);
  ut_free_1d_char (type);
  if (coo)
    ut_free_2d (coo, Tesr.CellQty + 1);
  ut_free_1d (size);

  return;
}
