* Can MetaPost draw directly datapoints from a data file?
@ 2017-12-28 17:36 Jorge Manuel
2017-12-28 18:47 ` Hans Hagen
0 siblings, 1 reply; 3+ messages in thread
From: Jorge Manuel @ 2017-12-28 17:36 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 1207 bytes --]
When I need to draw datapoints from a external data file I’m using lua, like this:
\starttext
\startluacode
Elemento = {}
x = {}
y ={}
z= {}
carga = {}
i=1
context("\\startMPcode”)
context("numeric u; u := 1.5cm;")
for line in io.lines("molecule.xyz") do
local a, b, c, d, e = string.match(line, '(%a+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)')
Elemento[i]= a
x[i]= tonumber(b)
y[i]= tonumber(c)
z[i]= tonumber(d)
carga[i]= tonumber(e)
context("label(\"%s\", (%0.6f u, %0.6f u));", a, x[i],y[i])
i=i+1
end
context("\\stopMPcode”)
\stopluacode
\stoptext
Is there a way to pass directly the external data to MetaPost without using Lua?
I know that I can use a similar function to gdata from graph.mp. I try to reuse this piece of code, but not works.
\startMPcode
vardef Grdln_@#(expr f) =
save n_, s_; string s_;
s_ = readfrom f;
string @#[];
if s_<>EOF:
@#1 := loptok s_;
n_ = if @#1="%": 0 else: 1 fi;
forever:
@#[incr n_] := loptok s_;
exitif @#[n_]=" ";
endfor
@#1<>" "
else: false
fi
enddef;
Grdln_$("molecule.xyz");
\stopMPcode
Jorge
[-- Attachment #1.2.1: Type: text/html, Size: 3225 bytes --]
[-- Attachment #1.2.2: molecule.xyz --]
[-- Type: application/octet-stream, Size: 3666 bytes --]
N -0.215180 -3.611590 1.013730 -0.980426
N -0.907220 -3.663400 -1.269030 -1.159281
H -1.020120 -4.516150 -1.239180 0.451727
H 0.592410 -3.533220 3.408420 0.466050
H 1.138340 -4.068520 -6.662270 0.121348
Zn 1.048810 0.361560 1.461840 1.085568
C 0.826190 0.806270 4.182580 0.885122
C 0.417080 1.494320 5.455600 -0.158259
C -0.185960 2.733600 5.457730 -0.038223
H -0.320120 3.185220 4.658260 0.111225
N 3.029810 0.215180 1.013730 -0.989702
N 2.978000 0.907220 -1.269030 -1.323792
H 2.125250 1.020120 -1.239180 0.552920
H 3.108180 -0.592410 3.408420 0.599787
O 0.611010 1.411960 3.076910 -0.805465
O 1.325620 -0.316130 4.242000 -0.786727
C -0.826190 -0.806270 -9.141970 0.932072
C -0.417080 -1.494310 -7.868950 -0.116686
C 0.185960 -2.733600 -7.866810 -0.092391
H 0.320120 -3.185210 -8.666290 0.116237
O -0.611010 -1.411960 -10.247640 -0.908737
O -1.325620 0.316130 -9.082550 -0.838830
C -0.487480 -3.005900 -0.183350 1.029873
C 0.217840 -2.574210 1.791620 1.084450
N -0.286910 -1.704180 -0.139370 -0.556339
N 0.174010 -1.421260 1.164300 -0.669532
H -1.062620 -3.235690 -1.998680 0.479136
N 0.588430 -2.745550 3.064650 -1.219932
H 0.822210 -2.066800 3.539000 0.535669
Zn -1.048810 -0.361560 -1.461840 1.115312
C -0.826190 -0.806270 -4.182580 0.930235
C -0.417080 -1.494310 -5.455600 -0.165126
C 0.185960 -2.733600 -5.457740 -0.068129
H 0.320120 -3.185210 -4.658260 0.125910
C -3.635500 -0.487480 0.183350 1.112801
C -4.067190 0.217840 -1.791620 1.089920
N -4.937220 -0.286910 0.139380 -0.669012
N -5.220140 0.174010 -1.164300 -0.683650
N -3.029810 -0.215180 -1.013730 -1.010564
N -2.978000 -0.907210 1.269030 -1.379275
H -3.405710 -1.062620 1.998680 0.465316
H -2.125250 -1.020120 1.239180 0.591458
N -3.895840 0.588430 -3.064650 -1.359821
H -4.574600 0.822210 -3.539000 0.462374
H -3.108170 0.592410 -3.408420 0.595021
O -0.611010 -1.411960 -3.076900 -0.846291
O -1.325620 0.316130 -4.242000 -0.789904
C 0.487480 3.005900 0.183350 1.054807
C -0.217840 2.574210 -1.791620 1.079690
N 0.286910 1.704180 0.139380 -0.596520
N -0.174000 1.421260 -1.164300 -0.651619
N 0.215180 3.611590 -1.013730 -0.982105
N 0.907220 3.663400 1.269030 -1.153898
H 1.062620 3.235690 1.998680 0.472248
H 1.020120 4.516150 1.239180 0.448303
N -0.588430 2.745550 -3.064650 -1.192789
H -0.822210 2.066800 -3.539000 0.511494
H -0.592410 3.533220 -3.408420 0.458606
C 0.599050 -3.311400 -6.662270 -0.253677
C -0.738520 -0.893930 -6.662270 -0.060650
H -1.176860 -0.074380 -6.662270 0.135692
C 0.826190 0.806270 9.141970 0.921571
C 0.417080 1.494320 7.868950 -0.136263
C -0.185960 2.733600 7.866820 -0.033469
H -0.320120 3.185220 8.666290 0.092701
O 0.611010 1.411960 10.247650 -0.900753
O 1.325620 -0.316130 9.082550 -0.836472
C -0.599050 3.311400 6.662270 -0.299314
H -1.138340 4.068520 6.662270 0.134260
C 0.738520 0.893930 6.662270 -0.048643
H 1.176860 0.074380 6.662270 0.130420
C 3.635500 0.487480 -0.183350 1.071205
C 4.067190 -0.217840 1.791620 1.085316
N 4.937220 0.286910 -0.139370 -0.660949
N 5.220140 -0.174000 1.164300 -0.672967
H 3.405710 1.062620 -1.998680 0.459743
N 3.895850 -0.588430 3.064650 -1.356568
H 4.574600 -0.822210 3.539000 0.457166
[-- Attachment #1.2.3: Type: text/html, Size: 318 bytes --]
[-- Attachment #1.2.4: read_external.tex --]
[-- Type: application/octet-stream, Size: 1006 bytes --]
\starttext
\startluacode
Elemento = {}
x = {}
y ={}
z= {}
carga = {}
i=1
context("\\startMPcode")
context("numeric u; u := 1.5cm;")
for line in io.lines("molecule.xyz") do
local a, b, c, d, e = string.match(line, '(%a+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)')
Elemento[i]= a
x[i]= tonumber(b)
y[i]= tonumber(c)
z[i]= tonumber(d)
carga[i]= tonumber(e)
context("label(\"%s\", (%0.6f u, %0.6f u));", a, x[i],y[i])
--context("\\par %s \\par %s \\par %s \\par %0.6f \\par %s", a, b, c, z[i], e)
i=i+1
end
context("\\stopMPcode")
\stopluacode
\startMPcode
vardef Grdln_@#(expr f) =
save n_, s_; string s_;
s_ = readfrom f;
string @#[];
if s_<>EOF:
@#1 := loptok s_;
n_ = if @#1="%": 0 else: 1 fi;
forever:
@#[incr n_] := loptok s_;
exitif @#[n_]=" ";
endfor
@#1<>" "
else: false
fi
enddef;
Grdln_$("molecule.xyz");
% for i=1 upto infinity:
%
% exitunless Grdln_$("molecule.xyz");
%
% label("A", (@#2,@#3));
%
% endfor
\stopMPcode
\stoptext
[-- Attachment #1.2.5: Type: text/html, Size: 436 bytes --]
[-- Attachment #2: Type: text/plain, Size: 492 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Can MetaPost draw directly datapoints from a data file?
2017-12-28 17:36 Can MetaPost draw directly datapoints from a data file? Jorge Manuel
@ 2017-12-28 18:47 ` Hans Hagen
2017-12-28 22:53 ` Jorge Manuel
0 siblings, 1 reply; 3+ messages in thread
From: Hans Hagen @ 2017-12-28 18:47 UTC (permalink / raw)
To: ntg-context
On 12/28/2017 6:36 PM, Jorge Manuel wrote:
> When I need to draw datapoints from a external data file I’m using lua,
> like this:
>
> \starttext
>
> \startluacode
>
> Elemento = {}
> x = {}
> y ={}
> z= {}
> carga = {}
> i=1
> context("\\startMPcode”)
>
> context("numeric u; u := 1.5cm;")
> for line in io.lines("molecule.xyz <http://molecule.xyz>") do
> local a, b, c, d, e = string.match(line,
> '(%a+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)%s*%s*(%S+)')
> Elemento[i]= a
> x[i]= tonumber(b)
> y[i]= tonumber(c)
> z[i]= tonumber(d)
> carga[i]= tonumber(e)
>
> context("label(\"%s\", (%0.6f u, %0.6f u));", a, x[i],y[i])
>
> i=i+1
> end
>
> context("\\stopMPcode”)
>
> \stopluacode
>
>
> \stoptext
>
> *Is there a way to pass directly the external data to MetaPost without
> using Lua?*
>
> I know that I can use a similar function to gdata from graph.mp. I try
> to reuse this piece of code, but not works.
>
> \startMPcode
>
> vardef Grdln_@#(expr f) =
> save n_, s_; string s_;
> s_ = readfrom f;
> string @#[];
> if s_<>EOF:
> @#1 := loptok s_;
> n_ = if @#1="%": 0 else: 1 fi;
> forever:
> @#[incr n_] := loptok s_;
> exitif @#[n_]=" ";
> endfor
> @#1<>" "
> else: false
> fi
> enddef;
>
> Grdln_$("molecule.xyz <http://molecule.xyz>");
>
> \stopMPcode
mp file handling is rather crippled so this is where lua is better
\starttext
\startluacode
MP.mycode = { }
local elements = { }
function MP.mycode.loadelements(filename)
elements = { }
for line in io.lines(filename) do
elements[#elements+1] = { string.match(line,
'(%S+)%s*(%S+)%s*(%S+)%s*(%S+)%s*(%S+)') }
end
end
function MP.mycode.nofelements()
mp.print(#elements)
end
function MP.mycode.getlabel(i)
mp.quoted(elements[i][1])
end
function MP.mycode.getoffset(i)
mp.pair(elements[i][2],elements[i][3])
end
\stopluacode
\startMPcode
lua.MP.mycode.loadelements("molecule.xyz") ;
for i=1 upto lua.MP.mycode.nofelements() :
label (lua.MP.mycode.getlabel(i),50 * lua.MP.mycode.getoffset(i)) ;
endfor ;
\stopMPcode
\startMPcode
def my_loadelements(expr name) =
lua.MP.mycode.loadelements(name) ;
enddef ;
vardef my_nofelements =
lua.MP.mycode.nofelements()
enddef ;
vardef my_label(expr i) =
lua.MP.mycode.getlabel(i)
enddef ;
vardef my_offset(expr i) =
lua.MP.mycode.getoffset(i)
enddef ;
my_loadelements("molecule.xyz") ;
for i=1 upto my_nofelements :
label (my_label(i),50 * my_offset(i)) ;
endfor ;
\stopMPcode
\stoptext
If you use that kind of code a lot you can hide the lua calls in macros
which makes it look like metapost does it all.
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Can MetaPost draw directly datapoints from a data file?
2017-12-28 18:47 ` Hans Hagen
@ 2017-12-28 22:53 ` Jorge Manuel
0 siblings, 0 replies; 3+ messages in thread
From: Jorge Manuel @ 2017-12-28 22:53 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 1916 bytes --]
Thanks a lot for this nice and clean solution. It’s fantastic. Now it is very easy to make scatter plots from the output of c++ libraries and other kind of applications: two axes and a path build like this
path curve;
numeric i; i:=1;
curve := my_offset(i) forever: hide(i := i + 1) exitif i > my_nofelements .. my_offset(i) endfor;
Thanks again
Have a great 2018
Jorge
> No dia 28/12/2017, às 18:47, Hans Hagen <pragma@wxs.nl> escreveu:
>
> \starttext
>
> \startluacode
> MP.mycode = { }
>
> local elements = { }
>
> function MP.mycode.loadelements(filename)
> elements = { }
> for line in io.lines(filename) do
> elements[#elements+1] = { string.match(line, '(%S+)%s*(%S+)%s*(%S+)%s*(%S+)%s*(%S+)') }
> end
> end
>
> function MP.mycode.nofelements()
> mp.print(#elements)
> end
>
> function MP.mycode.getlabel(i)
> mp.quoted(elements[i][1])
> end
>
> function MP.mycode.getoffset(i)
> mp.pair(elements[i][2],elements[i][3])
> end
> \stopluacode
>
> \startMPcode
> lua.MP.mycode.loadelements("molecule.xyz <http://molecule.xyz/>") ;
> for i=1 upto lua.MP.mycode.nofelements() :
> label (lua.MP.mycode.getlabel(i),50 * lua.MP.mycode.getoffset(i)) ;
> endfor ;
> \stopMPcode
>
> \startMPcode
> def my_loadelements(expr name) =
> lua.MP.mycode.loadelements(name) ;
> enddef ;
> vardef my_nofelements =
> lua.MP.mycode.nofelements()
> enddef ;
> vardef my_label(expr i) =
> lua.MP.mycode.getlabel(i)
> enddef ;
> vardef my_offset(expr i) =
> lua.MP.mycode.getoffset(i)
> enddef ;
>
> my_loadelements("molecule.xyz <http://molecule.xyz/>") ;
> for i=1 upto my_nofelements :
> label (my_label(i),50 * my_offset(i)) ;
> endfor ;
> \stopMPcode
>
> \stoptext
[-- Attachment #1.2: Type: text/html, Size: 40970 bytes --]
[-- Attachment #2: Type: text/plain, Size: 492 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-12-28 22:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-28 17:36 Can MetaPost draw directly datapoints from a data file? Jorge Manuel
2017-12-28 18:47 ` Hans Hagen
2017-12-28 22:53 ` Jorge Manuel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).