-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·193 lines (178 loc) · 7.44 KB
/
test.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
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
#!/usr/bin/env python3
import re
from datetime import datetime, timedelta
from os import listdir
from almanac import parse, gha_dec, init, sha_dec, dm, magnitude, v_value, d_value, hp_moon, equation_of_time, \
semi_diameter, meridian_passage, moon_age_phase, hm
def count_hours(filename):
hours = 0
with open(filename) as f:
for line in f:
if "|" in line:
cols = [c.strip() for c in line.strip().split("|")]
cols = list(filter(len, cols))
if isinstance(parse(cols[0]), int):
hours += 1
return hours
def read_page(filename):
hours = count_hours(filename)
data = []
with open(filename) as f:
for line in f:
if "dUT1" in line:
mode = None
date = line.split("dUT1")[0].strip()
t0 = datetime.strptime(date, "%Y %B %d (%a)")
t1 = t0 + timedelta(days=int(hours / 24 / 2))
t2 = t0 + timedelta(hours=hours / 2)
elif "|" in line:
cols = [c.strip() for c in line.strip().split("|")]
cols = list(filter(len, cols))
# print(cols)
if cols[0] == "UT":
mode = "GHA"
hour = 0
names = []
for c in cols:
name_mag = c.split()
name = name_mag[0]
names.append(name)
if len(name_mag) == 2:
data.append([t2, name, "mag", parse(name_mag[1])])
elif cols[0] == "Star":
mode = "SHA"
elif mode == "GHA" and isinstance(parse(cols[0]), int):
for i, c in enumerate(cols):
name = names[i]
# print(name, c)
if name == "UT":
th = t0 + timedelta(hours=hour)
assert th.hour == parse(c) % 24, (th, c)
hour += 1
elif name == "Aries":
data.append([th, name, "GHA", parse(c)])
elif name == "Moon":
c = c.split()
if len(c) == 7:
data.append([th, name, "GHA", parse(f"{c[0]} {c[1]}")])
data.append([th, name, "v", parse(c[2])])
data.append([th, name, "Dec", parse(f"{c[3]} {c[4]}")])
data.append([th, name, "d", parse(c[5])])
data.append([th, name, "HP", parse(c[6])])
elif len(c) == 4:
data.append([th, name, "GHA", parse(f"{c[0]} {c[1]}")])
data.append([th, name, "Dec", parse(f"{c[2]} {c[3]}")])
else:
assert 0, c
else:
c = c.split()
assert len(c) == 4, c
data.append([th, name, "GHA", parse(f"{c[0]} {c[1]}")])
data.append([th, name, "Dec", parse(f"{c[2]} {c[3]}")])
elif mode == "SHA" and len(cols) == 2:
name = cols[0]
c = cols[1].split()
data.append([t2, name, "SHA", parse(f"{c[0]} {c[1]}")])
data.append([t2, name, "Dec", parse(f"{c[2]} {c[3]}")])
elif mode == "GHA":
for i, c in enumerate(cols):
if c.startswith("SD") or c.startswith("v"):
v = c.split()
for k in range(0, len(v), 2):
data.append([t2, names[i + 1 + (1 if i > 1 else 0)], v[k], float(v[k + 1])])
elif c.startswith("EoT"):
v = c.split()
data.append([t1, names[i + 1], v[0], parse(v[1]) / 60])
data.append([t1 + timedelta(hours=12), names[i + 1], v[0], parse(v[2]) / 60])
elif c.startswith("Age"):
v = c.split()
data.append([t2, names[i + 1], v[0], int(v[1][:-1])])
data.append([t2, names[i + 1], v[2], int(v[3][:-1])])
elif c.startswith("Upper"):
v = c.split()
data.append([t1, names[i + 1], v[0], parse(v[1])])
data.append([t1, names[i + 1], v[2], parse(v[3])])
elif c.startswith("MP"):
v = c.split()
data.append([t1, names[i + 1], v[0], parse(v[1])])
elif ":" in c:
data.append([t1, names[i + 1], "MP", parse(c)])
elif i > 2:
v = parse(c)
if isinstance(v, float):
data.append([t1, names[i + 1], "SHA", v])
return data
def compare(filename):
aa = "AA" in filename # AirAlmanac
na = "NA" in filename or "EZ" in filename # NauticalAlmanac
data = read_page(filename)
init()
diff = {}
for r in data:
t, b, n, v = r
if n == "GHA":
w = gha_dec(t, b, na)[0]
elif n == "Dec":
w = gha_dec(t, b)[1]
elif n == "SHA":
w = sha_dec(t, b)[0]
elif n == "mag":
w = magnitude(t, b)
elif n == "v":
w = v_value(t, b)
elif n == "d":
w = d_value(t, b)
elif n == "HP":
assert b == "Moon", b
w = hp_moon(t)
elif n == "EoT":
assert b == "Sun", b
w = equation_of_time(t)
elif n == "SD":
w = semi_diameter(t, b)
elif n in ["MP", "Upper", "Lower"]:
w = meridian_passage(t, b, upper=n != "Lower")
elif n == "Age":
assert b == "Moon", b
w = moon_age_phase(t)[0]
elif n == "Phase":
assert b == "Moon", b
w = moon_age_phase(t)[1] * 100
else:
print(r)
continue
if n in ["GHA", "SHA", "Dec"]:
w = parse(dm(w))
elif n in ["EoT"]:
w = parse(hm(w)) / 60
elif n in ["MP", "Upper", "Lower"]:
w = parse(hm(w))
elif n in ["Age", "Phase"]:
w = round(w)
else:
w = round(w, 1)
d = abs(w - v)
if n in ["GHA", "SHA", "Dec", "MP", "Upper", "Lower"]:
d *= 60
mm = diff.setdefault(b, {}).setdefault(n, [0, 0, 0])
if not (b == "Moon" and aa):
assert d <= (0.25 if n in ["GHA", "Dec"] else 1), (filename, r, w, d)
mm[0] = max(mm[0], d)
mm[1] += 1 if d > 0 else 0
mm[2] += 1
diff2 = []
for b, v in diff.items():
for k, v in v.items():
diff2.append([b, k] + v)
print(filename)
r = "body", "value", "maxdev", "dev", "tot"
# print(f"{r[0]:15} {r[1]:6} {r[2]:6} {r[3]:>6}/{r[4]}")
for r in diff2:
if r[2]:
print(f"{r[0]:15} {r[1]:6} {r[2]:6.4f} {r[3]:6}/{r[4]}")
def main():
for d in listdir():
if re.match("daily-pages-\\d{4}-.*.txt", d):
compare(d)
if __name__ == "__main__":
main()