Esempio n. 1
0
        #dict is immutable -> therefore pmt_dict_add returns the new dict
        d = pmt.pmt_dict_add(d, python_to_pmt(k), python_to_pmt(v))
    return d

def numpy_to_blob(p):
    p = p.view(numpy.uint8)
    b = pmt.pmt_make_blob(len(p))
    pmt.pmt_blob_data(b)[:] = p
    return b

THE_TABLE = ( #python type, check pmt type, to python, from python
    (None, pmt.pmt_is_null, lambda x: None, lambda x: pmt.PMT_NIL),
    (bool, pmt.pmt_is_bool, pmt.pmt_to_bool, pmt.pmt_from_bool),
    (str, pmt.pmt_is_symbol, pmt.pmt_symbol_to_string, pmt.pmt_string_to_symbol),
    (int, pmt.pmt_is_integer, pmt.pmt_to_long, pmt.pmt_from_long),
    (long, pmt.pmt_is_uint64, lambda x: long(pmt.pmt_to_uint64(x)), pmt.pmt_from_uint64),
    (float, pmt.pmt_is_real, pmt.pmt_to_double, pmt.pmt_from_double),
    (complex, pmt.pmt_is_complex, pmt.pmt_to_complex, pmt_from_complex),
    (tuple, pmt.pmt_is_tuple, pmt_to_tuple, pmt_from_tuple),
    (list, pmt.pmt_is_vector, pmt_to_vector, pmt_from_vector),
    (dict, pmt.pmt_is_dict, pmt_to_dict, pmt_from_dict),
    (numpy.ndarray, pmt.pmt_is_blob, pmt.pmt_blob_data, numpy_to_blob),
)

def pmt_to_python(p):
    for python_type, pmt_check, to_python, from_python in THE_TABLE:
        if pmt_check(p): return to_python(p)
    return p #give up, we return the same

def python_to_pmt(p):
    for python_type, pmt_check, to_python, from_python in THE_TABLE:
def parse_header(p, VERBOSE=False):
    dump = pmt.PMT_NIL

    info = dict()

    if(pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("version"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("version"), dump)
        version = pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("rx_rate"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("rx_rate"), dump)
        samp_rate = pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("rx_time"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("rx_time"), dump)
        pmt_secs = pmt.pmt_tuple_ref(r, 0)
        pmt_fracs = pmt.pmt_tuple_ref(r, 1)
        secs = float(pmt.pmt_to_uint64(pmt_secs))
        fracs = pmt.pmt_to_double(pmt_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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("size"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("size"), dump)
        dsize = pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("type"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("type"), dump)
        dtype = pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("cplx"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("cplx"), dump)
        cplx = pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("strt"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("strt"), dump)
        seg_start = pmt.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.pmt_dict_has_key(p, pmt.pmt_string_to_symbol("bytes"))):
        r = pmt.pmt_dict_ref(p, pmt.pmt_string_to_symbol("bytes"), dump)
        nbytes = pmt.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
Esempio n. 3
0

def numpy_to_blob(p):
    p = p.view(numpy.uint8)
    b = pmt.pmt_make_blob(len(p))
    pmt.pmt_blob_data(b)[:] = p
    return b


THE_TABLE = (  #python type, check pmt type, to python, from python
    (None, pmt.pmt_is_null, lambda x: None, lambda x: pmt.PMT_NIL),
    (bool, pmt.pmt_is_bool, pmt.pmt_to_bool, pmt.pmt_from_bool),
    (str, pmt.pmt_is_symbol, pmt.pmt_symbol_to_string,
     pmt.pmt_string_to_symbol),
    (int, pmt.pmt_is_integer, pmt.pmt_to_long, pmt.pmt_from_long),
    (long, pmt.pmt_is_uint64, lambda x: long(pmt.pmt_to_uint64(x)),
     pmt.pmt_from_uint64),
    (float, pmt.pmt_is_real, pmt.pmt_to_double, pmt.pmt_from_double),
    (complex, pmt.pmt_is_complex, pmt.pmt_to_complex, pmt_from_complex),
    (tuple, pmt.pmt_is_tuple, pmt_to_tuple, pmt_from_tuple),
    (list, pmt.pmt_is_vector, pmt_to_vector, pmt_from_vector),
    (dict, pmt.pmt_is_dict, pmt_to_dict, pmt_from_dict),
    (numpy.ndarray, pmt.pmt_is_blob, pmt.pmt_blob_data, numpy_to_blob),
)


def pmt_to_python(p):
    for python_type, pmt_check, to_python, from_python in THE_TABLE:
        if pmt_check(p): return to_python(p)
    return p  #give up, we return the same