Is there a Fortran library for pretty-printing tabular data?

Does anybody know if there’s any Fortran library to pretty-print tabular data? In other words, give it some arrays and headers, and it produces a string or a console output with data formatted and justified?

I’m aware that this can be relatively easily done by hand using the built-in edit descriptors, but I’m curious if there’s already a packaged solution out there.

I am in the process of writing a Python module that does exactly that while debugging with GDB. With the idea to pretty-print matrices and dataframes but I hit a snag a while back so I temporarily gave up.

My easiest solution to this (when not debugging) would be to call Python’s matplotlib or plotly from Fortran (I think there should already be an fpm module that does that).

@milancurcic , it seems like you have a great idea here for an addition to Fortran stdlib under some utils section!

3 Likes

@milancurcic there is a table_io module that is part of the NASA cfdtools Github repository. Don’t know if it “pretty prints” but it looks like it might be close (with a little work probably) to what you want.

see /lib/table_io/table_io.f90 in

1 Like

ACM Transactions on Mathematical Software (TOMS) published in 2009
Algorithm 892: DISPMODULE, a Fortran 95 module for pretty-printing matrices

Abstract

A standard Fortran 95 module for printing scalars, vectors, and matrices to external files is provided. The module can display variables of default kind of all intrinsic types (integer, real, complex, logical, and character), and add-on modules are provided for data of the nondefault kind. The main module is self-contained and incorporating it only requires that it be compiled and linked with a program containing a “use dispmodule” statement. A generic interface and optional parameters are used, so that the same subroutine name, DISP, is used to display items of different data type and rank, irrespective of display options. The subroutine is quite versatile, and hopefully can improve Fortran’s competitiveness against other array programming languages. The module also contains a function TOSTRING to convert numerical scalars and vectors to strings.

The zip file has the code, paper, and manual. The paper says

Using an F edit descriptor an aligned display similar to the one given by Matlab is obtained, but this comes at a cost, as matrices with large elements can no longer be displayed. Fortran also of-fers list format for writing with default format. To take an example, the statement “write(,) A” might display 20.085537 0.049787067 7.389056 0.13533528. However, aligned output of matrices with multiple rows is not possible using this, and in addition the result is compiler dependent.

The purpose of the current algorithm is to remedy this deficiency, by providing a module written in standard Fortran, that will, with minimal fuss, display a vector or a matrix with sensible default format. At the same time the module offers considerable flexibility in controlling the format of the output (much more than Matlab’s disp). It is possible to specify the edit descriptor, the displayed items may be labelled, rows and columns may be numbered, and the out-put may be directed to a file, to the screen, or even (when calling Fortran from Matlab) to the Matlab command window. Several matrices / vectors may be written side by side.

2 Likes

DISPMODULE wrapped for fpm(1) usage:

My favorite is to write the data as HTML; which obviously can be browsed but can also be used very nicely in CLI mode using lynx/links/w3m text browsers as filters or as viewers. The M_html module h_array and the example program table2html give the general idea.

Working interactively with the data, I inspect the data with the M_matrix module, which pretty-prints small arrays and lets you save and load arrays and do many matlab-like operations on the array; it only pretty-prints relatively small arrays; but since you can do array opts and extract array subsections it lets you look at the data in the most flexible way I know of from a command line interface.

Last but not least, write the file as a CSV file and import it into a spread-sheet. Note that several spread sheets will import HTML as well, which can let you create subtables and add more extensive labeling in the spreadsheet than CSV files.

I cannot make any plotting tools for tables available that I use, but I have been meaning to make M_matrix write tables as gnuplot files; not sure if you want to plot the tables as well; but usually a spread sheet lets you handle small tables. For engineering and science scale tables they usually break; but there are other tools for that.

I look at tables a lot. It might show :slight_smile:

Never have time to bring it up to where I want it, but if you are using an fpm/GNU-Linux/X11 environment or Unix environment, maybe also look at

3 Likes

I noticed a pull request in stdlib to meet this need. Not sure what the current status is - has a “waiting for OP” flag: [stdlib_io] add `disp`(display variable values formatted). by zoziha · Pull Request #520 · fortran-lang/stdlib · GitHub

@milancurcic , I was looking for color in fortran strings to guide the eye in a messy fortran output and I have found this, which seems to include some formatting for tabular data, as you were looking for.

http://foul.sourceforge.net/

2 Likes

Neat, thanks. I can confirm that with gfortran and ifort, on both Windows and WSL2, you get the output shown:

screenshot_foul

The code has the deleted PAUSE statement and real*8 declarations so is not standard-conforming. GitHub - urbanjost/M_attr: set terminal text attributes using ANSI escape sequences by @urbanjost appears to be a similar project.

I try to keep things simple, and I am using the bash strings for colors, but I found this could be useful

I was planning to prepare a project myself and to volunteer to contribute to other projects at some point… but I have to stop being a dinosaur and learn about gitlab/github first, which I do not use at its full potential.

In addition to foul and M_attr there is M_escape, which explores several methods and is the predecessor to M_attr, and at the bottom of the repository description of M_attr there are a lot of related links to documentation about ANSI escape sequences. M_escape shows simpler approaches, but I found the one method to be the most flexible and so made M_attr. It can hopefully be useful as a guide for setting up the project in github, making an fpm package; and how to combine the test subcommand of fpm with CD/CI scripts in a reusable way to automate testing. The direct approach discussed in M_escape is also used in some routines in (proposed?) routines in stdlib; if any of that is useful. After using several approaches, I personally settled on M_attr; but needs and tastes vary. Even if you go a different way hopefully some of that info will save you some time.

As regards color, does anyone think it should be part of the Fortran formatting control? If, for example you could use the letter C for color, say like

      Cforeground.background.attribute

does anyone think that would be accepted? It might be simpler to use a letter for each field, especially if you wanted to skip one; but I think it would be nice to enter something like

101 format (C1,‘ERROR:’,C,‘divide by zero’)

And C1 would mean start color one, and C would be clear attributes; and

202 format(C1.7.U,‘ERROR:’)

could mean print “ERROR” in red with a black background, underlined; or something like that.
Since formats can be character variables now one of the modes I did not add to the M_escape mode was going to be that one, where you could create a FORMAT and run it through a function that would expand that syntax to be the escape sequences to demonstrate the idea and make it useable now; but never got around to it.

1 Like

I have always thought that color should be part of the fortran format attributes. Your idea
would work fine.

I like the idea. I think you might get a fair bit of push back from compilers about how they then have to add a bunch of stuff to their runtime library in terms of detecting execution environment and figuring out to actually colorize in various environments. Do other languages have options like this built in?

I know at least five compilers support ISATTY(3f), which is the most commonly used routine in auto-selecting ANSI color mode. Not aware of one that does not; like most of the POSIX-like procedure (except sadly in a non-standard syntax).