import sys
import struct
import numpy as np

try:
    # read from stdin
    frequency, = struct.unpack('f', sys.stdin.buffer.read(4))
    samplerate, = struct.unpack('i', sys.stdin.buffer.read(4))
    N, = struct.unpack('i', sys.stdin.buffer.read(4))
    taps = sys.stdin.buffer.read(N * 2 * 4)
    taps = np.frombuffer(taps, dtype=np.complex64)

    print(f"Writing new parameters to {sys.argv[1]}", file = sys.stderr)

    # write to tmp file
    with open(sys.argv[1], "bw") as e:
        e.write(np.array([frequency]).astype(np.float32).tobytes())
        e.write(np.array([samplerate]).astype(np.uint32).tobytes())
        e.write(np.array([len(taps)]).astype(np.uint32).tobytes())
        e.write(taps.astype(np.complex64).tobytes())

except:
    exit(1)
