def packets_gen(data, chaninfos, packet_filter): i = 0 while data: pheader_len, pheader = unwrap(data, PACKET_HEADER) type = header_type(pheader) if packet_filter == '*' or type == packet_filter.lower(): if type == 'sonar': sheader_len, sheader = unwrap(data[pheader_len:], SONAR_HEADER, 'XTFPINGHEADER') assert pheader_len + sheader_len == 256 assert pheader['num_chans_to_follow'] <= 6 if pheader['num_chans_to_follow'] > 1: raise NotImplementedError('Multiple channels in a packet') cheader_len, cheader = unwrap(data[pheader_len + sheader_len:], SONAR_CHANNEL_HEADER, 'XTFPINGCHANHEADER') assert cheader_len == 64 dstart = (pheader_len + sheader_len + cheader_len * pheader['num_chans_to_follow']) n = cheader['num_samples'] s = chaninfos[cheader['channel_number']]['bytes_per_sample'] raw_trace = data[dstart:dstart+s*n].tobytes() trace = np.frombuffer(raw_trace, {1: np.int8, 2: np.int16}[s]) yield SonarPacket(pheader, sheader, cheader, trace, raw_trace) #elif type == 'notes': # nheader_len, nheader = unwrap(data[pheader_len:], # """H year # B month # B day # B hour # B minute # B second # 35s reserved # 200s notes_text # """) # pprint(nheader) # assert nheader_len + pheader_len == \ # pheader['num_bytes_this_record'] else: yield Packet(pheader, data[:pheader['num_bytes_this_record']]) sys.stdout.write('Packet: %d\r' % i) # only the last message is shown without flush(), but flushing too # often slows things down if i % 42 == 0: sys.stdout.flush() data = data[pheader['num_bytes_this_record']:] i += 1 sys.stdout.write('\n')
def read_XTF(infile, packet_filter): file_data = memoryview(open(infile, 'rb').read()) header_len, header = unwrap(file_data, HEADER, data_factory = OrderedDict) nchannels = (header['number_of_sonar_channels'] + header['number_of_bathymetry_channels']) assert nchannels <= 6 chaninfos = [] for i in range(nchannels): chaninfo_len, chaninfo = unwrap(file_data[header_len+i*CHANINFO_LEN:], CHANINFO) assert chaninfo_len == CHANINFO_LEN chaninfos.append(chaninfo) pstart = HEADER_LEN return header, chaninfos, packets_gen(file_data[pstart:], chaninfos, packet_filter)