2008-03-22 14:10:46 +01:00
|
|
|
/**********
|
|
|
|
|
* From xgraph.c:
|
|
|
|
|
* Copyright 1992 Regents of the University of California. All rights reserved.
|
|
|
|
|
* Author: 1992 David A. Gates, U. C. Berkeley CAD Group
|
|
|
|
|
*
|
|
|
|
|
* Author: 2008 Stefano Pedretti
|
|
|
|
|
**********/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* gnuplot plots.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-12-11 19:05:00 +01:00
|
|
|
#include "ngspice/ngspice.h"
|
|
|
|
|
#include "ngspice/cpdefs.h"
|
|
|
|
|
#include "ngspice/ftedefs.h"
|
|
|
|
|
#include "ngspice/dvec.h"
|
|
|
|
|
#include "ngspice/fteparse.h"
|
2008-03-22 14:10:46 +01:00
|
|
|
#include "gnuplot.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define GP_MAXVECTORS 64
|
|
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
|
2015-05-17 17:34:29 +02:00
|
|
|
static void
|
|
|
|
|
quote_gnuplot_string(FILE *stream, char *s)
|
|
|
|
|
{
|
|
|
|
|
fputc('"', stream);
|
|
|
|
|
|
|
|
|
|
for (; *s; s++)
|
|
|
|
|
switch (*s) {
|
|
|
|
|
case '\n':
|
|
|
|
|
fputs("\\n", stream);
|
|
|
|
|
break;
|
|
|
|
|
case '"':
|
|
|
|
|
case '\\':
|
|
|
|
|
fputc('\\', stream);
|
|
|
|
|
default:
|
|
|
|
|
fputc(*s, stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fputc('"', stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-03-22 14:10:46 +01:00
|
|
|
void
|
|
|
|
|
ft_gnuplot(double *xlims, double *ylims, char *filename, char *title, char *xlabel, char *ylabel, GRIDTYPE gridtype, PLOTTYPE plottype, struct dvec *vecs)
|
|
|
|
|
{
|
|
|
|
|
FILE *file, *file_data;
|
2009-10-04 13:48:37 +02:00
|
|
|
struct dvec *v, *scale = NULL;
|
2015-12-19 19:48:00 +01:00
|
|
|
double xval, yval, prev_xval, extrange;
|
|
|
|
|
int i, dir, numVecs, linewidth, err, terminal_type;
|
2008-03-22 14:10:46 +01:00
|
|
|
bool xlog, ylog, nogrid, markers;
|
2012-11-21 22:12:27 +01:00
|
|
|
char buf[BSIZE_SP], pointstyle[BSIZE_SP], *text, plotstyle[BSIZE_SP], terminal[BSIZE_SP];
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2009-12-29 19:18:47 +01:00
|
|
|
char filename_data[128];
|
|
|
|
|
char filename_plt[128];
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2008-04-27 20:34:43 +02:00
|
|
|
sprintf(filename_data, "%s.data", filename);
|
|
|
|
|
sprintf(filename_plt, "%s.plt", filename);
|
2008-03-22 14:10:46 +01:00
|
|
|
|
|
|
|
|
/* Sanity checking. */
|
2012-09-20 20:30:53 +02:00
|
|
|
for (v = vecs, numVecs = 0; v; v = v->v_link2)
|
2011-11-26 00:04:09 +01:00
|
|
|
numVecs++;
|
2012-09-20 20:30:53 +02:00
|
|
|
|
2008-03-22 14:10:46 +01:00
|
|
|
if (numVecs == 0) {
|
2011-11-26 00:04:09 +01:00
|
|
|
return;
|
2008-03-22 14:10:46 +01:00
|
|
|
} else if (numVecs > GP_MAXVECTORS) {
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(cp_err, "Error: too many vectors for gnuplot.\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
return;
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
2012-10-09 19:47:24 +02:00
|
|
|
|
|
|
|
|
if (fabs((ylims[1]-ylims[0])/ylims[0]) < 1.0e-6) {
|
|
|
|
|
fprintf(cp_err, "Error: range min ... max too small for using gnuplot.\n");
|
|
|
|
|
fprintf(cp_err, " Consider plotting with offset %g.\n", ylims[0]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extrange = 0.05 * (ylims[1] - ylims[0]);
|
|
|
|
|
|
2012-11-21 22:12:27 +01:00
|
|
|
if (!cp_getvar("gnuplot_terminal", CP_STRING, terminal)) {
|
|
|
|
|
terminal_type = 1;
|
|
|
|
|
} else {
|
|
|
|
|
terminal_type = 1;
|
|
|
|
|
if (cieq(terminal,"png"))
|
|
|
|
|
terminal_type = 2;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-17 22:48:20 +02:00
|
|
|
if (!cp_getvar("xbrushwidth", CP_NUM, &linewidth))
|
2008-03-22 14:10:46 +01:00
|
|
|
linewidth = 1;
|
|
|
|
|
if (linewidth < 1) linewidth = 1;
|
|
|
|
|
|
2010-07-17 22:48:20 +02:00
|
|
|
if (!cp_getvar("pointstyle", CP_STRING, pointstyle)) {
|
2008-03-22 14:10:46 +01:00
|
|
|
markers = FALSE;
|
|
|
|
|
} else {
|
2012-09-20 20:30:53 +02:00
|
|
|
if (cieq(pointstyle,"markers"))
|
2011-11-26 00:04:09 +01:00
|
|
|
markers = TRUE;
|
2012-09-20 20:30:53 +02:00
|
|
|
else
|
2011-11-26 00:04:09 +01:00
|
|
|
markers = FALSE;
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Make sure the gridtype is supported. */
|
|
|
|
|
switch (gridtype) {
|
|
|
|
|
case GRID_LIN:
|
2011-11-26 00:04:09 +01:00
|
|
|
nogrid = xlog = ylog = FALSE;
|
|
|
|
|
break;
|
2008-03-22 14:10:46 +01:00
|
|
|
case GRID_XLOG:
|
2011-11-26 00:04:09 +01:00
|
|
|
xlog = TRUE;
|
|
|
|
|
nogrid = ylog = FALSE;
|
|
|
|
|
break;
|
2008-03-22 14:10:46 +01:00
|
|
|
case GRID_YLOG:
|
2011-11-26 00:04:09 +01:00
|
|
|
ylog = TRUE;
|
|
|
|
|
nogrid = xlog = FALSE;
|
|
|
|
|
break;
|
2008-03-22 14:10:46 +01:00
|
|
|
case GRID_LOGLOG:
|
2011-11-26 00:04:09 +01:00
|
|
|
xlog = ylog = TRUE;
|
|
|
|
|
nogrid = FALSE;
|
|
|
|
|
break;
|
2008-03-22 14:10:46 +01:00
|
|
|
case GRID_NONE:
|
2011-11-26 00:04:09 +01:00
|
|
|
nogrid = TRUE;
|
|
|
|
|
xlog = ylog = FALSE;
|
|
|
|
|
break;
|
2008-03-22 14:10:46 +01:00
|
|
|
default:
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(cp_err, "Error: grid type unsupported by gnuplot.\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
return;
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Open the output gnuplot file. */
|
2010-11-19 19:54:40 +01:00
|
|
|
if ((file = fopen(filename_plt, "w")) == NULL) {
|
2011-11-26 00:04:09 +01:00
|
|
|
perror(filename);
|
|
|
|
|
return;
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set up the file header. */
|
2014-01-05 00:43:34 +01:00
|
|
|
#if !defined(__MINGW__) && !defined(_MSC_VER)
|
2015-05-17 17:34:29 +02:00
|
|
|
fprintf(file, "set terminal X11 noenhanced\n");
|
|
|
|
|
#else
|
|
|
|
|
fprintf(file, "set termoption noenhanced\n");
|
2014-01-05 00:43:34 +01:00
|
|
|
#endif
|
2008-03-22 14:10:46 +01:00
|
|
|
if (title) {
|
2011-11-26 00:04:09 +01:00
|
|
|
text = cp_unquote(title);
|
2015-05-17 17:34:29 +02:00
|
|
|
fprintf(file, "set title ");
|
|
|
|
|
quote_gnuplot_string(file, text);
|
|
|
|
|
fprintf(file, "\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
tfree(text);
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
if (xlabel) {
|
2011-11-26 00:04:09 +01:00
|
|
|
text = cp_unquote(xlabel);
|
2015-05-17 17:34:29 +02:00
|
|
|
fprintf(file, "set xlabel ");
|
|
|
|
|
quote_gnuplot_string(file, text);
|
|
|
|
|
fprintf(file, "\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
tfree(text);
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
if (ylabel) {
|
2011-11-26 00:04:09 +01:00
|
|
|
text = cp_unquote(ylabel);
|
2015-05-17 17:34:29 +02:00
|
|
|
fprintf(file, "set ylabel ");
|
|
|
|
|
quote_gnuplot_string(file, text);
|
|
|
|
|
fprintf(file, "\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
tfree(text);
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
2009-12-29 19:18:47 +01:00
|
|
|
if (!nogrid) {
|
2011-11-26 00:04:09 +01:00
|
|
|
if (linewidth > 1)
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "set grid lw %d \n" , linewidth);
|
2011-11-26 00:04:09 +01:00
|
|
|
else
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "set grid\n");
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
if (xlog) {
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "set logscale x\n");
|
|
|
|
|
if (xlims)
|
2012-11-21 22:12:27 +01:00
|
|
|
fprintf(file, "set xrange [%1.0e:%1.0e]\n",
|
|
|
|
|
pow(10, floor(log10(xlims[0]))), pow(10, ceil(log10(xlims[1]))));
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "set xrange [%e:%e]\n", xlims[0], xlims[1]);
|
2012-11-21 22:12:27 +01:00
|
|
|
fprintf(file, "set mxtics 10\n");
|
|
|
|
|
fprintf(file, "set grid mxtics\n");
|
2008-03-22 14:10:46 +01:00
|
|
|
} else {
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "unset logscale x \n");
|
|
|
|
|
if (xlims)
|
|
|
|
|
fprintf(file, "set xrange [%e:%e]\n", xlims[0], xlims[1]);
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
if (ylog) {
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "set logscale y \n");
|
|
|
|
|
if (ylims)
|
2012-11-21 22:12:27 +01:00
|
|
|
fprintf(file, "set yrange [%1.0e:%1.0e]\n",
|
2012-10-09 19:47:24 +02:00
|
|
|
pow(10, floor(log10(ylims[0]))), pow(10, ceil(log10(ylims[1]))));
|
2012-11-21 22:12:27 +01:00
|
|
|
fprintf(file, "set mytics 10\n");
|
|
|
|
|
fprintf(file, "set grid mytics\n");
|
2008-03-22 14:10:46 +01:00
|
|
|
} else {
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "unset logscale y \n");
|
|
|
|
|
if (ylims)
|
2012-10-09 19:47:24 +02:00
|
|
|
fprintf(file, "set yrange [%e:%e]\n", ylims[0] - extrange, ylims[1] + extrange);
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "#set xtics 1\n");
|
|
|
|
|
fprintf(file, "#set x2tics 1\n");
|
|
|
|
|
fprintf(file, "#set ytics 1\n");
|
|
|
|
|
fprintf(file, "#set y2tics 1\n");
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2009-12-30 14:23:57 +01:00
|
|
|
if (linewidth > 1)
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "set border lw %d\n", linewidth);
|
2009-12-29 19:18:47 +01:00
|
|
|
|
2008-03-22 14:10:46 +01:00
|
|
|
if (plottype == PLOT_COMB) {
|
2011-11-26 00:04:09 +01:00
|
|
|
strcpy(plotstyle, "boxes");
|
2008-03-22 14:10:46 +01:00
|
|
|
} else if (plottype == PLOT_POINT) {
|
2011-11-26 00:04:09 +01:00
|
|
|
if (markers) {
|
2012-09-20 20:30:53 +02:00
|
|
|
// fprintf(file, "Markers: True\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
} else {
|
2012-09-20 20:30:53 +02:00
|
|
|
// fprintf(file, "LargePixels: True\n");
|
2011-11-26 00:04:09 +01:00
|
|
|
}
|
|
|
|
|
strcpy(plotstyle, "points");
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(plotstyle, "lines");
|
|
|
|
|
}
|
2008-03-22 14:10:46 +01:00
|
|
|
|
|
|
|
|
/* Open the output gnuplot data file. */
|
2010-11-19 19:54:40 +01:00
|
|
|
if ((file_data = fopen(filename_data, "w")) == NULL) {
|
2011-11-26 00:04:09 +01:00
|
|
|
perror(filename);
|
|
|
|
|
return;
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
2012-10-09 19:47:24 +02:00
|
|
|
fprintf(file, "set format y \"%%g\"\n");
|
|
|
|
|
fprintf(file, "set format x \"%%g\"\n");
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "plot ");
|
2008-03-22 14:10:46 +01:00
|
|
|
i = 0;
|
|
|
|
|
|
2008-04-27 20:34:43 +02:00
|
|
|
/* Write out the gnuplot command */
|
2012-09-20 20:30:53 +02:00
|
|
|
for (v = vecs; v; v = v->v_link2) {
|
2010-02-27 22:11:30 +01:00
|
|
|
scale = v->v_scale;
|
|
|
|
|
if (v->v_name) {
|
|
|
|
|
i = i + 2;
|
|
|
|
|
if (i > 2) fprintf(file, ",\\\n");
|
2015-05-17 17:34:29 +02:00
|
|
|
fprintf(file, "\'%s\' using %d:%d with %s lw %d title ",
|
|
|
|
|
filename_data, i-1, i, plotstyle, linewidth);
|
|
|
|
|
quote_gnuplot_string(file, v->v_name);
|
2010-02-27 22:11:30 +01:00
|
|
|
}
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "\n");
|
2016-08-07 21:58:42 +02:00
|
|
|
|
|
|
|
|
/* do not print an eps or png file if filename start with 'np_' */
|
|
|
|
|
if (!ciprefix("np_", filename)) {
|
|
|
|
|
fprintf(file, "set terminal push\n");
|
|
|
|
|
if (terminal_type == 1) {
|
|
|
|
|
fprintf(file, "set terminal postscript eps color noenhanced\n");
|
|
|
|
|
fprintf(file, "set out \'%s.eps\'\n", filename);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fprintf(file, "set terminal png noenhanced\n");
|
|
|
|
|
fprintf(file, "set out \'%s.png\'\n", filename);
|
|
|
|
|
}
|
|
|
|
|
fprintf(file, "replot\n");
|
|
|
|
|
fprintf(file, "set term pop\n");
|
2012-11-21 22:12:27 +01:00
|
|
|
}
|
2012-10-09 19:47:24 +02:00
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file, "replot\n");
|
2009-12-30 14:23:57 +01:00
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
(void) fclose(file);
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2008-04-27 20:34:43 +02:00
|
|
|
/* Write out the data and setup arrays */
|
2015-12-19 19:48:00 +01:00
|
|
|
dir = 0;
|
|
|
|
|
prev_xval = NAN;
|
2012-09-20 20:30:53 +02:00
|
|
|
for (i = 0; i < scale->v_length; i++) {
|
|
|
|
|
for (v = vecs; v; v = v->v_link2) {
|
2011-11-26 00:04:09 +01:00
|
|
|
scale = v->v_scale;
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2011-11-26 00:04:09 +01:00
|
|
|
xval = isreal(scale) ?
|
2012-02-07 20:53:12 +01:00
|
|
|
scale->v_realdata[i] : realpart(scale->v_compdata[i]);
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2011-11-26 00:04:09 +01:00
|
|
|
yval = isreal(v) ?
|
2012-02-07 20:53:12 +01:00
|
|
|
v->v_realdata[i] : realpart(v->v_compdata[i]);
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2015-12-19 19:48:00 +01:00
|
|
|
if (i > 0 && scale->v_plot && scale->v_plot->pl_scale == scale) {
|
|
|
|
|
if (dir * (xval - prev_xval) < 0) {
|
|
|
|
|
/* direction reversal, start a new graph */
|
|
|
|
|
fprintf(file_data, "\n");
|
|
|
|
|
dir = 0;
|
|
|
|
|
} else if (!dir && xval > prev_xval) {
|
|
|
|
|
dir = 1;
|
|
|
|
|
} else if (!dir && xval < prev_xval) {
|
|
|
|
|
dir = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-09 19:47:24 +02:00
|
|
|
fprintf(file_data, "%e %e ", xval, yval);
|
2015-12-19 19:48:00 +01:00
|
|
|
|
|
|
|
|
prev_xval = xval;
|
2011-11-26 00:04:09 +01:00
|
|
|
}
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file_data, "\n");
|
2008-03-22 14:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
(void) fclose(file_data);
|
2008-03-22 14:10:46 +01:00
|
|
|
|
2008-04-27 20:34:43 +02:00
|
|
|
#if defined(__MINGW32__) || defined(_MSC_VER)
|
2010-01-02 16:24:03 +01:00
|
|
|
/* for external fcn system() */
|
2012-09-20 20:30:53 +02:00
|
|
|
// (void) sprintf(buf, "start /B wgnuplot %s -" , filename_plt);
|
|
|
|
|
(void) sprintf(buf, "start /B wgnuplot -persist %s " , filename_plt);
|
2009-12-30 14:23:57 +01:00
|
|
|
_flushall();
|
2008-04-27 20:34:43 +02:00
|
|
|
#else
|
2011-11-26 00:04:09 +01:00
|
|
|
/* for external fcn system() from LINUX environment */
|
2012-09-20 20:30:53 +02:00
|
|
|
(void) sprintf(buf, "xterm -e gnuplot %s - &", filename_plt);
|
2008-04-27 20:34:43 +02:00
|
|
|
#endif
|
2012-09-20 20:30:53 +02:00
|
|
|
err = system(buf);
|
2008-03-22 14:10:46 +01:00
|
|
|
|
|
|
|
|
}
|
2010-02-27 22:11:30 +01:00
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
|
2011-11-26 00:04:09 +01:00
|
|
|
/* simple printout of data into a file, similar to data table in ft_gnuplot
|
2016-06-06 21:16:49 +02:00
|
|
|
command: wrdata file vecs, vectors of different length (from different plots)
|
|
|
|
|
may be printed. Data are written in pairs: scale vector, value vector. If
|
|
|
|
|
data are complex, a triple is printed (scale, real, imag).
|
|
|
|
|
Setting 'singlescale' as variable, the scale vector will be printed once only,
|
|
|
|
|
if scale vectors are of same length (there is little risk here!).
|
|
|
|
|
Width of numbers printed is set by option 'numdgt'.
|
2010-02-27 22:11:30 +01:00
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
ft_writesimple(double *xlims, double *ylims, char *filename, char *title, char *xlabel, char *ylabel, GRIDTYPE gridtype, PLOTTYPE plottype, struct dvec *vecs)
|
|
|
|
|
{
|
|
|
|
|
FILE *file_data;
|
2016-06-06 21:16:49 +02:00
|
|
|
struct dvec *v;
|
|
|
|
|
int i, numVecs, maxlen, preci;
|
|
|
|
|
bool appendwrite, singlescale, vecnames;
|
2010-02-27 22:11:30 +01:00
|
|
|
|
2010-11-16 21:38:24 +01:00
|
|
|
NG_IGNORE(xlims);
|
|
|
|
|
NG_IGNORE(ylims);
|
|
|
|
|
NG_IGNORE(title);
|
|
|
|
|
NG_IGNORE(xlabel);
|
|
|
|
|
NG_IGNORE(ylabel);
|
|
|
|
|
NG_IGNORE(gridtype);
|
|
|
|
|
NG_IGNORE(plottype);
|
2010-11-16 20:11:32 +01:00
|
|
|
|
2011-11-26 00:04:09 +01:00
|
|
|
appendwrite = cp_getvar("appendwrite", CP_BOOL, NULL);
|
2016-06-06 21:16:49 +02:00
|
|
|
singlescale = cp_getvar("wr_singlescale", CP_BOOL, NULL);
|
|
|
|
|
vecnames = cp_getvar("wr_vecnames", CP_BOOL, NULL);
|
2010-02-27 22:11:30 +01:00
|
|
|
|
|
|
|
|
/* Sanity checking. */
|
2012-09-20 20:30:53 +02:00
|
|
|
for (v = vecs, numVecs = 0; v; v = v->v_link2)
|
2011-11-26 00:04:09 +01:00
|
|
|
numVecs++;
|
2012-09-20 20:30:53 +02:00
|
|
|
|
|
|
|
|
if (numVecs == 0)
|
2011-11-26 00:04:09 +01:00
|
|
|
return;
|
2010-02-27 22:11:30 +01:00
|
|
|
|
2016-06-06 21:16:49 +02:00
|
|
|
/* print scale vector only once */
|
|
|
|
|
if (singlescale) {
|
|
|
|
|
/* check if all vectors have equal scale length */
|
|
|
|
|
maxlen = vecs->v_length; /* first length of vector read */
|
|
|
|
|
for (v = vecs; v; v = v->v_link2)
|
|
|
|
|
if (v->v_scale->v_length != maxlen) {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Error: Option 'singlescale' not possible.\n"
|
|
|
|
|
" Vectors %s and %s have different lengths!\n"
|
|
|
|
|
" No data written to %s!\n\n",
|
|
|
|
|
vecs->v_name, v->v_name, filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* find maximum scale length from all vectors */
|
|
|
|
|
maxlen = 0;
|
|
|
|
|
for (v = vecs; v; v = v->v_link2)
|
|
|
|
|
maxlen = MAX(v->v_scale->v_length, maxlen);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-27 22:11:30 +01:00
|
|
|
/* Open the output data file. */
|
2016-07-31 17:54:01 +02:00
|
|
|
if ((file_data = fopen(filename, appendwrite ? "a" : "w")) == NULL) {
|
2011-11-26 00:04:09 +01:00
|
|
|
perror(filename);
|
|
|
|
|
return;
|
2010-02-27 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-06 21:16:49 +02:00
|
|
|
/* If option numdgt is set, use it for printout precision. */
|
|
|
|
|
if (cp_numdgt > 0)
|
|
|
|
|
preci = cp_numdgt;
|
|
|
|
|
else
|
|
|
|
|
preci = 8;
|
2010-02-27 22:11:30 +01:00
|
|
|
|
2016-06-06 21:16:49 +02:00
|
|
|
/* Print names of vectors to first line */
|
|
|
|
|
if (vecnames) {
|
|
|
|
|
bool prscale = TRUE;
|
2012-09-20 20:30:53 +02:00
|
|
|
for (v = vecs; v; v = v->v_link2) {
|
2016-06-06 21:16:49 +02:00
|
|
|
struct dvec *scale = v->v_scale;
|
|
|
|
|
/* If wr_singlescale is set, print scale name only in first column */
|
|
|
|
|
if (prscale)
|
|
|
|
|
fprintf(file_data, " %-*s", preci + 7, scale->v_name);
|
2010-02-27 22:11:30 +01:00
|
|
|
|
2011-11-26 00:04:09 +01:00
|
|
|
if (isreal(v))
|
2016-06-06 21:16:49 +02:00
|
|
|
fprintf(file_data, " %-*s", preci + 7, v->v_name);
|
2011-11-26 00:04:09 +01:00
|
|
|
else
|
2016-06-06 21:16:49 +02:00
|
|
|
fprintf(file_data, " %-*s %-*s", preci + 7, v->v_name, preci + 7, v->v_name);
|
|
|
|
|
if (singlescale)
|
|
|
|
|
/* the following names are printed without scale vector names */
|
|
|
|
|
prscale = FALSE;
|
|
|
|
|
}
|
|
|
|
|
fprintf(file_data, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Write out the data as simple arrays */
|
|
|
|
|
for (i = 0; i < maxlen; i++) {
|
|
|
|
|
bool prscale = TRUE;
|
|
|
|
|
/* print scale from the first vector, then only if wr_singlescale is not set */
|
|
|
|
|
for (v = vecs; v; v = v->v_link2) {
|
|
|
|
|
struct dvec *scale = v->v_scale;
|
|
|
|
|
/* if no more scale and value data, just print spaces */
|
|
|
|
|
if (i >= scale->v_length) {
|
|
|
|
|
if (prscale)
|
|
|
|
|
fprintf(file_data, "%*s", preci + 8, "");
|
|
|
|
|
|
|
|
|
|
if (isreal(v))
|
|
|
|
|
fprintf(file_data, "%*s", preci + 8, "");
|
|
|
|
|
else
|
|
|
|
|
fprintf(file_data, "%*s", 2 * (preci + 8), "");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (prscale) {
|
|
|
|
|
double xval = isreal(scale)
|
|
|
|
|
? scale->v_realdata[i]
|
|
|
|
|
: realpart(scale->v_compdata[i]);
|
|
|
|
|
fprintf(file_data, "% .*e ", preci, xval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isreal(v))
|
|
|
|
|
fprintf(file_data, "% .*e ", preci, v->v_realdata[i]);
|
|
|
|
|
else
|
|
|
|
|
fprintf(file_data, "% .*e % .*e ", preci, realpart(v->v_compdata[i]), preci, imagpart(v->v_compdata[i]));
|
|
|
|
|
}
|
|
|
|
|
if (singlescale)
|
|
|
|
|
/* the following vectors are printed without scale vector */
|
|
|
|
|
prscale = FALSE;
|
2011-11-26 00:04:09 +01:00
|
|
|
}
|
2012-09-20 20:30:53 +02:00
|
|
|
fprintf(file_data, "\n");
|
2010-02-27 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-09-20 20:30:53 +02:00
|
|
|
(void) fclose(file_data);
|
2010-02-28 18:51:10 +01:00
|
|
|
}
|