Changeset 394


Ignore:
Timestamp:
05/25/2011 06:12:40 PM (12 years ago)
Author:
marcus
Message:

more plotting tests

Location:
SHX/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • SHX/trunk/SeismicHandler/patches/ObsPy.py

    r388 r394  
    2828 
    2929import numpy as np 
     30import math 
     31import wx 
    3032from obspy.core import Stream, Trace 
    3133#from obspy.core import UTCDateTime 
     
    7072    width : total width of plotting window in pixels 
    7173    height : hight of plotting window in pixels 
    72     zoom : global zoom factor 
    73     timescale : extension of current timescale (min and max) 
    74     norm : norm method: "window", "total" or value 
     74    zoom : global zoom factor (y extension) 
     75    timescale : extension of current timescale (tuple of min and max) 
     76    norm : norm method: "window", "total" or value (effects y extension) 
    7577    """ 
    7678    if not timescale: 
    77         # use trace's dimensions 
     79        # use trace's total dimensions 
    7880        windowstart, windowend = (self.stats.starttime, self.stats.endtime) 
    7981    else: 
     
    9193 
    9294    # width of the total screen that is covered by trace data (pixels) 
    93     portion = duration_trace * width / duration_total 
     95    pixel_width = duration_trace * width / duration_total 
     96    self.shxPixelWidth = pixel_width 
    9497     
    9598    # data points per pixel 
    9699    npts = pt.stats.npts 
    97     dpp = int(npts / portion) 
     100    dpp = int(npts / pixel_width) 
    98101 
    99102    # get copy of data 
    100     self.shx_plotdata = pt.data.copy() 
     103    self.shxPlotData = pt.data.copy() 
    101104 
    102105    # use minmax approach if more than 4 data points per pixel 
     106    # self.shx_plotdata will be changed 
    103107    if dpp >= 4: 
    104         dimy = npts // dpp 
    105         shape = dpp * dimy 
    106         print "minmax", npts, dpp, dimy, shape 
    107          
     108        # do integer division since array shape values cannot be floats 
     109        dimx = npts // dpp 
     110        covered = dpp * dimx 
    108111 
    109     print width, height, norm, duration_trace, duration_total, portion, dpp, type(self.shx_plotdata) 
     112        # get data for reshape operation 
     113        data = self.shxPlotData[:covered] 
    110114 
     115        # extra treatment for remaining values 
     116        remaining = self.shxPlotData[covered:] 
     117 
     118        # reshape data and get min/max values 
     119        data.shape = dimx, dpp 
     120        _min = data.min(axis=1) 
     121        _max = data.max(axis=1) 
     122        # combine data 
     123        joined = np.append([_min], [_max], axis=0).T.flatten() 
     124        # handle remaining 
     125        if len(remaining): 
     126            joined = np.append(joined, [remaining.min(), remaining.max()]) 
     127 
     128        print "pixel_width", pixel_width, "minmax", "npts", npts, "dpp", dpp, "dimx", dimx, \ 
     129              "covered", covered, "len_rest", len(remaining), "data_len", \ 
     130              len(data), "joined", len(joined) 
     131 
     132        self.shxPlotData = joined 
     133 
     134    # At this stage the x-transformed data can be cached! If pixel_width doesn't 
     135    # change, the data can be reused to save time. 
     136 
     137    print width, height, norm, duration_trace, duration_total, pixel_width, dpp, type(self.shxPlotData) 
     138 
     139    # get basis for normation 
    111140    if hasattr(norm, "lower"): 
    112141        norm = norm.lower() 
     
    117146        else: 
    118147            raise ValueError("Invalid input for normation!") 
    119     else: 
    120         norm = norm 
    121148 
    122 # traces in a stream are merge automatically 
     149    # Calculate y extension, leads to raw "image" data. 
     150    # Use a copy to save time if only y changes occur (e.g. zooming). 
     151    # Apply normation, calibration and total height factor 
     152    y = self.shxPlotData.copy() 
     153    y *= self.stats.calib / norm * height / 2 * zoom 
     154 
     155    print self.shxPlotData.min(), self.shxPlotData.max(), y.min(), y.max() 
     156 
     157    # factor for x stretching (trade-off between pixel_width and array shape) 
     158    stretch = pixel_width / len(y) 
     159    width = int(math.ceil(pixel_width)) 
     160 
     161    # calculate line data 
     162    # do not rely on framework axis' management -> shift/flip y-axis 
     163    offset = height // 2 
     164    lines = [] 
     165    oldx, oldy = 0, -y[0] + offset 
     166 
     167    for i in xrange(1, len(y)): 
     168        newx = i * stretch 
     169        newy = -y[i] + offset 
     170        lines.append([oldx, oldy, newx, newy]) 
     171        oldx, oldy = newx, newy 
     172 
     173    self.shxImageData = lines 
     174 
     175def __traceX(parameters): 
     176    """ 
     177    Only the x axis is adopted here. Zoom, norm etc. regarding the y extension 
     178    is done ... 
     179    """ 
     180    pass 
     181 
     182# monkey patching obspy stream class: traces are merge automatically if stream 
     183# get's altered. 
    123184Stream.__shx_init__, Stream.__init__ = Stream.__init__, streamAutoMergeInit 
    124185Stream.__shx_append, Stream.append = Stream.append, streamAutoMergeAppend 
     
    128189 
    129190# add method for faster plotting 
    130 Trace.shx_prepareImageData = tracePrepareDataForImage 
     191Trace.shxPrepareImageData = tracePrepareDataForImage 
    131192 
     193# just an indicator 
    132194patched = True 
  • SHX/trunk/sandbox/bc_example.py

    r354 r394  
    152152                 pos=wx.DefaultPosition, 
    153153                 size=wx.DefaultSize, 
    154                  style=wx.DEFAULT_FRAME_STYLE): 
     154                 style=wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE): 
    155155 
    156156        wx.Frame.__init__(self, parent, ID, title, pos, size, style) 
  • SHX/trunk/sandbox/traceplotter.py

    r383 r394  
    11# -*- coding: utf-8 -*- 
    22 
     3import wx 
     4import wx.lib.scrolledpanel as SP 
    35from SeismicHandler.core import read 
    4 #from obspy.core import UTCDateTime 
    56 
    6 st = read() 
    7 tr = st[0] 
     7class MyPanel(SP.ScrolledPanel): 
     8    def __init__(self, parent): 
     9        SP.ScrolledPanel.__init__(self, parent, style=wx.BORDER_SIMPLE) 
     10        self.Bind(wx.EVT_PAINT, self.OnPaint) 
     11#        self.Bind(wx.EVT_RESIZE, self.OnPaint) 
     12        self.Bind(wx.EVT_IDLE, self.OnIdle) 
     13        self.refresh = False 
     14        self.OnPaint(None) 
    815 
    9 start = tr.stats.starttime - 4 
    10 end = tr.stats.endtime + 4 
     16        st = read("/online4/2011/GR/BFO/BHZ.D/GR.BFO..BHZ.D.2011.124") 
     17        self.tr = st[0] 
    1118 
    12 tr.shx_prepareImageData(700, 100, (start, end)) 
     19    def OnIdle(self, evt): 
     20        if self.refresh: 
     21            self.Paint() 
     22            self.refresh = False 
     23 
     24    def OnPaint(self, evt): 
     25        self.refresh = True 
     26 
     27    def Paint(self): 
     28        width, height = self.GetClientSize() 
     29 
     30        start = self.tr.stats.starttime - 4 
     31        end = self.tr.stats.endtime + 4 
     32 
     33        self.tr.shxPrepareImageData(width, height, (start, end)) 
     34 
     35        bitmap = wx.EmptyBitmap(width, height) 
     36        buffer = wx.MemoryDC(bitmap) 
     37        buffer.Clear() 
     38#        dc = wx.GCDC(buffer) 
     39        dc = buffer 
     40        dc.BeginDrawing() 
     41        dc.DrawLineList(self.tr.shxImageData) 
     42        dc.EndDrawing() 
     43 
     44        dc2 = wx.AutoBufferedPaintDCFactory(self) 
     45        dc2.Blit(0, 0, width, height, buffer, 0, 0, wx.COPY) 
     46 
     47class MyFrame(wx.Frame): 
     48    def __init__(self, parent, title): 
     49        wx.Frame.__init__(self, parent, title=title, size=(640,480), style=wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE) 
     50        self.canvas = MyPanel(self) 
     51        self.Show() 
     52 
     53def main(): 
     54    app = wx.App(False) 
     55    frame = MyFrame(None, 'Trace Plotter Test') 
     56    app.MainLoop() 
     57 
     58if __name__ == '__main__': 
     59    main() 
Note: See TracChangeset for help on using the changeset viewer.