"""Q65 transmit signal generator: WSJT-X message packing, Q65 encoding, waveform and SigMF output.

Ported from WSJT-X 2.7.0:
  lib/77bit/packjt77.f90, lib/chkcall.f90       77-bit message packing (pack77) and display (unpack77)
  lib/qra/q65/genq65.f90                        message -> 85 channel symbols, incl. the RR73 fix-up
  lib/qra/q65/q65.c, qracodes.c                 CRC-12 and the QRA (65,15) encoder over GF(64)
  lib/qra/q65/qra15_65_64_irr_e23.c             code tables
  widgets/mainwindow.cpp, Modulator.cpp         samples per symbol, tone spacing, Tx start delay

The Fortran semantics are kept, including its quirks: words are truncated to 13 characters, the message
to 37, free text to 13, and the message types are tried in the same order as pack77().
"""

import datetime
import os
import subprocess
import tempfile
import wave
from dataclasses import dataclass
from fractions import Fraction

import numpy as np

# ---------------------------------------------------------------------------------------------------------
# 77-bit message packing (packjt77.f90)
# ---------------------------------------------------------------------------------------------------------

NTOKENS = 2063592
MAX22 = 4194304
MAXGRID4 = 32400

_A1 = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
_A2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
_A3 = "0123456789"
_A4 = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
_C38 = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/"
_C42 = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-./?"

_CSEC = ("AB AK AL AR AZ BC CO CT DE EB EMA ENY EPA EWA GA GH IA ID IL IN KS KY LA LAX NS MB MDC ME MI MN "
         "MO MS MT NC ND NE NFL NH NL NLI NM NNJ NNY TER NTX NV OH OK ONE ONN ONS OR ORG PAC PR QC RI SB SC "
         "SCV SD SDG SF SFL SJV SK SNJ STX SV TN UT VA VI VT WCF WI WMA WNY WPA WTX WV WWA WY DX PE NB").split()
_CMULT = ("AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY "
          "NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY NB NS QC ON MB SK AB BC NWT NF LB NU YT PEI "
          "DC DR FR GD GR OV ZH ZL").split() + ["X%02d" % i for i in range(1, 100)]

# hash tables of callsigns seen while packing, used to display <...> calls
_calls10, _calls12, _calls22 = {}, {}, {}


def _is_digit(c):
    return "0" <= c <= "9"


def _is_letter(c):
    return "A" <= c <= "Z"


def _is_grid4(g):
    return (len(g.rstrip()) == 4 and "A" <= g[0] <= "R" and "A" <= g[1] <= "R"
            and _is_digit(g[2]) and _is_digit(g[3]))


def _is_grid6(g):
    return (len(g.rstrip()) == 6 and "A" <= g[0] <= "R" and "A" <= g[1] <= "R" and _is_digit(g[2])
            and _is_digit(g[3]) and "A" <= g[4] <= "X" and "A" <= g[5] <= "X")


def _read_int(s):
    """Fortran list-directed integer read: the first value up to a blank, comma or slash."""
    for sep in ",/ ":
        s = s.split(sep)[0]
    return int(s)


def _div(a, b):
    """Fortran integer division (truncates toward zero)."""
    q = abs(a) // abs(b)
    return q if (a >= 0) == (b > 0) else -q


def _bits(v, n):
    """Fortran bN.N output; out-of-range values would print asterisks there, so raise instead."""
    if v < 0 or v >= 1 << n:
        raise ValueError("field value %d does not fit in %d bits" % (v, n))
    return format(v, "0%db" % n)


def ihashcall(call, m):
    n8 = 0
    for c in call.ljust(13)[:11]:
        n8 = 38 * n8 + _C38.find(c)
    return ((47055833459 * n8) & 0xFFFFFFFFFFFFFFFF) >> (64 - m)


def save_hash_call(c13):
    cw = c13
    if cw[:1] in ("", " ") or cw.startswith("<...>"):
        return
    if cw[0] == "<":
        cw = cw[1:]
    i = cw.find(">")
    if i >= 0:
        cw = cw[:i]
    cw = cw.rstrip()
    if len(cw) < 3:
        return
    _calls10[ihashcall(cw, 10)] = cw
    _calls12[ihashcall(cw, 12)] = cw
    _calls22[ihashcall(cw, 22)] = cw


def chkcall(w):
    """Could w be a standard or compound callsign? Returns (base call, ok)."""
    w = w.rstrip()
    bc = w[:6]
    n1 = len(w)
    if n1 > 11 or any(c in w for c in ".+-?") or (n1 > 6 and "/" not in w):
        return bc, False
    i0 = w.find("/") + 1
    if max(i0 - 1, n1 - i0) > 6:
        return bc, False
    if 2 <= i0 <= n1 - 1:
        bc = w[i0:n1] if i0 - 1 <= n1 - i0 else w[:i0 - 1]
    bc = bc[:6].rstrip()
    b = bc.ljust(6)
    if not _is_letter(b[0]) and not _is_letter(b[1]):
        return bc, False
    if b[0] == "Q" and b[:5] != "QU1RK":
        return bc, False
    i1 = 0
    if _is_digit(b[1]):
        i1 = 2
    if _is_digit(b[2]):
        i1 = 3
    if i1 == 0 or i1 == len(bc):
        return bc, False
    suffix = bc[i1:]
    if not all(_is_letter(c) for c in suffix):
        return bc, False
    return bc, 1 <= len(suffix) <= 3


def pack28(c13):
    c13 = c13.ljust(13)[:13]
    n28 = None
    if c13[:3] == "DE ":
        n28 = 0
    elif c13[:4] == "QRZ ":
        n28 = 1
    elif c13[:3] == "CQ ":
        n28 = 2
    elif c13[:3] == "CQ_":
        s = c13.rstrip()
        if 4 <= len(s) <= 7:
            nlet = sum(_is_letter(c) for c in s[3:])
            nnum = sum(_is_digit(c) for c in s[3:])
            if nnum == 3 and nlet == 0:
                n28 = 3 + int(s[3:6])
            elif 1 <= nlet <= 4 and nnum == 0:
                c4 = s[3:].ljust(4)[:4].rstrip().rjust(4)
                m = 0
                for c in c4:
                    m = 27 * m + (ord(c) - ord("A") + 1 if _is_letter(c) else 0)
                n28 = 3 + 1000 + m
    if n28 is None and c13[0] == "<":
        save_hash_call(c13)
        i2 = c13.find(">") + 1
        n28 = NTOKENS + ihashcall(c13[1:i2 - 1] if i2 > 0 else "", 22)
    if n28 is None:
        s = c13.rstrip()
        n = len(s)
        iarea = min(n, 1)
        for i in range(n, 1, -1):
            if _is_digit(s[i - 1]):
                iarea = i
                break
        npdig = sum(_is_digit(c) for c in s[:max(iarea - 1, 0)])
        nplet = sum(_is_letter(c) for c in s[:max(iarea - 1, 0)])
        nslet = sum(_is_letter(c) for c in s[iarea:])
        save_hash_call(c13)
        if iarea < 2 or iarea > 3 or nplet == 0 or npdig >= iarea - 1 or nslet > 3:
            n28 = NTOKENS + ihashcall(c13, 22)      # nonstandard callsign: 22-bit hash
        else:
            cs = (" " + c13[:5]) if iarea == 2 else c13[:6]
            i1, i2, i3 = _A1.find(cs[0]), _A2.find(cs[1]), _A3.find(cs[2])
            i4, i5, i6 = _A4.find(cs[3]), _A4.find(cs[4]), _A4.find(cs[5])
            n28 = (36 * 10 * 27 * 27 * 27 * i1 + 10 * 27 * 27 * 27 * i2 + 27 * 27 * 27 * i3
                   + 27 * 27 * i4 + 27 * i5 + i6 + NTOKENS + MAX22)
    return n28 & ((1 << 28) - 1)


def split77(msg):
    """Upper case, collapse blanks, split into words (truncated to 13 characters); merge 'CQ xxx CALL'."""
    msg = msg[:37].replace("\0", " ")
    msg = "".join(chr(ord(c) - 32) if "a" <= c <= "z" else c for c in msg)
    words = [x for x in msg.split(" ") if x]
    msg = " ".join(words)
    w = [x[:13] for x in words]
    if len(w) >= 3 and chkcall(w[2])[1] and w[0] == "CQ":
        w = ["CQ_" + w[1][:10]] + w[2:]
    return msg, w


def _pack77_01(w):
    # 0.1 DXpedition: K1ABC RR73; W9XYZ <KH1/KH7Z> -11
    if len(w) != 5 or w[1] != "RR73;" or w[3][:1] != "<" or ">" not in w[3]:
        return None
    try:
        n = _read_int(w[4])
    except ValueError:
        return None
    if n == -99:
        return None
    n5 = min(max(_div(n + 30, 2), 0), 31)
    if not chkcall(w[0])[1] or not chkcall(w[2])[1]:
        return None
    n28a, n28b = pack28(w[0]), pack28(w[2])
    save_hash_call(w[3])
    n10 = ihashcall(w[3][1:w[3].find(">")], 10)
    return _bits(n28a, 28) + _bits(n28b, 28) + _bits(n10, 10) + _bits(n5, 5) + "001" + "000", 0, 1


def _pack77_03(w):
    # 0.3 / 0.4 ARRL Field Day: WA9XYZ KA1ABC R 16A EMA
    nw = len(w)
    if nw < 4 or nw > 5 or not chkcall(w[0])[1] or not chkcall(w[1])[1]:
        return None
    sec = w[-1][:3].rstrip()
    if sec not in _CSEC:
        return None
    isec = _CSEC.index(sec) + 1
    if nw == 5 and w[2] != "R":
        return None
    j = len(w[-2]) - 1
    try:
        ntx = _read_int(w[-2][:j])
    except ValueError:
        return None
    if ntx < 1 or ntx > 32:
        return None
    nclass = ord(w[-2][j]) - ord("A")
    if not 2 <= len(w[-1]) <= 3:
        return None
    n3, intx = (3, ntx - 1) if ntx <= 16 else (4, ntx - 17)
    ir = 1 if w[2] == "R" else 0
    c77 = (_bits(pack28(w[0]), 28) + _bits(pack28(w[1]), 28) + str(ir) + _bits(intx, 4) + _bits(nclass, 3)
           + _bits(isec, 7) + _bits(n3, 3) + "000")
    return c77, 0, n3


def _pack77_06(w):
    # 0.6 WSPR types 1 and 2 (type 3 needs an i3/n3 hint, which genq65 never passes)
    nw = len(w)
    m1, m2 = len(w[0]), len(w[1]) if nw > 1 else 0
    m3 = len(w[2]) if nw > 2 else 0
    if nw == 3 and 3 <= m1 <= 6 and m2 == 4 and m3 <= 2:
        if not _is_grid4(w[1]) or not _is_digit(w[2][0]) or (m3 == 2 and not _is_digit(w[2][1])):
            return None
        g = w[1]
        igrid4 = ((ord(g[0]) - 65) * 18 * 10 * 10 + (ord(g[1]) - 65) * 10 * 10
                  + (ord(g[2]) - 48) * 10 + ord(g[3]) - 48)
        idbm = min(max(_read_int(w[2]), 0), 60)
        idbm = int(np.floor(np.float32(0.3) * np.float32(idbm) + np.float32(0.5)))
        return _bits(pack28(w[0]), 28) + _bits(igrid4, 15) + _bits(idbm, 5) + "00" + "0" * 21 + "110000", 0, 6
    if nw == 2 and 5 <= m1 <= 10 and m2 <= 2:
        i1 = w[0].find("/") + 1
        if i1 < 2 or i1 == m1 or not _is_digit(w[1][0]):
            return None
        if i1 == m1 - 3 and not _is_digit(w[0][m1 - 1]):
            return None
        if m2 == 2 and not _is_digit(w[1][1]):
            return None
        bcall, ok = chkcall(w[0])
        if not ok:
            return None
        if i1 <= 4:                                       # prefix
            npfx = _A2.find(w[0][0])
            if i1 >= 3:
                npfx = 36 * npfx + _A2.find(w[0][1])
            if i1 == 4:
                npfx = 36 * npfx + _A2.find(w[0][2])
        else:                                             # suffix
            sfx = w[0][i1:]
            if len(sfx) == 1:
                npfx = _A2.find(sfx[0])
            elif len(sfx) == 2:
                npfx = 36 * _A2.find(sfx[0]) + _A2.find(sfx[1])
            else:
                if not _is_digit(sfx[2]):
                    return None
                npfx = 36 * 10 * _A2.find(sfx[0]) + 10 * _A2.find(sfx[1]) + _A2.find(sfx[2])
            npfx += 46656
        idbm = min(max(_read_int(w[1]), 0), 60)
        idbm = int(np.floor(np.float32(0.3) * np.float32(idbm) + np.float32(0.5)))
        return (_bits(pack28(bcall), 28) + _bits(npfx, 16) + _bits(idbm, 5) + "1" + "0" * 21 + "110000",
                0, 6)
    return None


def _pack77_1(w):
    # 1 / 2 standard message: WA9XYZ/R KA1ABC/R R FN42, PA3XYZ/P GM4ABC/P R JO22
    nw = len(w)
    if nw < 2 or nw > 4:
        return None
    bc1, ok1 = chkcall(w[0])
    bc2, ok2 = chkcall(w[1])
    if w[0] in ("DE", "CQ", "QRZ") or w[0].startswith("CQ_"):
        ok1 = True
    if w[0][0] == "<" and w[0].find(">") + 1 >= 5:
        ok1 = True
    if w[1][0] == "<" and w[1].find(">") + 1 >= 5:
        ok2 = True
    if not ok1 or not ok2:
        return None
    if (w[0][0] == "<" and "/" in w[1]) or (w[1][0] == "<" and "/" in w[0]):
        return None
    if nw == 2 and w[1].find("/") >= 1:
        return None
    ir = 0
    if nw == 2:
        igrid4 = MAXGRID4 + 1
    else:
        last = w[-1]
        if not (_is_grid4(last[:4]) or last[0] in "+-" or last[:2] in ("R+", "R-")
                or last in ("RRR", "RR73", "73")):
            return None
        if _is_grid4(last[:4]):
            ir = 1 if w[2] == "R" else 0
            g = last[:4]
            igrid4 = ((ord(g[0]) - 65) * 18 * 10 * 10 + (ord(g[1]) - 65) * 10 * 10
                      + (ord(g[2]) - 48) * 10 + ord(g[3]) - 48)
        else:
            if last[0] in "+-" or last[:2] in ("R+", "R-"):
                ir = 1 if last[0] == "R" else 0
                try:
                    irpt = _read_int(last[ir:])
                except ValueError:
                    return None
                if -50 <= irpt <= -31:
                    irpt += 101
                irpt += 35
            else:
                irpt = {"RRR": 2, "RR73": 3, "73": 4}[last]
            igrid4 = MAXGRID4 + irpt
    if not (nw in (2, 3) or (nw == 4 and w[2] == "R")):
        return None
    i1p = (w[0] + " ").find("/P ") + 1
    i2p = (w[1] + " ").find("/P ") + 1
    i3 = 2 if i1p >= 4 or i2p >= 4 else 1
    n28a = pack28(w[0] if bc1[:3] == "CQ_" or w[0][0] == "<" else bc1)
    n28b = pack28(w[1] if w[1][0] == "<" else bc2)
    ipa = 1 if i1p >= 4 or (w[0] + " ").find("/R ") + 1 >= 4 else 0
    ipb = 1 if i2p >= 4 or (w[1] + " ").find("/R ") + 1 >= 4 else 0
    c77 = _bits(n28a, 28) + str(ipa) + _bits(n28b, 28) + str(ipb) + str(ir) + _bits(igrid4, 15) + _bits(i3, 3)
    return c77, i3, 0


def _pack77_3(w):
    # 3 ARRL RTTY contest: TU; W9XYZ K1ABC R 579 MA
    nw = len(w)
    if nw not in (4, 5, 6) or (w[0][0] == "<" and w[1][0] == "<"):
        return None
    itu = 1 if w[0] == "TU;" else 0
    if not chkcall(w[itu])[1] or not chkcall(w[itu + 1])[1]:
        return None
    crpt = w[-2][:3].ljust(3)
    if "-" in crpt or "+" in crpt:
        return None
    nserial = 0
    if crpt[0] == "5" and "2" <= crpt[1] <= "9" and crpt[2] == "9":
        try:
            nserial = _read_int(w[-1])
        except ValueError:
            nserial = 0
    imult = _CMULT.index(w[-1]) + 1 if w[-1] in _CMULT else -1
    if imult < 0 and nserial <= 0:
        return None
    nexch = 8000 + imult if imult > 0 else nserial
    ir = 1 if w[2 + itu] == "R" else 0
    try:
        irpt = _read_int(w[2 + itu + ir])
    except (ValueError, IndexError):
        return None
    irpt = min(max(_div(irpt - 509, 10) - 2, 0), 7)
    c77 = (str(itu) + _bits(pack28(w[itu]), 28) + _bits(pack28(w[itu + 1]), 28) + str(ir) + _bits(irpt, 3)
           + _bits(nexch, 13) + "011")
    return c77, 3, 0


def _pack77_4(w):
    # 4 one nonstandard call and one hashed call: <WA9XYZ> PJ4/KA1ABC RR73
    nw = len(w)
    if nw not in (2, 3):
        return None
    call_1 = w[0][1:len(w[0]) - 1] if w[0][0] == "<" else w[0]
    call_2 = w[1][1:len(w[1]) - 1] if w[1][0] == "<" else w[1]
    bc1, ok1 = chkcall(call_1)
    bc2, ok2 = chkcall(call_2)
    if call_1 == bc1 and call_2 == bc2 and ok1 and ok2:
        return None
    i3, icq = -1, 0
    if w[0] == "CQ" or (ok1 and ok2):
        if w[0] == "CQ" and len(w[1]) <= 4:
            return None
        i3 = 4
        icq = 1 if w[0] == "CQ" else 0
    if icq:
        # pack77_4 sets n12 = 0, but save_hash_call() then overwrites it with the 12-bit hash of the call
        n12, iflip, c11 = 0, 0, call_2[:11].rjust(11)
        save_hash_call(w[1])
        if len(call_2) >= 3:
            n12 = ihashcall(call_2, 12)
    elif w[0][0] == "<":
        i3, iflip, c11 = 4, 0, call_2[:11].rjust(11)
        save_hash_call(w[0])
        n12 = ihashcall(call_1, 12)
    elif w[1][0] == "<":
        i3, iflip, c11 = 4, 1, call_1[:11].rjust(11)
        save_hash_call(w[1])
        n12 = ihashcall(call_2, 12)
    elif i3 == 4:
        raise ValueError("compound callsign: put <> around the other callsign, e.g. '<K1ABC> PJ4/W9XYZ'")
    if i3 < 0:
        return None
    n58 = 0
    for c in c11:
        n58 = 38 * n58 + _C38.find(c)
    nrpt = {"RRR": 1, "RR73": 2, "73": 3}.get(w[2] if nw == 3 else "", 0)
    if icq:
        iflip, nrpt = 0, 0
    return _bits(n12, 12) + _bits(n58, 58) + str(iflip) + _bits(nrpt, 2) + str(icq) + "100", 4, 0


def _pack77_5(w):
    # 5 EU VHF contest: <PA3XYZ> <G4ABC/P> R 590003 IO91NP
    nw = len(w)
    if nw < 4 or nw > 5 or w[0][0] != "<" or w[1][0] != "<":
        return None
    try:
        nx = _read_int(w[-2])
    except ValueError:
        return None
    if nx < 520001 or nx > 594095 or not _is_grid6(w[-1][:6]):
        return None
    save_hash_call(w[0])
    n12 = ihashcall(w[0][1:w[0].find(">")], 12)
    save_hash_call(w[1])
    n22 = ihashcall(w[1][1:w[1].find(">")], 22)
    ir = 1 if w[2] == "R" else 0
    irpt = nx // 10000 - 52
    iserial = min(nx % 10000, 2047)
    g = w[-1][:6]
    igrid6 = ((ord(g[0]) - 65) * 18 * 10 * 10 * 24 * 24 + (ord(g[1]) - 65) * 10 * 10 * 24 * 24
              + (ord(g[2]) - 48) * 10 * 24 * 24 + (ord(g[3]) - 48) * 24 * 24 + (ord(g[4]) - 65) * 24
              + ord(g[5]) - 65)
    c77 = (_bits(n12, 12) + _bits(n22, 22) + str(ir) + _bits(irpt, 3) + _bits(iserial, 11) + _bits(igrid6, 25)
           + "101")
    return c77, 5, 0


def packtext77(c13):
    n = 0
    for c in c13[:13].ljust(13).rstrip().rjust(13):
        n = 42 * n + max(_C42.find(c), 0)
    return _bits(n, 71)


def pack77(msg):
    """Pack a message into 77 bits. Returns (c77 bit string, i3, n3)."""
    msg, w = split77(msg)
    if not w:
        raise ValueError("empty message")
    r = None
    if not (msg.startswith("CQ ") or msg.startswith("DE ") or msg.startswith("QRZ ")):
        r = _pack77_01(w) or _pack77_03(w)
        if r is None and len(w) < 2:
            # 0.5 telemetry: up to 18 hex digits
            c18 = msg.split(" ")[0][:18].rjust(18)
            hexd = c18.strip()
            ntel = None
            if hexd and all(c in "0123456789ABCDEF" for c in hexd):
                ntel = [int(c18[6 * i:6 * i + 6].strip() or "0", 16) for i in range(3)]
            if ntel is not None:
                if ntel[0] >= 1 << 23:
                    w = []                                    # straight to free text
                else:
                    r = _bits(ntel[0], 23) + _bits(ntel[1], 24) + _bits(ntel[2], 24) + "101000", 0, 5
    if r is None and w:
        for f in (_pack77_06, _pack77_1, _pack77_3, _pack77_4, _pack77_5):
            r = f(w)
            if r is not None:
                break
    if r is None:
        r = packtext77(msg[:13]) + "000000", 0, 0           # 0.0 free text
    return r


# ---------------------------------------------------------------------------------------------------------
# message display (unpack77, the common types)
# ---------------------------------------------------------------------------------------------------------

def unpack28(n28):
    if n28 < NTOKENS:
        if n28 <= 2:
            return ("DE", "QRZ", "CQ")[n28]
        if n28 <= 1002:
            return "CQ_%03d" % (n28 - 3)
        if n28 <= 532443:
            n = n28 - 1003
            s = ""
            for d in (27 * 27 * 27, 27 * 27, 27, 1):
                s += _A4[n // d]
                n %= d
            return "CQ_" + s.strip()
    n28 -= NTOKENS
    if n28 < MAX22:
        c = _calls22.get(n28)
        return "<%s>" % c if c else "<...>"
    n = n28 - MAX22
    s = ""
    for d, a in ((36 * 10 * 27 * 27 * 27, _A1), (10 * 27 * 27 * 27, _A2), (27 * 27 * 27, _A3),
                 (27 * 27, _A4), (27, _A4), (1, _A4)):
        s += a[n // d]
        n %= d
    return s.strip()


def unpack77(c77):
    """The message as WSJT-X will display it."""
    n3, i3 = int(c77[71:74], 2), int(c77[74:77], 2)
    if i3 == 0 and n3 == 0:
        n = int(c77[:71], 2)
        s = ""
        for _ in range(13):
            s = _C42[n % 42] + s
            n //= 42
        return s.strip()
    if i3 == 0 and n3 == 5:
        return ("%06X%06X%06X" % (int(c77[:23], 2), int(c77[23:47], 2), int(c77[47:71], 2))).lstrip("0")
    if i3 in (1, 2):
        n28a, ipa, n28b, ipb = int(c77[:28], 2), c77[28], int(c77[29:57], 2), c77[57]
        ir, igrid4 = c77[58], int(c77[59:74], 2)
        calls = []
        for n28, ip in ((n28a, ipa), (n28b, ipb)):
            c = unpack28(n28)
            if c.startswith("CQ_"):
                c = "CQ " + c[3:]
            if "<" not in c and (c + " ").find(" ") >= 3 and ip == "1":
                c += "/R" if i3 == 1 else "/P"
            calls.append(c)
        msg = " ".join(calls)
        if igrid4 <= MAXGRID4:
            g = igrid4
            grid = (chr(65 + g // 1800) + chr(65 + g % 1800 // 100) + str(g % 100 // 10) + str(g % 10))
            return msg + (" R " if ir == "1" else " ") + grid
        irpt = igrid4 - MAXGRID4
        if irpt <= 4:
            return msg + ("", "", " RRR", " RR73", " 73")[irpt]
        isnr = irpt - 35
        if isnr > 50:
            isnr -= 101
        return msg + (" R" if ir == "1" else " ") + "%+03d" % isnr
    if i3 == 4:
        n12, n58 = int(c77[:12], 2), int(c77[12:70], 2)
        iflip, nrpt, icq = c77[70], int(c77[71:73], 2), c77[73]
        c11 = ""
        for _ in range(11):
            c11 = _C38[n58 % 38] + c11
            n58 //= 38
        c11 = c11.strip()
        h = _calls12.get(n12)
        call_3 = "<%s>" % h if h else "<...>"
        if icq == "1":
            return "CQ " + c11
        c1, c2 = (call_3, c11) if iflip == "0" else (c11, call_3)
        return c1 + " " + c2 + ("", " RRR", " RR73", " 73")[nrpt]
    return "(message type %s, not unpacked here)" % ("%d.%d" % (i3, n3) if i3 == 0 else str(i3))


# ---------------------------------------------------------------------------------------------------------
# Q65 encoding (genq65.f90, q65.c, qracodes.c, qra15_65_64_irr_e23.c)
# ---------------------------------------------------------------------------------------------------------

# GF(64), primitive polynomial x^6 + x + 1
_GFEXP = [1]
for _ in range(62):
    _v = _GFEXP[-1] << 1
    _GFEXP.append(_v ^ 0x43 if _v & 0x40 else _v)
_GFLOG = [-1] * 64
for _i, _v in enumerate(_GFEXP):
    _GFLOG[_v] = _i

# accumulator chain of the QRA code qra15_65_64_irr_e23: input symbol index and weight logarithm
_ACC_IDX = [13, 1, 3, 4, 8, 12, 9, 14, 10, 5, 0, 7, 1, 11, 8, 9, 12, 6, 3, 10, 7, 5, 2, 13, 12, 4, 8, 0, 1, 11,
            2, 9, 14, 5, 6, 13, 7, 12, 11, 2, 9, 0, 10, 4, 7, 14, 8, 11, 3, 6]
_ACC_WLOG = [0, 14, 0, 0, 13, 37, 0, 27, 56, 62, 29, 0, 52, 34, 62, 4, 3, 22, 25, 0, 22, 0, 20, 10, 0, 43, 53,
             60, 0, 0, 0, 62, 0, 5, 0, 61, 36, 31, 61, 59, 10, 0, 29, 39, 25, 18, 0, 14, 11, 50]

SYNC_POS = [1, 9, 12, 13, 15, 22, 23, 26, 27, 33, 35, 38, 46, 50, 55, 60, 62, 66, 69, 74, 76, 85]  # 1-based
N_SYMBOLS = 85


def crc12(x):
    sr = 0
    for t in x:
        for _ in range(6):
            sr = (sr >> 1) ^ 0xF01 if (t ^ sr) & 1 else sr >> 1
            t >>= 1
    return [sr & 0x3F, sr >> 6]


def q65_encode(x13):
    """13 message symbols -> 63 transmitted code symbols (the 2 CRC-12 symbols are punctured)."""
    x = list(x13) + crc12(x13)
    chk, y = 0, []
    for k in range(50):
        t = x[_ACC_IDX[k]]
        if t:
            chk ^= _GFEXP[(_GFLOG[t] + _ACC_WLOG[k]) % 63]
        y.append(chk)
    return x[:13] + y


def message_symbols(c77):
    """77 bits + one 0 bit -> 13 six-bit symbols. Includes genq65's fix-up: grid 'RR73' is sent as RR73."""
    if int(c77[59:74], 2) == 32373:
        c77 = c77[:59] + "111111010010011" + c77[74:]
    b = c77 + "0"
    return [int(b[6 * i:6 * i + 6], 2) for i in range(13)]


def channel_tones(codeword63):
    """Insert the 22 sync symbols (tone 0); code symbol s is sent as tone s + 1."""
    data = iter(codeword63)
    return np.array([0 if i in SYNC_POS else next(data) + 1 for i in range(1, N_SYMBOLS + 1)])


@dataclass
class Encoded:
    message: str
    message_sent: str
    i3: int
    n3: int
    c77: str
    symbols: list
    codeword: list
    tones: np.ndarray


def encode(message):
    c77, i3, n3 = pack77(message)
    x = message_symbols(c77)
    cw = q65_encode(x)
    return Encoded(message, unpack77(c77), i3, n3, c77, x, cw, channel_tones(cw))


# ---------------------------------------------------------------------------------------------------------
# waveform
# ---------------------------------------------------------------------------------------------------------

NSPS = {15: 1800, 30: 3600, 60: 7200, 120: 16000, 300: 41472}   # samples per symbol at 12 kHz


@dataclass
class Q65Params:
    submode: str = "C"            # A-E
    tr_period: int = 60           # s: 15, 30, 60, 120 or 300
    sample_rate: float = 250e3    # Hz
    center_freq: float = 1299.650e6   # Hz, SDR tuning = WSJT-X dial frequency (USB)
    audio_freq: float = 1000.0    # Hz, WSJT-X Tx audio frequency = sync tone, above the dial frequency

    def __post_init__(self):
        if self.tr_period not in NSPS:
            raise ValueError("tr_period must be one of %s" % sorted(NSPS))
        if self.submode not in "ABCDE" or len(self.submode) != 1:
            raise ValueError("submode must be A, B, C, D or E")

    @property
    def name(self):
        return "Q65-%d%s" % (self.tr_period, self.submode)

    @property
    def nsps(self):
        return NSPS[self.tr_period]

    @property
    def baud(self):
        return 12000 / self.nsps

    @property
    def tone_spacing(self):
        return self.baud * 2 ** "ABCDE".index(self.submode)

    @property
    def start_delay(self):
        """Tx start after the T/R period boundary (Modulator.cpp)."""
        return 0.5 if self.nsps <= 3600 else 1.0

    @property
    def signal_s(self):
        return N_SYMBOLS * self.nsps / 12000

    @property
    def top_freq(self):
        """Audio frequency of the highest tone (64)."""
        return self.audio_freq + 64 * self.tone_spacing

    @property
    def samples_per_symbol(self):
        return Fraction(self.nsps, 12000) * Fraction(self.sample_rate).limit_denominator(10 ** 9)

    def summary(self):
        return "\n".join([
            "%s: %.3f baud, tone spacing %.3f Hz, %d symbols, %.2f s" % (
                self.name, self.baud, self.tone_spacing, N_SYMBOLS, self.signal_s),
            "Tx starts %.1f s into the %d s T/R period, ends at %.2f s" % (
                self.start_delay, self.tr_period, self.start_delay + self.signal_s),
            "Tones %.1f .. %.1f Hz above the dial frequency (%.1f Hz wide)" % (
                self.audio_freq, self.top_freq, self.top_freq - self.audio_freq),
            "RF %.6f .. %.6f MHz, %.4f samples per symbol at %g Hz" % (
                (self.center_freq + self.audio_freq) / 1e6, (self.center_freq + self.top_freq) / 1e6,
                float(self.samples_per_symbol), self.sample_rate),
        ])


def symbol_chunks(tones, p):
    """Continuous-phase FSK at the SDR sample rate, signal at +audio_freq from DC, unit amplitude.

    Yields (first sample index, complex128 samples) per symbol. The phase is continuous in continuous time:
    symbol k starts at exactly k * nsps / 12000 s, independent of the sample rate.
    """
    fs = p.sample_rate
    sps = p.samples_per_symbol
    n_total = int(np.ceil(N_SYMBOLS * sps))
    ts = p.nsps / 12000
    phi = 0.0
    for k, tone in enumerate(tones):
        n0, n1 = int(np.ceil(k * sps)), min(int(np.ceil((k + 1) * sps)), n_total)
        f = p.audio_freq + tone * p.tone_spacing
        t = (np.arange(n1 - n0) + float(n0 - k * sps)) / fs
        yield n0, np.exp(1j * (phi + 2 * np.pi * f * t))
        phi = (phi + 2 * np.pi * f * ts) % (2 * np.pi)


def n_signal_samples(p):
    return int(np.ceil(N_SYMBOLS * p.samples_per_symbol))


def envelope(n, n_total, ramp_samples):
    """Raised-cosine ramp over ramp_samples at both ends, for sample indices n."""
    if ramp_samples <= 0:
        return np.ones(len(n))
    m = np.minimum(n, n_total - 1 - n)
    return np.where(m < ramp_samples, 0.5 * (1 - np.cos(np.pi * (np.minimum(m, ramp_samples) + 0.5)
                                                       / ramp_samples)), 1.0)


def synthesize(tones, p, amplitude=1.0, ramp_s=0.01, dtype=np.complex64):
    """The whole signal as one array (see symbol_chunks)."""
    n_total = n_signal_samples(p)
    nr = int(round(ramp_s * p.sample_rate))
    out = np.empty(n_total, dtype=dtype)
    for n0, z in symbol_chunks(tones, p):
        n = np.arange(n0, n0 + len(z))
        out[n] = amplitude * envelope(n, n_total, nr) * z
    return out


# ---------------------------------------------------------------------------------------------------------
# timing and SigMF
# ---------------------------------------------------------------------------------------------------------

def period_start(p, start_utc=None):
    """T/R period start as an aware UTC datetime: the next one after now, or start_utc (ISO 8601, aligned)."""
    if start_utc is None:
        now = datetime.datetime.now(datetime.timezone.utc)
        t = (int(now.timestamp()) // p.tr_period + 1) * p.tr_period
        return datetime.datetime.fromtimestamp(t, datetime.timezone.utc)
    t0 = datetime.datetime.fromisoformat(start_utc.replace("Z", "+00:00"))
    if t0.tzinfo is None:
        t0 = t0.replace(tzinfo=datetime.timezone.utc)
    if t0.timestamp() % p.tr_period:
        raise ValueError("start_utc %s is not on a %d s T/R period boundary" % (start_utc, p.tr_period))
    return t0


def format_iso(t):
    return t.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")


def write_sigmf(base, enc, p, t0, backoff_db=1.0, ramp_s=0.01, lead_in=True, author="", tx_gain=None,
                extra_global=None):
    """Write base.sigmf-data (ci16_le) and base.sigmf-meta. Returns (meta file, peak int16, lead samples).

    lead_in=True:  the file starts at the T/R period boundary t0, with the Tx start delay as silence.
    lead_in=False: the file starts with the signal; core:datetime is t0 + start delay.
    """
    from sigmf import SigMFFile

    amplitude = (2 ** 15 - 1) * 10 ** (-backoff_db / 20)
    lead = int(round(p.start_delay * p.sample_rate)) if lead_in else 0
    n_sig = n_signal_samples(p)
    nr = int(round(ramp_s * p.sample_rate))
    iq = np.zeros(2 * (lead + n_sig), dtype="<i2")
    for n0, z in symbol_chunks(enc.tones, p):
        n = np.arange(n0, n0 + len(z))
        z = amplitude * envelope(n, n_sig, nr) * z
        iq[2 * (lead + n0):2 * (lead + n0 + len(z)):2] = np.round(z.real)
        iq[2 * (lead + n0) + 1:2 * (lead + n0 + len(z)):2] = np.round(z.imag)
    data_file, meta_file = base + ".sigmf-data", base + ".sigmf-meta"
    iq.tofile(data_file)
    peak = int(np.max(np.abs(iq)))

    global_info = {
        SigMFFile.DATATYPE_KEY: "ci16_le",
        SigMFFile.SAMPLE_RATE_KEY: p.sample_rate,
        SigMFFile.AUTHOR_KEY: author,
        SigMFFile.DESCRIPTION_KEY: "%s: %s" % (p.name, enc.message_sent),
        SigMFFile.EXTENSIONS_KEY: [{"name": "q65", "version": "1.0.0", "optional": True},
                                   {"name": "tx", "version": "1.0.0", "optional": True}],
        "tx:backoff_db": backoff_db,
        "tx:ramp_s": ramp_s,
        "q65:mode": p.name,
        "q65:submode": p.submode,
        "q65:tr_period": p.tr_period,
        "q65:message": enc.message,
        "q65:message_sent": enc.message_sent,
        "q65:i3": enc.i3,
        "q65:n3": enc.n3,
        "q65:audio_freq": p.audio_freq,
        "q65:tone_spacing": p.tone_spacing,
        "q65:baud": p.baud,
        "q65:start_delay_s": p.start_delay,
        "q65:lead_in": lead_in,
        "q65:tones": [int(t) for t in enc.tones],
    }
    if tx_gain is not None:
        global_info[SigMFFile.EXTENSIONS_KEY].insert(0, {"name": "vrt", "version": "1.0.0", "optional": True})
        global_info["vrt:tx_gain"] = tx_gain
    global_info.update(extra_global or {})

    meta = SigMFFile(data_file=data_file, global_info=global_info)
    t_file = t0 if lead_in else t0 + datetime.timedelta(seconds=p.start_delay)
    meta.add_capture(0, metadata={SigMFFile.FREQUENCY_KEY: p.center_freq,
                                  SigMFFile.DATETIME_KEY: format_iso(t_file)})
    meta.add_annotation(lead, n_sig, metadata={
        SigMFFile.FLO_KEY: p.center_freq + p.audio_freq - p.baud,
        SigMFFile.FHI_KEY: p.center_freq + p.top_freq + p.baud,
        SigMFFile.LABEL_KEY: p.name,
        SigMFFile.COMMENT_KEY: enc.message_sent,
    })
    meta.tofile(meta_file)
    return meta_file, peak, lead


def read_sigmf(meta_file):
    """Returns (complex64 samples, metadata dict)."""
    import json
    meta = json.load(open(meta_file))
    data_file = meta_file[:-len(".sigmf-meta")] + ".sigmf-data"
    iq = np.fromfile(data_file, dtype="<i2").astype(np.float32)
    return (iq[0::2] + 1j * iq[1::2]).astype(np.complex64), meta


# ---------------------------------------------------------------------------------------------------------
# WSJT-X check: IQ -> 12 kHz USB audio .wav -> jt9
# ---------------------------------------------------------------------------------------------------------

def to_audio(x, p, lead, snr_db=None, delay_s=0.0, seed=1):
    """Demodulate the IQ file as a USB receiver tuned to the dial frequency: 12 kHz real audio, one T/R period.

    x is the file content (lead samples before the signal). The signal is placed at start_delay + delay_s
    into the period; delay_s ~ 2.5 s mimics the EME path. snr_db is in 2500 Hz, as WSJT-X reports it.
    """
    from scipy.signal import resample_poly

    r = Fraction(12000) / Fraction(p.sample_rate).limit_denominator(10 ** 9)
    sig = x[lead:].astype(np.complex128)
    sig /= np.sqrt(np.mean(np.abs(sig) ** 2))                  # unit power complex -> 0.5 in the real part
    a = resample_poly(sig, r.numerator, r.denominator).real
    n = p.tr_period * 12000
    out = np.zeros(n)
    i0 = int(round((p.start_delay + delay_s) * 12000))
    m = min(len(a), n - i0)
    out[i0:i0 + m] = a[:m]
    if snr_db is None:
        audio = out * 3000 / np.max(np.abs(out))
    else:
        rng = np.random.default_rng(seed)
        sigma = np.sqrt(0.5 * 6000 / 2500 / 10 ** (snr_db / 10))   # noise std for signal power 0.5
        audio = (out + sigma * rng.standard_normal(n)) * 100 / sigma
    return np.clip(np.round(audio), -32767, 32767).astype("<i2")


def write_wav(path, audio):
    with wave.open(path, "wb") as f:
        f.setnchannels(1)
        f.setsampwidth(2)
        f.setframerate(12000)
        f.writeframes(audio.tobytes())


def _boost_stub_env(stub_dir):
    """Homebrew boost >= 1.89 no longer ships libboost_system.dylib (header-only since 1.69), which older
    WSJT-X builds still link. An empty stub library found via DYLD_FALLBACK_LIBRARY_PATH satisfies the loader."""
    lib = os.path.join(stub_dir, "libboost_system.dylib")
    if not os.path.exists(lib):
        os.makedirs(stub_dir, exist_ok=True)
        src = os.path.join(stub_dir, "stub.c")
        with open(src, "w") as f:
            f.write("void boost_system_stub(void) {}\n")
        subprocess.run(["clang", "-dynamiclib", "-o", lib, "-install_name",
                        "/opt/homebrew/opt/boost/lib/libboost_system.dylib", src], check=True)
    return dict(os.environ, DYLD_FALLBACK_LIBRARY_PATH=stub_dir)


def run_jt9(audio, p, t0, jt9="jt9", freq_tol=50, depth=3, args=(),
            boost_stub_dir=os.path.expanduser("~/.cache/wsjtx-boost-stub")):
    """Decode one T/R period of 12 kHz audio with WSJT-X's jt9 in Q65 mode. Returns the decode lines.

    args: extra jt9 options, e.g. ("-c", "MYCALL", "-x", "HISCALL", "-g", "GRID") for Q65 list decoding (q3).
    """
    with tempfile.TemporaryDirectory() as tmp:
        fmt = "%y%m%d_%H%M%S" if p.tr_period < 60 else "%y%m%d_%H%M"
        wav = os.path.join(tmp, t0.strftime(fmt) + ".wav")
        write_wav(wav, audio)
        cmd = [jt9, "-3", "-p", str(p.tr_period), "-b", p.submode, "-f", str(int(round(p.audio_freq))),
               "-F", str(freq_tol), "-d", str(depth), "-L", "200",
               "-H", str(int(max(4007, p.top_freq + 100))), "-a", tmp, "-t", tmp, *args, wav]
        r = subprocess.run(cmd, capture_output=True, text=True, cwd=tmp)
        if r.returncode != 0 and "libboost_system" in r.stderr:
            r = subprocess.run(cmd, capture_output=True, text=True, cwd=tmp, env=_boost_stub_env(boost_stub_dir))
        if r.returncode != 0:
            raise RuntimeError(r.stderr or r.stdout)
    return [s for s in r.stdout.splitlines() if s.strip() and not s.startswith("<DecodeFinished>")]
