#!/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 , 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 " % argv[0]) exit(1) for inputfile in argv[1:]: split_ax(inputfile)