def vectors_to_packets(data, tags, lengthtagname, vlen=1): lengthtags = [t for t in tags if pmt.pmt_symbol_to_string(t.key) == lengthtagname] lengths = {} for tag in lengthtags: if tag.offset in lengths: raise ValueError( "More than one tags with key {0} with the same offset={1}." .format(lengthtagname, tag.offset)) lengths[tag.offset] = pmt.pmt_to_long(tag.value)*vlen if 0 not in lengths: raise ValueError("There is no tag with key {0} and an offset of 0" .format(lengthtagname)) pos = 0 packets = [] while pos < len(data): if pos not in lengths: raise ValueError("There is no tag with key {0} and an offset of {1}." "We were expecting one." .format(lengthtagname, pos)) length = lengths[pos] if length == 0: raise ValueError("Packets cannot have zero length.") if pos+length > len(data): raise ValueError("The final packet is incomplete.") packets.append(data[pos: pos+length]) pos += length return packets
def handle_msg(self, msg): cell_id = pmt.pmt_to_long(msg) #print "generate pilot map: cell_id = " + str(cell_id) + "\tant_port = " + str(self.ant_port) Ncp = 1 # Always 1 for our purposes --> thus it's hard coded [rs_poss, rs_vals] = self.frame_pilot_value_and_position(self.N_rb_dl, cell_id, Ncp, self.ant_port) pmt_rs = self.rs_pos_to_pmt(rs_poss) pmt_vals = self.rs_val_to_pmt(rs_vals) pmt_pilots = pmt.pmt_list2(pmt_rs, pmt_vals) self.message_port_pub(self.msg_buf_out, pmt_pilots)
def print_tag(self, tag): my_string = "key = " + pmt.pmt_symbol_to_string(tag.key) + "\tsrcid = " + pmt.pmt_symbol_to_string(tag.srcid) my_string = my_string + "\tvalue = " + str(pmt.pmt_to_long(tag.value)) my_string = my_string + "\toffset = " + str(tag.offset) print my_string
def store_gps_offset(self, msg): self.gps_offset = pmt.pmt_to_long(msg)
def work(self, input_items, output_items): # g = open('./work.txt', 'ab') # g.write("FrameToFileSink start\n") # g.close nread = self.nitems_read(0) #number of items read on port 0 ninput_items = len(input_items[0]) input = np.array(input_items[0], dtype=np.int16) input.byteswap(True) #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread+ninput_items) frame_starts = [] frame_cnt = np.array([], dtype=np.uint32) threshold_events = [] for tag in tags: key_string = pmt.pmt_symbol_to_string(tag.key) if key_string == "Frame counter" : frame_start = tag.offset - nread if frame_start in frame_starts : continue frame_starts.append( frame_start ) # frame_cnt = np.append(frame_cnt, np.uint32(int(pmt.pmt_symbol_to_string(tag.value))) ) frame_cnt = np.append(frame_cnt, np.uint32(int(pmt.pmt_to_long(tag.value))) ) elif key_string == "Threshold event" : threshold_event = tag.offset - nread if threshold_event in threshold_events : continue threshold_events.append( threshold_event ) # f = open('./frame_temp.txt', 'ab') # f.write('BEGINBEGINBEGIN\n') # f.write('Threshold event len: %d\n'%len(threshold_events)) # for ii in xrange(len(threshold_events)): # f.write('Threshold event: %d\n'%threshold_events[ii]) # f.write('Frame start len: %d\n'%len(frame_starts)) # f.write('Frame cnt len: %d\n'%frame_cnt.size) # for ii in xrange(len(frame_starts)): # f.write('Frame start: %d, Frame cnt: %d\n'%(frame_starts[ii] , frame_cnt[ii])) # f.write('ENDENDENDEND\n\n') # f.close() ##################################### # Write data to file beginning = 0 while 1 : if len(threshold_events) == 0 : ending = ninput_items else : ending = threshold_events[0] threshold_events.pop(0) #LINUX compatibility # threshold_events = threshold_events[1:] if beginning != ending : if self.f == None : self.f = open('./collect_%d.bin'%self.file_counter, 'wb') self.file_counter += 1 start_idx = beginning while 1 : if frame_cnt.size == 0 or frame_starts[0] >= ending : input[start_idx:ending].tofile(self.f) break input[start_idx:frame_starts[0]].tofile(self.f) self.FrameConf.START_OF_FRAME.tofile(self.f) self.FrameConf.DATA_FRAME_ID.tofile(self.f) frame_cnt[0].newbyteorder('B').tofile(self.f) start_idx = frame_starts[0] frame_cnt = np.delete(frame_cnt, [0]) frame_starts.pop(0) #LINUX compatibility # frame_cnt = frame_cnt[1:] # frame_starts = frame_starts[1:] beginning = ending if beginning >= ninput_items : break if self.f is not None: self.f.close() self.f = None # g = open('./work.txt', 'ab') # g.write("FrameToFileSink stop\n") # g.close return ninput_items