forked from ETHZ-TEC/permafrostanalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_spectrogram.py
150 lines (124 loc) · 5.06 KB
/
plot_spectrogram.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
"""MIT License
Copyright (c) 2019, Swiss Federal Institute of Technology (ETH Zurich), Matthias Meyer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE."""
import stuett
from stuett.global_config import get_setting, setting_exists
import argparse
from pathlib import Path
from datetime import datetime
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
from tostools.signal.tfd import tfd_zam
parser = argparse.ArgumentParser(description="Seismic time series and spectogram plot")
parser.add_argument(
"-p",
"--path",
type=str,
default=str(Path(__file__).absolute().parent.joinpath("..", "..", "data/")),
help="The path to the folder containing the permafrost hackathon data",
)
parser.add_argument("-l", "--local", action="store_true", help="Only use local files and not data from Azure")
args = parser.parse_args()
data_path = Path(args.path)
if not args.local:
account_name = (
get_setting("azure")["account_name"]
if setting_exists("azure")
else "storageaccountperma8980"
)
account_key = (
get_setting("azure")["account_key"] if setting_exists("azure") else None
)
store = stuett.ABSStore(
container="hackathon-on-permafrost",
prefix="seismic_data/4D/",
account_name=account_name,
account_key=account_key,
)
else:
seismic_folder = Path(data_path).joinpath("seismic_data/4D/")
store = stuett.DirectoryStore(seismic_folder)
if "MH36/2017/EHE.D/4D.MH36.A.EHE.D.20171231_230000.miniseed" not in store:
raise RuntimeError(
"Please provide a valid path to the permafrost data or see README how to download it"
)
seismic_node = stuett.data.SeismicSource(
store=store,
station="MH36",
channel=["EHE", "EHN", "EHZ"],
start_time="2017-08-02 10:00:00",
end_time="2017-08-02 10:10:00",
)
''' If you have access to the [arclink service](arclink.ethz.ch/) you can use it to load your data
You can use the following two lines of code, but be careful not to accidentally publish your password!
```
stuett.global_config.set_setting("arclink", {"user": "yourusername", "password":"yourpassword"})
seismic_node = stuett.data.SeismicSource(use_arclink=True,store=....)
```
Better create a stuett config.yml file in your application folder which contains the line
```
arclink: {"user": "yourusername", "password":"yourpassword"}
```
The right location of the config file can be found by executing
```
python -c "import stuett;print(stuett.global_config.get_user_config_file())"
```'''
seismic_data = seismic_node()
print(seismic_data)
# Create figure
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.1)
fig.update_layout(title_text="Time series and spectrogram")
for i, seed_id in enumerate(seismic_data["seed_id"]):
for j, stream_id in enumerate(seismic_data["stream_id"]):
fig.add_trace(
go.Scatter(name = str(seed_id.values),
x=pd.to_datetime(seismic_data["time"].values),
y=seismic_data.sel(seed_id=seed_id, stream_id=stream_id).values,
),
row=1,
col=1,
)
# spectrogram = stuett.data.Spectrogram(nfft=512, stride=64, dim="time")
# spec = spectrogram(seismic_data)
#
# # select only one channel
# spec = spec.sel(seed_id="4D.MH36.A.EHE", stream_id=0)
#
# trace_hm = go.Heatmap(
# x=pd.to_datetime(spec["time"].values),
# y=spec["frequency"].values,
# z=np.log(spec.values),
# colorscale="Jet",
# hoverinfo="none",
# colorbar={"title": "Power Spectrum/dB"},
# )
# fig.add_trace(trace_hm, row=2, col=1)
s = pd.Series(seismic_data.sel(seed_id="4D.MH36.A.EHE", stream_id=0).values, index=pd.to_datetime(seismic_data["time"].values))
spec2 = np.absolute(tfd_zam(s, 512, stride=64, g_len=6000, h_len=768))
print("spec2 done ")
trace_hm2 = go.Heatmap(
x=spec2.index,
y=spec2.columns.values,
z=np.log(spec2.values.T),
colorscale="Jet",
hoverinfo="none",
colorbar={"title": "Power Spectrum/dB"},
)
fig.add_trace(trace_hm2, row=2, col=1)
fig.show()