Skip to content

Commit

Permalink
use real command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
n1kdo committed Dec 3, 2023
1 parent 4534557 commit b3326b8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 54 deletions.
27 changes: 4 additions & 23 deletions adif.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,25 +556,6 @@ def get_qsl_cards(username, password, filename=None):
return qsl_cards_header, qsl_cards


def adif_field_naive(s):
if '<' in s:
match = re.search(r'^<(.*)>(.*)$', s)
if match is None:
return None, None
if match.group(2):
payload = match.group(2)
title = match.group(1)
match = re.search(r'^(.*?):.*$', title)
if match is not None:
fn = str(match.group(1)).lower()
return fn, payload
else:
logging.error(f'problem matching adif field {title}')
else:
return str(match.group(1)).lower(), None
return None, None


def adif_field(s):
state = 0
element_name = ''
Expand Down Expand Up @@ -639,7 +620,7 @@ def read_adif_file(adif_file_name):
:param adif_file_name: the name of the file to read.
:return: adif header as dict, array of QSO data as list of dicts
"""
logging.debug('reading adif file {}'.format(adif_file_name))
logging.info('reading adif file {}'.format(adif_file_name))
qsos = []
header = {}
qso = {}
Expand Down Expand Up @@ -705,7 +686,7 @@ def read_adif_file(adif_file_name):
except FileNotFoundError as fnfe:
logging.warning('could not read file {}'.format(adif_file_name))
logging.warning(fnfe)
logging.debug('read {} QSOs from {}'.format(len(qsos), adif_file_name))
logging.info('read {} QSOs from {}'.format(len(qsos), adif_file_name))
return header, sorted(qsos, key=lambda qso: qso_key(qso))


Expand Down Expand Up @@ -781,7 +762,7 @@ def write_adif_field(key, item):


def write_adif_file(header, qsos, adif_file_name, abridge_results=True):
logging.debug('write_adif_file %s' % adif_file_name)
logging.info('write_adif_file %s' % adif_file_name)
save_keys = ['app_lotw_mode',
'app_lotw_modegroup',
'app_n1kdo_qso_combined',
Expand Down Expand Up @@ -844,7 +825,7 @@ def write_adif_file(header, qsos, adif_file_name, abridge_results=True):
else:
f.write(write_adif_field(key, value))
f.write('<eor>\n\n')
logging.debug('wrote_adif_file %s' % adif_file_name)
logging.info('wrote_adif_file %s' % adif_file_name)


def compare_lists(qso_list, cards_list):
Expand Down
69 changes: 46 additions & 23 deletions adif_log_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""

import argparse
import datetime
import logging
import os
Expand Down Expand Up @@ -87,9 +88,6 @@
'FN32', 'FN33', 'FN34', 'FN35', 'FN41', 'FN42', 'FN43', 'FN44', 'FN45', 'FN46', 'FN51', 'FN53',
'FN54', 'FN55', 'FN56', 'FN57', 'FN64', 'FN65', 'FN66', 'FN67']

logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO)
logging.Formatter.converter = time.gmtime
charts_dir = 'charts/'


Expand Down Expand Up @@ -492,56 +490,56 @@ def draw_charts(qso_list, callsign, start_date=None, end_date=None):
bin_data = crunch_data(qso_list)

# now draw the charts
logging.info('drawing QSOs by Date chart')
print('drawing QSOs by Date chart')
qso_charts.QSOsByDateChart(bin_data,
callsign + ' QSOs by Date',
file_callsign + '_qsos_by_date.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing DXCC and Challenge QSOs chart')
print('drawing DXCC and Challenge QSOs chart')
qso_charts.DXCCQSOsChart(bin_data,
callsign + ' DXCC and Challenge Confirmed QSOs',
file_callsign + '_dxcc_qsos.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing VUCC and FFMA QSOs chart')
print('drawing VUCC and FFMA QSOs chart')
qso_charts.VuccFfmaQSOsChart(bin_data,
callsign + ' Confirmed VUCC and FFMA QSOs',
file_callsign + '_vucc_qsos.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing confirmed challenge bands by date chart')
print('drawing confirmed challenge bands by date chart')
qso_charts.ChallengeBandsByDateChart(bin_data,
callsign + ' Confirmed Challenge Bands by Date',
file_callsign + '_challenge_bands_by_date.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing QSO Rate chart')
print('drawing QSO Rate chart')
qso_charts.QSOsRateChart(bin_data,
callsign + ' QSO Rate',
file_callsign + '_qso_rate.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing QSO Rate by Band chart')
print('drawing QSO Rate by Band chart')
qso_charts.QSOsByBandRateChart(bin_data,
callsign + ' QSO Rate by Band',
file_callsign + '_qsos_band_rate.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing QSO Rate by Mode chart')
print('drawing QSO Rate by Mode chart')
qso_charts.QSOsByModeRateChart(bin_data,
callsign + ' QSO Rate by Mode',
file_callsign + '_qsos_mode_rate.png',
start_date=start_date,
end_date=end_date)

logging.info('drawing Grid Squares Confirmed map')
print('drawing Grid Squares Confirmed map')
qso_charts.QSOsMap(qso_list,
callsign + ' Grid Squares Confirmed',
file_callsign + '_grids_map.png',
Expand All @@ -551,13 +549,29 @@ def draw_charts(qso_list, callsign, start_date=None, end_date=None):


def main():
print('N1KDO\'s ADIF analyzer version %s' % __version__)
if len(sys.argv) == 3:
callsign = sys.argv[1]
filename = sys.argv[2]
parser = argparse.ArgumentParser(description='Plot charts for ADIF data')
parser.add_argument('--debug', action='store_true', help='show logging informational output')
parser.add_argument('--info', action='store_true', help='show informational diagnostic output')
parser.add_argument('--marathon-year', type=str, help='create DX marathon charts for year')
parser.add_argument('--callsign', type=str, help='Callsign to chart for')
parser.add_argument('--filename', type=str, help='name of ADIF file')
args = parser.parse_args()

log_format = '%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s'
log_date_format = '%Y-%m-%d %H:%M:%S'
if args.debug:
logging.basicConfig(format=log_format, datefmt=log_date_format, level=logging.DEBUG)
elif args.info:
logging.basicConfig(format=log_format, datefmt=log_date_format, level=logging.INFO)
else:
callsign = ''
filename = ''
logging.basicConfig(format=log_format, datefmt=log_date_format, level=logging.WARNING)

logging.Formatter.converter = time.gmtime

print('N1KDO\'s ADIF analyzer version %s' % __version__)

callsign = args.callsign if args.callsign is not None else ''
filename = args.filename if args.filename is not None else ''

while len(callsign) < 3:
callsign = input('enter callsign: ')
Expand All @@ -573,7 +587,7 @@ def main():
logging.info('read {} qsls from {}'.format(len(qso_list), filename))

if qso_list is not None:
all_time_charts = True
all_time_charts = False # True
if all_time_charts:
start_date = None
end_date = None
Expand All @@ -582,19 +596,28 @@ def main():
# end_date = datetime.datetime.strptime('20181231', '%Y%m%d').date()
draw_charts(qso_list, callsign, start_date=start_date, end_date=end_date)

marathon_charts = True
marathon_charts = args.marathon_year is not None
if marathon_charts:
year = args.marathon_year
try:
year_int = int(year)
except Exception as exc:
year_int = 0
if year_int < 1900 or year_int > 2199:
logging.error(f'invalid year {year_int}')
exit(1)

# now produce marathon output
marathon_qso_list = []
callsign = callsign + '_2023'
start_date = datetime.datetime.strptime('20230101', '%Y%m%d').date()
end_date = datetime.datetime.strptime('20240101', '%Y%m%d').date()
callsign = callsign + f'_{year_int:04d}'
start_date = datetime.datetime.strptime(f'{year_int:04d}0101', '%Y%m%d').date()
end_date = datetime.datetime.strptime(f'{year_int+1:04d}0101', '%Y%m%d').date()
for qso in qso_list:
qso_date = datetime.datetime.strptime(qso['qso_date'], '%Y%m%d').date()
if start_date <= qso_date < end_date:
marathon_qso_list.append(qso)
draw_charts(marathon_qso_list, callsign, start_date=start_date, end_date=end_date)
logging.info('done.')
print('done.')


if __name__ == '__main__':
Expand Down
45 changes: 37 additions & 8 deletions get_lotw_adif.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
__license__ = 'Simplified BSD'
__version__ = '0.05'

import adif
import adif_log_analyzer
import argparse
import logging
import os.path
import time

logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG)
logging.Formatter.converter = time.gmtime
import adif
import adif_log_analyzer


def get_file_date_size(filename):
Expand Down Expand Up @@ -68,8 +66,38 @@ def get_password(password):


def main():
parser = argparse.ArgumentParser(description='get LoTW Records and plot')
parser.add_argument('--debug', action='store_true', help='show logging informational output')
parser.add_argument('--info', action='store_true', help='show informational diagnostic output')
parser.add_argument('--login-callsign', type=str, help='Callsign to use to log in to LoTW')
parser.add_argument('--password', type=str, help='Password to use to log into to LoTW')
parser.add_argument('--callsign', type=str, help='Callsign to analyze records for')
args = parser.parse_args()

log_format = '%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s'
log_date_format = '%Y-%m-%d %H:%M:%S'
if args.debug:
logging.basicConfig(format=log_format, datefmt=log_date_format, level=logging.DEBUG)
elif args.info:
logging.basicConfig(format=log_format, datefmt=log_date_format, level=logging.INFO)
else:
logging.basicConfig(format=log_format, datefmt=log_date_format, level=logging.WARNING)

logging.Formatter.converter = time.gmtime

login_callsign = ''
password = None

if args.callsign is not None:
callsign = args.callsign
if args.login_callsign is None:
login_callsign = args.callsign
if args.login_callsign is not None:
login_callsign = args.login_callsign

if args.password is not None:
password = args.password

data_dir = 'data/'
if not os.path.isdir(data_dir):
print(f'cannot find data directory {data_dir}, creating...')
Expand All @@ -82,9 +110,10 @@ def main():
while len(login_callsign) < 3:
login_callsign = input('Please enter your lotw login callsign: ')

callsign = input(f'Please enter your callsign ({login_callsign}): ')
if callsign == '':
callsign = login_callsign
while len(callsign) < 3:
callsign = input(f'Please enter your callsign ({login_callsign}): ')
if callsign == '':
callsign = login_callsign

filename_callsign = callsign.replace('/', '-')

Expand Down
1 change: 1 addition & 0 deletions qso_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def get_figure(self):

def save_chart(self):
if self.filename is not None:
logging.info(f'writing image file {self.filename}')
canvas = agg.FigureCanvasAgg(self.fig)
canvas.draw()
self.fig.savefig(self.filename, facecolor=BG)
Expand Down

0 comments on commit b3326b8

Please sign in to comment.