-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.lua
511 lines (369 loc) · 11.6 KB
/
compile.lua
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
--[[
* compile.lua
*
* Recurse a directory for *.json files formatted as per the WMO's site.
*
* Output a file containing only the forecast values.
* All cities are collected, there's no way to specify compiling of 1 city only.
*
* Options:
* '--purge' will delete all invalid or duplicated json files.
* '--import' will reuse an existing dat file and skip json files in dir tree.
]]
local wx = require("wx")
local json = require("lib.json")
local serpent = require("lib.serpent")
local trace = require("lib.trace")
local _format = string.format
local _insert = table.insert
-- ----------------------------------------------------------------------------
--
local m_trace = trace.new("compile")
-- ----------------------------------------------------------------------------
--
local m_App =
{
sAppName = "compile",
sAppVer = "0.0.4",
sRelDate = "2020/11/18",
sDefPath = "D:\\USR_2\\LUA\\WMOQuery\\data\\update", -- default path
sRootDir = nil, -- root directory
bPurge = false, -- remove duplicated files
bImport = false, -- import dat files
iTotScan = 0, -- total files processed
iFailed = 0, -- counter for any error
}
-- ----------------------------------------------------------------------------
--
local m_Samples =
{
-- { id, city_name, { { issue_date, { {date, min, max}, {date, min, max}, ... } },
-- { issue_date, { {date, min, max}, {date, min, max}, ... } },
-- ...
-- },
-- },
-- { id, ...
}
-- ----------------------------------------------------------------------------
-- get the row associated with a city ID
-- parameter inCityName is not essential but descreptive later
--
local function FindCityIdTable(inCityId, inCityName, inCreateNew)
-- m_trace:line("FindCityIdTable")
for _, tCity in ipairs(m_Samples) do
if inCityId == tCity[1] then return tCity end
end
if inCreateNew then
local tCityTbl = { inCityId, inCityName, { } } -- create table for city
_insert(m_Samples, tCityTbl) -- add new row to master table
return tCityTbl
end
return nil
end
-- ----------------------------------------------------------------------------
-- get the row associated with a issue_date
--
local function FindIssueDate(inCityTbl, inIssueDate, inCreateNew)
-- m_trace:line("FindIssueDate")
for _, tIssueDate in ipairs(inCityTbl[3]) do
if inIssueDate == tIssueDate[1] then return tIssueDate end
end
if inCreateNew then
local tDateTbl = { inIssueDate, { } } -- create table for issue_date
_insert(inCityTbl[3], tDateTbl) -- add new row to inCityTbl table
return tDateTbl
end
return nil
end
-- ----------------------------------------------------------------------------
-- update data collector
--
local function Collect(inCityId, inCityName, inTimedAt, inForecast)
-- m_trace:line("Collect")
m_trace:line("CityId [" .. inCityId .. "] TimedAt [" .. inTimedAt .. "]")
if not inForecast or 0 == #inForecast then
m_trace:line("---> Collect: no forecast dataset found, ignoring contents")
return false
end
-- get the table for the city
-- create if not existing
--
local tCityTbl = FindCityIdTable(inCityId, inCityName, true)
-- get the date-time of forecast
-- ask to fail if non-existent
--
local tIssueDate = FindIssueDate(tCityTbl, inTimedAt, false)
-- this is not possible
--
if tIssueDate then
m_trace:line("---> Collect: duplicate found, ignoring contents")
return false
end
-- now it is safe to create the entry for selected date
--
tIssueDate = FindIssueDate(tCityTbl, inTimedAt, true)
-- add expected values of forecast
-- { date, min, max }
--
local tRow
local sDate
local sMinT
local sMaxT
local iWarning = 0
for _, tExpect in ipairs(inForecast) do
tRow = { }
sDate = tExpect.forecastDate
if sDate and 0 < #sDate then
sMinT = tExpect.minTemp
sMaxT = tExpect.maxTemp
-- correct errors with impossible values
--
if not sMinT or 0 == #sMinT then
sMinT = "100"
iWarning = iWarning + 1
end
if not sMaxT or 0 == #sMaxT then
sMaxT = "-100"
iWarning = iWarning + 1
end
-- values for row
--
_insert(tRow, sDate)
_insert(tRow, tonumber(sMinT))
_insert(tRow, tonumber(sMaxT))
-- add to table
--
_insert(tIssueDate[2], tRow)
else
m_trace:line("---> Invalid forecast day found, ignoring contents")
end
end
if 0 < iWarning then
m_trace:line("---> Warnings raised because of invalid readings, total: " .. iWarning)
end
return true
end
-- ----------------------------------------------------------------------------
-- get data out of the json file
--
local function ProcessFile(inFilename)
m_trace:newline("ProcessFile [" .. inFilename .. "]")
local hFile = io.open(inFilename, "r")
local sBuffer = ""
local tJSonObjs
local tJSonCity
-- read file
--
if hFile then
sBuffer = hFile:read("*a")
hFile:close()
end
-- sanity check
--
if 0 == #sBuffer then
m_trace:line("---> ProcessFile: file read failed!")
return false
end
-- use the decoder to get a Lua table
--
tJSonObjs = json.decode(sBuffer)
-- sanity check
--
if not tJSonObjs then
m_trace:line("---> ProcessFile: json.decode failed!")
return false
end
-- test for the 'city' element
--
tJSonCity = tJSonObjs.city
if not tJSonCity then
m_trace:line("---> ProcessFile: format unknown!")
return false
end
m_trace:line("Parsing forecast data for city: " .. tJSonCity.cityName .. " [" .. tJSonCity.forecast.issueDate .. "]")
-- grab data
--
return Collect(tJSonCity.cityId, tJSonCity.cityName, tJSonCity.forecast.issueDate, tJSonCity.forecast.forecastDay)
end
-- ----------------------------------------------------------------------------
-- import data from the dat file
--
local function ProcessImport(inFilename)
m_trace:newline("ProcessImport [" .. inFilename .. "]")
-- import table with multiple stations
--
local tSamples = dofile(inFilename)
if not tSamples then return false end
for _, tCity in ipairs(tSamples) do
m_trace:line("Importing forecast data for city: " .. tCity[1] .. " [" .. tCity[2] .. "]")
-- get the table for the city
-- create if not existing
--
local tCityTbl = FindCityIdTable(tCity[1], tCity[2], true)
local iTotals = 0
for _, tEntry in ipairs(tCity[3]) do
m_App.iTotScan = m_App.iTotScan + 1
-- get the date-time of forecast
-- ask to fail if non-existent
--
local tIssueDate = FindIssueDate(tCityTbl, tEntry[1], false)
-- this is not possible
--
if tIssueDate then
m_trace:line("---> Collect: duplicate found, ignoring contents")
else
-- now it is safe to create the entry for selected date
--
tIssueDate = FindIssueDate(tCityTbl, tEntry[1], true)
-- simply pour rows from a table to the other
--
for _, tRow in ipairs(tEntry[2]) do
_insert(tIssueDate[2], tRow)
end
iTotals = iTotals + #tEntry[2]
end
end
m_trace:line("Imported for city: " .. tCity[2] .. " " .. iTotals .. " rows")
end
return true
end
-- ----------------------------------------------------------------------------
-- recurse directories and inspect pertinent files
--
local function ProcessDirectory(inPathname)
m_trace:newline("* ProcessDirectory [" .. inPathname .. "]")
local dir = wx.wxDir()
if not dir:Open(inPathname) then
m_trace:line("---> Cannot open directory [" .. inPathname .. "]")
return
end
local bSkipJson = false
-- scan for matching dat files
-- will not process the very first root directory
--
if m_App.bImport and m_App.sRootDir ~= inPathname then
local _, sFilename = dir:GetFirst("*.dat", wx.wxDIR_FILES)
local sFullpath
if sFilename and 0 < #sFilename then
sFullpath = inPathname .. "\\" .. sFilename
-- if fails to import data from existing file
-- then fallback to process files
--
bSkipJson = ProcessImport(sFullpath)
end
end
-- scan for matching json files
--
if not bSkipJson then
local _, sFilename = dir:GetFirst("*.json", wx.wxDIR_FILES)
local sFullpath
while sFilename and 0 < #sFilename do
m_App.iTotScan = m_App.iTotScan + 1
sFullpath = inPathname .. "\\" .. sFilename
if not ProcessFile(sFullpath) then
m_App.iFailed = m_App.iFailed + 1
-- delete the file
--
if m_App.bPurge then
os.remove(sFullpath)
end
end
_, sFilename = dir:GetNext()
end
-- scan further down
--
local _, sDirectory = dir:GetFirst("*", wx.wxDIR_DIRS | wx.wxDIR_NO_FOLLOW)
while sDirectory and 0 < #sDirectory do
ProcessDirectory(inPathname .. "\\" .. sDirectory)
_, sDirectory = dir:GetNext()
end
end
dir:Close()
end
-- ----------------------------------------------------------------------------
--
local function SaveCompiledTable(inRootDir)
-- m_trace:line("SaveCompiledTable")
local sObjFile = inRootDir .. "\\WMO Samples.dat"
local hFile = io.open(sObjFile, "w")
local sBuffer = serpent.dump(m_Samples)
if hFile then
hFile:write(sBuffer)
hFile:close()
else
m_trace:line("---> Failed to save compiled datasets in [" .. sObjFile .. "]")
return false
end
--[[ this is for internal testing
m_Samples = dofile(sObjFile)
trace.table(m_Samples)
--]]
m_trace:newline("Output saved in [" .. sObjFile .. "]")
return true
end
-- ----------------------------------------------------------------------------
--
local function RunApplication(...)
-- m_trace:line("RunApplication")
local sAppTitle = m_App.sAppName .. " [" .. m_App.sAppVer .. "]"
m_trace:time(sAppTitle .. " started")
assert(os.setlocale('us', 'all'))
m_trace:line("Current locale is [" .. os.setlocale() .. "]")
-- get the arguments on the command line
--
local tArgs = { }
for _, v in ipairs{...} do
if "--purge" == v then
m_App.bPurge = true
elseif "--import" == v then
m_App.bImport = true
else
tArgs[#tArgs + 1] = v
end
end
-- get the root directory
--
local sRootDir = tArgs[1] or m_App.sDefPath
if not sRootDir or 0 == #sRootDir then
m_trace:line("---> No root directory was specified, aborting")
return
end
-- try opening at least the root
--
if not wx.wxDir().Exists(sRootDir) then
m_trace:line("---> Cannot open directory [" .. sRootDir .. "], aborting")
return
end
-- now store the root directory
--
m_App.sRootDir = sRootDir
-- work it
--
m_trace:startwatch()
ProcessDirectory(sRootDir) -- here start the inspection
if 0 < #m_Samples then
SaveCompiledTable(sRootDir) -- save table to file
end
m_trace:stopwatch("Compile took")
-- give feedback
--
local iTotal = m_App.iTotScan
local iFailed = m_App.iFailed
local iSuccess = iTotal - iFailed
m_trace:summary("Touched files [" .. iTotal .. "] Failed: [" .. iFailed .. "]")
-- report to caller
--
io.stdout:write(_format("%d/%d\n", iSuccess, iTotal))
end
-- ----------------------------------------------------------------------------
-- redirect logging
--
m_trace:open()
-- run
--
RunApplication(...)
-- end
--
m_trace:close()
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------