Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set output terminal from the Fortran API #41

Open
beddalumia opened this issue Sep 15, 2022 · 12 comments
Open

Set output terminal from the Fortran API #41

beddalumia opened this issue Sep 15, 2022 · 12 comments

Comments

@beddalumia
Copy link

Do I understand correctly that the terminal option cannot be set from the Fortran client? Shouldn't ogpf allow the user to do that with the options command?

My understanding is that currently you need to directly modify the source at line:

ogpf/src/ogpf.f90

Lines 163 to 165 in 7b414be

! The terminal and font have been set for Windows operating system
! Correct to meet the requirements on other OS like Linux and Mac.
character(len=*), parameter :: gnuplot_term_type = 'wxt' ! Output terminal

This, in my opinion, makes unfeasible any usage through fpm. If your OS does not allow the hard-coded wxt parameter you have the only option to fork the source file and manually include it in your project, which totally defeats the purpose of a fpm.toml file.

beddalumia added a commit to QcmPlab/HoneyTools that referenced this issue Sep 16, 2022
Featuring the OGPF fpm library to interface with gnuplot.

There are some inconveniences:

• to my understanding the library hard-codes the output-terminal option
  in gnuplot, to a non-portable value, making it impossible to use with
  fpm... I verified that everything works as expected by a manual tweak
  of the OGPF source in fpm build directory
  > this is unacceptable
  > the alternative is to embed the source code in honeytools, which I
    would happily avoid
  > I submitted an issue on their github: kookma/ogpf#41

• there is no way to update the plot dynamically (a là matlab's hold on)
  > this may be too much to ask...

We'd probably want to iterate giving a try to https://github.com/jacobwilliams/pyplot-fortran
@14NGiestas
Copy link
Contributor

14NGiestas commented Sep 16, 2022

Have you tried using the options feature? I have this project where I just:

call gp % options("set term dumb;")

And it changes the terminal to ASCII as expected.

BTW: honeycombs you say :) can't wait to test it

@beddalumia
Copy link
Author

Uhm, well, apparently not for me:

[...]
case ("gnuplot")
         call gnu%options("set term qt;")
         call gnu%xlabel('x')
         call gnu%ylabel('y')
         call gnu%filename("hex_temp_script.gp")
         call gnu%plot(x,y,'with points pt 6 ps 1.2 lc rgb "#000000"')
[...]

gives

Plotting neighborhood of hex a...

set term wxt size 640,480 enhanced font "verdana,10" title "ogpf libray: Rev. 0.22 of March 9th, 2018"
         ^
"hex_temp_script.gp" line 6: unknown or ambiguous terminal type; type just 'set terminal' for a list

(But editing manually the source code with the same option (qt) does the trick.)

I've also tried moving around the options command (despite I think order should not matter, as far as it comes before plot) and nothing changed.

So here we go with the metadata:

#fpm.toml
[...]
[dependencies]
ogpf.git    = "https://github.com/kookma/ogpf.git"
ogpf.branch = "master" # So I should be on master head, right?
[...]

Did maybe this use to work?

PS: I hope to roll the first minimal viable product within the weekend, keen on feedback :)

@14NGiestas
Copy link
Contributor

The order is important. Be sure that this is the first thing you do.
Can you attatch the generated file?

@14NGiestas
Copy link
Contributor

14NGiestas commented Sep 16, 2022

Never mind, I bet the issue is that in platforms where wxt is not available (unknown) the script will halt.
Can you please check if the command

gnuplot -e "set terminal" | grep wxt

returns anything?

PS: I have no such issue because my platform supports wxWidgets, as :

$ gnuplot -e "set terminal" | grep wxt
              wxt  wxWidgets cross-platform interactive terminal
$ cat ogpf_temp_script.gp | grep "set term"
set term wxt size 640,480 enhanced font "verdana,10" title "ogpf libray: Rev. 0.22 of March 9th, 2018"
set term dumb

@beddalumia
Copy link
Author

Oh I see, the options command does indeed insert the desired terminal specification, but it positions it after the default line, so that gnuplot parses wxt anyway. Of course if wxt is supported by the system inserting the custom line after the default leads to the correct behavior but when not recognized makes gnuplot halt.

So... Is there's a way to make options override the first set term line? It should be something like removing that line from subroutine create_outputfile and calling internally options if you detect the user has not (but I imagine parsing all options calls would be just dumb. So maybe a new set_terminal public method, allowing direct access to (a non parameter version of) gnuplot_term_type?

Anyway the generated script has the same structure as you predicted:

hex_temp_script.gp
# ogpf libray
# Rev. 0.22 of March 9th, 2018
# Licence: MIT

# gnuplot global setting
set term wxt size 640,480 enhanced font "verdana,10" title "ogpf libray: Rev. 0.22 of March 9th, 2018"

# ogpf extra configuration
# -------------------------------------------
# color definitions
set style line 1 lc rgb "#800000" lt 1 lw 2
set style line 2 lc rgb "#ff0000" lt 1 lw 2
set style line 3 lc rgb "#ff4500" lt 1 lw 2
set style line 4 lc rgb "#ffa500" lt 1 lw 2
set style line 5 lc rgb "#006400" lt 1 lw 2
set style line 6 lc rgb "#0000ff" lt 1 lw 2
set style line 7 lc rgb "#9400d3" lt 1 lw 2

# Axes
set border linewidth 1.15
set tics nomirror

# grid
# Add light grid to plot
set style line 102 lc rgb "#d6d7d9" lt 0 lw 1
set grid back ls 102

# plot style
set style data linespoints

# -------------------------------------------

 
# options
set term qt


 
# plot scale
 
# Annotation: title and labels
set xlabel "x" font ","
set ylabel "y" font ","
 
# axes setting

plot "-" notitle with points pt 6 ps 1.2 lc rgb "#000000"
   3.3301270189221928        3.5000000000000000     
   2.4641016151377544        3.0000000000000004     
   2.4641016151377539        2.0000000000000000     
   3.3301270189221928        1.5000000000000000     
   4.1961524227066311        1.9999999999999996     
   4.1961524227066320        2.9999999999999991     
   1.5980762113533160        3.5000000000000000     
  0.73205080756887753        3.0000000000000004     
  0.73205080756887719        2.0000000000000000     
   1.5980762113533158        1.5000000000000000     
   2.4641016151377544        1.9999999999999996     
   2.4641016151377553        2.9999999999999991     
  0.73205080756887730        5.0000000000000000     
 -0.13397459621556129        4.5000000000000000     
 -0.13397459621556163        3.5000000000000000     
  0.73205080756887697        3.0000000000000000     
   1.5980762113533156        3.4999999999999996     
   1.5980762113533162        4.4999999999999991     
   1.5980762113533160        6.5000000000000000     
  0.73205080756887753        6.0000000000000000     
  0.73205080756887719        5.0000000000000000     
   1.5980762113533158        4.5000000000000000     
   2.4641016151377544        5.0000000000000000     
   2.4641016151377553        5.9999999999999991     
   3.3301270189221928        6.5000000000000000     
   2.4641016151377544        6.0000000000000000     
   2.4641016151377539        5.0000000000000000     
   3.3301270189221928        4.5000000000000000     
   4.1961524227066311        5.0000000000000000     
   4.1961524227066320        5.9999999999999991     
   4.1961524227066320        5.0000000000000000     
   3.3301270189221936        4.5000000000000000     
   3.3301270189221932        3.5000000000000000     
   4.1961524227066320        3.0000000000000000     
   5.0621778264910704        3.4999999999999996     
   5.0621778264910713        4.4999999999999991     
e

And likewise the full list of available terminals does not contain wxt:

gnuplot -e "set terminal"
Available terminal types:
       cairolatex  LaTeX picture environment using graphicx package and Cairo backend
           canvas  HTML Canvas object
              cgm  Computer Graphics Metafile
          context  ConTeXt with MetaFun (for PDF documents)
          domterm  DomTerm terminal emulator with embedded SVG
             dumb  ascii art for anything that prints text
              dxf  dxf-file for AutoCad (default size 120x80)
              emf  Enhanced Metafile format
         epscairo  eps terminal based on cairo
         epslatex  LaTeX picture environment using graphicx package
              fig  FIG graphics language V3.2 for XFIG graphics editor
              gif  GIF images using libgd and TrueType fonts
             hpgl  HP7475 and relatives [number of pens] [eject]
             jpeg  JPEG images using libgd and TrueType fonts
              lua  Lua generic terminal driver
               mf  Metafont plotting standard
               mp  MetaPost plotting standard
             pcl5  PCL5e/PCL5c printers using HP-GL/2
         pdfcairo  pdf terminal based on cairo
           pict2e  LaTeX2e picture environment
              png  PNG images using libgd and TrueType fonts
Press return for more: 
         pngcairo  png terminal based on cairo
       postscript  PostScript graphics, including EPSF embedded files (*.eps)
          pslatex  LaTeX picture environment with PostScript \specials
            pstex  plain TeX with PostScript \specials
         pstricks  LaTeX picture environment with PSTricks macros
               qt  Qt cross-platform interactive terminal
          sixelgd  sixel using libgd and TrueType fonts
              svg  W3C Scalable Vector Graphics
          tek40xx  Tektronix 4010 and others; most TEK emulators
          tek410x  Tektronix 4106, 4107, 4109 and 420X terminals
          texdraw  LaTeX texdraw environment
             tikz  TeX TikZ graphics macros via the lua script driver
         tkcanvas  Tk canvas widget
          unknown  Unknown terminal type - not a plotting device
            vttek  VT-like tek40xx terminal emulator
            xterm  Xterm Tektronix 4014 Mode

@14NGiestas
Copy link
Contributor

I was about to say:

Indeed assuming a default term is a problem, a possible fix would just creating a method to handle it, let's say:

call gp % set_terminal("qt") ! I like this one the most
call gp % term("qt")
call gp % set_term("qt")
call gp % terminal("qt")

create a character variable here

ogpf/src/ogpf.f90

Lines 207 to 209 in 7b414be

character(len=:), allocatable :: txtoptions ! a long string to store all type of gnuplot options
character(len=:), allocatable :: txtscript ! a long string to store gnuplot script
character(len=:), allocatable :: txtdatastyle ! lines, points, linepoints

But this requires changing it here as well (where the writing is done)

ogpf/src/ogpf.f90

Lines 2201 to 2204 in 7b414be

write(unit=this%file_unit, fmt='(a)') 'set term ' // gnuplot_term_type // &
' size ' // gnuplot_term_size // ' enhanced font "' // &
gnuplot_term_font // '"' // &
' title "' // md_name // ': ' // md_rev //'"' ! library name and version

The only big problem left is figuring out all where to put all the other configs like font, size, title (I guess it's the window title) for each terminal, figuring this out is no easy task, but it may not be needed.

It can be accomplished by just checking if, let's say this % txtterminal, is allocated and then just outputting its contents in the writing section, or writting the default way with wxt otherwise.

This solves the issue... at least partially, can you provide a PR?

@beddalumia
Copy link
Author

beddalumia commented Sep 16, 2022

call gp % set_terminal("qt") ! I like this one the most

Yeah, I agree!

title (I guess it's the window title)

Exactly, is where the "credits" go... I think this is not intended to be customizable (but you tell me)

But, for the others, we might want to have all these new methods:

  • set_terminal
  • set_font
  • set_size
  • set_window_title (?)

which would be optional to call (if not called the script is generated with the current hardcoded values).

I believe this could be handled by just some extra character variables and the classic

if(extra_variable/=gnuplot_term_<stuff>)then
    gnuplot_term_<stuff> = extra_variable
endif

Of course then, the gnuplot_term_<stuff> variables need to give up the parameter attribute, but I don't see big issues with this.

This solves the issue... at least partially, can you provide a PR?

Yeah if you agree with the approach described here I could give it a try, it does seem quite approachable. 🙃

beddalumia added a commit to QcmPlab/HoneyTools that referenced this issue Sep 16, 2022
The "gnuplot" backend remains available, but "pyplot" is the new default.

The output from the two backends is tuned to look fairly similar.

There are options to pass a script filename and a picture filename, if
the script name is not passed a default one will be produced, as we want
to /always/ have a saved script, for reproducibility. If the figure name
is not passed there will be no saved picture, but just a GUI popup for
both backends (or something similar for peculiar cases).

The "gnuplot" backend aimsˆ† to support also a custom <terminal> such as
<qt>, <wxt>, <dumb> (especially useful on HPC servers), <png>, <svg>,…
which instead are necessary to allow correct I/O if figure_name is given.

†) Issue kookma/ogpf#41 needs to be solved...
   > for now I just redirect the fpm.toml pointer to a relevant "devel"
     branch on my personal fork ==> for now just changes the default
     terminal, so to allow proper testing here, but a new public API
     should be defined to allow the user choose.

—————————————————————————————————————————————————————————————————————————

Let me brag the <dumb> terminal "gnuplot" output :)

—————————————————————————————————————————————————————————————————————————

                 7 +-------------------------------------------+
                   |                                           |
                   |               F          F                |
                 6 |-+        F          F          F          |
                   |                                           |
                   |                                           |
                 5 |-+        F          F          F          |
                   |                                           |
                   |    F          F          F          F     |
                 4 |-+                                         |
              y    |                                           |
                   |    F          F          F          F     |
                   |                                           |
                 3 |-+        F          F          F          |
                   |                                           |
                   |                                           |
                 2 |-+        F          F          F          |
                   |               F          F                |
                   |     +      +     +     +     +      +     |
                 1 +-------------------------------------------+
                  -1     0      1     2     3     4      5     6
                                         x

—————————————————————————————————————————————————————————————————————————
@14NGiestas
Copy link
Contributor

Yeah if you agree with the approach described here

I mean, I agree this is an issue but the final word here is from @kookma who has the writing permissions. That's why I suggest you open a PR so I can help reviewing and digesting the changes.

Of course then, the gnuplot_term_ variables need to give up the parameter attribute

Actually, it's better include/move them in the type, public :: gpf as a member of it (since you could have multiple instances with different terminals and so on).

I suggest you focus just on the set_terminal method as you may find that not every terminal will support all parameters ("set term png" will not support a window title because there is no such thing).

@beddalumia
Copy link
Author

Actually, it's better include/move them in the type, public :: gpf as a member of it (since you could have multiple instances with different terminals and so on).

Yes, sure, you're right.

I suggest you focus just on the set_terminal method as you may find that not every terminal will support all parameters ("set term png" will not support a window title because there is no such thing).

Oh let me do some trials, cause I thought Gnuplot is able to deal with invalid parameter and just skip it (surely does with the window-title when I set the terminal to dumb)

@beddalumia
Copy link
Author

Yes, I can confirm that if you change the default terminal to qt (in the source) and then

call gnu%options("set term png;")

you encounter no errors and get a correct png file.

@beddalumia
Copy link
Author

I just pop up to apologize for the long wait, I've been quite busy with HoneyTools itself and the PhD work it has been developed for... As summer ends I would probably keep being busy but I intend to work on this at some point. I'll let you know when there is some progress :)

@huijunchen9260
Copy link

Not sure whether it will be relevant, but this is how I choose the "default terminal" for gnuplot.

Create a file called .gnuplot under the $HOME directory, and add

set terminal qt enhanced font "Alegreya, 14" size 1600, 960

and now for all of the 2D figure my gnuplot will use qt instead of wxt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants