-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.py
More file actions
executable file
·360 lines (328 loc) · 10.1 KB
/
channel.py
File metadata and controls
executable file
·360 lines (328 loc) · 10.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from copy import copy
from decimal import Decimal
from math import atan2, cos, radians, sin, sqrt
FORMAT = "{call:6} {output:9.4f} {offset:+.2f} {modes} {comment}"
R = 6373.0
class Channel:
number_k = "Number"
call_k = "Call"
name_k = "Name"
output_k = "Output"
input_k = "Input"
bandwidth_k = "Bandwidth"
fm_k = "FM"
output_tone_k = "Output Tone"
input_tone_k = "Input Tone"
output_code_k = "Output Code"
input_code_k = "Input Code"
dmr_k = "DMR"
# TODO: Colour Codes are decimal, 0-15
dmr_cc_k = "DMR CC"
c4fm_k = "C4FM"
# TODO: C4FM DSQ, decimal 001-126 (3 digits), is obsoleted by DG-ID 00-99 (2 digits)
# Note: DG-ID is backward compatible, and 00 means "open"
c4fm_dsq_k = "C4FM DSQ"
dstar_k = "D-STAR"
dstar_mode_k = "D-STAR Mode"
p25_k = "P25"
p25_phase_k = "P25 Phase"
# TODO: P25 NAC is hexadecimal, default 0x293, from 0x000 to 0xfff
p25_nac_k = "P25 NAC"
nxdn_k = "NXDN"
# TODO: AFAICT NXDN RANs are decimal, from 1-63 (0 probably means open or all)
nxdn_ran_k = "NXDN RAN"
atv_k = "ATV"
datv_k = "DATV"
location_k = "Location"
latitude_k = "Latitude"
longitude_k = "Longitude"
rx_only_k = "RX Only"
fieldnames = (
number_k,
call_k,
name_k,
output_k,
input_k,
bandwidth_k,
fm_k,
output_tone_k,
input_tone_k,
output_code_k,
input_code_k,
dmr_k,
dmr_cc_k,
c4fm_k,
c4fm_dsq_k,
dstar_k,
dstar_mode_k,
p25_k,
p25_phase_k,
p25_nac_k,
nxdn_k,
nxdn_ran_k,
atv_k,
datv_k,
location_k,
latitude_k,
longitude_k,
rx_only_k,
)
name_length = 256
def __init__(
self,
call,
output,
input=None,
offset=None,
bandwidth="25",
modes=("FM",),
output_tone=None,
input_tone=None,
output_code=None,
input_code=None,
dmr_cc=None,
c4fm_dsq=None,
dstar_mode=None,
p25_phase=None,
p25_nac=None,
nxdn_ran=None,
location=None,
latitude=None,
longitude=None,
rx_only=False,
):
self.call = None
if call:
self.call = call.strip()
self.output = Decimal(output)
if input:
self.input = Decimal(input)
elif offset:
self.input = self.output + Decimal(offset)
else:
self.input = self.output
self.bandwidth = Decimal(bandwidth)
self.modes = modes or []
self.output_tone = None
if output_tone:
self.output_tone = Decimal(output_tone)
self.input_tone = None
if input_tone:
self.input_tone = Decimal(input_tone)
self.output_code = output_code or None
self.input_code = input_code or None
self.dmr_cc = dmr_cc or None
if "DMR" in self.modes:
if self.dmr_cc:
self.dmr_cc = Decimal(self.dmr_cc)
else:
self.dmr_cc = Decimal(0)
self.c4fm_dsq = c4fm_dsq or None
if "C4FM" in self.modes:
if self.c4fm_dsq:
self.c4fm_dsq = Decimal(self.c4fm_dsq)
else:
self.c4fm_dsq = Decimal(00)
self.dstar_mode = dstar_mode or None
self.p25_phase = p25_phase or None
self.p25_nac = p25_nac or None
self.nxdn_ran = nxdn_ran or None
if "NXDN" in self.modes:
if self.nxdn_ran:
self.nxdn_ran = Decimal(self.nxdn_ran)
else:
self.nxdn_ran = Decimal(0)
self.location = location or None
self.latitude = None
if latitude:
self.latitude = Decimal(latitude)
self.longitude = None
if longitude:
self.longitude = Decimal(longitude)
self.rx_only = rx_only or False
self.rules = {}
self._name = None
self._number = 0
@property
def offset(self):
return self.input - self.output
def __hash__(self):
return hash(
(
self.call,
self.output,
self.input,
self.input_tone,
self.input_code,
self.dmr_cc,
self.c4fm_dsq,
self.p25_nac,
self.nxdn_ran,
)
)
def __eq__(self, other):
return (
self.call == other.call
and self.output == other.output
and self.input == other.input
and self.input_tone == other.input_tone
and self.input_code == other.input_code
and self.dmr_cc == other.dmr_cc
and self.c4fm_dsq == other.c4fm_dsq
and self.p25_nac == other.p25_nac
and self.nxdn_ran == other.nxdn_ran
)
def __lt__(self, other):
return self.output < other.output
def __invert__(self):
inverse = copy(self)
inverse.output, inverse.input = self.input, self.output
return inverse
@property
def name(self):
if self._name is None:
location = self.location or ""
self._name = f"{self.call} {location}".rstrip(" ")
return self._name[: self.name_length].rstrip(" ")
@name.setter
def name(self, value):
self._name = value
@property
def fm(self):
return "FM" in self.modes
@property
def dmr(self):
return "DMR" in self.modes
@property
def c4fm(self):
return "C4FM" in self.modes
@property
def dstar(self):
return "D-STAR" in self.modes
@property
def p25(self):
return "P25" in self.modes
@property
def nxdn(self):
return "NXDN" in self.modes
@property
def atv(self):
return "ATV" in self.modes
@property
def datv(self):
return "DATV" in self.modes
@property
def number(self):
return self._number
@number.setter
def number(self, value):
self._number = value
@property
def access(self):
modes = []
if "FM" in self.modes:
mode = "FM"
if self.bandwidth < 25:
mode = "NFM"
if self.input_code:
modes.append(f"{mode} D{self.input_code}N")
elif self.input_tone:
modes.append(f"{mode} {self.input_tone:.1f}")
else:
modes.append(mode)
if "DMR" in self.modes:
modes.append(f"DMR CC{self.dmr_cc}")
if "C4FM" in self.modes:
modes.append(f"C4FM {self.c4fm_dsq}")
if "D-STAR" in self.modes:
modes.append(f"D-STAR {self.dstar_mode}")
if "P25" in self.modes:
modes.append(f"P25 {self.p25_nac}")
if "NXDN" in self.modes:
modes.append(f"NXDN {self.nxdn_ran}")
if "ATV" in self.modes:
modes.append("ATV")
if "DATV" in self.modes:
modes.append("DATV")
return " & ".join(modes) or "NONE"
@property
def errors(self):
_errors = []
if self.rules:
rule, match = sorted(
self.rules.items(), key=lambda x: len(x[1]), reverse=True
)[0]
if "offset" not in match:
_errors.append("WRONG OFFSET")
if "spacing" not in match:
_errors.append("MISALIGNED")
if "bandwidth" not in match:
_errors.append("TOO WIDE")
return _errors
def __str__(self):
output = f"{self.output:.6f}".rstrip("0").rstrip(".")
offset = f"{self.offset:+.2f}".rstrip("0").rstrip(".")
if Decimal(offset) == 0:
offset = "SX"
_str = f"{self.name} {output} {offset} {self.access}"
if self.latitude and self.longitude:
_str += f" ({self.latitude:.2f} {self.longitude:.2f})"
return _str
def __getitem__(self, key):
if key not in self.fieldnames:
raise KeyError(key)
return self.get(key)
def get(self, key, default=None):
for k, v in self.items():
if k == key:
return v
return default
def __setitem__(self, key, value):
if key == self.name_k:
self.name = value
elif key == self.number_k:
self.number = value
else:
raise KeyError(key)
def keys(self):
return {k: None for k in self.fieldnames}.keys()
def items(self):
yield self.number_k, self.number
yield self.call_k, self.call
yield self.name_k, self.name
yield self.output_k, self.output
yield self.input_k, self.input
yield self.bandwidth_k, self.bandwidth
yield self.fm_k, self.fm
yield self.output_tone_k, self.output_tone
yield self.input_tone_k, self.input_tone
yield self.output_code_k, self.output_code
yield self.input_code_k, self.input_code
yield self.dmr_k, self.dmr
yield self.dmr_cc_k, self.dmr_cc
yield self.c4fm_k, self.c4fm
yield self.c4fm_dsq_k, self.c4fm_dsq
yield self.dstar_k, self.dstar
yield self.dstar_mode_k, self.dstar_mode
yield self.p25_k, self.p25
yield self.p25_phase_k, self.p25_phase
yield self.p25_nac_k, self.p25_nac
yield self.nxdn_k, self.nxdn
yield self.nxdn_ran_k, self.nxdn_ran
yield self.atv_k, self.atv
yield self.datv_k, self.datv
yield self.location_k, self.location
yield self.latitude_k, self.latitude
yield self.longitude_k, self.longitude
yield self.rx_only_k, self.rx_only
def distance(self, lat, lon):
R = 6371 # Radius of the earth in km
lat = Decimal(lat)
lon = Decimal(lon)
dLat = radians(lat - self.latitude)
dLon = radians(lon - self.longitude)
a = sin(dLat / 2) * sin(dLat / 2) + cos(radians(self.latitude)) * cos(
radians(lat)
) * sin(dLon / 2) * sin(dLon / 2)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return R * c # Distance in km