-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathionogram_ips42.py
More file actions
227 lines (187 loc) · 7.1 KB
/
ionogram_ips42.py
File metadata and controls
227 lines (187 loc) · 7.1 KB
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
from math import log
from struct import unpack
from datetime import datetime, timedelta
from configparser import ConfigParser, NoSectionError, NoOptionError
from ionogram import Ionogram
class IonogramIps42(Ionogram):
def __init__(self, debug_level=0):
super().__init__(debug_level)
self.ionosonde_model = "IPS-42"
def load(self, file_name):
with open(file_name, "rb") as file:
head = file.read(64)
data = file.read(512 * 576 // 8)
data_tuple = unpack("H" * (len(data) // 2), data)
self.data = [[0 for x in range(576)] for y in range(512)]
for f in range(576):
for h in range(512 // 16):
i = f * 512 // 16 + h
for z in range(16):
alt = 511 - (h * 16 + 15 - z)
bit = (data_tuple[i] >> z) & 1
self.data[alt][f] = 0 if bit else 1
# self.data[0][0] = -1
self._extract_info()
if self.date:
self.load_sunspot()
def _digit_recognize(self, offset_f):
offset_alt = 36
digit = None
for da in range(-1, 2):
for df in range(-1, 2):
try:
digit = self._img2digit(offset_alt + da, offset_f + df)
except ValueError:
pass
else:
return digit
if self.debug_level == 0:
self._print_digit(offset_alt, offset_f)
raise ValueError("Digit is not recognized")
def _extract_info(self):
try:
y2 = self._digit_recognize(80)
y1 = self._digit_recognize(96)
year = y2 * 10 + y1
year += 1900 if year > 57 else 2000
d3 = self._digit_recognize(128)
d2 = self._digit_recognize(144)
d1 = self._digit_recognize(160)
doy = d3 * 100 + d2 * 10 + d1
h2 = self._digit_recognize(192)
h1 = self._digit_recognize(208)
hour = h2 * 10 + h1
m2 = self._digit_recognize(224)
m1 = self._digit_recognize(240)
minute = m2 * 10 + m1
self.date = datetime(year, 1, 1, hour, minute, 0)
self.date += timedelta(doy - 1)
except ValueError:
self.date = datetime.now()
print("Date is not recognized. Current date is used.")
try:
s4 = self._digit_recognize(0)
s3 = self._digit_recognize(16)
s2 = self._digit_recognize(32)
s1 = self._digit_recognize(48)
station = s4 * 1000 + s3 * 100 + s2 * 10 + s1
is_station = True
except ValueError:
is_station = False
print("Station is not recognized. Default parameters are used.")
if is_station:
config = ConfigParser()
config_path = "./data/IPS-42.ini"
config.read(config_path)
try:
self.station_name = config.get(str(station), "name")
except (NoSectionError, NoOptionError):
pass
try:
self.lat = config.get(str(station), "lat")
except (NoSectionError, NoOptionError):
pass
try:
self.lon = config.get(str(station), "lon")
except (NoSectionError, NoOptionError):
pass
try:
self.gyro = config.get(str(station), "gyro")
except (NoSectionError, NoOptionError):
pass
try:
self.dip = config.get(str(station), "dip")
except (NoSectionError, NoOptionError):
pass
try:
self.timezone = int(config.get(str(station), "timezone"))
except (NoSectionError, NoOptionError, ValueError):
pass
def _print_digit(self, offset_alt, offset_f):
for h in range(25):
print()
for f in range(13):
print(self.data[offset_alt + h][offset_f + f], end="")
print()
def _img2digit(self, offset_alt, offset_f):
LEVEL = 8
A = 1
B = 1 << 1
C = 1 << 2
D = 1 << 3
E = 1 << 4
F = 1 << 5
G = 1 << 6
H = 1 << 7
J = 1 << 8
digits = [
A | B | C | D | E | F, # 0
H | J, # 1
A | B | G | E | D, # 2
A | B | C | D | G, # 3
F | G | H | J, # 4
A | F | G | C | D, # 5
F | E | D | C | G, # 6
A | B | C, # 7
A | B | C | D | E | F | G, # 8
A | B | C | F | G, # 9
]
if self.debug_level > 0:
self._print_digit(offset_alt, offset_f)
print(offset_alt, offset_f, sep=", ")
"""
aaaaaaaaaaaaa
f h b
f h b
f h b
f h b
f h b
ggggggggggggg
e j c
e j c
e j c
e j c
e j c
ddddddddddddd
"""
a = sum(self.data[offset_alt][offset_f : offset_f + 13]) + sum(
self.data[offset_alt + 1][offset_f : offset_f + 13]
)
b = sum(self.data[offset_alt + i][offset_f + 12] for i in range(13))
c = sum(self.data[offset_alt + i + 13][offset_f + 12] for i in range(13))
d = sum(self.data[offset_alt + 23][offset_f : offset_f + 13]) + sum(
self.data[offset_alt + 24][offset_f : offset_f + 13]
)
e = sum(self.data[offset_alt + i + 13][offset_f] for i in range(13))
f = sum(self.data[offset_alt + i][offset_f] for i in range(13))
g = sum(self.data[offset_alt + 12][offset_f : offset_f + 13]) + sum(
self.data[offset_alt + 13][offset_f : offset_f + 13]
)
h = sum(self.data[offset_alt + i][offset_f + 6] for i in range(13))
j = sum(self.data[offset_alt + i + 13][offset_f + 6] for i in range(13))
array = [a, b, c, d, e, f, g, h, j]
if self.debug_level > 0:
print(array)
if self.debug_level > 0:
print([i > LEVEL for i in array])
p = 0
for i, e in enumerate(array):
p |= (e > LEVEL) << i
for i, d in enumerate(digits):
if d == p:
return i
raise ValueError("Digit is not recognized")
def get_extent(self):
left = self.freq_to_coord(1)
right = self.freq_to_coord(22.6)
bottom = -2
top = 796
return [left, right, bottom, top]
def get_freq_tics(self):
return [self.freq_to_coord(x) for x in self.get_freq_labels()]
def get_freq_labels(self):
return [1.0, 1.4, 2.0, 2.8, 4.0, 5.6, 8.0, 11.4, 16.0, 22.6]
def freq_to_coord(self, freq):
return log(float(freq), 22.6) * 575
def coord_to_freq(self, coord):
return 22.6 ** (coord / 575)