Copy of CI from master to 2020
[gromacs.git] / src / gromacs / fileio / matio.cpp
blobcbb9c96b79614fa2c4363729e82bafdb5f51a758
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2017,2018,2019,2020, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gmxpre.h"
39 #include "matio.h"
41 #include <cctype>
42 #include <cmath>
43 #include <cstdio>
44 #include <cstring>
46 #include <algorithm>
47 #include <regex>
48 #include <string>
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/math/functions.h"
52 #include "gromacs/math/utilities.h"
53 #include "gromacs/utility/binaryinformation.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/programcontext.h"
59 #include "gromacs/utility/smalloc.h"
61 using namespace gmx;
63 static const char mapper[] =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+{}|;:',<.>/"
65 "?";
66 #define NMAP static_cast<long int>(sizeof(mapper) / sizeof(mapper[0]))
68 #define MAX_XPM_LINELENGTH 4096
70 real** mk_matrix(int nx, int ny, gmx_bool b1D)
72 int i;
73 real** m;
75 snew(m, nx);
76 if (b1D)
78 snew(m[0], nx * ny);
81 for (i = 0; (i < nx); i++)
83 if (b1D)
85 m[i] = &(m[0][i * ny]);
87 else
89 snew(m[i], ny);
93 return m;
96 void done_matrix(int nx, real*** m)
98 int i;
100 for (i = 0; (i < nx); i++)
102 sfree((*m)[i]);
104 sfree(*m);
105 *m = nullptr;
108 static bool operator==(t_xpmelmt e1, t_xpmelmt e2)
110 return (e1.c1 == e2.c1) && (e1.c2 == e2.c2);
113 //! Return the index of the first element that matches \c c, or -1 if not found.
114 t_matelmt searchcmap(ArrayRef<const t_mapping> map, t_xpmelmt c)
116 auto findIt = std::find_if(map.begin(), map.end(), [&c](const auto& m) { return (m.code == c); });
117 return (findIt == map.end()) ? -1 : (findIt - map.begin());
120 //! Read the mapping table from in, return number of entries
121 static std::vector<t_mapping> getcmap(FILE* in, const char* fn)
123 int i, n;
124 char line[STRLEN];
125 char code[STRLEN], desc[STRLEN];
126 double r, g, b;
127 std::vector<t_mapping> m;
129 if (fgets2(line, STRLEN - 1, in) == nullptr)
131 gmx_fatal(FARGS,
132 "Not enough lines in colormap file %s"
133 "(just wanted to read number of entries)",
134 fn);
136 sscanf(line, "%d", &n);
137 m.resize(n);
138 for (i = 0; (i < n); i++)
140 if (fgets2(line, STRLEN - 1, in) == nullptr)
142 gmx_fatal(FARGS,
143 "Not enough lines in colormap file %s"
144 "(should be %d, found only %d)",
145 fn, n + 1, i);
147 sscanf(line, "%s%s%lf%lf%lf", code, desc, &r, &g, &b);
148 m[i].code.c1 = code[0];
149 m[i].code.c2 = 0;
150 m[i].desc = gmx_strdup(desc);
151 m[i].rgb.r = r;
152 m[i].rgb.g = g;
153 m[i].rgb.b = b;
156 return m;
159 std::vector<t_mapping> readcmap(const char* fn)
161 FilePtr in = openLibraryFile(fn);
162 return getcmap(in.get(), fn);
165 void printcmap(FILE* out, int n, t_mapping map[])
167 int i;
169 fprintf(out, "%d\n", n);
170 for (i = 0; (i < n); i++)
172 fprintf(out, "%c%c %20s %10g %10g %10g\n", map[i].code.c1 ? map[i].code.c1 : ' ',
173 map[i].code.c2 ? map[i].code.c2 : ' ', map[i].desc, map[i].rgb.r, map[i].rgb.g,
174 map[i].rgb.b);
178 void writecmap(const char* fn, int n, t_mapping map[])
180 FILE* out;
182 out = gmx_fio_fopen(fn, "w");
183 printcmap(out, n, map);
184 gmx_fio_fclose(out);
187 static char* fgetline(char** line, int llmax, int* llalloc, FILE* in)
189 char* fg;
191 if (llmax > *llalloc)
193 srenew(*line, llmax + 1);
194 *llalloc = llmax;
196 fg = fgets(*line, llmax, in);
197 trim(*line);
199 return fg;
202 static void skipstr(char* line)
204 int i, c;
206 ltrim(line);
207 c = 0;
208 while ((line[c] != ' ') && (line[c] != '\0'))
210 c++;
212 i = c;
213 while (line[c] != '\0')
215 line[c - i] = line[c];
216 c++;
218 line[c - i] = '\0';
221 static char* line2string(char** line)
223 int i;
225 if (*line != nullptr)
227 while (((*line)[0] != '\"') && ((*line)[0] != '\0'))
229 (*line)++;
232 if ((*line)[0] != '\"')
234 return nullptr;
236 (*line)++;
238 i = 0;
239 while (((*line)[i] != '\"') && ((*line)[i] != '\0'))
241 i++;
244 if ((*line)[i] != '\"')
246 *line = nullptr;
248 else
250 (*line)[i] = 0;
254 return *line;
257 //! If a label named \c label is found in \c line, return it. Otherwise return empty string.
258 static std::string findLabelInLine(const std::string& line, const std::string& label)
260 std::regex re(".*" + label + "\"(.*)\"");
261 std::smatch match;
262 if (std::regex_search(line, match, re) && match.size() > 1)
264 return match.str(1);
266 return std::string();
269 //! Read and return a matrix from \c in
270 static t_matrix read_xpm_entry(FILE* in)
272 char * line_buf = nullptr, *line = nullptr, *str, buf[256] = { 0 };
273 int i, m, col_len, nch = 0, llmax;
274 int llalloc = 0;
275 unsigned int r, g, b;
276 double u;
277 gmx_bool bGetOnWithIt, bSetLine;
278 t_xpmelmt c;
280 t_matrix mm;
282 llmax = STRLEN;
284 while ((nullptr != fgetline(&line_buf, llmax, &llalloc, in))
285 && (std::strncmp(line_buf, "static", 6) != 0))
287 std::string lineString = line_buf;
288 mm.title = findLabelInLine(lineString, "title");
289 mm.legend = findLabelInLine(lineString, "legend");
290 mm.label_x = findLabelInLine(lineString, "x-label");
291 mm.label_y = findLabelInLine(lineString, "y-label");
292 findLabelInLine(lineString, "type"); // discard the returned string
295 if (!line_buf || strncmp(line_buf, "static", 6) != 0)
297 gmx_input("Invalid XPixMap");
300 if (buf[0] && (gmx_strcasecmp(buf, "Discrete") == 0))
302 mm.bDiscrete = TRUE;
305 if (debug)
307 fprintf(debug, "%s %s %s %s\n", mm.title.c_str(), mm.legend.c_str(), mm.label_x.c_str(),
308 mm.label_y.c_str());
311 /* Read sizes */
312 int nmap = 0;
313 bGetOnWithIt = FALSE;
314 while (!bGetOnWithIt && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)))
316 line = line_buf;
317 while ((line[0] != '\"') && (line[0] != '\0'))
319 line++;
322 if (line[0] == '\"')
324 line2string(&line);
325 sscanf(line, "%d %d %d %d", &(mm.nx), &(mm.ny), &nmap, &nch);
326 if (nch > 2)
328 gmx_fatal(FARGS, "Sorry can only read xpm's with at most 2 characters per pixel\n");
330 if (mm.nx <= 0 || mm.ny <= 0)
332 gmx_fatal(FARGS, "Dimensions of xpm-file have to be larger than 0\n");
334 llmax = std::max(STRLEN, mm.nx + 10);
335 bGetOnWithIt = TRUE;
338 if (debug)
340 fprintf(debug, "mm.nx %d mm.ny %d nmap %d nch %d\n", mm.nx, mm.ny, nmap, nch);
342 if (nch == 0)
344 gmx_fatal(FARGS, "Number of characters per pixel not found in xpm\n");
347 /* Read color map */
348 mm.map.resize(nmap);
349 m = 0;
350 while ((m < nmap) && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)))
352 line = std::strchr(line_buf, '\"');
353 if (line)
355 line++;
356 /* Read xpm color map entry */
357 mm.map[m].code.c1 = line[0];
358 if (nch == 1)
360 mm.map[m].code.c2 = 0;
362 else
364 mm.map[m].code.c2 = line[1];
366 line += nch;
367 str = std::strchr(line, '#');
368 if (str)
370 str++;
371 col_len = 0;
372 while (std::isxdigit(str[col_len]))
374 col_len++;
376 if (col_len == 6)
378 sscanf(line, "%*s #%2x%2x%2x", &r, &g, &b);
379 mm.map[m].rgb.r = r / 255.0;
380 mm.map[m].rgb.g = g / 255.0;
381 mm.map[m].rgb.b = b / 255.0;
383 else if (col_len == 12)
385 sscanf(line, "%*s #%4x%4x%4x", &r, &g, &b);
386 mm.map[m].rgb.r = r / 65535.0;
387 mm.map[m].rgb.g = g / 65535.0;
388 mm.map[m].rgb.b = b / 65535.0;
390 else
392 gmx_file("Unsupported or invalid colormap in X PixMap");
395 else
397 str = std::strchr(line, 'c');
398 if (str)
400 str += 2;
402 else
404 gmx_file("Unsupported or invalid colormap in X PixMap");
406 fprintf(stderr, "Using white for color \"%s", str);
407 mm.map[m].rgb.r = 1;
408 mm.map[m].rgb.g = 1;
409 mm.map[m].rgb.b = 1;
411 line = std::strchr(line, '\"');
412 line++;
413 line2string(&line);
414 mm.map[m].desc = gmx_strdup(line);
415 m++;
418 if (m != nmap)
420 gmx_fatal(FARGS,
421 "Number of read colors map entries (%d) does not match the number in the header "
422 "(%d)",
423 m, nmap);
426 /* Read axes, if there are any */
427 bSetLine = FALSE;
430 if (bSetLine)
432 line = line_buf;
434 bSetLine = TRUE;
435 GMX_RELEASE_ASSERT(line, "Need to have valid line to parse");
436 if (strstr(line, "x-axis"))
438 line = std::strstr(line, "x-axis");
439 skipstr(line);
440 mm.axis_x.resize(0);
441 mm.axis_x.reserve(mm.nx + 1);
442 while (sscanf(line, "%lf", &u) == 1)
444 if (ssize(mm.axis_x) > mm.nx)
446 gmx_fatal(FARGS, "Too many x-axis labels in xpm (max %d)", mm.nx);
448 else if (ssize(mm.axis_x) == mm.nx)
450 mm.flags |= MAT_SPATIAL_X;
452 mm.axis_x.push_back(u);
453 skipstr(line);
456 else if (std::strstr(line, "y-axis"))
458 line = std::strstr(line, "y-axis");
459 skipstr(line);
460 mm.axis_y.resize(0);
461 mm.axis_y.reserve(mm.ny + 1);
462 while (sscanf(line, "%lf", &u) == 1)
464 if (ssize(mm.axis_y) > mm.ny)
466 gmx_fatal(FARGS, "Too many y-axis labels in xpm (max %d)", mm.ny);
468 else if (ssize(mm.axis_y) == mm.ny)
470 mm.flags |= MAT_SPATIAL_Y;
472 mm.axis_y.push_back(u);
473 skipstr(line);
476 } while ((line[0] != '\"') && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)));
478 /* Read matrix */
479 mm.matrix.resize(mm.nx, mm.ny);
480 int rowIndex = mm.ny - 1;
481 bSetLine = FALSE;
484 if (bSetLine)
486 line = line_buf;
488 bSetLine = TRUE;
489 if (rowIndex % (1 + mm.ny / 100) == 0)
491 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (mm.ny - rowIndex)) / mm.ny);
493 while ((line[0] != '\"') && (line[0] != '\0'))
495 line++;
497 if (line[0] != '\"')
499 gmx_fatal(FARGS, "Not enough characters in row %d of the matrix\n", rowIndex + 1);
501 else
503 line++;
504 for (i = 0; i < mm.nx; i++)
506 c.c1 = line[nch * i];
507 if (nch == 1)
509 c.c2 = 0;
511 else
513 c.c2 = line[nch * i + 1];
515 mm.matrix(i, rowIndex) = searchcmap(mm.map, c);
517 rowIndex--;
519 } while ((rowIndex >= 0) && (nullptr != fgetline(&line_buf, llmax, &llalloc, in)));
520 if (rowIndex >= 0)
522 gmx_incons("Not enough rows in the matrix");
525 sfree(line_buf);
526 return mm;
529 std::vector<t_matrix> read_xpm_matrix(const char* fnm)
531 FILE* in;
532 char* line = nullptr;
533 int llalloc = 0;
535 in = gmx_fio_fopen(fnm, "r");
537 std::vector<t_matrix> mat;
538 while (nullptr != fgetline(&line, STRLEN, &llalloc, in))
540 if (std::strstr(line, "/* XPM */"))
542 mat.emplace_back(read_xpm_entry(in));
545 gmx_fio_fclose(in);
547 if (mat.empty())
549 gmx_file("Invalid XPixMap");
552 sfree(line);
554 return mat;
557 real** matrix2real(t_matrix* in, real** out)
559 double tmp;
561 std::vector<real> rmap(in->map.size());
563 for (gmx::index i = 0; i != ssize(in->map); ++i)
565 if ((in->map[i].desc == nullptr) || (sscanf(in->map[i].desc, "%lf", &tmp) != 1))
567 fprintf(stderr,
568 "Could not convert matrix to reals,\n"
569 "color map entry %zd has a non-real description: \"%s\"\n",
570 i, in->map[i].desc);
571 return nullptr;
573 rmap[i] = tmp;
576 if (out == nullptr)
578 snew(out, in->nx);
579 for (int i = 0; i < in->nx; i++)
581 snew(out[i], in->ny);
584 for (int i = 0; i < in->nx; i++)
586 for (int j = 0; j < in->ny; j++)
588 out[i][j] = rmap[in->matrix(i, j)];
592 fprintf(stderr, "Converted a %dx%d matrix with %zu levels to reals\n", in->nx, in->ny, in->map.size());
594 return out;
597 static void write_xpm_header(FILE* out,
598 const std::string& title,
599 const std::string& legend,
600 const std::string& label_x,
601 const std::string& label_y,
602 gmx_bool bDiscrete)
604 fprintf(out, "/* XPM */\n");
605 fprintf(out, "/* This file can be converted to EPS by the GROMACS program xpm2ps */\n");
606 fprintf(out, "/* title: \"%s\" */\n", title.c_str());
607 fprintf(out, "/* legend: \"%s\" */\n", legend.c_str());
608 fprintf(out, "/* x-label: \"%s\" */\n", label_x.c_str());
609 fprintf(out, "/* y-label: \"%s\" */\n", label_y.c_str());
610 if (bDiscrete)
612 fprintf(out, "/* type: \"Discrete\" */\n");
614 else
616 fprintf(out, "/* type: \"Continuous\" */\n");
620 static int calc_nmid(int nlevels, real lo, real mid, real hi)
622 /* Take care that we have at least 1 entry in the mid to hi range
624 return std::min(std::max(0, static_cast<int>(((mid - lo) / (hi - lo)) * (nlevels - 1))), nlevels - 1);
627 static void
628 write_xpm_map3(FILE* out, int n_x, int n_y, int* nlevels, real lo, real mid, real hi, t_rgb rlo, t_rgb rmid, t_rgb rhi)
630 int i, nmid;
631 double r, g, b, clev_lo, clev_hi;
633 if (*nlevels > NMAP * NMAP)
635 fprintf(stderr, "Warning, too many levels (%d) in matrix, using %d only\n", *nlevels,
636 static_cast<int>(NMAP * NMAP));
637 *nlevels = NMAP * NMAP;
639 else if (*nlevels < 2)
641 fprintf(stderr, "Warning, too few levels (%d) in matrix, using 2 instead\n", *nlevels);
642 *nlevels = 2;
644 if (!((mid >= lo) && (mid < hi)))
646 gmx_fatal(FARGS, "Lo: %f, Mid: %f, Hi: %f\n", lo, mid, hi);
649 fprintf(out, "static char *gromacs_xpm[] = {\n");
650 fprintf(out, "\"%d %d %d %d\",\n", n_x, n_y, *nlevels, (*nlevels <= NMAP) ? 1 : 2);
652 nmid = calc_nmid(*nlevels, lo, mid, hi);
653 clev_lo = nmid;
654 clev_hi = (*nlevels - 1 - nmid);
655 for (i = 0; (i < nmid); i++)
657 r = rlo.r + (i * (rmid.r - rlo.r) / clev_lo);
658 g = rlo.g + (i * (rmid.g - rlo.g) / clev_lo);
659 b = rlo.b + (i * (rmid.b - rlo.b) / clev_lo);
660 fprintf(out, "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n", mapper[i % NMAP],
661 (*nlevels <= NMAP) ? ' ' : mapper[i / NMAP],
662 static_cast<unsigned int>(std::round(255 * r)),
663 static_cast<unsigned int>(std::round(255 * g)),
664 static_cast<unsigned int>(std::round(255 * b)), ((nmid - i) * lo + i * mid) / clev_lo);
666 for (i = 0; (i < (*nlevels - nmid)); i++)
668 r = rmid.r + (i * (rhi.r - rmid.r) / clev_hi);
669 g = rmid.g + (i * (rhi.g - rmid.g) / clev_hi);
670 b = rmid.b + (i * (rhi.b - rmid.b) / clev_hi);
671 fprintf(out, "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n", mapper[(i + nmid) % NMAP],
672 (*nlevels <= NMAP) ? ' ' : mapper[(i + nmid) / NMAP],
673 static_cast<unsigned int>(std::round(255 * r)),
674 static_cast<unsigned int>(std::round(255 * g)),
675 static_cast<unsigned int>(std::round(255 * b)),
676 ((*nlevels - 1 - nmid - i) * mid + i * hi) / clev_hi);
680 static void pr_simple_cmap(FILE* out, real lo, real hi, int nlevel, t_rgb rlo, t_rgb rhi, int i0)
682 int i;
683 real r, g, b, fac;
685 for (i = 0; (i < nlevel); i++)
687 fac = (i + 1.0) / (nlevel);
688 r = rlo.r + fac * (rhi.r - rlo.r);
689 g = rlo.g + fac * (rhi.g - rlo.g);
690 b = rlo.b + fac * (rhi.b - rlo.b);
691 fprintf(out, "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n", mapper[(i + i0) % NMAP],
692 (nlevel <= NMAP) ? ' ' : mapper[(i + i0) / NMAP],
693 static_cast<unsigned int>(std::round(255 * r)),
694 static_cast<unsigned int>(std::round(255 * g)),
695 static_cast<unsigned int>(std::round(255 * b)), lo + fac * (hi - lo));
699 static void pr_discrete_cmap(FILE* out, int* nlevel, int i0)
701 t_rgb rgbd[16] = {
702 { 1.0, 1.0, 1.0 }, /* white */
703 { 1.0, 0.0, 0.0 }, /* red */
704 { 1.0, 1.0, 0.0 }, /* yellow */
705 { 0.0, 0.0, 1.0 }, /* blue */
706 { 0.0, 1.0, 0.0 }, /* green */
707 { 1.0, 0.0, 1.0 }, /* purple */
708 { 1.0, 0.4, 0.0 }, /* orange */
709 { 0.0, 1.0, 1.0 }, /* cyan */
710 { 1.0, 0.4, 0.4 }, /* pink */
711 { 1.0, 1.0, 0.0 }, /* yellow */
712 { 0.4, 0.4, 1.0 }, /* lightblue */
713 { 0.4, 1.0, 0.4 }, /* lightgreen */
714 { 1.0, 0.4, 1.0 }, /* lightpurple */
715 { 1.0, 0.7, 0.4 }, /* lightorange */
716 { 0.4, 1.0, 1.0 }, /* lightcyan */
717 { 0.0, 0.0, 0.0 } /* black */
720 int i, n;
722 *nlevel = std::min(16, *nlevel);
723 n = *nlevel;
724 for (i = 0; (i < n); i++)
726 fprintf(out, "\"%c%c c #%02X%02X%02X \" /* \"%3d\" */,\n", mapper[(i + i0) % NMAP],
727 (n <= NMAP) ? ' ' : mapper[(i + i0) / NMAP],
728 static_cast<unsigned int>(round(255 * rgbd[i].r)),
729 static_cast<unsigned int>(round(255 * rgbd[i].g)),
730 static_cast<unsigned int>(round(255 * rgbd[i].b)), i);
735 static void write_xpm_map_split(FILE* out,
736 int n_x,
737 int n_y,
738 const int* nlevel_top,
739 real lo_top,
740 real hi_top,
741 t_rgb rlo_top,
742 t_rgb rhi_top,
743 gmx_bool bDiscreteColor,
744 int* nlevel_bot,
745 real lo_bot,
746 real hi_bot,
747 t_rgb rlo_bot,
748 t_rgb rhi_bot)
750 int ntot;
752 ntot = *nlevel_top + *nlevel_bot;
753 if (ntot > NMAP)
755 gmx_fatal(FARGS, "Warning, too many levels (%d) in matrix", ntot);
758 fprintf(out, "static char *gromacs_xpm[] = {\n");
759 fprintf(out, "\"%d %d %d %d\",\n", n_x, n_y, ntot, 1);
761 if (bDiscreteColor)
763 pr_discrete_cmap(out, nlevel_bot, 0);
765 else
767 pr_simple_cmap(out, lo_bot, hi_bot, *nlevel_bot, rlo_bot, rhi_bot, 0);
770 pr_simple_cmap(out, lo_top, hi_top, *nlevel_top, rlo_top, rhi_top, *nlevel_bot);
774 static void write_xpm_map(FILE* out, int n_x, int n_y, int* nlevels, real lo, real hi, t_rgb rlo, t_rgb rhi)
776 int i, nlo;
777 real invlevel, r, g, b;
779 if (*nlevels > NMAP * NMAP)
781 fprintf(stderr, "Warning, too many levels (%d) in matrix, using %d only\n", *nlevels,
782 static_cast<int>(NMAP * NMAP));
783 *nlevels = NMAP * NMAP;
785 else if (*nlevels < 2)
787 fprintf(stderr, "Warning, too few levels (%d) in matrix, using 2 instead\n", *nlevels);
788 *nlevels = 2;
791 fprintf(out, "static char *gromacs_xpm[] = {\n");
792 fprintf(out, "\"%d %d %d %d\",\n", n_x, n_y, *nlevels, (*nlevels <= NMAP) ? 1 : 2);
794 invlevel = 1.0 / (*nlevels - 1);
795 for (i = 0; (i < *nlevels); i++)
797 nlo = *nlevels - 1 - i;
798 r = (nlo * rlo.r + i * rhi.r) * invlevel;
799 g = (nlo * rlo.g + i * rhi.g) * invlevel;
800 b = (nlo * rlo.b + i * rhi.b) * invlevel;
801 fprintf(out, "\"%c%c c #%02X%02X%02X \" /* \"%.3g\" */,\n", mapper[i % NMAP],
802 (*nlevels <= NMAP) ? ' ' : mapper[i / NMAP],
803 static_cast<unsigned int>(std::round(255 * r)),
804 static_cast<unsigned int>(std::round(255 * g)),
805 static_cast<unsigned int>(std::round(255 * b)), (nlo * lo + i * hi) * invlevel);
809 static void writeXpmAxis(FILE* out, const char* axis, ArrayRef<const real> label)
811 if (label.empty())
813 return;
815 for (gmx::index i = 0; i != ssize(label); ++i)
817 if (i % 80 == 0)
819 if (i)
821 fprintf(out, "*/\n");
823 fprintf(out, "/* %s-axis: ", axis);
825 fprintf(out, "%g ", label[i]);
827 fprintf(out, "*/\n");
830 static void write_xpm_data(FILE* out, int n_x, int n_y, real** mat, real lo, real hi, int nlevels)
832 int i, j, c;
833 real invlevel;
835 invlevel = (nlevels - 1) / (hi - lo);
836 for (j = n_y - 1; (j >= 0); j--)
838 if (j % (1 + n_y / 100) == 0)
840 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (n_y - j)) / n_y);
842 fprintf(out, "\"");
843 for (i = 0; (i < n_x); i++)
845 c = roundToInt((mat[i][j] - lo) * invlevel);
846 if (c < 0)
848 c = 0;
850 if (c >= nlevels)
852 c = nlevels - 1;
854 if (nlevels <= NMAP)
856 fprintf(out, "%c", mapper[c]);
858 else
860 fprintf(out, "%c%c", mapper[c % NMAP], mapper[c / NMAP]);
863 if (j > 0)
865 fprintf(out, "\",\n");
867 else
869 fprintf(out, "\"\n");
874 static void write_xpm_data3(FILE* out, int n_x, int n_y, real** mat, real lo, real mid, real hi, int nlevels)
876 int i, j, c = 0, nmid;
877 real invlev_lo, invlev_hi;
879 nmid = calc_nmid(nlevels, lo, mid, hi);
880 invlev_hi = (nlevels - 1 - nmid) / (hi - mid);
881 invlev_lo = (nmid) / (mid - lo);
883 for (j = n_y - 1; (j >= 0); j--)
885 if (j % (1 + n_y / 100) == 0)
887 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (n_y - j)) / n_y);
889 fprintf(out, "\"");
890 for (i = 0; (i < n_x); i++)
892 if (mat[i][j] >= mid)
894 c = nmid + roundToInt((mat[i][j] - mid) * invlev_hi);
896 else if (mat[i][j] >= lo)
898 c = roundToInt((mat[i][j] - lo) * invlev_lo);
900 else
902 c = 0;
905 if (c < 0)
907 c = 0;
909 if (c >= nlevels)
911 c = nlevels - 1;
913 if (nlevels <= NMAP)
915 fprintf(out, "%c", mapper[c]);
917 else
919 fprintf(out, "%c%c", mapper[c % NMAP], mapper[c / NMAP]);
922 if (j > 0)
924 fprintf(out, "\",\n");
926 else
928 fprintf(out, "\"\n");
933 static void write_xpm_data_split(FILE* out,
934 int n_x,
935 int n_y,
936 real** mat,
937 real lo_top,
938 real hi_top,
939 int nlevel_top,
940 real lo_bot,
941 real hi_bot,
942 int nlevel_bot)
944 int i, j, c;
945 real invlev_top, invlev_bot;
947 invlev_top = (nlevel_top - 1) / (hi_top - lo_top);
948 invlev_bot = (nlevel_bot - 1) / (hi_bot - lo_bot);
950 for (j = n_y - 1; (j >= 0); j--)
952 if (j % (1 + n_y / 100) == 0)
954 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (n_y - j)) / n_y);
956 fprintf(out, "\"");
957 for (i = 0; (i < n_x); i++)
959 if (i < j)
961 c = nlevel_bot + roundToInt((mat[i][j] - lo_top) * invlev_top);
962 if ((c < nlevel_bot) || (c >= nlevel_bot + nlevel_top))
964 gmx_fatal(FARGS,
965 "Range checking i = %d, j = %d, c = %d, bot = %d, top = %d "
966 "matrix[i,j] = %f",
967 i, j, c, nlevel_bot, nlevel_top, mat[i][j]);
970 else if (i > j)
972 c = roundToInt((mat[i][j] - lo_bot) * invlev_bot);
973 if ((c < 0) || (c >= nlevel_bot + nlevel_bot))
975 gmx_fatal(FARGS,
976 "Range checking i = %d, j = %d, c = %d, bot = %d, top = %d "
977 "matrix[i,j] = %f",
978 i, j, c, nlevel_bot, nlevel_top, mat[i][j]);
981 else
983 c = nlevel_bot;
986 fprintf(out, "%c", mapper[c]);
988 if (j > 0)
990 fprintf(out, "\",\n");
992 else
994 fprintf(out, "\"\n");
999 void write_xpm_m(FILE* out, t_matrix m)
1001 gmx_bool bOneChar;
1002 t_xpmelmt c;
1004 bOneChar = (m.map[0].code.c2 == 0);
1005 write_xpm_header(out, m.title, m.legend, m.label_x, m.label_y, m.bDiscrete);
1006 fprintf(out, "static char *gromacs_xpm[] = {\n");
1007 fprintf(out, "\"%d %d %zu %d\",\n", m.nx, m.ny, m.map.size(), bOneChar ? 1 : 2);
1008 for (const auto& map : m.map)
1010 fprintf(out, "\"%c%c c #%02X%02X%02X \" /* \"%s\" */,\n", map.code.c1,
1011 bOneChar ? ' ' : map.code.c2, static_cast<unsigned int>(round(map.rgb.r * 255)),
1012 static_cast<unsigned int>(round(map.rgb.g * 255)),
1013 static_cast<unsigned int>(round(map.rgb.b * 255)), map.desc);
1015 writeXpmAxis(out, "x", m.axis_x);
1016 writeXpmAxis(out, "y", m.axis_y);
1017 for (int j = m.ny - 1; (j >= 0); j--)
1019 if (j % (1 + m.ny / 100) == 0)
1021 fprintf(stderr, "%3d%%\b\b\b\b", (100 * (m.ny - j)) / m.ny);
1023 fprintf(out, "\"");
1024 if (bOneChar)
1026 for (int i = 0; (i < m.nx); i++)
1028 fprintf(out, "%c", m.map[m.matrix(i, j)].code.c1);
1031 else
1033 for (int i = 0; (i < m.nx); i++)
1035 c = m.map[m.matrix(i, j)].code;
1036 fprintf(out, "%c%c", c.c1, c.c2);
1039 if (j > 0)
1041 fprintf(out, "\",\n");
1043 else
1045 fprintf(out, "\"\n");
1050 void write_xpm3(FILE* out,
1051 unsigned int flags,
1052 const std::string& title,
1053 const std::string& legend,
1054 const std::string& label_x,
1055 const std::string& label_y,
1056 int n_x,
1057 int n_y,
1058 real axis_x[],
1059 real axis_y[],
1060 real* mat[],
1061 real lo,
1062 real mid,
1063 real hi,
1064 t_rgb rlo,
1065 t_rgb rmid,
1066 t_rgb rhi,
1067 int* nlevels)
1069 /* See write_xpm.
1070 * Writes a colormap varying as rlo -> rmid -> rhi.
1073 if (hi <= lo)
1075 gmx_fatal(FARGS, "hi (%g) <= lo (%g)", hi, lo);
1078 write_xpm_header(out, title, legend, label_x, label_y, FALSE);
1079 write_xpm_map3(out, n_x, n_y, nlevels, lo, mid, hi, rlo, rmid, rhi);
1080 writeXpmAxis(out, "x", ArrayRef<real>(axis_x, axis_x + n_x + ((flags & MAT_SPATIAL_X) != 0U ? 1 : 0)));
1081 writeXpmAxis(out, "y", ArrayRef<real>(axis_y, axis_y + n_y + ((flags & MAT_SPATIAL_Y) != 0U ? 1 : 0)));
1082 write_xpm_data3(out, n_x, n_y, mat, lo, mid, hi, *nlevels);
1085 void write_xpm_split(FILE* out,
1086 unsigned int flags,
1087 const std::string& title,
1088 const std::string& legend,
1089 const std::string& label_x,
1090 const std::string& label_y,
1091 int n_x,
1092 int n_y,
1093 real axis_x[],
1094 real axis_y[],
1095 real* mat[],
1096 real lo_top,
1097 real hi_top,
1098 int* nlevel_top,
1099 t_rgb rlo_top,
1100 t_rgb rhi_top,
1101 real lo_bot,
1102 real hi_bot,
1103 int* nlevel_bot,
1104 gmx_bool bDiscreteColor,
1105 t_rgb rlo_bot,
1106 t_rgb rhi_bot)
1108 /* See write_xpm.
1109 * Writes a colormap varying as rlo -> rmid -> rhi.
1112 if (hi_top <= lo_top)
1114 gmx_fatal(FARGS, "hi_top (%g) <= lo_top (%g)", hi_top, lo_top);
1116 if (hi_bot <= lo_bot)
1118 gmx_fatal(FARGS, "hi_bot (%g) <= lo_bot (%g)", hi_bot, lo_bot);
1120 if (bDiscreteColor && (*nlevel_bot >= 16))
1122 gmx_impl("Can not plot more than 16 discrete colors");
1125 write_xpm_header(out, title, legend, label_x, label_y, FALSE);
1126 write_xpm_map_split(out, n_x, n_y, nlevel_top, lo_top, hi_top, rlo_top, rhi_top, bDiscreteColor,
1127 nlevel_bot, lo_bot, hi_bot, rlo_bot, rhi_bot);
1128 writeXpmAxis(out, "x", ArrayRef<real>(axis_x, axis_x + n_x + ((flags & MAT_SPATIAL_X) != 0U ? 1 : 0)));
1129 writeXpmAxis(out, "y", ArrayRef<real>(axis_y, axis_y + n_y + ((flags & MAT_SPATIAL_Y) != 0U ? 1 : 0)));
1130 write_xpm_data_split(out, n_x, n_y, mat, lo_top, hi_top, *nlevel_top, lo_bot, hi_bot, *nlevel_bot);
1133 void write_xpm(FILE* out,
1134 unsigned int flags,
1135 const std::string& title,
1136 const std::string& legend,
1137 const std::string& label_x,
1138 const std::string& label_y,
1139 int n_x,
1140 int n_y,
1141 real axis_x[],
1142 real axis_y[],
1143 real* mat[],
1144 real lo,
1145 real hi,
1146 t_rgb rlo,
1147 t_rgb rhi,
1148 int* nlevels)
1150 /* out xpm file
1151 * title matrix title
1152 * legend label for the continuous legend
1153 * label_x label for the x-axis
1154 * label_y label for the y-axis
1155 * n_x, n_y size of the matrix
1156 * axis_x[] the x-ticklabels
1157 * axis_y[] the y-ticklables
1158 * *matrix[] element x,y is matrix[x][y]
1159 * lo output lower than lo is set to lo
1160 * hi output higher than hi is set to hi
1161 * rlo rgb value for level lo
1162 * rhi rgb value for level hi
1163 * nlevels number of color levels for the output
1166 if (hi <= lo)
1168 gmx_fatal(FARGS, "hi (%f) <= lo (%f)", hi, lo);
1171 write_xpm_header(out, title, legend, label_x, label_y, FALSE);
1172 write_xpm_map(out, n_x, n_y, nlevels, lo, hi, rlo, rhi);
1173 writeXpmAxis(out, "x", ArrayRef<real>(axis_x, axis_x + n_x + ((flags & MAT_SPATIAL_X) != 0U ? 1 : 0)));
1174 writeXpmAxis(out, "y", ArrayRef<real>(axis_y, axis_y + n_y + ((flags & MAT_SPATIAL_Y) != 0U ? 1 : 0)));
1175 write_xpm_data(out, n_x, n_y, mat, lo, hi, *nlevels);