/* --------------------------------------------------------------------  */
/*                          CALCULIX                                     */
/*                   - GRAPHICAL INTERFACE -                             */
/*                                                                       */
/*     A 3-dimensional pre- and post-processor for finite elements       */
/*              Copyright (C) 1996 Klaus Wittig                          */
/*                                                                       */
/*     This program is free software; you can redistribute it and/or     */
/*     modify it under the terms of the GNU General Public License as    */
/*     published by the Free Software Foundation; version 2 of           */
/*     the License.                                                      */
/*                                                                       */
/*     This program is distributed in the hope that it will be useful,   */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */ 
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
/*     GNU General Public License for more details.                      */
/*                                                                       */
/*     You should have received a copy of the GNU General Public License */
/*     along with this program; if not, write to the Free Software       */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
/* --------------------------------------------------------------------  */

/* BUGS
 */

#include <cgx.h>
#include <time.h>

extern char **environ;


extern int   offset, maxIndex;                            /* offset+steps-1 = maxIndex */
extern int   basCol[3];                     /* color indexes due to basic colormap */
extern int   width_ini, height_ini; /* Grafig-Fensterbreite/hoehe */
extern int   width_menu, height_menu;
extern int   w0, w1, w_index, w_rgb, activWindow;
extern int   width_w0, height_w0;
extern int   width_w1, height_w1;
extern int   MouseMode;                                   /* status maustasten */
extern double dtx, dty, dtz, drx, dry, drz, ds;                 /* Verschiebungen */
extern double vmem[4];                                     /* kor. bis auswahl eines neuen drehpkts */
extern GLdouble R[4][4];                                   /* Rotationsmatrix */
extern unsigned char  surfFlag;                /* zeichne nur Oberflaechenelemente (1), sonst (0)*/
extern unsigned char  modelEdgeFlag;                /* zeichne mit Modell-Ecken (1), sonst (0)*/
extern unsigned char  frameFlag;                   /* mit (1) oder ohne Rahmen um das Grafikfenster */
extern unsigned char  filenamFlag;                   /* mit (1) oder ohne filename im Menufenster */
extern unsigned char  textFlag;                   /* mit (1) oder ohne text im Menufenster */
extern unsigned char  printFlag;                     /* printf 1:on 0:off */
extern unsigned char  cullFlag;                     /* (1) enable Culling */
extern unsigned char  frameFlag;               /* mit (1) oder ohne Rahmen um das Grafigfenster */
extern unsigned char  scalaFlag;                 /* mit (1) oder ohne scala und wertetexte */ 
extern double dx ,dy;                                      /* Mauskoordinaten */
extern int   steps;                   /* Schrittweite der Farbscala, und NURB aufloesung */
extern double gtol;
extern unsigned char  picture_text[MAX_LINE_LENGTH], datin[MAX_LINE_LENGTH];
extern unsigned char  psviewer[MAX_LINE_LENGTH];
extern unsigned char  delPntFlag;                    /* 1: deleted points exists */
extern unsigned char  delLineFlag;                   /* 1: deleted lines exists */
extern unsigned char  delLcmbFlag;                   /* 1: deleted lcmbs exists */
extern unsigned char  delSurfFlag;                   /* 1: deleted surfs exists */
extern unsigned char  delBodyFlag;                   /* 1: deleted bodys exists */

extern int     num_olc;          /* nr-of-original-loadcases (lc from input-file) */

extern Scale     scale[1];
extern double     *value_n;
extern Summen    anz[1];
extern Edges     *edge;
extern Nodes     *node;
extern Elements  *e_enqire;
extern Nodes     *n_anim;
extern Datasets *lcase;

extern Alias     *alias;
extern Sets      *set;
extern Psets     *pset;
extern Points    *point;
extern Lines     *line;
extern Lcmb      *lcmb;
extern Gsur      *surf;
extern Gbod      *body;
extern Nurbs     *nurbs;
extern Materials *material; 
extern Amplitudes *amplitude; 
extern SumGeo    anzGeo[1];
extern SumAsci   sumAsci[1];

extern int       num_olc;

extern int       offset;

/* for the sequence */
extern unsigned char  sequenceFlag;                  /* 1: play a sequence of LC */
extern int seqLC[4], seq_nlc;
extern int   seq_steps;                              /* anzahl der displaylisten einer sequenz */
extern int   entity_Nr;                               /* aktiv value */
extern unsigned char  entityName[13][MAX_LINE_LENGTH];              /* Bezeichnung des aktiven values */


void plot2D(unsigned char type, int setNr, int *dsNr, int entity )
{
  FILE *handle_out, *handle_gnu;
  int i,j, anz_lc=0, n, lc;
  time_t seconds;
  struct tm *date;

  double **dat, *lc_time=NULL;
  unsigned char  buffer[MAX_LINE_LENGTH], **lc_description=NULL;
  unsigned char  path[MAX_LINE_LENGTH];
  unsigned char  name[MAX_LINE_LENGTH];
  unsigned char  comp[MAX_LINE_LENGTH];

  static int graph_Nr=-1;

  graph_Nr++;

  if((dat=(double **)malloc((int)set[setNr].anz_n *sizeof(double *)))==NULL )
  {
    printf("\n\n ERROR: malloc failed \n\n") ;
  }
  for (i=0; i<set[setNr].anz_n; i++)
  {
    if((dat[i]=(double *)malloc((int)anz->l *sizeof(double)))==NULL )
      printf("\n\n ERROR: malloc failed \n\n") ;
  }
  
  /* compile the lc-data */
  for ( n=0; n<set[setNr].anz_n; n++)
  {
    anz_lc=0;
    for(i=1; i<=dsNr[0]; i++)
    {
      lc=dsNr[i];

      if((lc_time=(double *)realloc(lc_time, (anz_lc+2) *sizeof(double)))==NULL )
        printf("\n\n ERROR: realloc failed \n\n") ;
      lc_time[anz_lc]    =  lcase[lc].value;

      if ( ( lc_description = (unsigned char **)realloc(lc_description, (anz_lc+2) * sizeof(unsigned char *))) == NULL )
        printf("\n\n ERROR: realloc failure\n\n" );
      if ( ( lc_description[anz_lc] = (unsigned char *)malloc((MAX_LINE_LENGTH) * sizeof(unsigned char))) == NULL )
        printf("\n\n ERROR: realloc failure\n\n" );
      strcpy(lc_description[anz_lc], lcase[lc].dataset_text);

      /*
      printf("|%s| |%s|\n",lc_description[anz_lc], lcase[lc].dataset_text); 
      if((lc_description[anz_lc][0]==' ')||(lc_description[anz_lc][0]==(unsigned char)NULL))
        sprintf(lc_description[anz_lc],"0");
      */
      dat[n][anz_lc] =  lcase[lc].dat[entity][set[setNr].node[n]] ;
      anz_lc++;
    }
  }


  /* create special purpose plotfile1 for the cad department */

  /* skip the trailing blanks */
  j=0;
  while((lcase[dsNr[1]].name[j]!=' ')&&(lcase[dsNr[1]].name[j]!=(unsigned char)NULL))
  {  name[j]=lcase[dsNr[1]].name[j]; j++; }
  name[j]=(unsigned char)NULL;
  j=0;
  while((lcase[dsNr[1]].compName[entity][j]!=' ')&&(lcase[dsNr[1]].compName[entity][j]!=(unsigned char)NULL))
  {  comp[j]=lcase[dsNr[1]].compName[entity][j]; j++; }
  comp[j]=(unsigned char)NULL;
  sprintf(buffer, "graph_%s_%s_%s.out", set[setNr].name, name, comp);
  handle_out = fopen (buffer, "w+b" );
  if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",
     buffer); return;}

  /* get the actual path */
  path[0]=(unsigned char)NULL;
  i=0; while(environ[i]!=0)
  {
    if(compare(environ[i],"PWD",3)==3) { strcpy(path,&environ[i][4]); break; }
    i++;
  }

  /* get the date */
  time(&seconds);
  date = gmtime(&seconds);

  /* write the header and data */
  fprintf (handle_out, "HEADER\n");
  fprintf (handle_out, "FILENAME:      %s/%s\n", path, datin);
  fprintf (handle_out, "DATE:          %d.%d.%d\n", date->tm_mday, date->tm_mon+1, date->tm_year+1900);
  fprintf (handle_out, "DATASET-NAME:  %s\n", lcase[dsNr[1]].name);
  fprintf (handle_out, "DATASET-ENTITY:%s\n", lcase[dsNr[1]].compName[entity]);
  fprintf (handle_out, "--------------------------------------------------------\n\n");
  if((type=='N')||(type=='n'))
    fprintf (handle_out, "DATASET INCREMENT / NODE\n");
  else if((type=='T')||(type=='t'))
    fprintf (handle_out, "TIME INCREMENT / NODE\n");
  else if((type=='D')||(type=='d'))
    fprintf (handle_out, "DESCRIPTION INCREMENT / NODE\n");
  else { printf(" ERROR: option:%c not known, use either n, t or d\n", type); return; }
  fprintf (handle_out, "              ");
  for ( n=0; n<set[setNr].anz_n; n++)
    fprintf (handle_out, "; %13d ", set[setNr].node[n]);
  fprintf (handle_out, "\n");
  for (lc=0; lc<anz_lc; lc++)
  {
    fprintf (handle_out, "% -.6e ", lc_time[lc]);
    for ( n=0; n<set[setNr].anz_n; n++)
      fprintf (handle_out, "; % -.6e ", dat[n][lc]);
    fprintf (handle_out, "\n");
  }
  fclose(handle_out);


  /* create plotfile, one line per Dataset */

  sprintf(buffer, "graph_%d.out", graph_Nr);
  handle_out = fopen (buffer, "w+b" );
  if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",
     buffer); return;}
  for (lc=0; lc<anz_lc; lc++)
  {
    fprintf (handle_out, "%-5d %-12.5e %f ", lc+1, lc_time[lc], atof(lc_description[lc]));
    for ( n=0; n<set[setNr].anz_n; n++)
      fprintf (handle_out, "%-12.5e ", dat[n][lc]);
    fprintf (handle_out, "\n");
  }
  fclose(handle_out);


  /* create special purpose plotfile2, one line per node */

  sprintf(buffer, "graph_%d.out2", graph_Nr);
  handle_out = fopen (buffer, "w+b" );
  if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",
     buffer); return;}
  fprintf (handle_out, "# node    x   y   z   dat(LC1)  dat(LC2) .. for the same entity-nr\n");
  for (n=0; n<set[setNr].anz_n; n++)
  {
    fprintf (handle_out, "%-10d %-12.5e %-12.5e %-12.5e", set[setNr].node[n], node[set[setNr].node[n]].nx* scale->w+scale->x, node[set[setNr].node[n]].ny* scale->w+scale->y, node[set[setNr].node[n]].nz* scale->w+scale->z );
    for (lc=0; lc<anz_lc; lc++)
      fprintf ( handle_out, " %-12.5e", dat[n][lc]);
    fprintf (handle_out, "\n");
  }
  fclose(handle_out);

  /* gnuplot-command-file  */

  sprintf(buffer, "graph_%d.gnu", graph_Nr);
  handle_gnu = fopen (buffer, "w+b" );
  if (handle_gnu==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",
     buffer); return;}
  fprintf(handle_gnu, "set term postscript landscape monochrom  \n");
  fprintf(handle_gnu, "#set term x11 \n");
  sprintf(buffer, "graph_%d.ps", graph_Nr);
  fprintf(handle_gnu, "set out \"%s\"\n", buffer);
  fprintf(handle_gnu, "set grid\n");
  fprintf(handle_gnu, "set title \"Values at Nodes (%s)\"\n", datin);
  fprintf(handle_gnu, "set ylabel \" %s \"\n", lcase[dsNr[1]].compName[entity]);

  sprintf(buffer, "graph_%d.out", graph_Nr);
  if((type=='N')||(type=='n'))
  {
    fprintf(handle_gnu, "set xlabel \" Dataset \"\n");
    fprintf(handle_gnu, "plot ");
    for (n=0; n<set[setNr].anz_n-1; n++)
    {
      fprintf(handle_gnu, "\"%s\" using 1:%d title 'Node=%d' with linespoints, ",
            buffer, n+4, set[setNr].node[n]);
    }
    fprintf(handle_gnu, "\"%s\" using 1:%d title 'Node=%d' with linespoints\n",
            buffer, n+4, set[setNr].node[n]);
  }
  else if((type=='T')||(type=='t'))
  {
    fprintf(handle_gnu, "set xlabel \" Time \"\n");
    fprintf(handle_gnu, "plot ");
    for (n=0; n<set[setNr].anz_n-1; n++)
    {
      fprintf(handle_gnu, "\"%s\" using 2:%d title 'Node=%d' with linespoints, ",
            buffer, n+4, set[setNr].node[n]);
    }
    fprintf(handle_gnu, "\"%s\" using 2:%d title 'Node=%d' with linespoints\n ",
            buffer, n+4, set[setNr].node[n]);
  }
  else if((type=='D')||(type=='d'))
  {
    fprintf(handle_gnu, "set xlabel \" Description \"\n");
    fprintf(handle_gnu, "plot ");
    for (n=0; n<set[setNr].anz_n-1; n++)
    {
      fprintf(handle_gnu, "\"%s\" using 3:%d title 'Node=%d' with linespoints, ",
            buffer, n+4, set[setNr].node[n]);
    }
    fprintf(handle_gnu, "\"%s\" using 3:%d title 'Node=%d' with linespoints\n ",
            buffer, n+4, set[setNr].node[n]);
  }
  else printf(" ERROR: option:%c not known, use either n, t or d\n", type);

  fclose(handle_gnu);


  sprintf(buffer, "graph_%d.ps", graph_Nr);
  printf ("write %s \n", buffer);
  sprintf(buffer, "gnuplot graph_%d.gnu", graph_Nr );
  system (buffer);
  sprintf(buffer, "%s  -landscape graph_%d.ps &", psviewer, graph_Nr );
  system (buffer);

  for (i=0; i<set[setNr].anz_n; i++)  free(dat[i]);
  free(dat);

  printf ("ready \n");
}




void graph( unsigned char *record)
{
  int  i,j,length, indx=0, nr, sum;
  int lc, *dsNr=NULL, anz_lc, eNr=-1;
  unsigned char name[MAX_LINE_LENGTH], type[MAX_LINE_LENGTH];
  unsigned char dataset[MAX_LINE_LENGTH], entity[MAX_LINE_LENGTH];
  unsigned char  buffer[MAX_LINE_LENGTH];

  FILE *handle_out, *handle_gnu;

  length= sscanf( record, "%s%s%s%s", name, type, dataset, entity );

  if ((compare(type, "nr", 1)==1) || (compare(type, "time",1)==1) || (compare(type, "desc", 1)==1))
  {
    operateAlias( name, "se" );
    indx=getSetNr( name );
    if (indx<0)
    {
      errMsg(" ERROR: Set (%s) is undefined", name );
      return;
    }

    /* get the list of datasets */
    if(length>2)
    {
      /* compile the lc-data based on a dataset-name */
      anz_lc=0;
      for(lc=0; lc<num_olc; lc++)
      {
        if( compare( lcase[lc].name, dataset, strlen(dataset)) == strlen(dataset) )
        {
          anz_lc++; 
          if((dsNr=(int *)realloc((int *)dsNr, (anz_lc+2) *sizeof(int)))==NULL )
            printf("\n\n ERROR: malloc failed \n\n") ;
          dsNr[anz_lc]=lc;
	}
      }
      if(!anz_lc)
      {
        printf(" ERROR: found no matching Dataset for string:%s\n", dataset);
        return;
      }
      dsNr[0]=anz_lc;

      if(length<4) eNr = entity_Nr-1;
      else
      {
        for(i=0; i<lcase[dsNr[1]].ncomps; i++)
        {
          j=0;
          while((lcase[dsNr[1]].compName[i][j]!=' ')&&(lcase[dsNr[1]].compName[i][j]!=(unsigned char)NULL))
	  {  buffer[j]=lcase[dsNr[1]].compName[i][j]; j++; }
          buffer[j]=(unsigned char)NULL;
	  //printf("|%s|%s|\n",buffer,entity);
          if(compareStrings(buffer, entity)>0) { eNr =i; break; } 
        }
      }
      if(eNr==-1)
      {
        printf(" ERROR: found no matching entity for string:%s\n", entity);
        return;
      }
    }
    else
    {
      /* compile the lc-data based on a previous lc-selection */
      if (seq_nlc!=3)
      {
        errMsg(" ERROR: no sequence is defined, choose the menu-entry \"Animate->Toggle Dataset Sequence\" and define a sequence\n\n");
        return;
      }
      anz_lc=0;
      for(lc=seqLC[0]; lc<seqLC[2]+1; lc+=seqLC[1]-seqLC[0])
      {
        anz_lc++; 
        if((dsNr=(int *)realloc((int *)dsNr, (anz_lc+2) *sizeof(int)))==NULL )
          printf("\n\n ERROR: malloc failed \n\n") ;
        dsNr[anz_lc]=lc;
      }
      dsNr[0]=anz_lc;
      eNr         =  entity_Nr-1;
    }
    plot2D( type[0], indx, dsNr, eNr);
    free(dsNr);
  } 

  else if (compare(type, "amp", 1)==1)
  {
    if(anz->amps==1)
    {
      strcpy(name,amplitude[0].name);
    }
    if(compareStrings(name, "all")>0) sum=anz->amps;
    else sum=1;

    for (nr=0; nr<sum; nr++)
    {
      if(sum==1) indx=getAmplitudeNr( name,1 );
      else indx=nr;
      if (indx<0)
      {
        printf ("ERROR: amplitude:'%s' does not exist\n", name);
        return;
      }

      sprintf(buffer, "amplitude%d.out",nr);
      handle_out = fopen (buffer, "w+b" );
      if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", buffer); return;}
      for (i=0; i<amplitude[indx].n; i++)
      {
        fprintf (handle_out, "%d %e %e\n", i+1, amplitude[indx].x[i],amplitude[indx].y[i]);
      }
      fclose(handle_out);
    }

    sprintf(buffer, "amplitude.gnu");
    handle_gnu = fopen (buffer, "w+b" );
    if (handle_gnu==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",buffer); return;}
    fprintf(handle_gnu, "set term postscript landscape monochrom  \n");
    fprintf(handle_gnu, "#set term x11 \n");
    sprintf(buffer, "amplitude.ps");
    fprintf(handle_gnu, "set out \"%s\"\n", buffer);
    fprintf(handle_gnu, "set grid\n");
    fprintf(handle_gnu, "set title \"Amplitude\"\n");
    fprintf(handle_gnu, "set ylabel \" y \"\n");
    fprintf(handle_gnu, "set xlabel \" x \"\n");
    for (nr=0; nr<sum; nr++)
    {
      if(sum==1) indx=getAmplitudeNr( name,1 );
      else indx=nr;
      sprintf(buffer, "amplitude%d.out", nr);
      fprintf(handle_gnu, "plot \"%s\" using 2:3 title '%s' with linespoints\n", buffer, amplitude[indx].name);
    }
    fclose(handle_gnu);


    sprintf(buffer, "amplitude.ps");
    printf ("write %s \n", buffer);
    sprintf(buffer, "gnuplot amplitude.gnu" );
    system (buffer);
    sprintf(buffer, "%s  -landscape amplitude.ps &", psviewer );
    system (buffer);
  } 

  else if (compare(type, "mat", 1)==1)
  {
    if(anz->mats==1)
    {
      strcpy(name,material[0].name);
      printf("%s %s nn\n",name,material[0].name);
    }
    if(compareStrings(name, "all")>0) sum=anz->mats;
    else sum=1;

    for (nr=0; nr<sum; nr++)
    {
      if(sum==1) indx=getMatNr( name,1 );
      else indx=nr;
      if (indx<0)
      {
        printf ("ERROR: material:'%s' does not exist\n", name);
        return;
      }

      if(material[indx].nela)
      {
        sprintf(buffer, "elastic%d.out",nr);
        handle_out = fopen (buffer, "w+b" );
        if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", buffer); return;}
        for (i=0; i<material[indx].nela; i++)
        {
          fprintf (handle_out, "%d %e %e %e\n", i+1,material[indx].ela[i],material[indx].nue[i], material[indx].tela[i]);
        }
        fclose(handle_out);
      }

      if(material[indx].nexp)
      {
        sprintf(buffer, "expansion%d.out",nr);
        handle_out = fopen (buffer, "w+b" );
        if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", buffer); return;}
        for (i=0; i<material[indx].nexp; i++)
        {
          fprintf (handle_out, "%d %e %e\n", i+1,material[indx].exp[i],material[indx].texp[i]);
        }
        fclose(handle_out);
      }

      if(material[indx].ncon)
      {
        sprintf(buffer, "conductivity%d.out",nr);
        handle_out = fopen (buffer, "w+b" );
        if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", buffer); return;}
        for (i=0; i<material[indx].ncon; i++)
        {
          fprintf (handle_out, "%d %e %e\n", i+1,material[indx].con[i],material[indx].tcon[i]);
        }
        fclose(handle_out);
      }

      if(material[indx].nsph)
      {
        sprintf(buffer, "spec_heat%d.out",nr);
        handle_out = fopen (buffer, "w+b" );
        if (handle_out==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", buffer); return;}
        for (i=0; i<material[indx].nsph; i++)
        {
          fprintf (handle_out, "%d %e %e\n", i+1,material[indx].sph[i],material[indx].tsph[i]);
        }
        fclose(handle_out);
      }
    }

    sprintf(buffer, "material.gnu");
    handle_gnu = fopen (buffer, "w+b" );
    if (handle_gnu==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",buffer); return;}
    fprintf(handle_gnu, "set term postscript landscape monochrom  \n");
    fprintf(handle_gnu, "#set term x11 \n");
    sprintf(buffer, "material.ps");
    fprintf(handle_gnu, "set out \"%s\"\n", buffer);
    fprintf(handle_gnu, "set grid\n");
    fprintf(handle_gnu, "set title \"Material\"\n");

    if(material[indx].nela)
    {
      fprintf(handle_gnu, "set ylabel \" Elastic Modulus \"\n");
      fprintf(handle_gnu, "set xlabel \" Temperature \"\n");
      fprintf(handle_gnu, "plot ");
      for (nr=0; nr<sum-1; nr++)
      {
        if(sum==1) indx=getMatNr( name,1 );
        else indx=nr;
        sprintf(buffer, "elastic%d.out", nr);
        fprintf(handle_gnu, "\"%s\" using 4:2 title '%s' with linespoints, ", buffer, material[indx].name);
      }
      if(sum==1) indx=getMatNr( name,1 );
      else indx=nr;
      sprintf(buffer, "elastic%d.out", nr);
      fprintf(handle_gnu, "\"%s\" using 4:2 title '%s' with linespoints\n", buffer, material[indx].name);
    }

    if(material[indx].nexp)
    {
      fprintf(handle_gnu, "set ylabel \" Expansion Coefficient \"\n");
      fprintf(handle_gnu, "set xlabel \" Temperature \"\n");
      fprintf(handle_gnu, "plot ");
      for (nr=0; nr<sum-1; nr++)
      {
        if(sum==1) indx=getMatNr( name,1 );
        else indx=nr;
        sprintf(buffer, "expansion%d.out", nr);
        fprintf(handle_gnu, "\"%s\" using 3:2 title '%s' with linespoints, ", buffer, material[indx].name);
      }
      if(sum==1) indx=getMatNr( name,1 );
      else indx=nr;
      sprintf(buffer, "expansion%d.out", nr);
      fprintf(handle_gnu, "\"%s\" using 3:2 title '%s' with linespoints\n", buffer, material[indx].name);
    }

    if(material[indx].ncon)
    {
      fprintf(handle_gnu, "set ylabel \" Conductivity \"\n");
      fprintf(handle_gnu, "set xlabel \" Temperature \"\n");
      fprintf(handle_gnu, "plot ");
      for (nr=0; nr<sum-1; nr++)
      {
        if(sum==1) indx=getMatNr( name,1 );
        else indx=nr;
        sprintf(buffer, "conductivity%d.out", nr);
        fprintf(handle_gnu, "\"%s\" using 3:2 title '%s' with linespoints, ", buffer, material[indx].name);
      }
      if(sum==1) indx=getMatNr( name,1 );
      else indx=nr;
      sprintf(buffer, "conductivity%d.out", nr);
      fprintf(handle_gnu, "\"%s\" using 3:2 title '%s' with linespoints\n", buffer, material[indx].name);
    }

    if(material[indx].nsph)
    {
      fprintf(handle_gnu, "set ylabel \" Specific Heat \"\n");
      fprintf(handle_gnu, "set xlabel \" Temperature \"\n");
      fprintf(handle_gnu, "plot ");
      for (nr=0; nr<sum-1; nr++)
      {
        if(sum==1) indx=getMatNr( name,1 );
        else indx=nr;
        sprintf(buffer, "spec_heat%d.out", nr);
        fprintf(handle_gnu, "\"%s\" using 3:2 title '%s' with linespoints, ", buffer, material[indx].name);
      }
      if(sum==1) indx=getMatNr( name,1 );
      else indx=nr;
      sprintf(buffer, "spec_heat%d.out", nr);
      fprintf(handle_gnu, "\"%s\" using 3:2 title '%s' with linespoints\n", buffer, material[indx].name);
    }
    fclose(handle_gnu);

    sprintf(buffer, "material.ps");
    printf ("write %s \n", buffer);
    sprintf(buffer, "gnuplot material.gnu" );
    system (buffer);
    sprintf(buffer, "%s  -landscape material.ps &", psviewer );
    system (buffer);
  } 

  else printf("ERROR: type:%s not known\n\n", type);
}
