import re
import maidenhead as mh
fl = "C:\\Users\\danielsullivan\\AppData\\Local\\WSJT-X\\ALL.TXT"
fw = "callsign_locations.txt"
lines = None
with open(fl, "r") as rf:
lines = rf.readlines()
with open(fw, "w") as wf:
wf.write("CALLSIGN")
wf.write('\t')
wf.write("DATE")
wf.write('\t')
wf.write("FREQ")
wf.write('\t')
wf.write("SNR")
wf.write('\t')
wf.write("DRIFT_SECONDS")
wf.write('\t')
wf.write("GRID")
wf.write('\t')
wf.write("LAT")
wf.write('\t')
wf.write("LNG")
wf.write('\n')
for ln in lines:
utcd = ln[0:17].strip()
pts = utcd.split('_')
yr = pts[0][0:2]
mn = pts[0][2:4]
dy = pts[0][4:6]
dts = mn + "/" + dy + "/" + yr
frq = ln[17:24].strip()
snr = ln[30:38].strip()
timedrift = ln[37:43].strip()
msg = ln[47:].strip()
grd = ""
lat = None
lng = None
mparts = msg.split(' ')
if len(mparts) == 3:
chk = mparts[2].strip()
if len(chk) == 4:
if chk[0].isalpha() and \
chk[1].isalpha() and \
chk[2].isdigit() and \
chk[3].isdigit():
print(chk)
ll = mh.to_location(chk)
grd = chk
lat = ll[0]
lng = ll[1]
wf.write(mparts[1])
wf.write('\t')
wf.write(dts)
wf.write('\t')
wf.write(frq)
wf.write('\t')
wf.write(snr)
wf.write('\t')
wf.write(timedrift)
wf.write('\t')
wf.write(grd)
wf.write('\t')
wf.write(str(lat))
wf.write('\t')
wf.write(str(lng))
wf.write('\n')