Skip to content

Instantly share code, notes, and snippets.

@lkarsten
Created July 9, 2017 12:09
Show Gist options
  • Select an option

  • Save lkarsten/f6ad5a12c8f21013a3cd256f59a518a7 to your computer and use it in GitHub Desktop.

Select an option

Save lkarsten/f6ad5a12c8f21013a3cd256f59a518a7 to your computer and use it in GitHub Desktop.

Revisions

  1. lkarsten created this gist Jul 9, 2017.
    40 changes: 40 additions & 0 deletions splitax.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #!/usr/bin/env python3
    """
    Split Airmar .ax files consisting of multiple SREC blocks into separate files,
    so srec_cat/objcopy can read them.
    Author: Lasse Karstensen <[email protected]>, July 2017.
    """
    from sys import argv
    from os.path import basename

    def split_ax(inputfile):
    data = open(inputfile).readlines()
    idx = []
    start = 0
    for i in range(0, len(data)):
    if data[i].startswith("S9"):
    idx.append((start, i+1))
    start = i+1

    if len(idx) == 0:
    print("ERROR: File %s has no SREC start marker. Incorrect file type?" % inputfile)
    return
    else:
    print("File %s has %i sections." % (inputfile, len(idx)))

    for i, ptr in enumerate(idx):
    start, end = ptr
    filename = "%s__part%i.srec" % (basename(inputfile).replace(".ax", ""), i)
    with open(filename, "w") as fp:
    for line in data[start:end]:
    fp.write(line)
    print("Wrote %s" % filename)

    if __name__ == "__main__":
    if len(argv) == 1:
    print("Usage: %s inputfile.ax <inputfile2.ax> <inputfileN.ax..>" % argv[0])
    exit(1)

    for inputfile in argv[1:]:
    split_ax(inputfile)