forked from mariondubuisson/csv_parsing
/
main.py
86 lines (64 loc) · 2.68 KB
/
main.py
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
import csv
from models.pitot_measure import PitotMeasureModel
from tkinter import filedialog
from lib.csv_parser import CSVParser
from os import path
from functools import reduce
#########################################################################
## PROGRAM PARAMETERS ##
#########################################################################
# Correction coeff. of the reference delta P measure
A_REF = 1
# correction coeff. of the gradient delta P measure
A_Z = 0.987644543
# Head coeff. of the Pitot reference
K_REF = 1.029
# Head coeff. of the Pitot reference
K_Z = 1
# air density in kg/m³
RHO = 1.196
#########################################################################
## END PROGRAM PARAMETERS ##
#########################################################################
def flatten(listOfLists):
return reduce(list.__add__, listOfLists)
def transformation_pipeline(csv_file_stream, model):
# Parse CSV content to get an array of data
model_parser = CSVParser(
output_model=model.parsing_output_model, separator="\t")
parsed_model_measures = model_parser.parse(
csv_file_stream)
# Sanitize
sanitized_model_measures = model.sanitize(parsed_model_measures)
# Compute
model_computed_measures = model.compute(sanitized_model_measures)
return model_computed_measures
def get_altitude_from_filename(filename):
return int(path.basename(filename).replace('h', '').replace('_u17', '').replace('.csv', ''))
def main(args=None):
# Open a file selection dialog, restrict to CSV extensions
csv_file_streams = filedialog.askopenfiles(filetypes=[('CSV files', '.csv'), ('All files', '*')])
pipeline_outputs = flatten([
transformation_pipeline(
csv_file_stream,
PitotMeasureModel(
z=get_altitude_from_filename(csv_file_stream.name),
a_ref=A_REF,
a_z=A_Z,
rho=RHO,
k_ref=K_REF,
k_z=K_Z
)
)
for csv_file_stream in csv_file_streams])
# Exploit result, create csv result file
output_file = filedialog.asksaveasfilename(
filetypes=[('CSV files', '.csv'), ('All files', '*')],
defaultextension='.csv')
with open(output_file, "w", newline="") as result_file:
result_writer = csv.writer(result_file)
result_writer.writerow(list(pipeline_outputs[0]._fields))
for row in pipeline_outputs:
result_writer.writerow(list(row))
if __name__ == "__main__":
main()