/*******************************************************************************
| Moritz Haag      8.01.2003
| Header zum erstellen von GnuPlot Files und zum anzeigen der Daten.
| zu gnuplot.c
********************************************************************************/

#ifndef GNUPLOT_H

#define GNUPLOT_H
#include <stdio.h>
#include <stdlib.h>

#define GNU_PROCESS FILE
#define MAX_LABLE_LEN 20
#define MAX_CMD_LEN 250
#define MAX_COLUMNS 5
#define MAX_GROUPS 10
#define GNUPLOT_PATH "/usr/local/bin/gnuplot"
#define AddSPlot(x,y) AddPlot(x,1,y)
#define CloseGnuPlot(x) pclose(x);

/*****************************************************************************
|  Datentypen fuer Kommunikation mit GnuPlotfunktionen
*****************************************************************************/
/*----------------------------------------------------------------------------
| Speicher den Befehl zum plotten und eine Zahl die es erlaubt mehrere Plots
| in ein fenster zu zeichnen. Dazu brauchen Sie die gleiche Plotgroup
/ muss ein Wert zwischen 0 und MAX_GROUPS sein
----------------------------------------------------------------------------*/
typedef struct
{

 char Str[MAX_CMD_LEN+1];
 int PlotGroup;
} PlotCmd;


/*----------------------------------------------------------------------------
| Stores Chartproperties and plot-Data(function, datalinks)
----------------------------------------------------------------------------*/
typedef struct
{
  double xmin, xmax;
  double ymin, ymax;
  int plotcnt;
  int grpcnt;
  char name[MAX_LABLE_LEN + 1];
  char title[MAX_LABLE_LEN + 1];
  char xlable[MAX_LABLE_LEN + 1];
  char ylable[MAX_LABLE_LEN + 1];
  char zlable[MAX_LABLE_LEN + 1];
  PlotCmd *plots;
  
} GnuPlot;


/*----------------------------------------------------------------------------
| Stores YColumns with labels
----------------------------------------------------------------------------*/
typedef struct
{
  double *data;
  char name[MAX_LABLE_LEN + 1];
} GnuYData;


/*----------------------------------------------------------------------------
| This structure stores pointers to those double arrays ,that should be 
| written into the file. The maximum number of colums is defined by
| MAX_COLUMNS
----------------------------------------------------------------------------*/
typedef struct
{
  int xcnt;
  int ycolumns;
  double *xdata;
  char xname[MAX_LABLE_LEN + 1];
  GnuYData ydata[MAX_COLUMNS];
} GnuData;


/*****************************************************************************
| Function    SendGnuCmd
| PARAMETERS: igp     ProcessNumber of GnuPlot
|             aktCmd  0 terminated String, contains command for GnuPlot
| RESULT    : Command is executed in GnuPlot window
| CONDITIONS: Process is open and still active
|             String ends with \n
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
void SendGnuCmd(GNU_PROCESS *igp, char *aktCmd);


/*****************************************************************************
| Function    CreatePlotCmd
| PARAMETERS: aktCmd  0 terminated String, contains function to plot
| RESULT    : String that contains command and function in correct syntax
| CONDITIONS: aktcmd is correct and has no praefix or suffix
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
char *CreatePlotCmd(char *aktCmd);


/*****************************************************************************
| Function    CreateGnufile
| PARAMETERS: aktplot    Chartparamters and plots stored in GnuPlot structure
|             overwrite  0 or 1 to indicate weather the 
                         existing file shold be overwritten
| RESULT    : File with gnuplot->name.gnu
| CONDITIONS: File is writeable and GnuPlot struct contains valid data
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
void CreateGnuFile(GnuPlot *aktplot, int overwrite);


/*****************************************************************************
| Function    ShowPlot
| PARAMETERS: aktplot  Chartparamters and plots stored in GnuPlot structure
|             gp       ProcessNumber of GnuPlot
| RESULT    : plots are shown in GnuPlot Window
| CONDITIONS: If gp is Null a new GnuPlotProcess is opend
|             GnuPlot struct contains valid data
|             Files to be shown exists
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
void ShowPlot(GnuPlot *aktplot,  GNU_PROCESS *gp);


/*****************************************************************************
| Function    OpenGnuPlot
| PARAMETERS: 
| RESULT    : GnuPlotProcess Number
| CONDITIONS: GnuPlot is closed later on
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
GNU_PROCESS *OpenGnuPlot();


/*****************************************************************************
| Function    CreatePlotData
| PARAMETERS: aktPlot   GnuPlotStructure in that the link should be included
|             data      GnuData structure that was previosly filled with
|                         xcnt, xdata and ydata
|             fname     String that contains the name of the data file
|             PlotGroup Index that indicates with wich plots the points should 
|                         be plotted in one window (between 0 and MAX_GROUPS)
| RESULT    : new Datafile and link to it in *aktplot
| CONDITIONS: fname can be overwritten
|             data columns are in double and their labels not longer than  
|             MAX_LABLE_LEN and xcnt contains the correct rownumber and 
|             ycolumns the correct number of USED columns
| EFFECTS   : AddPlot
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
void CreatePlotData(GnuPlot *aktplot, char *fname, GnuData *data, int PlotGroup);


/*****************************************************************************
| Function    AddPlot
| PARAMETERS: aktPlot   GnuPlotStructure in that the plot should be included
              Group     Index that indicates those plots that are put together 
                        in one window musr be between 0 and MAX_GROUPS
              newCmd    String that contains function to plot with no pre- or suffix
| RESULT    : new Plot in *aktplot
| CONDITIONS: There is only one GnuPlotStructure, because plotcnt doesnot work 
              properly
| EFFECTS   : plotcnt++
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
void AddPlot(GnuPlot *aktplot, int Group, char *newCmd);

/*****************************************************************************
| Function    init_plot
| PARAMETERS: DefPlot   Plot to be initalised with Defaults
| RESULT:     none 
| CONDITIONS: DefPlot is already defined
| EFFECTS   : plotcnt=0, [x,y]min=-5,  [x,y]max=5
|
| EXAMPLE:    See main-function for example
*****************************************************************************/
/*****************************************************************************
| Set Plotarray data to initial values.
| plotcnt MUST be set to zero.
*****************************************************************************/
void init_plot(GnuPlot *DefPlot);

#endif /**GNUPLOT_H**/
