-
Notifications
You must be signed in to change notification settings - Fork 0
/
grids.py
executable file
·269 lines (230 loc) · 8.6 KB
/
grids.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
#! /usr/bin/python3
################################################################################
#
# grids.py - Rev 2.0
# Copyright (C) 2021-3 by Joseph B. Attili, aa2il AT arrl DOT net
#
# GUI to plot confirmed 6m & Satellite states & grids.
# New version to use cartopy instead of basemap.
#
# Notes:
# Linux:
# pip install pyhamtools
# Windoz:
# pip install xlrd unidecode pyhamtools serial
#
################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
################################################################################
import numpy as np
import xlrd
import sys
import os
from unidecode import unidecode
import argparse
from pprint import pprint
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.patches as mpatches
from matplotlib.image import imread
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from shapely import geometry
from datetime import timedelta,datetime
from pyhamtools.locator import locator_to_latlong
from settings import *
################################################################################
# User params
ATOLL_CUTOFF = 0.005
US_ONLY=False
#US_ONLY=True
################################################################################
# Structure to contain processing params
class PARAMS:
def __init__(self):
# Process command line args
# Can add required=True to anything that is required
arg_proc = argparse.ArgumentParser()
# Unflagged args allowing us to readily add specific bands
#arg_proc.add_argument('Bands', metavar='Bands',
# type=str, nargs='*', default=None,
# help='Specific band')
arg_proc.add_argument('-sat', action='store_true',help='Satellites Confirmed')
arg_proc.add_argument('-6m', dest='six',action='store_true',help='Satellites Confirmed')
arg_proc.add_argument('-160m', dest='onesixty',action='store_true',help='Satellites Confirmed')
args = arg_proc.parse_args()
self.SAT = args.sat
"""
if len(args.Bands)>0:
self.SIX = '6m' in args.Bands
self.ONESIXTY = '160m' in args.Bands
else:
self.SIX = False
self.ONESIXTY = False
"""
self.SIX = args.six
self.ONESIXTY = args.onesixty
#if not (self.SAT or self.SIX or self.ONESIXTY):
# print('\n*** PARAMETER ERROR - Must select either -sat -6 or -160\n')
# sys.exit(0)
self.SETTINGS,junk = read_settings('.keyerrc')
################################################################################
# JBA - Newer versions of Shapely are finicky - make sure we're dealing with a list
def geo_fixup(geo):
#print('geo type=',geo.geom_type)
if geo.geom_type=='Polygon':
return [geo]
else:
return geo
################################################################################
print("\n\n***********************************************************************************")
print("\nStarting Grid Plotter ...")
P=PARAMS()
print("\nP=",end=' ')
#print "\nP=",
pprint(vars(P))
# Init
DATE = datetime.now().strftime('%m/%d/%y')
MY_CALL=P.SETTINGS['MY_CALL']
if P.SAT:
band='Satellites'
elif P.SIX:
band='6-meters'
elif P.ONESIXTY:
band='160-meters'
else:
print('\n*** PARAMETER ERROR - Must select either -sat -6m or -160m\n')
sys.exit(0)
# Read XLS format spreadsheet and pull out sheet with confirmation data
MY_CALL = P.SETTINGS['MY_CALL'].replace('/','_')
print('MY_CALL=',MY_CALL)
fname = os.path.expanduser('~/'+MY_CALL+'/states.xls')
print('fname=',fname)
book = xlrd.open_workbook(fname,formatting_info=True)
sheet1 = book.sheet_by_name(band)
# Digest confirmed states
confirmed_states=[]
for i in range(1, sheet1.nrows):
state = unidecode( sheet1.cell(i,0).value )
if len(state)>0 and 'Paper' not in state:
a=state.split('(')
confirmed_states.append( a[0].strip() )
print('\nConfirmed States:',confirmed_states,len(confirmed_states))
# Digest confirmed grids
grids=[]
for i in range(1, sheet1.nrows):
grid = unidecode( sheet1.cell(i,3).value )
if len(grid)>0 and 'Paper' not in grid:
grids.append( grid.upper() )
grids.sort()
print('\nConfirmed Grids:',grids,len(grids))
# Digest confirmed countries
dxccs=[]
for i in range(1, sheet1.nrows):
dxcc = unidecode( sheet1.cell(i,6).value )
if 'UNITED STATES' in dxcc:
dxcc='UNITED STATES'
if len(dxcc)>0 and 'Paper' not in dxcc:
dxccs.append( dxcc.upper() )
dxccs.sort()
print('\nConfirmed DXCCs:',dxccs,len(dxccs))
#sys.exit(0)
################################################################################
# Create the map
fig = plt.figure(num=MY_CALL+' - '+band)
ax = fig.add_subplot(1, 1, 1,
projection=ccrs.PlateCarree(central_longitude=-75))
# Put a background image on for nice sea rendering.
if False:
# This doesn't work under pyinstaller ...
ax.stock_img()
else:
# ... so we load image directly instead
fname='../data/50-natural-earth-1-downsampled.png'
#print('fname=',fname)
img = imread(fname)
ax.imshow(img, origin='upper', transform=ccrs.PlateCarree(),
extent=[-180, 180, -90, 90])
ax.set_aspect('auto')
fig.tight_layout(pad=0) # 3 To show title
# Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
states_provinces = cfeature.NaturalEarthFeature(
category='cultural',
name='admin_1_states_provinces_lines',
scale='50m',
facecolor='none')
# Add boundaries
#ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(states_provinces, edgecolor='gray')
ax.set_title(MY_CALL+' - '+band + \
' Confirmed \n DXCCs ('+str(len(dxccs)) + \
'), States ('+str(len(confirmed_states))+ \
') & Grids ('+str(len(grids))+') as of '+DATE)
# Read country and state shape data
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
countries = shpreader.Reader(shpfilename).records()
#print(countries)
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_1_states_provinces_lakes')
states = shpreader.Reader(shpfilename).records()
# Plot confirmed countries
# We don't have geomtery data for all countries so somtimes this leaves somthing out
for country in countries:
name=country.attributes['NAME_LONG'].replace('\0',' ').strip().upper()
#print(name)
#if name=='COLOMBIA':
# name='COLUMBIA'
#print(name,len(name))
#print(country.geometry)
if name in dxccs:
print("***",name)
try:
ax.add_geometries(geo_fixup(country.geometry),
ccrs.PlateCarree(),
facecolor='red' ,alpha=0.5)
except Exception as e:
print(e)
print('Problem drawing country - Oh well')
# Plot confirmed states
for state in states:
name = state.attributes['name'].replace('\0',' ').strip()
if name in confirmed_states:
try:
ax.add_geometries(geo_fixup(state.geometry), ccrs.PlateCarree(),
facecolor='red',
edgecolor='#FFFFFF',
linewidth=.25)
except Exception as e:
print(e)
print('Problem drawing state - name=',name)
# Plot confirmed grids
for grid in grids:
try:
lat,lon = locator_to_latlong(grid)
except:
print('Problem with grid',grid)
sys.exit(0)
geom = geometry.box(minx=lon-1,maxx=lon+1,
miny=lat-0.5,maxy=lat+0.5)
ax.add_geometries([geom], ccrs.PlateCarree(),
facecolor='blue',
edgecolor='blue',
linewidth=.25)
plt.show()