-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwspr_to_curl_file.py
286 lines (237 loc) · 9.1 KB
/
wspr_to_curl_file.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
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
def haversine(lat1, lon1, lat2, lon2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
Bearing=atan2(sin(dlon)*cos(lat2),
cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(dlon))/pi*180
if Bearing < 0:
Bearing+=360
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return int(km), int(Bearing)
def locator_to_latlong (locator):
"""converts Maidenhead locator in the corresponding WGS84 coordinates
Args:
locator (string): Locator, either 4 or 6 characters
Returns:
tuple (float, float): Latitude, Longitude
Raises:
ValueError: When called with wrong or invalid input arg
TypeError: When arg is not a string
Example:
The following example converts a Maidenhead locator into Latitude and Longitude
>>> from pyhamtools.locator import locator_to_latlong
>>> latitude, longitude = locator_to_latlong("JN48QM")
>>> print latitude, longitude
48.5208333333 9.375
Note:
Latitude (negative = West, positive = East)
Longitude (negative = South, positive = North)
"""
locator = locator.upper()
if len(locator) == 5 or len(locator) < 4:
raise ValueError
if ord(locator[0]) > ord('R') or ord(locator[0]) < ord('A'):
raise ValueError
if ord(locator[1]) > ord('R') or ord(locator[1]) < ord('A'):
raise ValueError
if ord(locator[2]) > ord('9') or ord(locator[2]) < ord('0'):
raise ValueError
if ord(locator[3]) > ord('9') or ord(locator[3]) < ord('0'):
raise ValueError
if len(locator) == 6:
if ord(locator[4]) > ord('X') or ord(locator[4]) < ord('A'):
raise ValueError
if ord (locator[5]) > ord('X') or ord(locator[5]) < ord('A'):
raise ValueError
longitude = (ord(locator[0]) - ord('A')) * 20 - 180
latitude = (ord(locator[1]) - ord('A')) * 10 - 90
longitude += (ord(locator[2]) - ord('0')) * 2
latitude += (ord(locator[3]) - ord('0'))
if len(locator) == 6:
longitude += ((ord(locator[4])) - ord('A')) * (2 / 24)
latitude += ((ord(locator[5])) - ord('A')) * (1 / 24)
# move to center of subsquare
longitude += 1 / 24
latitude += 0.5 / 24
else:
# move to center of square
longitude += 1;
latitude += 0.5;
return latitude, longitude
def wspr_to_json(in_str,wspr_reporter,wspr_loc_reporter,wspr_comment):
# in_str = '160310 1942 1 -24 -4.0 7.0395714 PA2W JO22 37 1 6739 -48'
wspr_date, wspr_time, wspr_other, wspr_snr, wspr_dt, wspr_freq, wspr_call, wspr_loc, wspr_pwr, wspr_drift, wspr_rest = re.split(r'[;,\s]\s*', in_str,10)
band_vec = (2200,630,160,80,60,60,40,30,20,17,15,12,10,6)
freq_vec = (0.1,0.4,1.8,3.5,5.2,5.3,7.0,10.1,14.0,18.1,21.0,24.9,28.1,50.2)
wspr_band = band_vec[freq_vec.index(round(float(wspr_freq) - 0.05,1))]
# band_vec = ('LF', 'MW', '160m', '80m', '60m', '40m', '30m', '20m', '17m', '15m', '12m', '10m', '6m')
# freq_vec = (0.1, 0.4, 1.8, 3.5, 5.2, 7.0, 10.1, 14.0, 18.1, 21.0, 24.9, 28.1, 50.2)
# wspr_band = band_vec[freq_vec.index(round(float(wspr_freq) - 0.05, 1))]
wspr_tuple_time = strptime(wspr_date + wspr_time, "%y%m%d%H%M")
wspr_time = strftime("%Y-%m-%dT%H:%M:%SZ", wspr_tuple_time)
wspr_epoch = "%.0f" % (calendar.timegm(wspr_tuple_time))
loclon_reporter = locator_to_latlong(wspr_loc_reporter)
wspr_geohash_reporter = Geohash.encode(loclon_reporter[0], loclon_reporter[1], precision=7)
calckm_az = False
if wspr_call.find('/') < 0 or wspr_call.find('<') == 0:
calckm_az = True
else:
wspr_dist = -1
wspr_az = -1
wspr_drift = wspr_pwr
wspr_pwr = wspr_loc
wspr_loc = 'AA00'
wspr_geohash = '0000'
if wspr_call.find('<') == 0:
wspr_call = wspr_call[1:-1]
if wspr_call.find('..') >= 0:
wspr_call = "00" + wspr_loc
# clean up callsign string
wspr_call = re.sub('[<>]', '', wspr_call)
if wspr_call.find('..') >= 0:
wspr_call = "00" + wspr_loc
if calckm_az:
loclon = locator_to_latlong(wspr_loc)
wspr_dist, wspr_az = haversine(loclon_reporter[0], loclon_reporter[1], loclon[0], loclon[1])
wspr_geohash = Geohash.encode(loclon[0], loclon[1], precision=7)
json_body = [
{
"measurement": "wspr_main",
"tags": {
"reporter": wspr_reporter,
"band": wspr_band,
"loc_reporter": wspr_loc_reporter,
"geohash_reporter": wspr_geohash_reporter,
"comment": str(wspr_comment)
},
"time": wspr_time,
# fields should be real(integer values -- not strings
"fields": {
"call": wspr_call,
"loc": wspr_loc,
"geohash": wspr_geohash,
"snr": int(wspr_snr),
"freq": float("%.6f" % float(wspr_freq)), # limit freq to 6 digits, but keep it a float, not a string!
"drift": int(wspr_drift),
"dt": float("%.1f" % float(wspr_dt)),
"dist": int(wspr_dist),
"az": int(wspr_az),
"pwr": int(wspr_pwr) # dBm
}
}
]
return json_body
def json_curl_str(json_body):
#json_body = [
#{
# "fields": {
# "az": 217,
# "call": "EA4URA",
# "dist": 1497,
# "drift": 0,
# "dt": -0.6,
# "freq": 7.040134,
# "geohash": "ezjm9dq",
# "loc": "IN80CI",
# "pwr": 20,
# "snr": -22
# },
# "measurement": "wspr_main",
# "tags": {
# "band": "..40",
# "comment": "Wdm",
# "geohash_reporter": "u1hvw66",
# "loc_reporter": "JO31LO",
# "reporter": "DL2ZZ"
# },
# "time": "2018-01-26T17:58:00Z"
# }
#]
curl_str = json_body[0]['measurement']
for tag in json_body[0]['tags']:
curl_str = curl_str + "," + tag + "=" + str(json_body[0]['tags'][tag])
i=0
for field in json_body[0]['fields']:
if(isinstance(json_body[0]['fields'][field], str)): #influxdb needs quotes on string fields (not necessary on string tags)
cur_field = "\"" + json_body[0]['fields'][field] + "\""
else:
cur_field = json_body[0]['fields'][field]
if(i==0):
curl_str = curl_str + " " + field + "=" + str(cur_field)
else:
curl_str = curl_str + "," + field + "=" + str(cur_field)
i = i + 1
wspr_tuple_time = strptime(json_body[0]['time'], "%Y-%m-%dT%H:%M:%SZ")
wspr_epoch = "%.0f" % (calendar.timegm(wspr_tuple_time))
curl_str = curl_str + ' ' + wspr_epoch
return curl_str
if __name__ == '__main__': # noqa
import re
import json
import calendar
from time import strftime, strptime, mktime
from math import radians, cos, sin, asin, sqrt, atan2, pi
import geohash as Geohash
import argparse
parser = argparse.ArgumentParser(
description='Convert ALL_WSPR.TXT like file to prepare for curl uploading to wsprlive.net Influxdb',
epilog="""... epilog ...""")
parser.add_argument(
'-r', '--reporter',
type=str,
help="Reporter call sign",
default='PA7T',
required=True)
parser.add_argument(
'-rl', '--reporter_locator',
type=str,
help="Reporter locator",
default='JO22FD',
required=True)
parser.add_argument(
'-rc', '--reporter_comment',
type=str,
help="Reporter comment on configuration i.e. 'ANT=windom, PREAMP=30dB'",
default='',
required=False)
group = parser.add_argument_group('files')
parser.add_argument(
'-fi',
type=str,
help="filename of ALL_WSPR.TXT-file",
default='ALL_WSPR.TXT')
args = parser.parse_args()
try:
f = open(args.fi, "r")
#print(args)
except (IOError, OSError):
print("Error: Cannot open file {} for reading!\n".format(args.fi))
exit(1)
else:
#print("Processing file {} ...\n".format(args.fi))
try:
# iterate over lines
wspr_no = sum(1 for line in f)
f.seek(0)
i = 1
for in_str in f:
if args.reporter_comment:
comment = args.reporter_comment
else:
comment = ''
#print(in_str)
json_body = wspr_to_json(in_str,args.reporter,args.reporter_locator,comment)
#print(json.dumps(json_body, indent=2, sort_keys=True))
curl_str = json_curl_str(json_body)
print(curl_str)
i=i+1
finally:
f.close()