-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
74 lines (51 loc) · 1.47 KB
/
parse.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
import sys
if len(sys.argv) < 2:
print("Usage: python3 parse.py <dataset_filename>")
exit(1)
filename = f"ContactLocator{sys.argv[1]}.txt"
output = f"dataset{sys.argv[1]}.csv"
content = ""
with open(filename) as f:
content = f.read()
lines = [x.strip() for x in content.split("\n")]
header = lines[2]
# get all header, remove empty string
headers = [x for x in header.split(" ") if x != ""]
# if a string starts with '(', belongs to last header
for i in range(len(headers)):
if headers[i].startswith("("):
headers[i - 1] += " " + headers[i]
headers[i] = ""
headers = [x for x in headers if x != ""]
headers = [
"Pass Number",
"Observer",
"Time (UTCGregorian)",
"Azimuth (deg)",
"Elevation (deg)",
"Range (km)",
]
print("HEADERS:", headers)
lines = lines[4:]
dataset = []
for line in lines:
line = [x.strip() for x in line.split(" ")]
if len(line) < 6:
continue
line = [x for x in line if x != ""]
date = line[2] + " " + line[3] + " " + line[4] + " " + line[5]
antena = line[1]
passn = line[0]
azim = line[6]
elevation = line[7]
range = line[8]
# get a dict with header_name: value
data = dict(zip(headers, [passn, antena, date, azim, elevation, range]))
dataset.append(data)
import csv
# save dataset to csv
with open(output, "w") as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(dataset)
print(*dataset[0:10], sep="\n")