Playing with ADIF parser
When I was waiting for my Raspberry Pi year ago I started to learn python language. First I tried composed ADIF parser. During the time, I modified it little to use it via import command and made also some scripts.
adif.py
- import re
- import datetime
- import time
- ADIF_REC_RE = re.compile(r'<(.*?):(\d+).*?>([^<\t\f\v\Z]+)')
- def parse(fn):
- raw = re.split('<eor>|<eoh>(?i)', open(fn).read() )
- logbook =[]
- for record in raw[1:-1]:
- qso = {}
- tags = ADIF_REC_RE.findall(record)
- for tag in tags:
- qso[tag[0].lower()] = tag[2][:int(tag[1])]
- logbook.append(qso)
- return logbook
- def conv_datetime(adi_date, adi_time):
- return datetime.datetime.strptime(adi_date+adi_time.ljust(6,"0"), "%Y%m%d%H%M%S")
Here is example extract only first QSO. It expected source ADIF is sorted chronologically and output is DXKeeper script set request to QSLsent field.
adi_unique.py
- import adif
- file_name = 'logy/cuc_qsl.adi'
- fh = open("cuc_first.txt","w")
- ch = []
- l = adif.parse(file_name)
- for qso in l:
- t = adif.conv_datetime(qso["qso_date"],qso["time_on"])
- if qso["call"] not in ch:
- ch.append(qso["call"])
- print qso["call"], t.strftime("%Y-%m-%d %H:%M:%S")
- fh.write('filter call="%s" and qso_begin=#%s#\n' % (qso["call"], t.strftime("%Y-%m-%d %H:%M:%S")))
- fh.write('modify qsl_sent R\n')
- fh.close()
After RDA contest I needed move RDA designator from SRX field, where Writelog stored it to proper logbook field. Input is ADIF produced by Writelog contest program, output is again DXKeeper script manage the change.
adi_rda.py
- import adif
- file_name = 'logy/rda-2013-ok1rig.adi'
- fh = open("rda_ok1rig_2013.txt","w")
- l = adif.parse(file_name)
- for qso in l:
- t = adif.conv_datetime(qso["qso_date"],qso["time_on"])
- rda = qso["srx"][0:2]+'-'+qso["srx"][2:4]
- print qso["call"], t.strftime("%Y-%m-%d %H:%M:%S"), rda
- fh.write('filter call="%s" and qso_begin=#%s#\n' % (qso["call"], t.strftime("%Y-%m-%d %H:%M:%S")))
- fh.write('modify CNTY %s\n' % rda)
- fh.close()
Do you like request your QSL card online? Here is script which will find your QSO suitable with OQRS. It is based on OQRS list in python dictionary available at http://oqrs.wikispaces.com . Output is on the screen.
adi_oqrs.py
- import adif
- import oqrs
- file_name = 'logy/export_2013-08-06.adi'
- log = adif.parse(file_name)
- for qso in log:
- if qso['call'].lower() in oqrs.data:
- qsl = qso.get('qsl_rcvd', 'n').lower()
- ## if qsl == 'y':
- ## continue
- t = adif.conv_datetime(qso['qso_date'],qso['time_on'])
- print '%s | %4s | %4s | %s | %s via %s' % (t.strftime('%Y-%m-%d %H:%M:%S'),
- qso['band'],
- qso['mode'],
- qsl.upper(),
- qso['call'],
- oqrs.data[qso['call'].lower()].upper() )