0
|
1 #!/usr/bin/env luajit
|
|
2
|
|
3 function string:split(sep, n)
|
|
4 local a, start = {}, 1;
|
|
5 sep = sep or "%s+";
|
|
6 repeat
|
|
7 local b, e = self:find(sep, start);
|
|
8 if b == nil then
|
|
9 table.insert(a, self:sub(start));
|
|
10 break
|
|
11 end
|
|
12 a[#a+1] = self:sub(start, b - 1);
|
|
13 start = e + 1;
|
|
14 if n and #a == n then
|
|
15 table.insert(a, self:sub(start));
|
|
16 break
|
|
17 end
|
|
18 until start > #self;
|
|
19 return a;
|
|
20 end
|
|
21
|
|
22 function io.xopen(fn, mode)
|
|
23 mode = mode or 'r';
|
|
24 if fn == nil then return io.stdin;
|
|
25 elseif fn == '-' then return (mode == 'r' and io.stdin) or io.stdout;
|
|
26 elseif fn:sub(-3) == '.gz' then return (mode == 'r' and io.popen('gzip -dc ' .. fn, 'r')) or io.popen('gzip > ' .. fn, 'w');
|
|
27 elseif fn:sub(-4) == '.bz2' then return (mode == 'r' and io.popen('bzip2 -dc ' .. fn, 'r')) or io.popen('bgzip2 > ' .. fn, 'w');
|
|
28 else return io.open(fn, mode) end
|
|
29 end
|
|
30
|
|
31 local eps = {};
|
|
32
|
|
33 function eps.func(fp)
|
|
34 fp = fp or io.stdout
|
|
35 fp:write("/C { dup 255 and 255 div exch dup -8 bitshift 255 and 255 div 3 1 roll -16 bitshift 255 and 255 div 3 1 roll setrgbcolor } bind def\n")
|
|
36 fp:write("/L { 4 2 roll moveto lineto } bind def\n")
|
|
37 fp:write("/LX { dup 4 -1 roll exch moveto lineto } bind def\n")
|
|
38 fp:write("/LY { dup 4 -1 roll moveto exch lineto } bind def\n")
|
|
39 fp:write("/LS { 3 1 roll moveto show } bind def\n")
|
|
40 fp:write("/RS { dup stringwidth pop 4 -1 roll exch sub 3 -1 roll moveto show } bind def\n")
|
|
41 fp:write("/B { 4 copy 3 1 roll exch 6 2 roll 8 -2 roll moveto lineto lineto lineto closepath } bind def\n")
|
|
42 end
|
|
43
|
|
44 function eps.font(ft, size, fp)
|
|
45 fp = fp or io.stdout
|
|
46 fp:write(string.format('/FS %d def\n', size));
|
|
47 fp:write('/FS4 FS 4 div def\n');
|
|
48 fp:write('/' .. ft .. ' findfont FS scalefont setfont\n');
|
|
49 end
|
|
50
|
|
51 local scale = 8;
|
|
52
|
|
53 if #arg == 0 then
|
|
54 print("Usage: r2plot.lua <in.txt>");
|
|
55 os.exit(1)
|
|
56 end
|
|
57
|
|
58 local fp = io.xopen(arg[1]);
|
|
59 local n = tonumber(fp:read());
|
|
60
|
|
61 print('%!PS-Adobe-3.0 EPSF-3.0');
|
|
62 print('%%' .. string.format('BoundingBox: -%d -%d %.3f %.3f\n', 10*scale, scale, (n+1)*scale, (n+1)*scale));
|
|
63 print(string.format('%.3f setlinewidth', scale));
|
|
64 print(string.format('/plot { setgray moveto 0 %d rlineto } def', scale));
|
|
65 print(string.format('/plothalf { setgray moveto 0 %.2f rlineto } def', scale/2));
|
|
66 eps.func();
|
|
67 eps.font('Helvetica', scale-1);
|
|
68
|
|
69 local i = 1;
|
|
70 for l in fp:lines() do
|
|
71 local t = l:split('\t');
|
|
72 print(string.format("%d %d FS4 add (%s) RS", (i-1)*scale-2, (i-1)*scale, t[1]));
|
|
73 for j = 2, #t do
|
|
74 if tonumber(t[j]) > 0.01 then
|
|
75 print(string.format('%.2f %.2f %.2f plot stroke', (i-1+.5)*scale, (j-2)*scale, 1.-t[j]));
|
|
76 end
|
|
77 end
|
|
78 i = i + 1;
|
|
79 end
|
|
80 for j = 1, 21 do
|
|
81 print(string.format('%.2f %.2f %.2f plothalf stroke', -8*scale, (j-1) * scale/2, 1.-(j-1)/20));
|
|
82 end
|
|
83 print('showpage');
|