plot.pl 1.17 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
:- module(
  plot,
  [
    plot/0,
    export_plot/1
  ]
).


plot :-
  biocham_command,
  doc('plots the current trace.'),
  export_plot(plot),
  process_create(path(gnuplot), ['-persist', 'plot.plot'], []).


export_plot(FileTemplate) :-
  biocham_command,
  doc('
    saves the numerical trace of the last simulation into two files:
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
  export_trace(CsvFile),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
26 27
  get_trace_data([HeaderRow | _ ]),
  HeaderRow =.. [row, _TimeHeader | Headers],
Thierry Martinez's avatar
Thierry Martinez committed
28 29
  setup_call_cleanup(
    open(PlotFile, write, Stream),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
30
    export_plot_stream(Stream, CsvFile, Headers),
Thierry Martinez's avatar
Thierry Martinez committed
31 32 33
    close(Stream)
  ).

Thierry Martinez's avatar
Plot  
Thierry Martinez committed
34 35
export_plot_stream(Stream, CsvFile, Headers) :-
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
36 37
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
plot '),
  \+ (
    nth1(Index, Headers, Header),
    \+ (
      (
        Index > 1
      ->
        write(Stream, ', ')
      ;
        true
      ),
      ColumnIndex is Index + 1,
      format(
        Stream,
        '"~a" using 1:~d title "~a"',
        [CsvFile, ColumnIndex, Header]
      )
    )
  ),
  write(Stream, '\n').