Debian Codegraph @

texk/texlive/linked_scripts/diadia/diadia.lua

raw symbols SCIP
1 #!/usr/bin/env texlua
2 --
3 -- diadia [options]
4 --
5 -- loads and processes a diadia data file
6 --
7 -- License: LPPL
8 --
9 local version = "v1.0 (2015/05/15)"
10
11 local infile = ""
12 local outfile = ""
13 local mode = "*"
14 local startdate = ""
15 local enddate = ""
16 local columns = ""
17 local data = {}
18 function pversion()
19 print("diadia.lua " .. version)
20 print("(C) Josef Kleber 2015 License: LPPL")
21 os.exit(0)
22 end
23 function phelp()
24 print([[
25 diadia.lua [options]
26
27 allows you to
28
29 - cut a chunk out of the data file
30 e.g.: -i in.dat -o out.dat -s YYYY-MM-DD -e YYYY-MM-DD
31
32 - compose a new data file based on given columns of an
33 existing data file
34 e.g.: -i in.dat -o out.dat -c 1,2
35
36 - create a new data file with date and value (1st and
37 2nd column of existing file) and added value average
38 columns of the last 7, 14, 30, 60 and 90 days
39 e.g.: -i in.dat -o out.dat [-s YYYY-MM-DD -e YYYY-MM-DD]
40
41 Options:
42
43 -m specify the mode (cut|compose|average)
44
45 -i specify the input file
46
47 -o specify the output file
48
49 -c specify the columns for compose mode
50
51 -s specify the start date (YYYY-MM-DD) in
52 cut and average mode
53
54 -e specify the end date
55
56 -v prints version information
57
58 -h prints help information
59
60 ]])
61 pversion()
62 end
63 function check_date(date)
64 if string.find(date, "(%d%d%d%d)-(%d%d)-(%d%d)") == nil
65 then
66 io.stderr:write ("Error 21: wrong date format (YYYY-MM-DD)\n")
67 os.exit(11)
68 end
69 end
70 function parse_date(date)
71 return string.match(date, "(%d%d%d%d)%-(%d%d)%-(%d%d)")
72 end
73 function parse_dateinline(line)
74 return string.match(line, "(%d%d%d%d%-%d%d%-%d%d)")
75 end
76 function daystring(unixtime)
77 return os.date("%Y-%m-%d", unixtime)
78 end
79 function unixtime(year,month,day)
80 return os.time{year=year, month=month, day=day}
81 end
82 function round(number)
83 return math.floor(number+0.5)
84 end
85 function ptd(value)
86 local val = tostring(value)
87 local slen = string.len(val)
88 if slen == 3
89 then
90 return val
91 else
92 return val .. " "
93 end
94 end
95 function calc_avg(data,date,days)
96 local sum = 0
97 local wdays = 0
98 local wday
99 local endday = unixtime(parse_date(date))
100 local startday = endday - 60*60*24*(days-1)
101 while startday <= endday
102 do
103 wday = daystring(startday)
104 if data[wday] ~= nil
105 then
106 sum = sum + data[wday]
107 wdays = wdays + 1
108 end
109 startday = startday + 60*60*24
110 end
111 if wdays == 0
112 then
113 return "nan"
114 else
115 return tostring(round(sum/wdays))
116 end
117 end
118 function read_data(file)
119 local data = {}
120 local date
121 local startdate
122 local enddate
123 local dat
124 local firstline = true
125 for line in io.lines(file)
126 do
127 if string.match(line, "date")
128 then
129 else
130 date, dat = string.match(line, "(%d%d%d%d%-%d%d%-%d%d)%s+(%S+)")
131 if firstline == true
132 then
133 startdate = date
134 firstline = false
135 end
136 if dat ~= "nan" and dat ~= "{}" and dat ~= ""
137 then
138 data[date] = dat
139 end
140 end
141 end
142 enddate = date
143 return data,startdate,enddate
144 end
145 function write_avg_file(data,file,startdate,enddate)
146 local sdate
147 local edate
148 local wday
149 sdate = unixtime(parse_date(startdate))
150 edate = unixtime(parse_date(enddate))
151 outfile = assert(io.open(file, "w"))
152 outfile:write("date value avg07 avg14 avg30 avg60 avg90")
153 while sdate <= edate+7200
154 do
155 wday = daystring(sdate)
156 if data[wday] ~= nil
157 then
158 outfile:write("\n" .. wday .. " "
159 .. ptd(data[wday]) .. " "
160 .. ptd(calc_avg(data,wday,7)) .. " "
161 .. ptd(calc_avg(data,wday,14)) .. " "
162 .. ptd(calc_avg(data,wday,30)) .. " "
163 .. ptd(calc_avg(data,wday,60)) .. " "
164 .. ptd(calc_avg(data,wday,90)))
165 end
166 sdate = sdate + 60*60*24
167 end
168 outfile:close()
169 end
170 do
171 local newarg = {}
172 local i, limit = 1, #arg
173 while (i <= limit) do
174 if arg[i] == "-i" then
175 infile = arg[i+1]
176 i = i + 1
177 elseif arg[i] == "-o" then
178 outfile = arg[i+1]
179 i = i + 1
180 elseif arg[i] == "-s" then
181 startdate = arg[i+1]
182 i = i + 1
183 elseif arg[i] == "-e" then
184 enddate = arg[i+1]
185 i = i + 1
186 elseif arg[i] == "-c" then
187 columns = arg[i+1]
188 i = i + 1
189 elseif arg[i] == "-m" then
190 mode = arg[i+1]
191 i = i + 1
192 elseif arg[i] == "-v" then
193 pversion()
194 elseif arg[i] == "-h" then
195 phelp()
196 else
197 newarg[#newarg+1] = arg[i]
198 end
199 i = i + 1
200 end
201 arg = newarg
202 end
203 if mode == "average"
204 then
205 local startd
206 local endd
207
208 print("set mode to " .. mode)
209 print("reading data file " .. infile)
210 data,startd,endd = read_data(infile)
211 if startdate ~= ""
212 then
213 startd = startdate
214 end
215 if enddate ~= ""
216 then
217 endd = enddate
218 end
219 print("writing data file " .. outfile)
220 write_avg_file(data,outfile,startd,endd)
221 os.exit(0)
222 end
223 if mode == "compose"
224 then
225 local row = 0
226 local column = 0
227 local ofile
228 local cols
229
230 print("set mode to " .. mode)
231 print("reading data file " .. infile)
232 for line in io.lines(infile)
233 do
234 row = row + 1
235 data[row] = {}
236 column = 0
237 for value in string.gmatch(line, "%S+")
238 do
239 column = column + 1
240 data[row][column] = value
241 end
242 end
243 cols = assert(load("return table.concat({"..columns:gsub("%d+","(...)[%0]").."},' ')"))
244 ofile = assert(io.open(outfile, "w"))
245 print("writing data file " .. outfile)
246 for irow = 1,row
247 do
248 if irow == row
249 then
250 ofile:write(cols(data[irow]))
251 else
252 ofile:write(cols(data[irow]).."\n")
253 end
254 end
255 ofile:close()
256 os.exit(0)
257 end
258 if mode == "cut"
259 then
260 local ofile
261 local date
262 local sdate
263 local edate
264 local cdate
265
266 check_date(startdate)
267 check_date(enddate)
268 sdate = unixtime(parse_date(startdate))
269 edate = unixtime(parse_date(enddate))
270 print("set mode to " .. mode)
271 print("reading data file " .. infile)
272 print("writing data file " .. outfile)
273 ofile = assert(io.open(outfile, "w"))
274 for line in io.lines(infile)
275 do
276 if string.match(line, "date")
277 then
278 ofile:write(line)
279 else
280 date = parse_dateinline(line)
281 cdate = unixtime(parse_date(date))
282 if cdate >= sdate and cdate <= edate
283 then
284 ofile:write("\n" .. line)
285 end
286 end
287 end
288 ofile:close()
289 os.exit(0)
290 end
291 if mode == "*"
292 then
293 io.stderr:write ("Error 11: no mode specified!")
294 os.exit(11)
295 else
296 io.stderr:write ("Error 12: invalid mode " .. mode)
297 os.exit(12)
298 end