summaryrefslogtreecommitdiff
path: root/gr-radio-astronomy/src/python/ra_stripchartsink.py
blob: 4efb0cd564063856c1de46f4385fa276933e265b (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python
#
# Copyright 2003,2004,2005 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.
# 

from gnuradio import gr, gru
from gnuradio.wxgui import stdgui
import wx
import gnuradio.wxgui.plot as plot
import Numeric
import threading
import math    
import ephem
import time

default_stripchartsink_size = (640,140)
global_yvalues = []

class stripchart_sink_base(object):
    def __init__(self, input_is_real=False, y_per_div=10, ref_level=50,
                 sample_rate=1, stripsize=4,
                 title='',xlabel="X", ylabel="Y", divbase=0.025,
                 parallel=False, scaling=1.0, autoscale=False):

        # initialize common attributes
        self.y_divs = 8
        self.y_per_div=y_per_div
        self.ref_level = ref_level
        self.autoscale = autoscale
        self.sample_rate = sample_rate
        self.parallel = parallel
        self.title = title
        self.xlabel = xlabel
        self.ylabel = ylabel
        self.divbase = divbase
        self.scaling = scaling
        self.input_is_real = input_is_real
        self.msgq = gr.msg_queue(2)         # queue that holds a maximum of 2 messages
        self.vector=Numeric.zeros(stripsize,Numeric.Float64)
        self.wcnt = 0
        self.timecnt = 0
        self.stripsize=stripsize

    def set_y_per_div(self, y_per_div):
        self.y_per_div = y_per_div

    def set_ref_level(self, ref_level):
        self.ref_level = ref_level

    def set_autoscale(self, auto):
        self.autoscale = auto

class stripchart_sink_f(gr.hier_block, stripchart_sink_base):
    def __init__(self, fg, parent,
                 y_per_div=10, ref_level=50, sample_rate=1,
                 title='', stripsize=4,
                 size=default_stripchartsink_size,xlabel="X", 
                 ylabel="Y", divbase=0.025,
                 parallel=False, scaling=1.0, autoscale=False):

        stripchart_sink_base.__init__(self, input_is_real=True,
                               y_per_div=y_per_div, ref_level=ref_level,
                               sample_rate=sample_rate,
                               stripsize=stripsize,
                               xlabel=xlabel, ylabel=ylabel, 
                               divbase=divbase, title=title,
                               parallel=parallel, 
                               scaling=scaling, autoscale=autoscale)
                               
        if (parallel == True):
            one = gr.keep_one_in_n (gr.sizeof_float*stripsize, 1)
            sink = gr.message_sink(gr.sizeof_float*stripsize, self.msgq, True)
        else:
            one = gr.keep_one_in_n (gr.sizeof_float, 1)
            sink = gr.message_sink(gr.sizeof_float, self.msgq, True)
        fg.connect (one, sink)

        gr.hier_block.__init__(self, fg, one, sink)

        self.win = stripchart_window(self, parent, size=size)



# ------------------------------------------------------------------------

myDATA_EVENT = wx.NewEventType()
EVT_DATA_EVENT = wx.PyEventBinder (myDATA_EVENT, 0)


class DataEvent(wx.PyEvent):
    def __init__(self, data):
        wx.PyEvent.__init__(self)
        self.SetEventType (myDATA_EVENT)
        self.data = data

    def Clone (self): 
        self.__class__ (self.GetId())


class input_watcher (threading.Thread):
    def __init__ (self, msgq, evsize, event_receiver, **kwds):
        threading.Thread.__init__ (self, **kwds)
        self.setDaemon (1)
        self.msgq = msgq
        self.evsize = evsize
        self.event_receiver = event_receiver
        self.keep_running = True
        self.start ()

    def run (self):
        while (self.keep_running):
            msg = self.msgq.delete_head()  # blocking read of message queue
            itemsize = int(msg.arg1())
            nitems = int(msg.arg2())

            s = msg.to_string()            # get the body of the msg as a string

            # There may be more than one frame in the message.
            # If so, we take only the last one
            if nitems > 1:
                start = itemsize * (nitems - 1)
                s = s[start:start+itemsize]

            complex_data = Numeric.fromstring (s, Numeric.Float32)
            de = DataEvent (complex_data)
            wx.PostEvent (self.event_receiver, de)
            del de

class stripchart_window(plot.PlotCanvas):
    def __init__ (self, stripchartsink, parent, id = -1,
                  pos = wx.DefaultPosition, size = wx.DefaultSize,
                  style = wx.DEFAULT_FRAME_STYLE, name = ""):
        plot.PlotCanvas.__init__ (self, parent, id, pos, size, style, name)

        self.y_range = None
        self.stripchartsink = stripchartsink

        self.SetEnableGrid (True)
        # self.SetEnableZoom (True)
        # self.SetBackgroundColour ('black')
        
        self.build_popup_menu()
        
        EVT_DATA_EVENT (self, self.set_data)

        wx.EVT_CLOSE (self, self.on_close_window)
        self.Bind(wx.EVT_RIGHT_UP, self.on_right_click)

        self.input_watcher = input_watcher(stripchartsink.msgq, 1, self)


    def on_close_window (self, event):
        print "stripchart_window:on_close_window"
        self.keep_running = False


    def set_data (self, evt):
        indata = evt.data
        L = len (indata)

        calc_min = min(indata)
        calc_max = max(indata)
        d = calc_max - calc_min
        d = d * 0.1
        if self.stripchartsink.autoscale == True and self.stripchartsink.parallel == True:
            self.y_range = self._axisInterval ('min', calc_min-d, calc_max+d)

        N = self.stripchartsink.stripsize
        if self.stripchartsink.parallel != True:
    	    for i in range(1,N):
                pooey = N-i
                self.stripchartsink.vector[pooey] = self.stripchartsink.vector[pooey-1]
    
            self.stripchartsink.vector[0] = indata

        else:
            self.stripchartsink.vector = indata

        if self.stripchartsink.parallel == True:
            avg = 0
            for i in range(0,self.stripchartsink.stripsize):
                if self.stripchartsink.vector[i] > 0:
                    avg += self.stripchartsink.vector[i]
                if self.stripchartsink.vector[i] < calc_min:
                    calc_min = self.stripchartsink.vector[i]
                if self.stripchartsink.vector[i] > calc_max:
                    calc_max = self.stripchartsink.vector[i]

            avg /= self.stripchartsink.stripsize
            markers = []
            placedmarkers = 0
            for i in range(0,self.stripchartsink.stripsize):
                if (self.stripchartsink.vector[i] > 0 and
                    self.stripchartsink.vector[i] > (avg*5)):
                       markers.append((i*self.stripchartsink.scaling,
                       self.stripchartsink.vector[i]))
                       placedmarkers += 1
            
        points = Numeric.zeros((N,2), Numeric.Float64)
        for i in range(0,N):
            if self.stripchartsink.scaling == 1.0:
                points[i,0] = i
            else:
                points[i,0] = i * self.stripchartsink.scaling
            points[i,1] = self.stripchartsink.vector[i]

        if self.stripchartsink.parallel == True and placedmarkers > 1:
            for i in range(0,N):
                self.stripchartsink.vector[i] = 0

            marks = plot.PolyMarker(markers, colour='BLACK', marker='triangle_down')

        lines = plot.PolyLine (points, colour='RED')

        # Temporary--I'm find the markers distracting
        placedmarkers = 0
        xlab = self.stripchartsink.xlabel
        ylab = self.stripchartsink.ylabel
        if (self.stripchartsink.parallel == False) or (placedmarkers <= 1):
            graphics = plot.PlotGraphics ([lines],
                                      title=self.stripchartsink.title,
                                      xLabel = xlab, yLabel = ylab)

        else:
            graphics = plot.PlotGraphics ([lines,marks],
                                      title=self.stripchartsink.title,
                                      xLabel = xlab, yLabel = ylab)

        self.Draw (graphics, xAxis=None, yAxis=self.y_range)

        if self.stripchartsink.autoscale == False or self.stripchartsink.parallel == False:
            self.update_y_range ()


    def update_y_range (self):
        ymax = self.stripchartsink.ref_level
        ymin = self.stripchartsink.ref_level - self.stripchartsink.y_per_div * self.stripchartsink.y_divs
        self.y_range = self._axisInterval ('min', ymin, ymax)

    def on_incr_ref_level(self, evt):
        # print "on_incr_ref_level"
        self.stripchartsink.set_ref_level(self.stripchartsink.ref_level
                                   + self.stripchartsink.y_per_div)

    def on_decr_ref_level(self, evt):
        # print "on_decr_ref_level"
        self.stripchartsink.set_ref_level(self.stripchartsink.ref_level
                                   - self.stripchartsink.y_per_div)

    def on_autoscale(self, evt):
        self.stripchartsink.set_autoscale(evt.IsChecked())

    def on_incr_y_per_div(self, evt):
        divbase = self.stripchartsink.divbase
        x1 = 1 * divbase
        x2 = 2 * divbase
        x4 = 4 * divbase
        x10 = 10 * divbase
        x20 = 20 * divbase
        # print "on_incr_y_per_div"
        self.stripchartsink.set_y_per_div(next_up(self.stripchartsink.y_per_div, (x1,x2,x4,x10,x20)))

    def on_decr_y_per_div(self, evt):
        # print "on_decr_y_per_div"
        divbase = self.stripchartsink.divbase
        x1 = 1 * divbase
        x2 = 2 * divbase
        x4 = 4 * divbase
        x10 = 10 * divbase
        x20 = 20 * divbase
        self.stripchartsink.set_y_per_div(next_down(self.stripchartsink.y_per_div, (x1,x2,x4,x10,x20)))

    def on_y_per_div(self, evt):
        # print "on_y_per_div"
        divbase=self.stripchartsink.divbase
        Id = evt.GetId()
        if Id == self.id_y_per_div_1:
            self.stripchartsink.set_y_per_div(1*divbase)
        elif Id == self.id_y_per_div_2:
            self.stripchartsink.set_y_per_div(2*divbase)
        elif Id == self.id_y_per_div_5:
            self.stripchartsink.set_y_per_div(4*divbase)
        elif Id == self.id_y_per_div_10:
            self.stripchartsink.set_y_per_div(10*divbase)
        elif Id == self.id_y_per_div_20:
            self.stripchartsink.set_y_per_div(20*divbase)

        
    def on_right_click(self, event):
        menu = self.popup_menu
        for id, pred in self.checkmarks.items():
            item = menu.FindItemById(id)
            item.Check(pred())
        self.PopupMenu(menu, event.GetPosition())


    def build_popup_menu(self):
        divbase=self.stripchartsink.divbase
        self.id_incr_ref_level = wx.NewId()
        self.id_decr_ref_level = wx.NewId()
        self.id_autoscale = wx.NewId()
        self.id_incr_y_per_div = wx.NewId()
        self.id_decr_y_per_div = wx.NewId()
        self.id_y_per_div_1 = wx.NewId()
        self.id_y_per_div_2 = wx.NewId()
        self.id_y_per_div_5 = wx.NewId()
        self.id_y_per_div_10 = wx.NewId()
        self.id_y_per_div_20 = wx.NewId()

        self.Bind(wx.EVT_MENU, self.on_incr_ref_level, id=self.id_incr_ref_level)
        self.Bind(wx.EVT_MENU, self.on_decr_ref_level, id=self.id_decr_ref_level)
        self.Bind(wx.EVT_MENU, self.on_autoscale, id=self.id_autoscale)
        self.Bind(wx.EVT_MENU, self.on_incr_y_per_div, id=self.id_incr_y_per_div)
        self.Bind(wx.EVT_MENU, self.on_decr_y_per_div, id=self.id_decr_y_per_div)
        self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_1)
        self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_2)
        self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_5)
        self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_10)
        self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_20)


        # make a menu
        menu = wx.Menu()
        self.popup_menu = menu
        menu.Append(self.id_incr_ref_level, "Incr Ref Level")
        menu.Append(self.id_decr_ref_level, "Decr Ref Level")
        menu.AppendSeparator()
        menu.AppendCheckItem(self.id_autoscale, "Auto Scale")
        # we'd use RadioItems for these, but they're not supported on Mac
        v = 1.0*divbase
        s = "%.3f" % v
        menu.AppendCheckItem(self.id_y_per_div_1, s)
        v = 2.0*divbase
        s = "%.3f" % v
        menu.AppendCheckItem(self.id_y_per_div_2, s)
        v = 4.0*divbase
        s = "%.3f" % v
        menu.AppendCheckItem(self.id_y_per_div_5, s)
        v = 10*divbase
        s = "%.3f" % v
        menu.AppendCheckItem(self.id_y_per_div_10, s)
        v = 20*divbase
        s = "%.3f" % v
        menu.AppendCheckItem(self.id_y_per_div_20, s)

        self.checkmarks = {
            self.id_autoscale : lambda : self.stripchartsink.autoscale,
            self.id_y_per_div_1 : lambda : self.stripchartsink.y_per_div == 1*divbase,
            self.id_y_per_div_2 : lambda : self.stripchartsink.y_per_div == 2*divbase,
            self.id_y_per_div_5 : lambda : self.stripchartsink.y_per_div == 4*divbase,
            self.id_y_per_div_10 : lambda : self.stripchartsink.y_per_div == 10*divbase,
            self.id_y_per_div_20 : lambda : self.stripchartsink.y_per_div == 20*divbase,
            }


def next_up(v, seq):
    """
    Return the first item in seq that is > v.
    """
    for s in seq:
        if s > v:
            return s
    return v

def next_down(v, seq):
    """
    Return the last item in seq that is < v.
    """
    rseq = list(seq[:])
    rseq.reverse()

    for s in rseq:
        if s < v:
            return s
    return v