-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdr_plot.py
223 lines (166 loc) · 6.47 KB
/
sdr_plot.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
from __future__ import unicode_literals
from pyhamtools.locator import locator_to_latlong
import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
from geopy.geocoders import Nominatim
from pyhamtools import LookupLib, Callinfo
import itertools
import requests
import sys
import xml.dom.minidom
from xml.etree import ElementTree
import lxml.etree as etree
import xmltodict, json
qrzlines = []
with open('qrz.txt') as f:
qrzlines = f.read().splitlines()
progress = 0
with open('progress.txt') as f:
progress = int(f.read().splitlines()[0])
print("resuming from line",progress)
apiusername=qrzlines[0]
apipassword=qrzlines[1]
apikey = ''
def GetApiKey():
global apikey
url = 'http://xmldata.qrz.com/xml/current/?username='+apiusername+';password='+apipassword+';agent=q5.0'
response = requests.get(url)
tree = ElementTree.fromstring(response.content)
for child in tree:
for node in child:
if 'Key' in node.tag:
print(node.text)
apikey = node.text
def GetCallSignInfo(callsign):
callsign_dict = {}
with open('contacts.json', 'r') as infile:
try:
callsign_dict = json.load(infile)
if callsign in callsign_dict:
return(callsign_dict[callsign])
except ValueError:
print('empty dict')
callsign_dict = {}
#check if callsign is in file
# use it
#otherwise use website then write it to file
my_lookuplib = LookupLib(lookuptype="qrz", username=apiusername, pwd=apipassword)
cic = Callinfo(my_lookuplib)
#if cic.is_valid_callsign(callsign):
url = 'http://xmldata.qrz.com/xml/current/?s='+apikey+';callsign='+callsign
response = requests.get(url)
link='{http://xmldata.qrz.com}'
root = ElementTree.fromstring(response.content)
contact_dict = {}
for child in root:
if(child.tag == link+'Callsign'):
for item in child:
itemname = item.tag.replace(link,'')
contact_dict[itemname] = item.text
callsign_dict[callsign] = contact_dict
with open('contacts.json', 'w') as outfile:
json.dump(callsign_dict,outfile)
return(callsign_dict[callsign])
#else:
# return None
####################################################
GetApiKey()
lat_list = []
lon_list = []
callsigns = []
desired_countries = []
desired_states = []
with open('seen_countries.txt') as f:
lines = f.read().splitlines()
for line in lines:
if line not in desired_countries:
desired_countries.append(line)
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = "ALL_WSPR.TXT"
print("loading file")
lineList = [line.rstrip('\n') for line in open(filename)]
print("file loaded")
#progress = 0
#for line in lineList:
with open(filename) as f:
for line in itertools.islice(f,progress, None):
progress = progress + 1
#print(progress,"/",len(lineList))
#print(line)
with open("progress.txt", "w") as myfile:
myfile.write(str(progress))
if ("FT8" in line):
#latitude, longitude = locator_to_latlong(line.split()[7])
callsign = line.split()[7]
#if callsign == "CQ":
# callsign = line.split()[8]
# else:
#latitude, longitude = locator_to_latlong(line.split()[7])
# callsign = line.split()[6]
# if callsign == '<...>':
# callsign = line.split()[7]
if callsign not in callsigns:
callsigns.append(callsign)
try:
contact_dict = GetCallSignInfo(callsign)
if contact_dict['country'] not in desired_countries:
print("new country seen", contact_dict['country'])
with open("seen_countries.txt", "a") as myfile:
myfile.write(contact_dict['country']+'\n')
desired_countries.append(contact_dict['country'])
lat_list.append(float(contact_dict['lat']))
lon_list.append(float(contact_dict['lon']))
with open("contacts_latlon.txt", "a") as myfile:
myfile.write(callsign+','+str(float(contact_dict['lat']))+','+str(float(contact_dict['lon']))+','+contact_dict['country']+'\n')
except:
print('invalid callsign - ',callsign)
with open("invalid.txt", "a") as myfile:
myfile.write(line)
with open('contacts_latlon.txt') as f:
lines = f.read().splitlines()
for line in lines:
lat_list.append(float(line.split(',')[1]))
lon_list.append(float(line.split(',')[2]))
country = line.split(',')[3]
if country not in desired_countries:
desired_countries.append(country)
plt.clf()
projection = ccrs.LambertConformal()
ax = plt.axes(projection=projection, aspect='auto')
ax.plot(lon_list, lat_list, 'b.', ms=1.0)
#ax.plot(-32.049523,115.891969, 'r.', ms=2.0)
#ax.plot(115.891969,-32.049523, 'r.', ms=2.0)
#ax.plot(99.771, -37.216, 'ro', ms=1.0)
#shapename = 'admin_1_states_provinces_lakes_shp'
shapename = 'admin_0_countries'
#shapename = 'admin_0_details'
reader = shpreader.Reader(shpreader.natural_earth(resolution='10m', category='cultural', name=shapename))
countries = reader.records()
ax.add_geometries(list(reader.geometries()), projection, facecolor=(0.7, 0.7, 0.7))
plot_countries = []
print("plotting countries")
desired_countries.append('United States of America')
desired_countries.append('Czechia')
desired_countries.append('Slovakia')
desired_countries.append('Republic of Serbia')
desired_countries.append('United Kingdom')
desired_countries.append('Puerto Rico')
desired_countries.append('United States Virgin Islands')
desired_countries.append('Saint Martin')
desired_countries.append('Guam')
desired_countries.append('Saint Helena')
desired_countries.append('Kosovo')
desired_countries.append('Saint Kitts and Nevis')
desired_countries.append('Gambia')
desired_countries.append('Cook Islands')
#desired_countries.append('Curacao') #TODO
#desired_countries.append('Sao Tome and Principe') #TODO
for country in list(reader.records()):
if country.attributes['GEOUNIT'] in str(desired_countries):
if country.attributes['GEOUNIT'] != "Sudan": #TODO
plot_countries.append(country.geometry)
ax.add_geometries(plot_countries, projection, facecolor=(0.9, 0.9, 0.9))
plt.show()