kookma / ogpf

ogpf is Object based interface to GnuPlot from Fortran 2003, 2008 and later
Other
166 stars 46 forks source link

Set output terminal from the Fortran API #41

Open beddalumia opened 1 year ago

beddalumia commented 1 year ago

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:

https://github.com/kookma/ogpf/blob/7b414be0d1bdeccc11bd80ed3033a0862468e630/src/ogpf.f90#L163-L165

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.

14NGiestas commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

14NGiestas commented 1 year ago

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 commented 1 year ago

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 ```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 commented 1 year ago

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

https://github.com/kookma/ogpf/blob/7b414be0d1bdeccc11bd80ed3033a0862468e630/src/ogpf.f90#L207-L209

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

https://github.com/kookma/ogpf/blob/7b414be0d1bdeccc11bd80ed3033a0862468e630/src/ogpf.f90#L2201-L2204

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 commented 1 year ago

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:

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. 🙃

14NGiestas commented 1 year ago

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 gnuplotterm 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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.