-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
332 lines (304 loc) · 8.92 KB
/
main.go
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
// glas
// Copyright 2018 — 2020 softlandia@gmail.com
// Обработка las файлов. Построение словаря и замена мнемоник на справочные
// TODO убрать поясняющую строку в разделе ~A
// TODO ввести настройку для количества знаков после запятой при записи данных
// TODO может быть сделать возможность задавать правила на которые выполняется проверка
// например: FIELD == "Весеннее"
// соответственно при команде INFO в журнал выводятся случаи нарушения данного правила
package main
import (
"fmt"
"log"
"os"
"github.com/fatih/color"
"github.com/softlandia/glasio"
"github.com/softlandia/xlib"
"gopkg.in/ini.v1"
)
const (
fileNameMnemonic = "mnemonic.ini"
globalConfigName = "glas.ini"
configFileName = "las.ini"
)
var (
//Cfg - global programm config
Cfg *Config
//Mnemonic - map of std mnemonic
Mnemonic map[string]string
//Dic - mnemonic substitution dictionary
Dic map[string]string
)
// comandLineParameters - check input parameters
// >glas w "d:\input" "e:\output"
func comandLineParameters() bool {
//back door for TEST
if (len(os.Args) == 2) && (os.Args[1] == "-") {
color.Set(color.FgYellow, color.Bold)
fmt.Printf("WARNING! using glas.ini command\n")
color.Unset()
return true
}
if len(os.Args) < 3 { //minimum: glas w d:\input
fmt.Printf("using: >glas c 'd:\\input' 'e:\\output'\n")
fmt.Printf("c - command: i, x\n")
fmt.Printf("'d:\\input' - path to exist folder\n")
fmt.Printf("'d:\\output' - path to exist folder\n")
return false
}
if len(os.Args[1]) > 1 { //command only one symbol
fmt.Print("using: >glas ")
color.Set(color.FgYellow, color.Bold)
fmt.Print("C")
color.Unset()
fmt.Printf(" 'd:\\input' 'e:\\output'\n")
fmt.Printf("C - command must be 'i' or 'x'\n")
fmt.Printf("You entered the wrong command: '%s'\n", os.Args[1])
return false
}
switch os.Args[1] {
case "i":
Cfg.Comand = "info"
case "x":
Cfg.Comand = "repair"
default:
Cfg.Comand = "test"
}
if !xlib.FileExists(os.Args[2]) {
fmt.Print("using: >glas w 'd:\\input' 'e:\\output'\n")
fmt.Print("input folder: ")
color.Set(color.FgYellow, color.Bold)
fmt.Printf("'%s'", os.Args[2])
color.Unset()
fmt.Print(" not exist\n")
return false
}
//path to input folder ok
Cfg.Path = os.Args[2]
if len(os.Args) == 4 { //full version: glas x c:\idata c:\odata
if !xlib.FileExists(os.Args[3]) {
fmt.Print("using: >glas w 'd:\\input' ")
fmt.Printf("'e:\\output'\n")
fmt.Printf("output folder: ")
color.Set(color.FgYellow, color.Bold)
fmt.Printf("'%s'", os.Args[3])
color.Unset()
fmt.Printf(" not exist\n")
return false
}
//path to otput folder ok
Cfg.pathToRepaire = os.Args[3]
}
return true
}
//============================================================================
func main() {
log.Println("start ", os.Args[0])
//configuration & dictionaries are filled in here
//initialize() stop programm if error occure
//initialize() read glas.ini file and configure global var Cfg
initialize()
//comand line parameters rather then ini file
//and redefine if exist
if !comandLineParameters() {
os.Exit(1)
}
color.Set(color.FgCyan)
fmt.Printf("init:\tok.\n")
//fmt.Printf("precision:\t%v\n", Cfg.Epsilon)
fmt.Printf("debug level:\t%v\n", Cfg.LogLevel)
fmt.Printf("dictionary:\t%v\n", Cfg.DicFile)
fmt.Printf("input path:\t%v\n", Cfg.Path)
fmt.Printf("output path:\t%v\n", Cfg.pathToRepaire)
fmt.Printf("std NULL:\t%v\n", Cfg.Null)
fmt.Printf("replace NULL:\t%v\n", Cfg.NullReplace)
fmt.Printf("verify date:\t%v\n", Cfg.verifyDate)
fmt.Printf("report files:\t'%s'\n", Cfg.lasInfoReport)
fmt.Printf("message report:\t%s\n", Cfg.lasMessageReport)
fmt.Printf("missing log:\t%s\n", Cfg.logMissingReport)
fmt.Printf("warning report:\t%s\n", Cfg.lasCheckReport)
color.Set(color.FgYellow, color.Bold)
fmt.Printf("command:\t%v\n", Cfg.Comand)
color.Unset()
fileList := make([]string, 0, 10)
//makeFilesList() stop programm if error occure
n := makeFilesList(&fileList, Cfg.Path)
switch Cfg.Comand {
case "test":
TEST(n)
case "convert":
log.Println("convert code page: ")
convertCodePage(&fileList)
case "verify":
log.Println("verify las:")
case "repair":
log.Println("repaire las:")
repairLas(&fileList, &Dic, Cfg.Path, Cfg.pathToRepaire, Cfg.lasMessageReport, Cfg.lasCheckReport)
case "info":
log.Println("collect log info:") //fileMsgOpen, fileMsgCheck, fileReport, fileLogMissing
statisticLas(&fileList, &Dic, Cfg)
}
}
///////////////////////////////////////////
func verifyLas(fl *[]string) error {
log.Printf("action 'verify' not define")
return nil
}
///////////////////////////////////////////
func convertCodePage(fl *[]string) error {
log.Printf("action 'convert' not define")
return nil
}
////////////////////////////////////////////////////////////
//load std mnemonic
func readGlobalMnemonic(iniFileName string) (map[string]string, error) {
iniMnemonic, err := ini.Load(iniFileName)
if err != nil {
log.Printf("error on load std mnemonic, check out file 'mnemonic.ini'\n")
return nil, err
}
sec, err := iniMnemonic.GetSection("mnemonic")
if err != nil {
log.Printf("error on read 'mnemonic.ini'")
return nil, err
}
x := make(map[string]string)
for _, s := range sec.KeyStrings() {
x[s] = sec.Key(s).Value()
}
if Cfg.LogLevel == "DEBUG" {
log.Println("__mnemonics:")
for k, v := range x {
fmt.Printf("mnemonic: %s, desc: %s\n", k, v)
}
}
return x, nil
}
////////////////////////////////////////////////////////////
//init programm, read config, ini files and init dictionary
//stop programm if not successful
func initialize() {
var err error
//read global config from yaml file
Cfg, err = readGlobalConfig(configFileName)
if err != nil {
log.Printf("Fail read '%s' config file. %v", configFileName, err)
os.Exit(1)
}
Mnemonic, err = readGlobalMnemonic(fileNameMnemonic)
if err != nil {
log.Println(err)
os.Exit(1)
}
//read dectionary from ini file
iniDic, err := ini.Load(Cfg.DicFile)
if err != nil {
log.Printf("Fail to read file: %v\n", err)
os.Exit(2)
}
sec, err := iniDic.GetSection("LOG")
if err != nil {
log.Println("Fail load section 'LOG' from file 'dic.ini'. ", err)
os.Exit(3)
}
//fill dictionary
Dic = make(map[string]string)
for _, s := range sec.KeyStrings() {
Dic[s] = sec.Key(s).Value()
}
//словарь заполнен
if Cfg.LogLevel == "DEBUG" {
log.Println("__dic:")
for k, v := range Dic {
fmt.Println("key: ", k, " val: ", v)
}
}
}
// makeFilesList - find and load to array all founded las files
//TODO makeFilesList() there is no need to exit the program when an error occurs
func makeFilesList(fileList *[]string, path string) int {
n, err := xlib.FindFilesExt(fileList, Cfg.Path, ".las")
if err != nil {
log.Println("error at search files. verify path: ", Cfg.Path, err)
log.Println("stop")
os.Exit(4)
}
if n == 0 {
log.Println("files 'las' not found. verify parameter path: '", Cfg.Path, "' and change in 'main.yaml'")
log.Println("stop")
os.Exit(5)
}
if Cfg.LogLevel == "DEBUG" {
log.Println("founded ", n, " las files:")
if Cfg.LogLevel == "DEBUG" {
for i, s := range *fileList {
log.Println(i, " : ", s)
}
}
}
return n
}
// TEST - test read and write las files
//TODO report las.warning.md written without filename
func TEST(m int) {
//test file "1.las"
las := glasio.NewLas()
//las.maxWarningCount = 25
n, err := las.Open("1.las")
if n == 7 {
fmt.Println("TEST read 1.las OK")
fmt.Println(err)
} else {
fmt.Printf("TEST read 1.las ERROR, n = %d, must 7\n", n)
fmt.Println(err)
}
las.SaveWarning("1.warning.md")
/*err = las.setNull(Cfg.Null)
fmt.Println("set new null value done, error: ", err)*/
err = las.Save("-1.las")
if err != nil {
fmt.Println("TEST save -1.las ERROR: ", err)
} else {
fmt.Println("TEST save -1.las OK")
}
las = nil
las = glasio.NewLas()
n, err = las.Open("-1.las")
if (n == 7) && (las.Null == -999.25) {
fmt.Println("TEST read -1.las OK")
fmt.Println(err)
} else {
fmt.Println("TEST read -1.las ERROR")
fmt.Println("NULL not -999.25 or count dept points != 7")
fmt.Println(err)
}
las = nil
las = glasio.NewLas()
n, err = las.Open("2.las")
if n == 4895 {
fmt.Println("TEST read 2.las OK")
fmt.Println(err)
} else {
fmt.Println("TEST read 2.las ERROR")
fmt.Println(err)
}
err = las.Save("-2.las")
if err != nil {
fmt.Println("TEST save -2.las ERROR")
fmt.Println(err)
} else {
fmt.Println("TEST save -2.las OK")
}
las = nil
las = glasio.NewLas()
n, err = las.Open("4.las")
if err != nil {
fmt.Printf("error: %v\n", err)
}
if n == 23 {
fmt.Printf("TEST read 4.las OK, count data must 23, actualy: %d\n", n)
} else {
fmt.Printf("TEST read 4.las ERROR, count data must 23, actualy: %d\n", n)
}
las = nil
}