summaryrefslogtreecommitdiff
path: root/gr-blocks/python
diff options
context:
space:
mode:
authorDaniel Estévez <daniel@destevez.net>2020-08-25 21:14:42 +0200
committermormj <34754695+mormj@users.noreply.github.com>2020-10-20 12:28:36 -0400
commit912d48596569812ddcd05539bc7086617971b65e (patch)
tree9a0552142b6346d84fac62713cef5b2b158b71a2 /gr-blocks/python
parenta93d6c3bf2e20f3f3795fc631af33acc9ef1f187 (diff)
blocks: prevent losing rx_time precision in gr_read_file_metadata
The utility gr_read_file_metadata uses parse_file_metadata.parse_header() from gr-blocks to parse and print headers in a metadata file. Currently, the rx_time field is printed with us precision. However, the rx_time is stored as the integer seconds of the UNIX timestamp in an uint64_t plus a double storing the fraction of a second, so the rx_time has more precision. A precision on the order of 1ns is necessary for many ranging applications, since 1us is approximately 300m of range. This modifies parse_file_metadata.parse_header() to print rx_time with 16 decimal places, without losing precision. Aditionally, it adds "rx_time_secs" and "rx_time_fracs" fields to the dict() return value of parse_header(), in case they are ever needed. The "rx_time" field in this dict() is a double, and as such it is unable to store a UNIX timestamp with 1ns precision.
Diffstat (limited to 'gr-blocks/python')
-rw-r--r--gr-blocks/python/blocks/parse_file_metadata.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/gr-blocks/python/blocks/parse_file_metadata.py b/gr-blocks/python/blocks/parse_file_metadata.py
index 5beede0e72..d98a6c6127 100644
--- a/gr-blocks/python/blocks/parse_file_metadata.py
+++ b/gr-blocks/python/blocks/parse_file_metadata.py
@@ -10,6 +10,7 @@
import sys
+import decimal
from gnuradio import gr, blocks
import pmt
@@ -75,12 +76,20 @@ def parse_header(p, VERBOSE=False):
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))
+ secs = pmt.to_uint64(secs)
fracs = pmt.to_double(fracs)
- t = secs + fracs
+ t = float(secs) + fracs
+ info["rx_time_secs"] = secs
+ info["rx_time_fracs"] = fracs
info["rx_time"] = t
if(VERBOSE):
- print("Seconds: {0:.6f}".format(t))
+ # We need are going to print with ~1e-16 resolution,
+ # which is the precision we can expect in secs.
+ # The default value of precision for decimal
+ # is 28. The value of secs is not expected to be larger
+ # than 1e12, so this precision is enough.
+ s = decimal.Decimal(secs) + decimal.Decimal(fracs)
+ print("Seconds: {0:.16f}".format(s))
else:
sys.stderr.write("Could not find key 'time': invalid or corrupt data file.\n")
sys.exit(1)