-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_3.py
More file actions
54 lines (45 loc) · 1.48 KB
/
Copy pathexample_3.py
File metadata and controls
54 lines (45 loc) · 1.48 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
import numpy as np
import matplotlib.pyplot as plt
import csv
from datetime import datetime
from utilities import create_folder
from plot_utilities import set_font_size
save_folder = "./images/example_3/"
create_folder(save_folder)
set_font_size(18)
# Load data from csv file
csv_path = "data/GCMT_Catalog.csv"
# Example line from the csv file:
# ,Lon,Lat,Depth,Time,Mag
# 0,-177.64,-28.61,59000.0,1976-01-01T01:29:39.600000Z,7.25
with open(csv_path, newline="") as csvfile:
reader = csv.DictReader(csvfile)
time = []
magnitude = []
for row in reader:
time_str = row["Time"]
time_float = datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S.%f%z").timestamp()
time.append(time_float)
magnitude.append(float(row["Mag"]))
time = np.array(time)
time_0 = time[0]
time -= time_0 # Normalize time to start from zero
# time /= 365.25 * 24 * 3600 # Convert time to years
magnitude = np.array(magnitude)
def time_to_string(time_float):
time_float += time_0 # Add back the original time offset
dt = datetime.fromtimestamp(time_float)
return dt.strftime("%Y-%m-%d %H:%M:%S")
assert len(time) == len(magnitude)
n = len(time)
n = n // 20
time = time[:n]
magnitude = magnitude[:n]
print(f"Number of samples: {n}")
print(f"Time range: {time_to_string(time[0])} to {time_to_string(time[-1])}")
plt.figure()
plt.plot(time, magnitude, marker="o", linestyle="-")
plt.xlabel("Time (seconds)")
plt.ylabel("$s$")
plt.tight_layout()
plt.savefig(f"{save_folder}/signal.pdf")