Dynamic File Names in GNU Radio Companion

IQ data recorderI needed a quick USRP IQ data recorder today to record the APT downlink from NOAA satellites. This is really trivial to implement using the GNU Radio Companion, but I had to figure out how to use dynamic filenames to avoid overwriting previously recorded data. Of course, I could just rename each file when recording has finished but I am too lazy to do things like that. Using the current date and time in the file name seemed to be a good solution.

This quick how-to explains how to use the Python datetime module to generate a unique filename in GRC.

GNU Radio companion offers to use any valid Python statement to generate the value of a parameter or a variable. We can take advantage of this for generating a unique file name for the filesink block consisting of the current date and time. The example presented here will create files with names following the format:

  /some/prefix/path/YYYY.MM.DD.hh.mm.ss.dat

  YYYY is the 4 digit year number
  MM is the two digit month number
  DD is the two digit day number
  hh is the hour
  mm is the minute
  ss is the second

First, we need to import the datetime module. In GRC double click on Misc → Import and type in the Import field:

  from datetime import datetime

Next, we create a static variable where we can specify a prefix – this can be very useful to specify the folder where we want to save the files and/or to add a static prefix to the file name. In GRC double click Variables → Variable and type:

  ID: prefix
  Value: /home/alexc/gnuradio/

You can of course type anything you like in the Value field but if you specify a path remember to end with a “/”

Next, we create the variable to actually generate the file name. This variable will concatenate the prefix with the date and time and append a “.dat” suffix. The syntax to generate a string from the current date and time is:

  datetime.now().strftime("%Y.%m.%d.%H.%M.%S")

where the argument is the format string. You can experiment with your own formats using the Python interpreter:

  # python
  Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
  [GCC 4.4.3] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> from datetime import datetime
  >>> datetime.now().strftime("%Y.%m.%d.%H.%M.%S")
  '2010.07.03.13.42.13'
  >>>

Once you have found the format of your choice you can proceed by adding a new variable block in GRC with the following parameters:

  ID: recfile
  Value: prefix + datetime.now().strftime("%Y.%m.%d.%H.%M.%S") + ".dat"
Input type: Complex

Finally, remember to use the variable name recfile as File parameter in the filesink block.

The screenshot below shows a simple flow graph with a wide band FM demodulator (for NOAA APT) and a file recorder using this trick. You can get the .grc file here.

NOAA APT receiver with IQ data recorder.

Obviously, there are many interesting opportunities to expand on this. We could also include the USRP frequency, decimation, RF gain, et cetera in the file name. This is left as an exercise 😉