| 1 |
#!/usr/bin/luatex |
| 2 |
|
| 3 |
--[[ |
| 4 |
|
| 5 |
prepcontour_cli [Lua variant] - prepare contour lines (for pgfplots) |
| 6 |
|
| 7 |
Version: 1.4 (2021-02-22) |
| 8 |
|
| 9 |
Copyright (C) 2020-2021 Francesco Poli <[email protected]> |
| 10 |
|
| 11 |
This program is free software; you can redistribute it and/or modify |
| 12 |
it under the terms of the GNU General Public License as published by |
| 13 |
the Free Software Foundation; either version 2 of the License, or |
| 14 |
(at your option) any later version. |
| 15 |
|
| 16 |
This program is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License along |
| 22 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 23 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 |
|
| 25 |
--]] |
| 26 |
|
| 27 |
-- directory separator ('\' for Windows, '/' for all other OSes) |
| 28 |
dsep = package.config:sub(1, 1) |
| 29 |
-- directory where the script lives |
| 30 |
mydir = arg[0]:match("(.*" .. dsep .. ")") |
| 31 |
-- add mydir to the search path for Lua loaders |
| 32 |
package.path = package.path .. ";" .. mydir .. "?.lua" |
| 33 |
-- load the module |
| 34 |
require "prepcontour" |
| 35 |
|
| 36 |
-- main program |
| 37 |
usage = "Usage: " .. arg[0] .. [[ |
| 38 |
CL NB NL {x|y} [VAL]... < IN > OUT |
| 39 |
|
| 40 |
where: |
| 41 |
CL number of text lines to be copied from the input beginning to the output |
| 42 |
NB number of text blocks to be read from the input |
| 43 |
NL number of text lines included in each input block |
| 44 |
x|y ordering ('x' if x varies, 'y' if y varies) |
| 45 |
VAL level for contour lines or |
| 46 |
min:n:max for n levels between min and max or |
| 47 |
corners=true to enable enhanced corner algorithm or |
| 48 |
corners=false to disable it (this is the default initial state) |
| 49 |
IN input file |
| 50 |
OUT output file |
| 51 |
]] |
| 52 |
|
| 53 |
-- simplistic command-line argument parsing |
| 54 |
-- TODO: use a better approach (argparse? some other library?) |
| 55 |
if arg[4] == nil then |
| 56 |
io.stderr:write(arg[0] .. ": too few arguments\n" .. usage) |
| 57 |
os.exit(1) |
| 58 |
end |
| 59 |
copylines = tonumber(arg[1]) |
| 60 |
nblocks = tonumber(arg[2]) |
| 61 |
nlines = tonumber(arg[3]) |
| 62 |
if copylines == nil or nblocks == nil or nlines == nil then |
| 63 |
io.stderr:write(arg[0] .. ": failed to parse arguments\n" .. usage) |
| 64 |
os.exit(2) |
| 65 |
end |
| 66 |
yvaries = arg[4]:sub(1, 1) |
| 67 |
if yvaries ~= 'x' and yvaries ~= 'y' then |
| 68 |
io.stderr:write(arg[0] .. ": failed to parse argument '" |
| 69 |
.. arg[4] .. "'!\n" .. usage) |
| 70 |
os.exit(3) |
| 71 |
end |
| 72 |
yvaries = (yvaries == 'y') |
| 73 |
|
| 74 |
mesh = PrepcMesh.new(yvaries, nblocks, nlines, copylines) |
| 75 |
corners = false |
| 76 |
for n = 5, #arg do |
| 77 |
if arg[n] == 'corners=true' then |
| 78 |
corners = true |
| 79 |
elseif arg[n] == 'corners=false' then |
| 80 |
corners = false |
| 81 |
else |
| 82 |
level = tonumber(arg[n]) |
| 83 |
if level then |
| 84 |
mesh:contour(level, corners) |
| 85 |
else |
| 86 |
lmin, nlevels, lmax = arg[n]:match('([^%:]+)%:([^%:]+)%:([^%:]+)') |
| 87 |
nlevels = math.tointeger(tonumber(nlevels)) |
| 88 |
lmin = tonumber(lmin) |
| 89 |
lmax = tonumber(lmax) |
| 90 |
if nlevels and lmin and lmax then |
| 91 |
mesh:autocontour(nlevels, lmin, lmax, corners) |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
mesh:printcontours() |