# Filename using time-stamp in GRC

**URL:** <https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002>\
**Category:** GNU Radio\
**Created:** [January 19, 2011, 7:48pm UTC](https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002 "2011-01-19T19:48:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 19, 2011, 7:48pm UTC](https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002/1 "2011-01-19T19:48:13Z")

</div>

He all-

is there a way to implement a button controlling a record-to-file  
function where the filename is generated instantly from the current time  
stamp? I can do this manually in python as follows (taken from a  
previously existing gnuradio code):

```
    #
    # Recording file, in case we ever need to record baseband data
    #
    self.recording = gr.file_sink(gr.sizeof_float, "/dev/null")
    self.recording_state = False
    # Filename prefix for recording file
    self.prefix = options.prefix

    # We come up with recording turned off, but the user may
    # request recording later on
    self.recording.close()

```

.  
.  
.

self.connect (self.source, self.recording)

.  
.  
.

```
    # Data recording control
    buttonbox = wx.BoxSizer(wx.HORIZONTAL)
    self.record_control = form.button_with_callback(self.panel,
          label="Recording Data: Off 

```

",  
callback=self.toggle\_recording)

```
    buttonbox.Add(self.record_control, 0, wx.CENTER)

```

.  
.  
.

```
#
# Turn recording on/off
# Called-back by "Recording" button
#
def toggle_recording(self):
    # Pick up localtime, for generating filenames
    timestamp = time.localtime()

    # Generate filenames for both data and header file
    filename = "%04d_%02d_%02d_%02d:%02d:%02d.dat" % 

```

(timestamp.tm\_year, timestamp.tm\_mon,  
timestamp.tm\_mday, timestamp.tm\_hour,  
timestamp.tm\_min,timestamp.tm\_sec)

```
    # Current recording? Flip state
    if (self.recording_state == True):
      self.recording_state = False
      self.record_control.SetLabel("Recording Data: Off 

```

")  
self.recording.close()  
# Not recording?  
else:  
self.recording\_state = True  
self.record\_control.SetLabel("Recording Data to: "+filename)

```
      # Cause gr_file_sink object to accept new filename
      # note use of self.prefix--filename prefix from
      # command line (defaults to ./)
      #
      self.recording.open (self.prefix+filename)

```

Thanks!  
eric

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 20, 2011, 5:16pm UTC](https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002/2 "2011-01-20T17:16:46Z")

</div>

If you created a custom block in python that would do the right thing as  
far as writing the file and handling the data; then you could use the  
variable chooser (button) or variable text box (string input) to  
generate the event.

-Josh

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 20, 2011, 6:42pm UTC](https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002/3 "2011-01-20T18:42:12Z")

</div>

On Thu, Jan 20, 2011 at 5:41 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

…

> [[Discuss-gnuradio] GRC selector audio sink error](http://lists.gnu.org/archive/html/discuss-gnuradio/2011-01/msg00080.html)
> 
> If so, will this approach work if I upgrade via git?

Hi Eric,

The problem with this simple example is that the variable containing

“prefix + datetime.now().strftime(”%Y.%m.%d.%H.%M.%S") + “.bin”

is evaluated when you start the script, so it will only work for a  
single file per execution. I believe it is for the same reason that  
you see no file with your modification: The file sink is initialized  
with /dev/null as filename and it can not change the file name during  
runtime because the required code for “close file, set new file name,  
open new file” is not generated by GRC - I am not exactly sure about  
this but it should be obvious if you look in the generated code. I  
would try to do as Josh said in the other mail, implement the required  
code yourself and embed it as a custom block.

Alex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 20, 2011, 5:44pm UTC](https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002/4 "2011-01-20T17:44:25Z")

</div>

— On Wed, 1/19/11, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> existing gnuradio code):  
> self.prefix = options.prefix
> 
> ```
> self.record_control =
> 
> ```
> 
> def toggle\_recording(self):  
> timestamp.tm\_hour, timestamp.tm\_min,timestamp.tm\_sec)
> 
> Thanks!  
> eric

Following up on this, I found this site that had some useful  
suggestions:[GRC Examples - OZ9AEC Website](http://www.oz9aec.net/index.php/gnu-radio/grc-examples)

A grc example is provided that implements a dynamic time stamp, but NOT  
a button to control it. So I am trying to use a Variable Chooser to  
select between “/dev/null” and the filename that is based on the  
time-stamp. However, nothing is generated. In particular, I have:

A file sink with File: recfile  
A Variable with ID prefix with Value “./”  
A Variable Chooser with ID: recfile, Default Value: “/dev/null” and  
Choices of “/dev/null” or “prefix +  
datetime.now().strftime(”%Y.%m.%d.%H.%M.%S") + “.bin”

I was hoping this would dynamically create a file with the current time  
stamp for the name, but no files are actually being generated. Could  
this be and issue related to a bug that is described in the link below?

> **[\[Discuss-gnuradio\] GRC selector audio sink error](https://lists.gnu.org/archive/html/discuss-gnuradio/2011-01/msg00080.html)**

If so, will this approach work if I upgrade via git?

Thanks!  
eric

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 20, 2011, 8:00pm UTC](https://www.ruby-forum.com/t/filename-using-time-stamp-in-grc/202002/5 "2011-01-20T20:00:44Z")

</div>

— On Thu, 1/20/11, Alexandru C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> useful suggestions:[GRC Examples - OZ9AEC Website](http://www.oz9aec.net/index.php/gnu-radio/grc-examples)  
> “/dev/null” and
> 
> > If so, will this approach work if I upgrade via git?  
> > is evaluated when you start the script, so it will only  
> > sure about  
> > this but it should be obvious if you look in the generated  
> > code. I  
> > would try to do as Josh said in the other mail, implement  
> > the required  
> > code yourself and embed it as a custom block.
> 
> Alex

Thank you all.

Eric
