-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_file_format.py
More file actions
63 lines (53 loc) · 1.81 KB
/
json_file_format.py
File metadata and controls
63 lines (53 loc) · 1.81 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
import datetime
import json
from typing import Iterable
from ionogram import Ionogram
from ionospheric_layer_trace import Modes
class JsonFileIO:
def __init__(self):
pass
@staticmethod
def save(filename: str, iono: Ionogram, traces: Iterable):
date_ut = iono.date - datetime.timedelta(hours=iono.timezone)
date_str = date_ut.isoformat()
json_data = {
date_str: {
"station_name": iono.get_station_name(),
"latitude": iono.lat,
"longitude": iono.lon,
"gyro": iono.gyro,
"dip": iono.dip,
"sunspot": iono.sunspot,
"date": iono.date.isoformat(),
"timezone": iono.timezone,
"traces": [trace.to_dict() for trace in traces],
}
}
try:
with open(filename, "wt", encoding="utf-8") as file:
json.dump(json_data, file, indent=4)
except IOError:
return False
# print(date_str)
return True
@staticmethod
def load(filename: str):
try:
with open(filename, "rt", encoding="utf-8") as file:
loaded_data = json.load(file)
except IOError:
return
return loaded_data
@staticmethod
def get_trace_points(trace_name: str, traces: Iterable):
trace_points = []
critical_frequency = None
for trace in traces:
if (
trace["name"] == trace_name
and trace["trace_type"] == Modes.ORDINARY
):
critical_frequency = trace["critical_frequency"]
for f, h in zip(trace["freqs"], trace["heights"]):
trace_points.append(f"{f} {h}")
return critical_frequency, trace_points