-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveform_fetch.py
More file actions
110 lines (85 loc) · 5.18 KB
/
waveform_fetch.py
File metadata and controls
110 lines (85 loc) · 5.18 KB
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
import os
import pandas as pd
import numpy as np
from obspy.core import UTCDateTime
from obspy.clients.fdsn.client import FDSNNoDataException, Client
main_path = os.path.dirname(os.path.abspath(__file__))
output_path = os.path.join(main_path, 'geofon_waveforms')
os.makedirs(output_path, exist_ok=True)
if __name__ == "__main__":
#PANDAS DATAFRAME--------------------------------------------------------------------------------------------------
filepath = os.path.join(main_path, 'earthquakes_filtered.txt')
events = pd.read_csv(filepath)
#OBSPY PARAMETERS---------------------------------------------------------------------------------------------
GEOFON_client = Client("GEOFON")
network_code = "GE"
station_code = "LVC"
channel_code = 'BHZ, BHN, BHE' #B (broad band, high sample rate 10-80 Hz); H(weak motion sensor - e.g. velocity); Z, 1, 2 (single component sensor)
channel_code_alt = 'BHZ, BH1, BH2'
location_code = "10" #reserved for weak motion sensors
dt = 60 #we take waveform in the span of 1 min before and after the event time stamp
waveform_array_lenght = 4801 #stdard waveform array lenght
#FILE PARAMETERS------------------------------------------------------------------------------------------------
start_row = 4000 #the events before this had no data to provide - it saves some time
end_row = len(events) #we go until the end
files_created = 0
desired_amount_of_files = 50000
#EVENT ITERATION-------------------------------------------------------------------------------------------------
for idx, row in events.iloc[start_row:end_row,:].iterrows(): #iterate through events
if files_created < desired_amount_of_files:
time_stamp = UTCDateTime(int(row['year']),
int(row['month']),
int(row['day']),
int(row['hour']),
int(row['minute']),
float(row['second']))
t1 = time_stamp - 60
t2 = time_stamp + 60
try:
#waveform fetching
waveform = GEOFON_client.get_waveforms(network = network_code,
station = station_code,
location = location_code,
channel = channel_code,
starttime = t1,
endtime = t2)
traces = [trace.data for trace in waveform]
if (len(traces) == 3):
if (len(traces[0]) == len(traces[1]) == len(traces[2]) == waveform_array_lenght):
#writing to mseed file
waveform_file_name = f"{int(row['event_id'])}.mseed"
waveform_file_path = os.path.join(output_path, waveform_file_name)
waveform.write(waveform_file_path, format = "MSEED")
files_created += 1
print(f"Created file {waveform_file_name} for event at {time_stamp}")
else:
try:
waveform = GEOFON_client.get_waveforms(
network = network_code,
station = station_code,
location = location_code,
channel = channel_code_alt,
starttime = t1,
endtime = t2
)
traces = [trace.data for trace in waveform]
if (len(traces) == 3):
if (len(traces[0]) == len(traces[1]) == len(traces[2]) == waveform_array_lenght):
#writing to mseed file
waveform_file_name = f"{int(row['event_id'])}.mseed"
waveform_file_path = os.path.join(output_path, waveform_file_name)
waveform.write(waveform_file_path, format = "MSEED")
files_created += 1
print(f"Created file {waveform_file_name} for event at {time_stamp}")
else:
print(f"Event {int(row['event_id'])} at {time_stamp} doesnt have array of same lenghts")
else:
print(f"Event {int(row['event_id'])} at {time_stamp} had a direction missing")
except FDSNNoDataException: #if we couldnt find the waveform for the given event
print(f"No available data for event {int(row['event_id'])} at {time_stamp}")
continue
except FDSNNoDataException: #if we couldnt find the waveform for the given event
print(f"No available data for event {int(row['event_id'])} at {time_stamp}")
continue
else:
break