def test_pdu(self): cs = starcoder.command_source() snk = blocks.message_debug() self.tb.msg_connect((cs, 'out'), (snk, 'store')) msg = starcoder_pb2.BlockMessage() md = msg.pair_value.car.dict_value.entry.add() md.key.symbol_value = 'metadata_1' md.value.integer_value = -2 md = msg.pair_value.car.dict_value.entry.add() md.key.symbol_value = 'metadata_2' md.value.symbol_value = 'val' msg.pair_value.cdr.uniform_vector_value.f64_value.value.extend( [2.4, -12.3, 21.2]) pmt_dict = pmt.make_dict() pmt_dict = pmt.dict_add(pmt_dict, pmt.intern('metadata_1'), pmt.from_long(-2)) pmt_dict = pmt.dict_add(pmt_dict, pmt.intern('metadata_2'), pmt.intern('val')) expected = pmt.cons(pmt_dict, pmt.init_f64vector(3, [2.4, -12.3, 21.2])) self.tb.start() cs.push(msg.SerializeToString()) time.sleep(0.1) self.tb.stop() self.tb.wait() self.assertEqual(snk.num_messages(), 1) self.assertTrue(pmt.is_dict(snk.get_message(0))) self.assertTrue(pmt.equal(snk.get_message(0), expected))
def msg_handler(self, m): if not pmt.is_pair(m): return meta = pmt.car(m) if not pmt.is_dict(meta): return blockstart = pmt.to_long( pmt.dict_ref(meta, pmt.intern('blockstart'), pmt.from_long(-1024))) blockend = pmt.to_long( pmt.dict_ref(meta, pmt.intern('blockend'), pmt.from_long(-1024))) if blockstart == -1024 or blockend == -1024: return rel_cfreq = pmt.to_double( pmt.dict_ref(meta, pmt.intern('rel_cfreq'), pmt.from_double(-1.0))) rel_bw = pmt.to_double( pmt.dict_ref(meta, pmt.intern('rel_bw'), pmt.from_double(-1.0))) if rel_cfreq < 0.0 or rel_bw < 0.0: return blockleft = int(self.normwidth * (rel_cfreq - rel_bw / 2.0)) blockright = int( numpy.ceil(self.normwidth * (rel_cfreq + rel_bw / 2.0))) #print('new msg: {} {} {} {} {} {}'.format(blockstart, blockend, rel_cfreq, rel_bw, blockleft, blockright)) self.msg_puffer += [(blockstart, blockend, blockleft, blockright)]
def handle_ctrl_msg(self, msg): if pmt.is_dict(): m = pmt.pmt_to_python.pmt_to_dict(msg) if m.has_key("cmd") and m["cmd"] == "set_audio_freq": #print(m) #print(self.last_station_indices) freq_index = self.freq_to_index(m["freq"]) if m["chan"] == "left" and freq_index < self.fft_len - 5: if self.last_station_indices[0] == freq_index: self.index_fixed[0] = False print("decoder 0 free") else: self.last_station_indices[0] = freq_index self.index_fixed[0] = True print("decoder 0 fixed to %i" % m["freq"]) if m["chan"] == "right" and freq_index < self.fft_len - 5: if self.last_station_indices[1] == freq_index: self.index_fixed[1] = False print("decoder 1 free") else: self.last_station_indices[1] = freq_index self.index_fixed[1] = True print("decoder 1 fixed to %i" % m["freq"]) #print(self.last_station_indices) if m.has_key("cmd") and m["cmd"] == "switch mode": self.searchMode = not self.searchMode print("searchMode: %s" % self.searchMode)
def pdu_arg_add(pdu, k, v): meta = pmt.car(pdu); data = pmt.cdr(pdu); if(pmt.is_null(meta)): meta = pmt.make_dict(); assert(pmt.is_dict(meta)); meta = pmt.dict_add(meta, k, v); return pmt.cons(meta,data);
def pdu_arg_add(pdu, k, v): meta = pmt.car(pdu) data = pmt.cdr(pdu) if (pmt.is_null(meta)): meta = pmt.make_dict() assert (pmt.is_dict(meta)) meta = pmt.dict_add(meta, k, v) return pmt.cons(meta, data)
def monitor_msg(self, msg): if not pmt.is_dict(msg): raise TypeError('monitor msg should be type pmt dict') info = pmt.to_python(msg) for key, value in info.items(): print >> sys.stderr, key, ':', value print >> sys.stderr, '\n'
def handler(self, msg): # get input meta = pmt.car(msg) samples = pmt.cdr(msg) # ensure we have a dictionary assert (pmt.is_dict(meta)) #print pmt.to_python(pmt.dict_keys(meta)); # get the value from dict and publish it val = pmt.dict_ref(meta, self.key, pmt.PMT_NIL) self.message_port_pub(self.outport, val)
def handler(self, msg): # get input meta = pmt.car(msg); samples = pmt.cdr(msg); # ensure we have a dictionary assert(pmt.is_dict(meta)) #print pmt.to_python(pmt.dict_keys(meta)); # get the value from dict and publish it val = pmt.dict_ref(meta, self.key, pmt.PMT_NIL) self.message_port_pub(self.outport, val);
def msg_handler(self, msg): if not pmt.is_pair(msg) or pmt.is_dict(msg) or pmt.is_pdu(msg): gr.log.warn("Input message %s is not a simple pair, dropping" % repr(msg)) return new_val = pmt.to_python(pmt.cdr(msg)) try: self.callback(new_val) except Exception as e: gr.log.error("Error when calling " + repr(self.callback.name()) + " with " + repr(new_val) + " (reason: %s)" % repr(e))
def is_u8_pdu(msg): """Checks if a PMT is a uint8 PDU""" if not pmt.is_pair(msg): return False if not pmt.is_dict(pmt.car(msg)): return False if not pmt.is_u8vector(pmt.cdr(msg)): return False return True
def msg_handler(self, msg): if not pmt.is_pair(msg): return if not pmt.is_dict(pmt.car(msg)): return if not pmt.is_uniform_vector(pmt.cdr(msg)): return arr = pmt.to_python(msg)[1] pmt.set_cdr(msg, pmt.to_pmt(arr[self.start_trim_length:-self.end_trim_length or len(arr)])) self.message_port_pub(pmt.intern("out"), msg)
def handle_input(self, pdu): #self.setSortingEnabled(False) # we expect a pdu if not pmt.is_pair(pdu): print("Message is not a PDU") return meta = pmt.car(pdu) if not pmt.is_dict(meta): print("No meta field present") return meta_dict = pmt.to_python(meta) if(not type(meta_dict) == type({})): return # for now, we insist on having the row_id pmt within the meta field if meta_dict.has_key(self.row_id): # get the current row identifier id_value = meta_dict[self.row_id] cur_idx = self.rowcount create_new_row = id_value not in self.ids.keys() if create_new_row: #print("Creating new Table Entry with "+str(id_value)) tab_item = QtGui.QTableWidgetItem(str(id_value)) tab_item.setData(QtCore.Qt.EditRole, id_value) tab_item.setBackground(QtGui.QColor(225,225,225)) self.setRowCount(self.rowcount + 1) self.setItem(self.rowcount, 0, tab_item) self.ids[id_value] = tab_item else: #print("Updating Table Entry " + str(id_value)) # if row id already exists, get and use the respective row idx cur_idx = self.ids[id_value].row() for col, idx in self.column_dict.iteritems(): if meta_dict.has_key(col) and col is not self.row_id: value = meta_dict[col] # for now, we wont allow meta field entrys other than the specified columns tab_item = QtGui.QTableWidgetItem(str(value)) tab_item.setData(QtCore.Qt.EditRole, value) self.setItem(cur_idx, idx, tab_item) if create_new_row: self.rowcount += 1 self.setRowCount(self.rowcount) if self.scroll_to_bottom: self.updateTrigger.emit() else: print("Meta Field "+self.row_id+" not found.")
def test19(self): max_key = pmt.intern("MAX") _max = pmt.from_long(self.MAXINT32) min_key = pmt.intern("MIN") _min = pmt.from_long(self.MININT32) d = pmt.make_dict() d = pmt.dict_add(d, max_key, _max) d = pmt.dict_add(d, min_key, _min) s = pmt.serialize_str(d) deser = pmt.deserialize_str(s) self.assertTrue(pmt.is_dict(deser)) self.assertTrue(pmt.equal(d, deser)) p_dict = pmt.to_python(deser) self.assertEqual(self.MAXINT32, p_dict["MAX"]) self.assertEqual(self.MININT32, p_dict["MIN"])
def parse_extra_dict(p, info, VERBOSE=False): if(pmt.is_dict(p) is False): sys.stderr.write("Extra header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) items = pmt.dict_items(p) nitems = pmt.length(items) for i in range(nitems): item = pmt.nth(i, items) key = pmt.symbol_to_string(pmt.car(item)) val = pmt.cdr(item) info[key] = val if(VERBOSE): print("{0}: {1}".format(key, val)) return info
def parse_extra_dict(p, info, VERBOSE=False): if(pmt.is_dict(p) is False): sys.stderr.write("Extra header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) items = pmt.dict_items(p) nitems = pmt.length(items) for i in xrange(nitems): item = pmt.nth(i, items) key = pmt.symbol_to_string(pmt.car(item)) val = pmt.cdr(item) info[key] = val if(VERBOSE): print "{0}: {1}".format(key, val) return info
def handle_control(self, msg): # Test whether message is a dict, per UHD requirements if not pmt.is_dict(msg): self.logger.info( "{} received non-dict control message, ignoring".format( self.uuid_str)) return try: keys = pmt.to_python(pmt.dict_keys(msg)) except pmt.wrong_type as e: self.logger.debug( "{} received pair instead of dict, fixing".format( self.uuid_str)) msg = pmt.dict_add(pmt.make_dict(), pmt.car(msg), pmt.cdr(msg)) keys = pmt.to_python(pmt.dict_keys(msg)) self.logger.debug( "echo agent received command with keys: {}".format(keys)) self.handle_control_mod(keys) self.handle_control_demod(keys)
def handle_input(self, pdu): # Expect a pdu if not pmt.is_pair(pdu): print("Message is not a PDU") return meta = pmt.car(pdu) if not pmt.is_dict(meta): print("No meta field present") return meta_dict = pmt.to_python(meta) if not isinstance(meta_dict, dict): return self.setRowCount(self.rowcount + 1) # get the current row identifier for col, idx in self.column_dict.items(): if col == self.body_label: msg_pmt = pmt.cdr(pdu) msg_bytes = bytes(pmt.u8vector_elements(msg_pmt)) value = msg_bytes.decode("utf8", errors="replace") display = str(value) elif col in ("freq", "frequency"): value = meta_dict.get(col) display = eng_notation.num_to_str(value) + "Hz" elif col == "rx_time": value = meta_dict.get(col)[0] display = str(datetime.fromtimestamp(value)) else: value = meta_dict.get(col) display = str(value) tab_item = QtWidgets.QTableWidgetItem(display) self.setItem(self.rowcount, idx, tab_item) self.rowcount += 1 if self.scroll_to_bottom: self.updateTrigger.emit()
def handle_control(self, msg): # Test whether message is a dict, per UHD requirements if not pmt.is_dict(msg): self.logger.info( "{} received non-dict control message, ignoring".format( self.uuid_str)) return try: keys = pmt.to_python(pmt.dict_keys(msg)) except pmt.wrong_type as e: self.logger.debug( "{} received pair instead of dict, fixing".format( self.uuid_str)) msg = pmt.dict_add(pmt.make_dict(), pmt.car(msg), pmt.cdr(msg)) keys = pmt.to_python(pmt.dict_keys(msg)) print("KEYS: {}".format(keys)) # Check for keys we care about if "freeze" in keys: self.run_mode = "freeze" self.logger.info( "neural demod {}: freezing model and saving state".format( self.uuid_str)) torch.save(self.model.state_dict(), "demod_neural_weights-{}".format(self.uuid_str)) print("Saving final demod constellation @{}".format( self.packet_cnt)) data_vis = gen_demod_grid(points_per_dim=40, min_val=-2.5, max_val=2.5)['data'] labels_si_g = self.demodulate(data_c=data_vis, mode='exploit') numpy.savez("neural_demod_constellation_{:05d}_{}".format( self.packet_cnt, time.strftime('%Y%m%d_%H%M%S')), iq=data_vis, labels=labels_si_g) elif "train" in keys: self.run_mode = "train" self.logger.info("neural demod {}: resuming training".format( self.uuid_str))
def test_dict(self): cs = starcoder.command_source() snk = blocks.message_debug() self.tb.msg_connect((cs, 'out'), (snk, 'store')) msg = starcoder_pb2.BlockMessage() pl = msg.dict_value.entry.add() pl.key.symbol_value = 'key' pl.value.double_value = -2.3 expected = pmt.make_dict() expected = pmt.dict_add(expected, pmt.intern('key'), pmt.from_double(-2.3)) self.tb.start() cs.push(msg.SerializeToString()) time.sleep(0.1) self.tb.stop() self.tb.wait() self.assertEqual(snk.num_messages(), 1) self.assertTrue(pmt.is_dict(snk.get_message(0))) self.assertTrue(pmt.equal(snk.get_message(0), expected))
def handle_control(self, msg): # Test whether message is a dict, per UHD requirements if not pmt.is_dict(msg): self.logger.info("{} received non-dict control message, ignoring".format(self.uuid_str)) return try: keys = pmt.to_python(pmt.dict_keys(msg)) except pmt.wrong_type as e: self.logger.debug("{} received pair instead of dict, fixing".format(self.uuid_str)) msg = pmt.dict_add(pmt.make_dict(), pmt.car(msg), pmt.cdr(msg)) keys = pmt.to_python(pmt.dict_keys(msg)) # Check for keys we care about if "freeze" in keys: self.run_mode = "freeze" self.logger.info("neural mod {}: freezing model and saving state".format(self.uuid_str)) torch.save(self.model.state_dict(), "mod_neural_weights-{}".format(self.uuid_str)) print("Saving final mod constellation @{}".format(self.train_cnt)) np.save("neural_mod_constellation_{:05d}_{}".format(self.train_cnt, time.strftime('%Y%m%d_%H%M%S')), get_constellation(self)) elif "train" in keys: self.logger.info("neural mod {}: resuming training".format(self.uuid_str)) self.run_mode = "train"
def parse_header(p, VERBOSE=False): dump = pmt.PMT_NIL info = dict() if(pmt.is_dict(p) is False): sys.stderr.write("Header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) # GET FILE FORMAT VERSION NUMBER if(pmt.dict_has_key(p, pmt.string_to_symbol("version"))): r = pmt.dict_ref(p, pmt.string_to_symbol("version"), dump) version = pmt.to_long(r) if(VERBOSE): print "Version Number: {0}".format(version) else: sys.stderr.write("Could not find key 'version': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SAMPLE RATE if(pmt.dict_has_key(p, pmt.string_to_symbol("rx_rate"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_rate"), dump) samp_rate = pmt.to_double(r) info["rx_rate"] = samp_rate if(VERBOSE): print "Sample Rate: " + eng_notation.num_to_str(samp_rate) + "SPS" else: sys.stderr.write("Could not find key 'sr': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT TIME STAMP if(pmt.dict_has_key(p, pmt.string_to_symbol("rx_time"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_time"), dump) secs = pmt.tuple_ref(r, 0) fracs = pmt.tuple_ref(r, 1) secs = float(pmt.to_uint64(secs)) fracs = pmt.to_double(fracs) t = secs + fracs info["rx_time"] = t if(VERBOSE): time = datetime.fromtimestamp(t).strftime('%m/%d/%Y %H:%M:%S') print "Timestamp (Unix Epoch): " + time print "Integer Seconds: " + repr(secs) print "Fractional Seconds: " + repr(fracs) #print "Linux Epoch: {0:.6f}".format(t) + " Seconds" else: sys.stderr.write("Could not find key 'time': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT ITEM SIZE if(pmt.dict_has_key(p, pmt.string_to_symbol("size"))): r = pmt.dict_ref(p, pmt.string_to_symbol("size"), dump) dsize = pmt.to_long(r) info["size"] = dsize if(VERBOSE): print "Item Size: " + eng_notation.num_to_str(dsize) + " Bytes" else: sys.stderr.write("Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT DATA TYPE if(pmt.dict_has_key(p, pmt.string_to_symbol("type"))): r = pmt.dict_ref(p, pmt.string_to_symbol("type"), dump) dtype = pmt.to_long(r) stype = ftype_to_string[dtype] info["type"] = stype if(VERBOSE): print "Data Type: {0} ({1})".format(stype, dtype) else: sys.stderr.write("Could not find key 'type': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT COMPLEX if(pmt.dict_has_key(p, pmt.string_to_symbol("cplx"))): r = pmt.dict_ref(p, pmt.string_to_symbol("cplx"), dump) #global cplx cplx = pmt.to_bool(r) info["cplx"] = cplx if(VERBOSE): print "Complex? {0}".format(cplx) global vecchk global tsize #print cplx #print dtype #print dsize if(cplx==False): if(dtype==0): tsize=1 elif(dtype==1): tsize=4 elif(dtype==2): tsize=4 elif(dtype==3): tsize=4 elif(dtype==5): tsize=4 elif(dtype==6): tsize=8 else: tsize=64 #print tsize vecchk = dsize/tsize #print vecchk if(vecchk>1): print "The data is a vector containing {0} elements.".format(vecchk) else: print "The data is not a vector." '''else: sys.stderr.write("Could not find key 'cplx': invalid or corrupt data file.\n") sys.exit(1) ''' # EXTRACT WHERE CURRENT SEGMENT STARTS if(pmt.dict_has_key(p, pmt.string_to_symbol("strt"))): r = pmt.dict_ref(p, pmt.string_to_symbol("strt"), dump) seg_start = pmt.to_uint64(r) info["hdr_len"] = seg_start info["extra_len"] = seg_start - HEADER_LENGTH info["has_extra"] = info["extra_len"] > 0 if(VERBOSE): print "Header Length: {0} bytes".format(info["hdr_len"]) print "Extra Length: {0}".format((info["extra_len"])) print "Extra Header? {0}".format(info["has_extra"]) else: sys.stderr.write("Could not find key 'strt': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SIZE OF DATA if(pmt.dict_has_key(p, pmt.string_to_symbol("bytes"))): r = pmt.dict_ref(p, pmt.string_to_symbol("bytes"), dump) nbytes = pmt.to_uint64(r) nitems = nbytes/dsize info["nitems"] = nitems info["nbytes"] = nbytes #info["types"] = types if(VERBOSE): #print "Size of Data: {0:2.1e} bytes".format(nbytes) print "Segment Size (bytes): " + eng_notation.num_to_str(nbytes) #print " {0:2.1e} items".format(nitems) print "Segment Size (items): " + eng_notation.num_to_str(nitems) else: sys.stderr.write("Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) return info
def change_sign(self, msg): if pmt.is_dict(msg): d = pmt.to_python(msg) for key, value in d.items(): d[key] *= -1 self.message_port_pub(pmt.intern("dict_out"), pmt.to_pmt(d))
def parse_header(p, VERBOSE=False): dump = pmt.PMT_NIL info = dict() if (pmt.is_dict(p) is False): sys.stderr.write( "Header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) # GET FILE FORMAT VERSION NUMBER if (pmt.dict_has_key(p, pmt.string_to_symbol("version"))): r = pmt.dict_ref(p, pmt.string_to_symbol("version"), dump) version = pmt.to_long(r) if (VERBOSE): print("Version Number: {0}".format(version)) else: sys.stderr.write( "Could not find key 'version': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SAMPLE RATE if (pmt.dict_has_key(p, pmt.string_to_symbol("rx_rate"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_rate"), dump) samp_rate = pmt.to_double(r) info["rx_rate"] = samp_rate if (VERBOSE): print("Sample Rate: {0:.2f} sps".format(samp_rate)) else: sys.stderr.write( "Could not find key 'sr': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT TIME STAMP if (pmt.dict_has_key(p, pmt.string_to_symbol("rx_time"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_time"), dump) secs = pmt.tuple_ref(r, 0) fracs = pmt.tuple_ref(r, 1) secs = float(pmt.to_uint64(secs)) fracs = pmt.to_double(fracs) t = secs + fracs info["rx_time"] = t if (VERBOSE): print("Seconds: {0:.6f}".format(t)) else: sys.stderr.write( "Could not find key 'time': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT ITEM SIZE if (pmt.dict_has_key(p, pmt.string_to_symbol("size"))): r = pmt.dict_ref(p, pmt.string_to_symbol("size"), dump) dsize = pmt.to_long(r) info["size"] = dsize if (VERBOSE): print("Item size: {0}".format(dsize)) else: sys.stderr.write( "Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT DATA TYPE if (pmt.dict_has_key(p, pmt.string_to_symbol("type"))): r = pmt.dict_ref(p, pmt.string_to_symbol("type"), dump) dtype = pmt.to_long(r) stype = ftype_to_string[dtype] info["type"] = stype if (VERBOSE): print("Data Type: {0} ({1})".format(stype, dtype)) else: sys.stderr.write( "Could not find key 'type': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT COMPLEX if (pmt.dict_has_key(p, pmt.string_to_symbol("cplx"))): r = pmt.dict_ref(p, pmt.string_to_symbol("cplx"), dump) cplx = pmt.to_bool(r) info["cplx"] = cplx if (VERBOSE): print("Complex? {0}".format(cplx)) else: sys.stderr.write( "Could not find key 'cplx': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT WHERE CURRENT SEGMENT STARTS if (pmt.dict_has_key(p, pmt.string_to_symbol("strt"))): r = pmt.dict_ref(p, pmt.string_to_symbol("strt"), dump) seg_start = pmt.to_uint64(r) info["hdr_len"] = seg_start info["extra_len"] = seg_start - HEADER_LENGTH info["has_extra"] = info["extra_len"] > 0 if (VERBOSE): print("Header Length: {0} bytes".format(info["hdr_len"])) print("Extra Length: {0}".format((info["extra_len"]))) print("Extra Header? {0}".format(info["has_extra"])) else: sys.stderr.write( "Could not find key 'strt': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SIZE OF DATA if (pmt.dict_has_key(p, pmt.string_to_symbol("bytes"))): r = pmt.dict_ref(p, pmt.string_to_symbol("bytes"), dump) nbytes = pmt.to_uint64(r) nitems = nbytes / dsize info["nitems"] = nitems info["nbytes"] = nbytes if (VERBOSE): print("Size of Data: {0} bytes".format(nbytes)) print(" {0} items".format(nitems)) else: sys.stderr.write( "Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) return info
def message_handler(self, msg): if not pmt.is_dict(msg): return try: # this will fail if message is a PDU with non-PMT_NIL arguments n = pmt.length(pmt.dict_items(msg)) # a PDU with one element equal to PMT_NIL still looks like a # dictionary...grrrrr! if (n == 1) and (pmt.equal(pmt.car(msg), pmt.PMT_NIL) or pmt.equal(pmt.cdr(msg), pmt.PMT_NIL)): # treat as a pdu car = pmt.car(msg) cdr = pmt.cdr(msg) else: car = msg cdr = pmt.init_u8vector(0, []) except: try: # message is a pdu pmt.length(pmt.dict_items(pmt.car(msg))) car = pmt.car(msg) cdr = pmt.cdr(msg) except: return if self.find_metadata: keys = pmt.dict_keys(car) self.header = [(pmt.nth(i, keys), pmt.symbol_to_string) for i in range(pmt.length(keys))] header = ','.join([ pmt.symbol_to_string(pmt.nth(i, keys)) for i in range(pmt.length(keys)) ]) if self.fid: self.fid.write(header + '\n') # ensure we no longer search for metadata self.find_metadata = False if self.fid: # add metadata if self.add_metadata: self.print_metadata(car) # cdr must be a uniform vector type if not pmt.is_uniform_vector(cdr): self.fid.write('\n') return # add data values = self.data_type_mappings[self.data_type](cdr) if (self.precision > 0) and (self.data_type in [ 'float', 'double', 'complex float', 'complex double' ]): self.fid.write(','.join( ['{:.{n}f}'.format(i, n=self.precision) for i in values])) else: self.fid.write(','.join([str(i) for i in values])) self.fid.write('\n')
def handler(self, msg): if not self.archive: # no need to waste any time return if not pmt.is_dict(msg): return try: # this will fail if message is a PDU with non-PMT_NIL arguments n = pmt.length(pmt.dict_items(msg)) # a PDU with one element equal to PMT_NIL still looks like a # dictionary...grrrrr! if (n == 1) and (pmt.equal(pmt.car(msg),pmt.PMT_NIL) or \ pmt.equal(pmt.cdr(msg),pmt.PMT_NIL)): # treat as a pdu meta = pmt.car(msg) else: # it's a dictionary meta = msg except: try: # message is a pdu pmt.length(pmt.dict_items(pmt.car(msg))) meta = pmt.car(msg) except: return # extract file components try: fname = pmt.dict_ref(meta, self.filename_tag, pmt.PMT_NIL) file_time = pmt.dict_ref(meta, self.time_tag, pmt.PMT_NIL) freq = pmt.dict_ref(meta, self.freq_tag, pmt.PMT_NIL) rate = pmt.dict_ref(meta, self.rate_tag, pmt.PMT_NIL) if pmt.equal(fname, pmt.PMT_NIL): self.log.warn("No file specified") return f = pmt.symbol_to_string(fname) if self.fname_format == "": # copy immediately self.copy_file( f, os.path.join(self.output_path, os.path.basename(f))) else: base_fname = copy.deepcopy(self.fname_format) # add frequency information to file name if not pmt.equal(freq, pmt.PMT_NIL): freq = pmt.to_double(freq) for freq_spec in self.freq_specs: base_fname = base_fname.replace( freq_spec[0], '%0.0f' % int(freq / freq_spec[1])) if not pmt.equal(rate, pmt.PMT_NIL): rate = pmt.to_double(rate) for rate_spec in self.rate_specs: base_fname = base_fname.replace( rate_spec[0], '%0.0f' % int(rate / rate_spec[1])) # time update if not pmt.equal(file_time, pmt.PMT_NIL): t = pmt.to_uint64(pmt.tuple_ref(file_time,0)) + \ pmt.to_double(pmt.tuple_ref(file_time,1)) base_fname = datetime.datetime.utcfromtimestamp( t).strftime(base_fname) # archive file self.copy_file(f, os.path.join(self.output_path, base_fname)) except Exception as e: self.log.error("Unable to process message:{}".format(e))
def parse_header(p, VERBOSE=False): dump = pmt.PMT_NIL info = dict() if (pmt.is_dict(p) is False): sys.stderr.write( "Header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) # GET FILE FORMAT VERSION NUMBER if (pmt.dict_has_key(p, pmt.string_to_symbol("version"))): r = pmt.dict_ref(p, pmt.string_to_symbol("version"), dump) version = pmt.to_long(r) if (VERBOSE): print "Version Number: {0}".format(version) else: sys.stderr.write( "Could not find key 'version': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SAMPLE RATE if (pmt.dict_has_key(p, pmt.string_to_symbol("rx_rate"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_rate"), dump) samp_rate = pmt.to_double(r) info["rx_rate"] = samp_rate if (VERBOSE): print "Sample Rate: " + eng_notation.num_to_str(samp_rate) + "SPS" else: sys.stderr.write( "Could not find key 'sr': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT TIME STAMP if (pmt.dict_has_key(p, pmt.string_to_symbol("rx_time"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_time"), dump) secs = pmt.tuple_ref(r, 0) fracs = pmt.tuple_ref(r, 1) secs = float(pmt.to_uint64(secs)) fracs = pmt.to_double(fracs) t = secs + fracs info["rx_time"] = t if (VERBOSE): time = datetime.fromtimestamp(t).strftime('%m/%d/%Y %H:%M:%S') print "Timestamp (Unix Epoch): " + time print "Integer Seconds: " + repr(secs) print "Fractional Seconds: " + repr(fracs) #print "Linux Epoch: {0:.6f}".format(t) + " Seconds" else: sys.stderr.write( "Could not find key 'time': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT ITEM SIZE if (pmt.dict_has_key(p, pmt.string_to_symbol("size"))): r = pmt.dict_ref(p, pmt.string_to_symbol("size"), dump) dsize = pmt.to_long(r) info["size"] = dsize if (VERBOSE): print "Item Size: " + eng_notation.num_to_str(dsize) + " Bytes" else: sys.stderr.write( "Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT DATA TYPE if (pmt.dict_has_key(p, pmt.string_to_symbol("type"))): r = pmt.dict_ref(p, pmt.string_to_symbol("type"), dump) dtype = pmt.to_long(r) stype = ftype_to_string[dtype] info["type"] = stype if (VERBOSE): print "Data Type: {0} ({1})".format(stype, dtype) else: sys.stderr.write( "Could not find key 'type': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT COMPLEX if (pmt.dict_has_key(p, pmt.string_to_symbol("cplx"))): r = pmt.dict_ref(p, pmt.string_to_symbol("cplx"), dump) #global cplx cplx = pmt.to_bool(r) info["cplx"] = cplx if (VERBOSE): print "Complex? {0}".format(cplx) global vecchk global tsize #print cplx #print dtype #print dsize if (cplx == False): if (dtype == 0): tsize = 1 elif (dtype == 1): tsize = 4 elif (dtype == 2): tsize = 4 elif (dtype == 3): tsize = 4 elif (dtype == 5): tsize = 4 elif (dtype == 6): tsize = 8 else: tsize = 64 #print tsize vecchk = dsize / tsize #print vecchk if (vecchk > 1): print "The data is a vector containing {0} elements.".format( vecchk) else: print "The data is not a vector." '''else: sys.stderr.write("Could not find key 'cplx': invalid or corrupt data file.\n") sys.exit(1) ''' # EXTRACT WHERE CURRENT SEGMENT STARTS if (pmt.dict_has_key(p, pmt.string_to_symbol("strt"))): r = pmt.dict_ref(p, pmt.string_to_symbol("strt"), dump) seg_start = pmt.to_uint64(r) info["hdr_len"] = seg_start info["extra_len"] = seg_start - HEADER_LENGTH info["has_extra"] = info["extra_len"] > 0 if (VERBOSE): print "Header Length: {0} bytes".format(info["hdr_len"]) print "Extra Length: {0}".format((info["extra_len"])) print "Extra Header? {0}".format(info["has_extra"]) else: sys.stderr.write( "Could not find key 'strt': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SIZE OF DATA if (pmt.dict_has_key(p, pmt.string_to_symbol("bytes"))): r = pmt.dict_ref(p, pmt.string_to_symbol("bytes"), dump) nbytes = pmt.to_uint64(r) nitems = nbytes / dsize info["nitems"] = nitems info["nbytes"] = nbytes #info["types"] = types if (VERBOSE): #print "Size of Data: {0:2.1e} bytes".format(nbytes) print "Segment Size (bytes): " + eng_notation.num_to_str(nbytes) #print " {0:2.1e} items".format(nitems) print "Segment Size (items): " + eng_notation.num_to_str(nitems) else: sys.stderr.write( "Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) return info
def parse_header(p, VERBOSE=False): dump = pmt.PMT_NIL info = dict() if(pmt.is_dict(p) is False): sys.stderr.write("Header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) # GET FILE FORMAT VERSION NUMBER if(pmt.dict_has_key(p, pmt.string_to_symbol("version"))): r = pmt.dict_ref(p, pmt.string_to_symbol("version"), dump) version = pmt.to_long(r) if(VERBOSE): print("Version Number: {0}".format(version)) else: sys.stderr.write("Could not find key 'version': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SAMPLE RATE if(pmt.dict_has_key(p, pmt.string_to_symbol("rx_rate"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_rate"), dump) samp_rate = pmt.to_double(r) info["rx_rate"] = samp_rate if(VERBOSE): print("Sample Rate: {0:.2f} sps".format(samp_rate)) else: sys.stderr.write("Could not find key 'sr': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT TIME STAMP if(pmt.dict_has_key(p, pmt.string_to_symbol("rx_time"))): r = pmt.dict_ref(p, pmt.string_to_symbol("rx_time"), dump) secs = pmt.tuple_ref(r, 0) fracs = pmt.tuple_ref(r, 1) secs = float(pmt.to_uint64(secs)) fracs = pmt.to_double(fracs) t = secs + fracs info["rx_time"] = t if(VERBOSE): print("Seconds: {0:.6f}".format(t)) else: sys.stderr.write("Could not find key 'time': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT ITEM SIZE if(pmt.dict_has_key(p, pmt.string_to_symbol("size"))): r = pmt.dict_ref(p, pmt.string_to_symbol("size"), dump) dsize = pmt.to_long(r) info["size"] = dsize if(VERBOSE): print("Item size: {0}".format(dsize)) else: sys.stderr.write("Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT DATA TYPE if(pmt.dict_has_key(p, pmt.string_to_symbol("type"))): r = pmt.dict_ref(p, pmt.string_to_symbol("type"), dump) dtype = pmt.to_long(r) stype = ftype_to_string[dtype] info["type"] = stype if(VERBOSE): print("Data Type: {0} ({1})".format(stype, dtype)) else: sys.stderr.write("Could not find key 'type': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT COMPLEX if(pmt.dict_has_key(p, pmt.string_to_symbol("cplx"))): r = pmt.dict_ref(p, pmt.string_to_symbol("cplx"), dump) cplx = pmt.to_bool(r) info["cplx"] = cplx if(VERBOSE): print("Complex? {0}".format(cplx)) else: sys.stderr.write("Could not find key 'cplx': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT WHERE CURRENT SEGMENT STARTS if(pmt.dict_has_key(p, pmt.string_to_symbol("strt"))): r = pmt.dict_ref(p, pmt.string_to_symbol("strt"), dump) seg_start = pmt.to_uint64(r) info["hdr_len"] = seg_start info["extra_len"] = seg_start - HEADER_LENGTH info["has_extra"] = info["extra_len"] > 0 if(VERBOSE): print("Header Length: {0} bytes".format(info["hdr_len"])) print("Extra Length: {0}".format((info["extra_len"]))) print("Extra Header? {0}".format(info["has_extra"])) else: sys.stderr.write("Could not find key 'strt': invalid or corrupt data file.\n") sys.exit(1) # EXTRACT SIZE OF DATA if(pmt.dict_has_key(p, pmt.string_to_symbol("bytes"))): r = pmt.dict_ref(p, pmt.string_to_symbol("bytes"), dump) nbytes = pmt.to_uint64(r) nitems = nbytes / dsize info["nitems"] = nitems info["nbytes"] = nbytes if(VERBOSE): print("Size of Data: {0} bytes".format(nbytes)) print(" {0} items".format(nitems)) else: sys.stderr.write("Could not find key 'size': invalid or corrupt data file.\n") sys.exit(1) return info