summaryrefslogtreecommitdiff
path: root/gr-utils/src/python/gr_read_file_metadata
blob: ba530ca540c7b3e92afce129309fa58bf01c3337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
#
# Copyright 2012 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

import sys
from optparse import OptionParser

from gnuradio import gr
from gnuradio import parse_file_metadata

def main(filename):
    handle = open(filename, "rb")

    nheaders = 0
    nread = 0
    while(True):
        # read out next header bytes
        hdr_start = handle.tell()
        header_str = handle.read(parse_file_metadata.HEADER_LENGTH)
        if(len(header_str) == 0):
            break

        # Convert from string to PMT (should be a dictionary)
        try:
            header = gr.pmt_deserialize_str(header_str)
        except RuntimeError:
            sys.stderr.write("Could not deserialize header: invalid or corrupt data file.\n")
            sys.exit(1)

        print "HEADER {0}".format(nheaders)
        info = parse_file_metadata.parse_header(header, True)

        if(info["extra_len"] > 0):
            extra_str = handle.read(info["extra_len"])
            if(len(extra_str) == 0):
                break

            try:
                extra = gr.pmt_deserialize_str(extra_str)
            except RuntimeError:
                sys.stderr.write("Could not deserialize extras: invalid or corrupt data file.\n")
                sys.exit(1)

            print "\nExtra Header:"
            extra_info = parse_file_metadata.parse_extra_dict(extra, info, True)
        
        nheaders += 1
        nread += info['nbytes'] + parse_file_metadata.HEADER_LENGTH + info["extra_len"]
        handle.seek(nread, 0)
        print "\n\n"

if __name__ == "__main__":
    usage="%prog: [options] filename"
    description = "Read in a GNU Radio file with meta data, extracts the header and prints it."

    parser = OptionParser(conflict_handler="resolve",
                          usage=usage, description=description)
    (options, args) = parser.parse_args ()

    if(len(args) < 1):
        sys.stderr.write("No filename given\n")
        sys.exit(1)

    filename = args[0]
    main(filename)