-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatalogPipeline.py
325 lines (277 loc) · 11.1 KB
/
CatalogPipeline.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
"""
Allows the execution of the catalog creation pipeline, including:
- event detection (for all stations)
- gap complementation
- catalog consolidation
- earthquake classification
"""
from awesam import EarthquakeClassification, EventCatalog, CatalogConsolidation
from awesam import config, util
import datetime as dt
import pandas as pd
import multiprocessing
import traceback
import logging
import os
def execute_catalog_pipeline(
name: str,
start: dt.datetime,
end: dt.datetime,
csv_directory: str = None,
checkpoint: bool = False,
) -> None:
"""
Note: Use multiprocessing_pipeline instead of this function. If multiprocessing is unwanted,
you can disable it with use_multiprocessing=False.
The CatalogPipeline includes:
- Creation of event- and gap- catalogs for all stations.
- Catalog consolidation (transfer gaps, event-probability)
- Earthquake classification (earthquake-probability)
- Saving final catalogs.
The seismic data must alredy exist in the mseed_directory.
- `name`: Name used to identify catalog
- `start`: start day
- `end`: end day (inclusive)
- `csv_directory`: output directory of catalogs
- `checkpoint`: While creating the catalog, checkpoint files are generated,
to return to the current progress after interruption. If `True` these files are used.
"""
try:
if csv_directory is None:
csv_directory = config.settings["general"]["catalog_directory"]
# Logging
logger = _get_logger()
logger.name = f"pipeline.{start.strftime('%Y-%m-%d')}"
logger.info(
f'Starting CatalogPipeline from {start.strftime("%Y-%m-%d")} to {end.strftime("%Y-%m-%d")}'
)
# get station codes from settings
station_seeds = util.get_station_seeds(config.settings["general"]["stations"])
# Create catalog (catalogs are generated and saved in csv_directory)
# besides the event_catalog, a gap_catalog is also created.
for station_seed in station_seeds:
EventCatalog.create_catalog(
start,
end,
os.path.join(
config.settings["general"]["mseed_directory"],
util.get_station_name_from_seed(station_seed).lower(),
"mseed",
),
csv_directory,
name,
station_seed,
checkpoint=checkpoint,
)
logger.info(f"Finished generating EventCatalog for {station_seed}.")
logger.info("Finished generating EventCatalog for all stations.")
# Load catalogs for all stations
catalogs, gaps = {}, {}
for station_seed in station_seeds:
station_name = util.get_station_name_from_seed(station_seed)
catalogs[station_seed] = _load_catalog(
csv_directory, name, station_name.lower()
)
gaps[station_seed] = _load_gaps_catalog(
csv_directory, name, station_name.lower()
)
# Catalog Consolidation
if config.settings["general"]["pipeline"]["use_catalog_consolidation"]:
if len(station_seeds) < 2:
raise ValueError("Two stations are needed for catalog consolidation")
# defining station identifiers
principal_station = station_seeds[0]
complementary_station = station_seeds[1]
logger.info(
f"Start CatalogConsolidation with data from {principal_station} and {complementary_station}"
)
catalog_final = CatalogConsolidation.consolidate_catalog(
catalogs[principal_station],
catalogs[complementary_station],
gaps[principal_station],
gaps[complementary_station],
util.get_channels(principal_station),
util.get_channels(complementary_station),
config.settings["CatalogConsolidation"]["default_metric"],
config.settings["CatalogConsolidation"]["window_length"],
start,
)
else:
catalog_final = catalogs[list(catalogs)[0]]
# Earthquake Classification
if config.settings["general"]["pipeline"]["use_earthquake_classification"]:
catalog_earthquake = _load_earthquake_catalog(
config.settings["general"]["earthquake_catalog_directory"], start, end
)
catalog_final[
"earthquake_probability"
] = CatalogConsolidation.compute_earthquake_probabilities(
catalog_final,
catalog_earthquake,
start,
config.settings["EarthquakeClassification"]["earthquake_metric"],
config.settings["CatalogConsolidation"]["window_length"],
)
# Saving
_save_catalog(catalog_final, name, csv_directory)
logger.info(
f'Finished from {start.strftime("%Y-%m-%d")} to {end.strftime("%Y-%m-%d")}'
)
except Exception as e:
print(traceback.format_exc())
raise e
def multiprocessing_pipeline(
name: str,
start: dt.datetime,
end: dt.datetime,
checkpoint: bool = False,
use_multiprocessing=True,
) -> None:
"""
CatalogPipeline with multiprocessing.
See CatalogPipeline.execute_catalog_pipeline for details.
"""
util.check_settings_integrity()
csv_directory = config.settings["general"]["catalog_directory"]
chunk_length = dt.timedelta(days=max(4, min(50, (end - start).days // 10)))
chunks = pd.date_range(start, end, freq=chunk_length).to_pydatetime()
# make directory for temporary files of individual processes (if not exists)
if not os.path.isdir(os.path.join(csv_directory, "tmp")):
os.mkdir(os.path.join(csv_directory, "tmp"))
# compile parameter list
parameter_list = []
for i, (date_start) in enumerate(chunks):
if date_start == chunks[-1]: # if last chunk
date_end = end
else:
date_end = date_start + chunk_length - dt.timedelta(days=1)
parameter_list.append(
[
f"{name}_{i}",
date_start,
date_end,
os.path.join(csv_directory, "tmp"),
checkpoint,
]
)
# start catalog pipeline
if use_multiprocessing:
p = multiprocessing.Pool(32)
p.starmap(execute_catalog_pipeline, parameter_list)
p.close()
p.join()
else:
for i, params in enumerate(parameter_list):
execute_catalog_pipeline(*params)
# merge and delete all files that were created temporarily.
_join_temporary_files(name, start, end, csv_directory)
def _join_temporary_files(
name: str, start: dt.datetime, end: dt.datetime, csv_directory: str
) -> None:
"""
When using multiprocessing_pipeline, temporary files are generated.
This method merges everything into one catalog.
"""
station_names = [
s.lower() + "_events"
for s in util.get_station_names(config.settings["general"]["stations"])
]
# get and filter files in the tmp directory by `name`
files = os.listdir(os.path.join(csv_directory, "tmp"))
files = [file for file in files if file.find(name) != -1]
# compose catalog for each station and FINAL
for catalog_type in ["FINAL", *station_names]:
final_files = [f for f in files if f.find(f"{catalog_type}.csv") != -1]
final_catalogs = [
pd.read_csv(os.path.join(os.path.join(csv_directory, "tmp"), f))
for f in final_files
]
final_df = pd.concat(final_catalogs)
final_df.sort_values("time", inplace=True)
final_df.to_csv(
os.path.join(csv_directory, f"{name}_{catalog_type}.csv"), index=False
)
# gap catalog (using all stations)
gap_catalogs = []
for station in util.get_station_names(config.settings["general"]["stations"]):
# filter by station and gap-catalog
station_files = [
file
for file in files
if (file.find(station.lower()) != -1) and (file.find("gaps.csv") != -1)
]
for f in station_files:
c = pd.read_csv(os.path.join(os.path.join(csv_directory, "tmp"), f))
c["station"] = station
gap_catalogs.append(c)
gap_df = pd.concat(gap_catalogs)
gap_df.to_csv(os.path.join(csv_directory, f"{name}_gaps.csv"), index=False)
# delete temporary files
for f in files:
os.remove(os.path.join(csv_directory, "tmp", f))
def _get_logger():
logger = logging.getLogger("Pipeline")
if not len(logger.handlers):
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)
ch.setLevel(logging.INFO)
logger.addHandler(ch)
return logger
def _load_catalog(
csv_directory: str, name: str, station: str, suffix="_events.csv"
) -> pd.DataFrame:
"""
Loads the event-catalog.
"""
df = pd.read_csv(os.path.join(csv_directory, name + "_" + station + suffix))
df["time"] = pd.to_datetime(df["time"])
return df
def _load_gaps_catalog(csv_directory: str, name: str, station: str) -> pd.DataFrame:
"""
Loads the gap-catalog.
"""
df = pd.read_csv(os.path.join(csv_directory, name + "_" + station + "_gaps.csv"))
df["start"] = pd.to_datetime(df["start"])
df["end"] = pd.to_datetime(df["end"])
return df
def _load_earthquake_catalog(
directory: str, start: dt.datetime, end: dt.datetime, return_full: bool = False
) -> pd.DataFrame:
"""
Reads the specified earthquake catalogs.
Also computes the intensity for each earthquake and only returns earthquakes over the threshold.
The catalog is localized for the volcano.
"""
# load all necessary years
eq_catalog = []
for year in range(start.year, end.year + 1):
try:
c = pd.read_csv(os.path.join(directory, str(year) + ".csv"))
except FileNotFoundError:
raise FileNotFoundError(f"Earthquake Catalog for {year} does not exist.")
else:
if "date" in c.columns:
c.rename(columns={"date": "time"}, inplace=True)
eq_catalog.append(c)
eq_catalog = pd.concat(eq_catalog)
eq_catalog["time"] = pd.to_datetime(eq_catalog["time"])
# filter catalog
eq_catalog = eq_catalog[(eq_catalog["time"] > start) & (eq_catalog["time"] < end)]
# localize catalog
eq_catalog = EarthquakeClassification._earthquake_catalog_to_local(eq_catalog)
eq_catalog = eq_catalog[
eq_catalog["intensity"]
> config.settings["EarthquakeClassification"]["intensity_threshold"]
]
if return_full:
return eq_catalog
else:
return eq_catalog[["arrival_time", "amplitude"]]
def _save_catalog(catalog: pd.DataFrame, name: str, directory: str) -> None:
"""
Writes the final catalog to disk, with the suffix '_FINAL'.
"""
catalog.to_csv(os.path.join(directory, name + "_FINAL.csv"), index=False)