Browse Source

adding restitution and utils

master
Marius Isken 5 months ago
parent
commit
6abdc60167
  1. 8
      .pre-commit-config.yaml
  2. 1
      requirements-dev.txt
  3. 1
      requirements.txt
  4. 27
      src/__init__.py
  5. 53
      src/restitution.py
  6. 22
      src/utils.py
  7. 9
      tests/test_remove_idas_vibration.py

8
.pre-commit-config.yaml

@ -13,11 +13,3 @@ repos:
hooks:
- id: black
# language_version: python3.6
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]

1
requirements-dev.txt

@ -0,0 +1 @@
pytest

1
requirements.txt

@ -1 +1,2 @@
pyrocko
numpy

27
src/__init__.py

@ -0,0 +1,27 @@
import pyrocko
import numpy as num
from .utils import traces_to_numpy_and_meta
def remove_idas_instrument_vibration(traces, copy=True, return_ref=False):
data, meta = traces_to_numpy_and_meta(traces)
ntraces_ref = int((meta.start_distance * 0.9) / meta.spatial_resolution)
ref = num.mean(data[:ntraces_ref], axis=1)
out_traces = []
for tr in traces:
if copy:
tr = tr.copy()
tr.ydata -= ref
out_traces.append(tr)
if return_ref:
trace_ref = tr.copy(data=False)
trace_ref.ydata = ref
trace_ref.station = "ref"
out_traces.append(trace_ref)
return out_traces

53
src/restitution.py

@ -0,0 +1,53 @@
import numpy as num
from .utils import traces_to_numpy_and_meta
def de_to_e(traces, copy=False):
out_traces = []
for tr in traces:
if copy:
tr = tr.copy()
tr.set_ydata(num.cumsum(tr.ydata) * tr.deltat)
out_traces.append(tr)
return out_traces
def de_to_velocity_static_slowness(traces, slowness, copy=False):
out_traces = de_to_e(traces, copy)
for tr in out_traces:
tr.set_ydata(tr.ydata / slowness)
return out_traces
def de_to_acceleration_static_slowness(traces, slowness, copy=False):
out_traces = []
for tr in traces:
if copy:
tr = tr.copy()
tr.set_ydata(tr.ydata / slowness)
return out_traces
def de_to_relative_displacement(traces, copy=False):
traces = de_to_e(traces, copy)
data, meta = traces_to_numpy_and_meta(traces)
data = num.cumsum(data, axis=0) * meta.spatial_resolution
for itr, tr in enumerate(traces):
tr.set_ydata(data[itr])
return traces
def de_to_relative_velocity(traces, copy=False):
traces = de_to_e(traces, copy)
data, meta = traces_to_numpy_and_meta(traces)
data = num.diff(data, n=1, axis=0) / meta.spatial_resolution
for itr, tr in enumerate(traces):
tr.set_ydata(data[itr])
return traces

22
src/utils.py

@ -0,0 +1,22 @@
import numpy as num
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def traces_to_numpy_and_meta(traces):
ntraces = len(traces)
nsamples = set(tr.nsamples for tr in traces)
assert len(nsamples) == 1, "Traces nsamples differ"
nsamples = nsamples[0]
data = num.zeros(ntraces, nsamples)
for itr, tr in enumerate(traces):
data[itr, :] = tr.ydata
meta = tr.meta
return data, AttrDict(meta)

9
tests/test_remove_idas_vibration.py

@ -0,0 +1,9 @@
from pyrocko import io
from pyrocko_das import remove_idas_instrument_vibration
traces = io.read("")
def test_remove_idas_vibration():
remove_idas_instrument_vibration(traces)
Loading…
Cancel
Save