Exemplo n.º 1
0
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
Exemplo n.º 3
0
    def check_file(self, fname, expected, has_header=False, data_type='uint8'):
        # instantiate objects
        debug = blocks.message_debug()
        reader = csv_reader(fname=fname,
                            has_header=has_header,
                            period=10,
                            repeat=False,
                            start_delay=0,
                            data_type=data_type)

        tb = gr.top_block()
        tb.msg_connect((reader, 'out'), (debug, 'store'))
        tb.start()
        time.sleep(.25)
        tb.stop()
        tb.wait()

        # collect results
        got = debug.get_message(0)
        got_car = pmt.car(got)

        print("got: {}".format(got))
        print("expected: {}".format(expected))

        # do a manual equal on elements because pmt equal is not the best
        expected_items = pmt.dict_items(pmt.car(expected))
        valid = True
        for i in range(pmt.length(expected_items)):
            key = pmt.car(pmt.nth(i, expected_items))
            value = pmt.cdr(pmt.nth(i, expected_items))
            got_value = pmt.dict_ref(got_car, key, pmt.PMT_NIL)
            if not pmt.equal(got_value, value):
                valid = False
                break

        return (valid and pmt.equal(pmt.cdr(got), pmt.cdr(expected)))
Exemplo n.º 4
0
    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))
Exemplo n.º 5
0
    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')