source: SHX/trunk/sandbox/bc_example.py @ 394

Revision 394, 5.5 KB checked in by marcus, 13 years ago (diff)

more plotting tests

  • Property svn:eol-style set to native
Line 
1# -*- coding: utf-8 -*-
2
3import wx
4import wx.lib.scrolledpanel as SP
5import sys
6from bufferedcanvas import *
7
8from obspy.core import read
9
10class TestCanvas(BufferedCanvas):
11    def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, size=wx.DefaultSize):
12        try:
13            fname = sys.argv[1]
14        except IndexError:
15            fname = "../SeismicHandler/tests/data/mseed/2010.284.GR.GRA1..BHN"
16        self.trace = read(fname)[0]
17
18        # demean
19        self.trace.data = self.trace.data-self.trace.data.mean()
20
21        min, max = self.trace.data.min(), self.trace.data.max()
22        self.base = abs(self.trace.max())
23        self.npts = self.trace.stats.npts
24
25        self.overlay = wx.Overlay()
26
27        BufferedCanvas.__init__(self, parent, ID, size=size)
28
29        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
30        self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
31        self.Bind(wx.EVT_MOTION, self.OnMouseMove)
32
33        try:
34            parent.Bind(wx.EVT_SCROLLWIN_CHANGED, self.OnScrollDone)
35        except:
36            parent.Bind(wx.EVT_SCROLLWIN, self.OnScrollDone)
37
38        parent.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheel)
39
40    def OnMouseWheel(self, evt):
41        if evt.ControlDown():
42            print "wheel CTRL event"
43        else:
44            evt.Skip()
45
46    def OnScrollDone(self, evt):
47        print "scrolling finished"
48#        self.overlay.Reset()
49        dc = wx.ClientDC(self)
50        odc = wx.DCOverlay(self.overlay, dc)
51        odc.Clear()
52        del odc
53        evt.Skip()
54
55    def OnRightDown(self, evt):
56        # Capture the mouse and save the starting position for the
57        # rubber-band
58        self.CaptureMouse()
59        self.startPos = (evt.GetPosition()[0], 10)
60
61    def OnMouseMove(self, evt):
62        if not evt.RightIsDown():
63#            print "move to", evt.GetPosition()
64            dc = wx.ClientDC(self)
65            odc = wx.DCOverlay(self.overlay, dc)
66            odc.Clear()
67            dc.SetPen(wx.Pen("Blue", 1))
68            dc.SetBrush(wx.TRANSPARENT_BRUSH)
69            dc.DrawLine(evt.GetPosition()[0], 10, evt.GetPosition()[0], self.buffer.Height-10)
70            del odc
71
72        if evt.Dragging() and evt.RightIsDown():
73            rect = wx.RectPP(self.startPos, (evt.GetPosition()[0], self.buffer.Height-10))
74
75            # Draw the rubber-band rectangle using an overlay so it
76            # will manage keeping the rectangle and the former window
77            # contents separate.
78            dc = wx.ClientDC(self)
79            odc = wx.DCOverlay(self.overlay, dc)
80            odc.Clear()
81
82            dc.SetPen(wx.Pen("red", 2))
83            if 'wxMac' in wx.PlatformInfo:
84                dc.SetBrush(wx.Brush(wx.Colour(0xC0, 0xC0, 0xC0, 0x80)))
85            else:
86                dc.SetBrush(wx.TRANSPARENT_BRUSH)
87            dc.DrawRectangleRect(rect)
88
89            del odc # work around a bug in the Python wrappers to make
90                    # sure the odc is destroyed before the dc is.
91
92    def OnRightUp(self, evt):
93        if self.HasCapture():
94            self.ReleaseMouse()
95        self.startPos = None
96
97        # When the mouse is released we reset the overlay and it
98        # restores the former content to the window.
99        dc = wx.ClientDC(self)
100        odc = wx.DCOverlay(self.overlay, dc)
101        odc.Clear()
102        del odc
103        self.overlay.Reset()
104
105    def draw(self, dc):
106        self.overlay.Reset()
107
108        w, h = self.buffer.Width-20, self.buffer.Height
109        spp = self.npts // w
110        tail = self.npts % w
111
112        print "samples per pixel: %u remains: %u" % (spp, tail)
113
114        # resample
115        if spp > 2:
116            pass
117
118        zoomx = float(w)/self.npts
119        zoomy = float(h)/self.base/2.1
120       
121        dc.SetBackground(wx.Brush("White"))
122        dc.Clear()
123
124        dc.SetPen(wx.Pen('Black', 1))
125
126        lines = []
127        offsetx, offsety = 10, int(h / 2.)
128        print "x: %u y: %u zoom: x=%.2f y=%.2f offset: x=%i y=%i" % (w, h, zoomx, zoomy, offsetx, offsety)
129
130        oldx, oldy = offsetx, offsety
131        for n, i in enumerate(self.trace.data):
132            newx = n*zoomx+offsetx
133            newy = -i*zoomy+offsety
134            lines.append([oldx, oldy, newx, newy])
135            oldx, oldy = newx, newy
136
137        dc.SetPen(wx.Pen('Grey', 1, wx.LONG_DASH))
138        dc.DrawLine(offsetx, offsety, offsetx+w, offsety)
139        dc.DrawLabel("TEST", (0, 100, offsetx, offsety))
140
141        pen = wx.Pen((80,80,80), 1)
142#        pen.SetJoin(wx.JOIN_ROUND)
143#        pen.SetCap(wx.CAP_ROUND)
144        dc.SetPen(pen)
145        dc.DrawLineList(lines)
146
147class TestFrame(wx.Frame):
148    def __init__(self,
149                 parent=None,
150                 ID=-1,
151                 title="BufferedCanvas Trace Test",
152                 pos=wx.DefaultPosition,
153                 size=wx.DefaultSize,
154                 style=wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE):
155
156        wx.Frame.__init__(self, parent, ID, title, pos, size, style)
157
158        panel = SP.ScrolledPanel(
159            parent=self,
160            pos=pos,
161            size=(1, 1),
162            style=wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER,
163            name="panel"
164        )
165
166        self.canvas = TestCanvas(panel, size=[500,200])
167
168        sizer  = wx.BoxSizer()
169        sizer.Add(self.canvas, proportion=2)
170
171        panel.SetSizer(sizer)
172        panel.SetupScrolling()
173
174        self.Bind(wx.EVT_CLOSE, self.onClose)
175
176    def onClose(self,event):
177        self.Show(False)
178        self.Destroy()
179
180def main():
181    app = wx.PySimpleApp()
182    frame = TestFrame(size=[700,200])
183    frame.Show(True)
184    app.MainLoop()
185
186if __name__ == '__main__':
187    main()
Note: See TracBrowser for help on using the repository browser.