1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2008-2011 Marcus Walther (walther@szgrf.bgr.de) |
---|
4 | # |
---|
5 | # This file is part of Seismic Handler eXtended (SHX) |
---|
6 | # Full details can be found at project website http://www.seismic-handler.org/ |
---|
7 | # |
---|
8 | # SHX is free software; you can redistribute it and/or modify |
---|
9 | # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
---|
10 | # the Free Software Foundation; either version 3 of the License, or |
---|
11 | # (at your option) any later version. |
---|
12 | # |
---|
13 | # SHX is distributed in the hope that it will be useful, |
---|
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | # GNU Lesser General Public License for more details. |
---|
17 | # |
---|
18 | # You should have received a copy of the GNU Lesser General Public License |
---|
19 | # along with SHX (see license.txt); if not, write to the Free Software |
---|
20 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
---|
21 | |
---|
22 | """ |
---|
23 | This modules defines extensions to obspy. They are patched into the |
---|
24 | original obspy modules at a very basic level in SeismicHandler/__init__.py |
---|
25 | |
---|
26 | This module is named ObsPy.py to prevent import loops. |
---|
27 | """ |
---|
28 | |
---|
29 | import numpy as np |
---|
30 | from obspy.core import Stream, Trace |
---|
31 | #from obspy.core import UTCDateTime |
---|
32 | |
---|
33 | __all__ = [ |
---|
34 | "patched", |
---|
35 | ] |
---|
36 | |
---|
37 | def _streamAutoMerge(self, *args, **kwargs): |
---|
38 | """ |
---|
39 | Call obspy's Stream merge method to fill up gaps using the latest pre-gap |
---|
40 | sample value. This destroys the spectra of the data but it's common. |
---|
41 | """ |
---|
42 | if self.traces: |
---|
43 | self.merge(method=1, fill_value='latest') |
---|
44 | |
---|
45 | def streamAutoMergeInit(self, *args, **kwargs): |
---|
46 | self.__shx_init__(*args, **kwargs) |
---|
47 | _streamAutoMerge(self) |
---|
48 | |
---|
49 | def streamAutoMergeAppend(self, *args, **kwargs): |
---|
50 | self.__shx_append(*args, **kwargs) |
---|
51 | _streamAutoMerge(self, *args, **kwargs) |
---|
52 | |
---|
53 | def streamAutoMergeExtend(self, *args, **kwargs): |
---|
54 | self.__shx_extend(*args, **kwargs) |
---|
55 | _streamAutoMerge(self, *args, **kwargs) |
---|
56 | |
---|
57 | def streamAutoMergeInsert(self, *args, **kwargs): |
---|
58 | self.__shx_insert(*args, **kwargs) |
---|
59 | _streamAutoMerge(self, *args, **kwargs) |
---|
60 | |
---|
61 | def streamAutoMergeAdd(self, *args, **kwargs): |
---|
62 | self.__shx_add__(*args, **kwargs) |
---|
63 | _streamAutoMerge(self, *args, **kwargs) |
---|
64 | |
---|
65 | def tracePrepareDataForImage(self, width, height=None, timescale=None, zoom=1, norm="window"): |
---|
66 | """ |
---|
67 | Preparing trace data for fast plotting. Using numpy's minmax feature - this |
---|
68 | will not just downsample the data! |
---|
69 | |
---|
70 | width : total width of plotting window in pixels |
---|
71 | 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 |
---|
75 | """ |
---|
76 | if not timescale: |
---|
77 | # use trace's dimensions |
---|
78 | windowstart, windowend = (self.stats.starttime, self.stats.endtime) |
---|
79 | else: |
---|
80 | windowstart, windowend = timescale |
---|
81 | |
---|
82 | # XXX |
---|
83 | if not height: |
---|
84 | height = 1000 |
---|
85 | |
---|
86 | # get slice for time window |
---|
87 | pt = self.slice(windowstart, windowend) |
---|
88 | |
---|
89 | duration_total = windowend - windowstart |
---|
90 | duration_trace = pt.stats.endtime - pt.stats.starttime |
---|
91 | |
---|
92 | # width of the total screen that is covered by trace data (pixels) |
---|
93 | portion = duration_trace * width / duration_total |
---|
94 | |
---|
95 | # data points per pixel |
---|
96 | npts = pt.stats.npts |
---|
97 | dpp = int(npts / portion) |
---|
98 | |
---|
99 | # get copy of data |
---|
100 | self.shx_plotdata = pt.data.copy() |
---|
101 | |
---|
102 | # use minmax approach if more than 4 data points per pixel |
---|
103 | if dpp >= 4: |
---|
104 | dimy = npts // dpp |
---|
105 | shape = dpp * dimy |
---|
106 | print "minmax", npts, dpp, dimy, shape |
---|
107 | |
---|
108 | |
---|
109 | print width, height, norm, duration_trace, duration_total, portion, dpp, type(self.shx_plotdata) |
---|
110 | |
---|
111 | if hasattr(norm, "lower"): |
---|
112 | norm = norm.lower() |
---|
113 | if norm == "window": |
---|
114 | norm = abs(pt.max()) |
---|
115 | elif norm == "total": |
---|
116 | norm = self.max() |
---|
117 | else: |
---|
118 | raise ValueError("Invalid input for normation!") |
---|
119 | else: |
---|
120 | norm = norm |
---|
121 | |
---|
122 | # traces in a stream are merge automatically |
---|
123 | Stream.__shx_init__, Stream.__init__ = Stream.__init__, streamAutoMergeInit |
---|
124 | Stream.__shx_append, Stream.append = Stream.append, streamAutoMergeAppend |
---|
125 | Stream.__shx_extend, Stream.extend = Stream.extend, streamAutoMergeExtend |
---|
126 | Stream.__shx_insert, Stream.insert = Stream.insert, streamAutoMergeInsert |
---|
127 | Stream.__shx_add__, Stream.__add__ = Stream.__add__, streamAutoMergeAdd |
---|
128 | |
---|
129 | # add method for faster plotting |
---|
130 | Trace.shx_prepareImageData = tracePrepareDataForImage |
---|
131 | |
---|
132 | patched = True |
---|