Skip to content

Commit dd57812

Browse files
authored
Add files via upload
1 parent 1defc8d commit dd57812

File tree

1 file changed

+66
-47
lines changed

1 file changed

+66
-47
lines changed

RESTORE.py

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def filter_catalog(catalog, t_start, serial_times, fault_length_multiplier):
231231

232232
idx_big_shock = np.argmax(catalog[:, MagnColumn])
233233
mag_big_shock = catalog[idx_big_shock, MagnColumn]
234+
print("Mag_big_shock = ", mag_big_shock)
234235
lat_big_shock = catalog[idx_big_shock, LatColumn]
235236
lon_big_shock = catalog[idx_big_shock, LonColumn]
236237

@@ -241,8 +242,8 @@ def filter_catalog(catalog, t_start, serial_times, fault_length_multiplier):
241242
ymax, xmax = trainv(lat_big_shock, lon_big_shock, fault_length_multiplier * l_big_shock, fault_length_multiplier * l_big_shock)
242243
ymin, xmin = trainv(lat_big_shock, lon_big_shock, -fault_length_multiplier * l_big_shock, -fault_length_multiplier * l_big_shock)
243244
print(f"Subcatalog limits: "
244-
f"LAT_MIN = {ymin}, LAT_MAX = {ymax} "
245-
f"LON_MIN = {xmin}, LON_MAX = {xmax}")
245+
f"LAT_MIN = {round(ymin, 2)}, LAT_MAX = {round(ymax, 2)} "
246+
f"LON_MIN = {round(xmin, 2)}, LON_MAX = {round(xmax, 2)}")
246247

247248
mask = (ymin <= catalog[:, LatColumn]) & (catalog[:, LatColumn] <= ymax) & \
248249
(xmin <= catalog[:, LonColumn]) & (catalog[:, LonColumn] <= xmax)
@@ -311,6 +312,26 @@ def serial_time(input_):
311312
return out2
312313

313314

315+
def dynamic_date_formatter(dates):
316+
max_date = max(dates)
317+
min_date = min(dates)
318+
date_range_days = (max_date - min_date).days
319+
320+
if date_range_days > 365: # Over a year
321+
fmt = '%b-%Y'
322+
elif date_range_days > 30: # Over a month
323+
fmt = '%b-%d'
324+
else: # Shorter intervals
325+
fmt = '%b-%d'
326+
return mdates.DateFormatter(fmt)
327+
328+
329+
def equally_spaced_ticks(dates, num_ticks=6):
330+
min_date = min(dates)
331+
max_date = max(dates)
332+
return mdates.drange(min_date, max_date, (max_date - min_date) / (num_ticks - 1))
333+
334+
314335
def timestamp_to_datetime(timestamp):
315336
# Converts custom timestamps to datetime objects
316337

@@ -539,15 +560,16 @@ def mc_vs_time(magcat, magcat_presequence, mc_ok, st_dev_multiplier, alpha, seri
539560
datenums1 = mdates.date2num(dates1)
540561
mc_time_plot = [mc_time[i] for i in idx_t_window_plot]
541562

542-
fig, ax = plt.subplots(figsize=(8, 6))
543-
ax.xaxis_date()
544-
fmt = mdates.DateFormatter('%b %Y')
545-
ax.xaxis.set_major_formatter(fmt)
563+
fig, ax = plt.subplots(figsize=(10, 6))
564+
ticks = equally_spaced_ticks(dates1)
565+
ax.set_xticks(ticks)
566+
ax.xaxis.set_major_formatter(dynamic_date_formatter(dates1))
546567
ax.plot(datenums1, mc_time_plot, label='Mc(t)', ls='-')
547568
ax.fill_between(datenums1, upper_lim, mc_time_plot,
548-
where=upper_lim <= mc_time_plot, color='C1', alpha=0.7, label = f"$M_c \geq M^*_c$ + {st_dev_multiplier} $\sigma$")
549-
ax.set_xlabel("Time")
550-
ax.set_ylabel("Mc")
569+
where=upper_lim <= mc_time_plot, color='C1', alpha=0.7,
570+
label=f"$M_c \geq M^*_c$ + {st_dev_multiplier} $\sigma$")
571+
ax.set_xlabel("Time", labelpad=10, fontsize=14)
572+
ax.set_ylabel("Mc", labelpad=10, fontsize=14)
551573
ax.legend(loc='upper right')
552574
plt.savefig('fig/Mc_Time.pdf', format='pdf')
553575

@@ -576,17 +598,14 @@ def map_plot(catalog, lon1, lat1, lon2, lat2):
576598

577599
parallels = np.arange(-90, 90, lat_interval)
578600
meridians = np.arange(-180, 180, lon_interval)
579-
m.drawparallels(parallels, labels=[0, 1, 0, 0], fontsize=8, dashes=[1, 5])
580-
m.drawmeridians(meridians, labels=[0, 0, 1, 0], fontsize=8, dashes=[1, 5])
601+
m.drawparallels(parallels, labels=[1, 1, 1, 1], fontsize=10, dashes=[1, 5])
602+
m.drawmeridians(meridians, labels=[1, 1, 1, 1], fontsize=10, dashes=[1, 5])
581603
m.fillcontinents(color='#F0F0F0', lake_color='#F0F0F0')
582-
583604
x1, y1 = m(lon1, lat1)
584605
x2, y2 = m(lon2, lat2)
585-
586606
plt.plot(x1, y1, 'o', color='0.3', alpha=.7, markersize=5, markeredgecolor='w')
587607
plt.plot(x2, y2, 'o', color='steelblue', alpha=.7, markersize=5, markeredgecolor='w')
588-
plt.xlabel('Longitude', fontsize=12, labelpad=10)
589-
plt.ylabel('Latitude', fontsize=12, labelpad=10)
608+
590609

591610
return m
592611

@@ -645,7 +664,7 @@ def magnitude_distribution(magcat):
645664
bin_edges = np.arange(min_m, max_m + bin_m, bin_m)
646665
bin_counts = np.zeros(len(bin_edges), dtype=int)
647666
for mag in magcat:
648-
bin = int((mag - min_m) / bin_m)
667+
bin = int(round(mag / bin_m - min_m*10))
649668
bin_counts[bin] += 1
650669

651670
idx_zeros = np.where(bin_counts == 0)[0]
@@ -734,16 +753,17 @@ def ghost_element(catalog, t_start, mc_user, b_user, size, step, st_dev_multipli
734753

735754
# Fit hypo depth distribution
736755
z_data = filtered_catalog[:, DepthColumn]
756+
bins = np.linspace(min(z_data), max(z_data), 101)
737757
if depth_distribution == 'bimodal':
738-
y, x = np.histogram(z_data, bins=100)
758+
y, x = np.histogram(z_data, bins=bins)
739759
else:
740-
y, x = np.histogram(z_data, density=1., bins=100)
760+
y, x = np.histogram(z_data, density=1., bins=bins)
741761
x = (x[1:]+x[:-1])/2 # bin centers
742762

743763
distribution_function = select_distribution(depth_distribution)
744764
params = fit_distribution(distribution_function, x, y, p0=p0)
745765
print('Fitted parameters = ', params)
746-
y, x, _ = plt.hist(z_data, 100)
766+
y, x, _ = plt.hist(z_data, bins)
747767
x_fit = np.linspace(x.min(), x.max(), 500)
748768
y_fit = distribution_function(x_fit, *params)
749769

@@ -879,7 +899,7 @@ def ghost_element(catalog, t_start, mc_user, b_user, size, step, st_dev_multipli
879899
z_ghost.extend([round(value, 1) for value in samples])
880900

881901
# Generate n=n_to_sample random numbers btw 0 and 1
882-
u = np.random.random(n_ghost_hole[i])
902+
u = np.random.random(n_to_sample)
883903

884904
for j in range(len(u)):
885905
if u[j] < cumulative_sum[0]:
@@ -897,21 +917,25 @@ def ghost_element(catalog, t_start, mc_user, b_user, size, step, st_dev_multipli
897917
lat_ghost.extend(lat_ghost_hole)
898918

899919
# Plot hypo depth distribution
900-
fig, ax = plt.subplots()
920+
921+
data_min = np.min(z_ghost)
922+
data_max = np.max(z_ghost)
923+
margin = (data_max - data_min) * 0.95
924+
depth_min, depth_max = data_min - margin, data_max + margin
925+
926+
fig, ax = plt.subplots(figsize=(8, 6))
901927
if depth_distribution == 'bimodal':
902-
plt.hist(z_data, bins=100, alpha=0.3, color='0.3', label='Hypo depths (filtered) original')
903-
plt.plot(x_fit, y_fit, color='red', label=f"Fitted distribution: {depth_distribution}")
904-
plt.hist(z_ghost, bins=50, color='steelblue', label='Hypo depths replenished')
905-
plt.xlabel('Hypocenter Depth [Km]')
906-
plt.legend()
907-
plt.savefig('fig/Hypo_Depth_Distribution.pdf', format='pdf')
928+
ax.hist(z_data, bins=bins, alpha=0.3, color='0.3', label='Hypo depths (filtered) original')
929+
ax.plot(x_fit, y_fit, color='red', label=f"Fitted distribution: {depth_distribution}")
930+
ax.hist(z_ghost, bins=bins, color='steelblue', label='Hypo depths replenished')
908931
else:
909-
plt.hist(z_data, bins=100, density=True, alpha=0.3, color='0.3', label='Hypo depths (filtered) original')
910-
plt.plot(x_fit, y_fit, color='red', label=f"Fitted distribution: {depth_distribution}")
911-
plt.hist(z_ghost, bins=50, density=True, color='steelblue', label='Hypo depths replenished')
912-
plt.xlabel('Hypocenter Depth [Km]')
913-
plt.legend()
914-
plt.savefig('fig/Hypo_Depth_Distribution.pdf', format='pdf')
932+
ax.hist(z_data, bins=bins, density=True, alpha=0.3, color='0.3', label='Hypo depths (filtered) original')
933+
ax.plot(x_fit, y_fit, color='red', label=f"Fitted distribution: {depth_distribution}")
934+
ax.hist(z_ghost, bins=bins, density=True, color='steelblue', label='Hypo depths replenished')
935+
ax.set_xlim(depth_min, depth_max)
936+
ax.set_xlabel('Hypocenter Depth [km]', labelpad=10, fontsize=14)
937+
ax.legend()
938+
plt.savefig('fig/Hypo_Depth_Distribution.pdf', format='pdf')
915939

916940
####
917941

@@ -1036,7 +1060,7 @@ def replenished_catalog(catalog, t_start, mc_user, b_user, size, step, st_dev_mu
10361060
# flag: 0 for original events, 1 for simulated events
10371061
flag = np.array([0] * len(new_times))
10381062
for i in range(len(new_times)):
1039-
if i <= len(serial_times_compl):
1063+
if i < len(serial_times_compl):
10401064
flag[i] = 0
10411065
else:
10421066
flag[i] = 1
@@ -1085,19 +1109,14 @@ def replenished_catalog(catalog, t_start, mc_user, b_user, size, step, st_dev_mu
10851109
bin_edges_replenished, log_bin_counts_replenished, log_cum_counts_replenished = \
10861110
magnitude_distribution(replenished_catalog[:, MagnColumn])
10871111

1088-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 6))
1089-
ax1.plot(bin_edges_original, log_bin_counts_original, 'o', color='0.5', markersize=3, label='Non-cumulative counts')
1090-
ax1.plot(bin_edges_original, log_cum_counts_original, 'ok', markersize=3, label='Cumulative counts')
1091-
ax1.legend(loc='upper right')
1092-
ax1.set_xlabel("Magnitude")
1093-
ax1.set_ylabel("Frequency")
1094-
ax1.set_title("Original Catalog")
1095-
ax2.plot(bin_edges_replenished, log_bin_counts_replenished, 'o', color='0.5', markersize=3, label='Non-cumulative counts')
1096-
ax2.plot(bin_edges_replenished, log_cum_counts_replenished, 'ok', markersize=3, label='Cumulative counts')
1097-
ax2.legend(loc='upper right')
1098-
ax2.set_xlabel("Magnitude")
1099-
ax2.set_ylabel("Frequency")
1100-
ax2.set_title("Replenished Catalog")
1112+
fig, ax = plt.subplots(figsize=(8, 10))
1113+
ax.plot(bin_edges_original, log_bin_counts_original, 'o', color='0.5', markersize=3, label='Non-cumulative counts (Original)', alpha=0.7)
1114+
ax.plot(bin_edges_original, log_cum_counts_original, '-', color='0.5', label='Cumulative counts (Original)', alpha=0.7)
1115+
ax.plot(bin_edges_replenished, log_bin_counts_replenished, 'o', color='black', markersize=3, label='Non-cumulative counts (Replenished)', alpha=0.7)
1116+
ax.plot(bin_edges_replenished, log_cum_counts_replenished, '-', color='black', label='Cumulative counts (Replenished)', alpha=0.7)
1117+
ax.set_xlabel("Magnitude", labelpad=10, fontsize=14)
1118+
ax.set_ylabel("Frequency", labelpad=10, fontsize=14)
1119+
ax.legend(loc='upper right')
11011120
plt.savefig('fig/Magnitude_Frequency_Distribution.pdf', format='pdf')
11021121

11031122
return replenished_catalog

0 commit comments

Comments
 (0)