-
Notifications
You must be signed in to change notification settings - Fork 22
/
base.py
462 lines (327 loc) · 13.3 KB
/
base.py
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import numpy as np
import warnings
from copy import copy, deepcopy
from mtuq.event import Origin
from mtuq.station import Station
from mtuq.dataset import Dataset
from mtuq.util.signal import check_time_sampling
from obspy.core import Stream, Trace
from obspy.geodetics import gps2dist_azimuth
from scipy.signal import fftconvolve
class GreensTensor(Stream):
""" Holds multiple time series corresponding to the independent elements
of an elastic Green's tensor
.. note::
Besides the methods below, `GreensTensor` includes many useful
data processing methods inherited from ``obspy.core.Stream``. See
`ObsPy documentation <https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.html>`_
for more information.
"""
def __init__(self,
traces=None,
station=None,
origin=None,
id=None,
tags=[],
include_mt=True,
include_force=False):
""" Constructor method
"""
# argument checking starts now
for trace in traces:
assert isinstance(trace, Trace)
assert check_time_sampling(traces), NotImplementedError(
"Time sampling differs from trace to trace.")
super(GreensTensor, self).__init__(traces)
if not isinstance(station, Station):
raise TypeError
if not isinstance(origin, Origin):
raise TypeError
# the main work of the constructor starts now
if id:
self.id = id
else:
self.id = station.id
self.station = station.copy()
self.origin = origin.copy()
self.tags = tags
self.include_mt = include_mt
self.include_force = include_force
self.distance_in_m, self.azimuth, _ = gps2dist_azimuth(
origin.latitude,
origin.longitude,
station.latitude,
station.longitude)
def _set_components(self, components):
""" Gets called before or during `get_synthetics` to specify which
components are returned
.. note:
Sometimes it makes sense to call this method partway through a
script. For example, if all three componets are originally present
at a particular station but the transerve component is later
discovered to be bad, calling ``_set_components(['Z', 'R'])``
would remove it
"""
if components==getattr(self, 'components', None):
return
if components is None:
components = []
for component in components:
assert component in ['Z', 'R', 'T']
self.components = components
self._preallocate()
self._precompute()
def _preallocate(self):
""" Preallocates structures used by `get_synthetics`
.. note:
Every time ``get_synthetics(inplace=True)`` is called, the numeric
trace data get overwritten. Every time ``_set_components`` is
called, the traces get overwritten. The stream itself never gets
overwritten.
"""
nc, nr, nt = self._get_shape()
# allocate NuPy array to hold Green's function time series
self._array = np.zeros((nc, nr, nt))
# allocate ObsPy structures to hold synthetics
self._synthetics = self._allocate_stream()
def _precompute(self):
""" Precomputes NumPy array used by `get_synthetics`
"""
# the formulas relating the original time series to the linear
# combination array vary depending on the scheme being used, so
# are deferred to the subclass
raise NotImplementedError("Must be implemented by subclass.")
def _get_shape(self):
""" Returns shape of NumPy array used by `get_synthetics`
"""
nt = len(self[0].data)
nc = len(self.components)
nr = 0
if self.include_mt:
nr += 6
if self.include_force:
nr+= 3
return nc, nr, nt
def _allocate_stream(self):
""" Allocates ObsPy stream used by `get_synthetics`
"""
nc, nr, nt = self._get_shape()
stream = Stream()
for component in self.components:
# add stats object
stats = self.station.copy()
stats.update({'npts': nt, 'channel': component})
# add trace object
stream += Trace(np.zeros(nt), stats)
return stream
def get_synthetics(self, source, components=None, inplace=False):
""" Generates synthetics through a linear combination of time series
Returns an ObsPy stream
.. rubric :: Input arguments
``source`` (`MomentTensor`, `Force` or `CompositeSource`):
Source object
``components`` (`list`):
List containing zero or more of the following components:
``Z``, ``R``, ``T``. (Defaults to ``['Z', 'R', 'T']``.)
"""
if components is None:
# Components argument was not given, so check that attribute is
# already set
assert(hasattr(self, 'components'))
else:
self._set_components(components)
# arrays used in linear combination
source = source.as_vector()
array = self._array
if inplace:
synthetics = self._synthetics
else:
synthetics = self._allocate_stream()
for _i, component in enumerate(self.components):
# Even with careful attention to index order, np.dot is very slow.
# For some reason the following is faster
data = synthetics[_i].data
data[:] = 0.
for _j in range(len(source)):
data += source[_j]*array[_i, _j, :]
return synthetics
def convolve(self, wavelet):
""" Convolves time series with given wavelet
Returns MTUQ `GreensTensor`
.. rubric :: Input arguments
``wavelet`` (`Wavelet` object):
Source wavelet
"""
for trace in self:
wavelet.convolve(trace)
def select(self, component=None, channel=None):
""" Selects time series that match the supplied metadata criteria
"""
return Stream([trace for trace in self]).select(
component=component, channel=channel)
def __add__(self, *args):
raise Exception("Adding time series to an existing GreensTensor is "
" not currently supported")
def __iadd__(self, *args):
raise Exception("Adding time series to an existing GreensTensor is "
" not currently supported")
class GreensTensorList(list):
""" Container for one or more `GreensTensor` objects
"""
def __init__(self, tensors=[], id=None, tags=[]):
# typically the id is the event name or origin time
self.id = id
for tensor in tensors:
self.append(tensor)
for tag in copy(tags):
self.tag_add(tag)
def append(self, tensor):
""" Appends `GreensTensor` to the container
"""
if not hasattr(tensor, 'station'):
raise Exception("GreensTensor lacks station metadata")
elif not hasattr(tensor, 'origin'):
raise Exception("GreensTensor lacks origin metadata")
super(GreensTensorList, self).append(tensor)
def select(self, selector):
""" Selects `GreensTensors` that match the given station or origin
"""
if type(selector) is Station:
selected = self.__class__(id=self.id, tensors=filter(
lambda tensor: tensor.station==selector, self))
elif type(selector) is Origin:
selected = self.__class__(id=self.id, tensors=filter(
lambda tensor: tensor.origin==selector, self))
else:
raise TypeError("Bad selector: %s" % type(selector).__name__)
if len(selected)==0:
if len(self) > 0:
warnings.warn("Nothing found matching given selector "
"(%s)\n" % type(selector).__name__)
return selected
def get_synthetics(self, source, components=None, mode='apply', **kwargs):
""" Generates synthetics through a linear combination of time series
Returns an MTUQ `Dataset`
.. rubric :: Input arguments
``source`` (`MomentTensor`, `Force` or `CompositeSource`):
Source object
``components`` (`list`):
List containing zero or more of the following components:
``Z``, ``R``, ``T``. (Defaults to ``['Z', 'R', 'T']``.)
"""
if mode=='map':
synthetics = Dataset()
for _i, tensor in enumerate(self):
synthetics.append(
tensor.get_synthetics(source, components=components[_i], **kwargs))
return synthetics
elif mode=='apply':
synthetics = Dataset()
for tensor in self:
synthetics.append(
tensor.get_synthetics(source, components=components, **kwargs))
return synthetics
else:
raise ValueError
# the next three methods can be used to apply signal processing or other
# operations to all time series in all GreensTensors
def apply(self, function, *args, **kwargs):
""" Applies function to all `GreensTensors`
Applies a function to each `GreensTensor` in the list, similar to the
Python built-in ``apply``.
.. warning ::
Although ``apply`` returns a new `GreensTensorList`, contents of the
original `GreensTensorList` may still be overwritten, depending on
the function. To preserve the original, consider making a
`copy` first.
"""
processed = []
for tensor in self:
processed +=\
[function(tensor, *args, **kwargs)]
return self.__class__(processed)
def map(self, function, *sequences):
""" Maps function to all `GreensTensors`
Maps a function to each `GreensTensor` in the list. If one or more
optional sequences are given, the function is called with an argument
list consisting of the corresponding item of each sequence, similar
to the Python built-in ``map``.
.. warning ::
Although ``map`` returns a new `GreensTensorList`, contents of the
original `GreensTensorList` may still be overwritten, depending on
the function. To preserve the original, consider making a
`copy` first.
"""
processed = []
for _i, tensor in enumerate(self):
args = [sequence[_i] for sequence in sequences]
processed +=\
[function(tensor, *args)]
return self.__class__(processed)
#def parralell_map(self, function, *sequences):
# """ Parallelized version of `map`
# Maps a function to each `GreensTensor` in the list. If one or more
# optional sequences are given, the function is called with an argument
# list consisting of the corresponding item of each sequence, similar
# to the Python built-in ``map``.
# Parallelized using mpi4py
# .. warning ::
# Although ``map`` returns a new `GreensTensorList`, contents of the
# original `GreensTensorList` may still be overwritten, depending on
# the function. To preserve the original, consider making a
# `copy` first.
# """
# raise NotImplementedError
def convolve(self, wavelet):
""" Convolves time series with given wavelet
Returns MTUQ `GreensTensorList`
.. rubric :: Input arguments
``wavelet`` (`Wavelet` object):
Source wavelet
"""
for tensor in self:
tensor.convolve(wavelet)
def tag_add(self, tag):
""" Appends string to tags list
Tags can be used to support customized uses, such as storing metdata not
included in ``mtuq.Station``
"""
if type(tag) not in [str, unicode]:
raise TypeError
for tensor in self:
if tag not in tensor.tags:
tensor.tags.append(tag)
def tag_remove(self, tag):
""" Removes string from tags list
"""
for tensor in self:
if tag in tensor.tags:
tensor.tags.remove(tag)
def sort_by_distance(self, reverse=False):
""" Sorts in-place by hypocentral distance
"""
self.sort_by_function(lambda stream: stream.distance,
reverse=reverse)
def sort_by_azimuth(self, reverse=False):
""" Sorts in-place by source-receiver azimuth
"""
self.sort_by_function(lambda stream: stream.azimuth,
reverse=reverse)
def sort_by_function(self, function, reverse=False):
""" Sorts in-place using the python built-in `sort`
"""
self.sort(key=function, reverse=reverse)
def __copy__(self):
try:
new_id = self.id+'_copy'
except:
new_id = None
new_ds = type(self)(id=new_id)
for stream in self:
new_ds.append(deepcopy(stream))
return new_ds
def write(self, filename):
""" Writes a Python pickle of current `GreensTensorList`
"""
with open(filename, "wb") as file:
pickle.dump(self, file)