|
| 1 | +# BEGIN 1 |
| 2 | +import numpy as np |
| 3 | +import pygrt |
| 4 | + |
| 5 | +modarr = np.loadtxt("milrow") |
| 6 | + |
| 7 | +pymod = pygrt.PyModel1D(modarr, depsrc=10, deprcv=0.0) |
| 8 | + |
| 9 | +nt = 500 |
| 10 | +dt = 10 |
| 11 | + |
| 12 | +st_grn = pymod.compute_grn(5000, nt=nt, dt=dt, keepAllFreq=True, statsfile="pygrtstats")[0] |
| 13 | +# END 1 |
| 14 | + |
| 15 | +# 仅绘制一个分量做示例 |
| 16 | +CHANNEL = "SSR" |
| 17 | + |
| 18 | +# ================================================================= |
| 19 | +# 绘制波形 |
| 20 | +import matplotlib.pyplot as plt |
| 21 | +from obspy import * |
| 22 | +plt.rcParams.update({ |
| 23 | + "font.sans-serif": "Times New Roman", |
| 24 | + "mathtext.fontset": "cm" |
| 25 | +}) |
| 26 | + |
| 27 | +t = np.arange(nt)*dt |
| 28 | +freqs = np.arange(nt//2 + 1)/(nt*dt) |
| 29 | + |
| 30 | +def plot_waves(tr:Trace): |
| 31 | + fig, ax = plt.subplots(figsize=(8, 1.5)) |
| 32 | + ax.plot(t, tr.data, 'k-', lw=0.5) |
| 33 | + ax.grid() |
| 34 | + ax.text(0.02, 0.9, CHANNEL, transform=ax.transAxes, fontsize=10, ha='left', va='top', bbox=dict(lw=1, fc='w')) |
| 35 | + ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) |
| 36 | + ax.set_xlim(0, nt*dt) |
| 37 | + ax.set_xlabel("Time (s)", fontsize=12) |
| 38 | + return fig, ax |
| 39 | + |
| 40 | +fig, ax = plot_waves(st_grn.select(channel=CHANNEL)[0]) |
| 41 | +fig.savefig("grn.png", dpi=300, bbox_inches='tight') |
| 42 | +# ================================================================= |
| 43 | + |
| 44 | +# ================================================================= |
| 45 | +# 回退虚频率补偿 |
| 46 | +st_grn2 = st_grn.copy() |
| 47 | +wI = st_grn2[0].stats.sac['user0'] |
| 48 | +for tr in st_grn2: |
| 49 | + tr.data[:] *= np.exp(-t*wI) |
| 50 | + |
| 51 | +fig, ax = plot_waves(st_grn2.select(channel=CHANNEL)[0]) |
| 52 | +fig.savefig("grn2.png", dpi=300, bbox_inches='tight') |
| 53 | +# ================================================================= |
| 54 | + |
| 55 | + |
| 56 | +# ================================================================= |
| 57 | +# 绘制频谱 |
| 58 | +from scipy.fft import rfft, irfft |
| 59 | + |
| 60 | +def plot_freqs(tr:Trace): |
| 61 | + fig, ax = plt.subplots(figsize=(8, 1.5)) |
| 62 | + data = tr.data.copy() |
| 63 | + |
| 64 | + freqs0 = freqs.copy() |
| 65 | + freqs0[0] *= 0.1 |
| 66 | + ax.plot(freqs0[:-1], np.abs(rfft(data))[:-1], 'k-', lw=0.8) |
| 67 | + ax.grid() |
| 68 | + ax.text(0.02, 0.9, CHANNEL, transform=ax.transAxes, fontsize=10, ha='left', va='top', bbox=dict(lw=1, fc='w')) |
| 69 | + ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) |
| 70 | + |
| 71 | + ax.set_xscale('log') |
| 72 | + ax.set_yscale('log') |
| 73 | + ax.set_xlim(1e-4, 0.5/dt) |
| 74 | + ax.set_xlabel("Frequency (Hz)", fontsize=12) |
| 75 | + |
| 76 | + return fig, ax |
| 77 | + |
| 78 | +fig, ax = plot_freqs(st_grn.select(channel=CHANNEL)[0]) |
| 79 | +fig.savefig("grn_freqs.png", dpi=300, bbox_inches='tight') |
| 80 | +fig, ax = plot_freqs(st_grn2.select(channel=CHANNEL)[0]) |
| 81 | +fig.savefig("grn2_freqs.png", dpi=300, bbox_inches='tight') |
| 82 | +# ================================================================= |
| 83 | + |
| 84 | + |
| 85 | +# ================================================================= |
| 86 | +# 读入核函数 |
| 87 | +import glob |
| 88 | +paths = glob.glob("pygrtstats/K_000[0-5]_*") |
| 89 | +paths.sort() |
| 90 | +print(paths) |
| 91 | + |
| 92 | +fig, axs = plt.subplots(len(paths), 1, figsize=(8, len(paths)*1), gridspec_kw=dict(hspace=0), sharex=True) |
| 93 | +for i in range(len(paths)): |
| 94 | + ax = axs[i] |
| 95 | + path = paths[i] |
| 96 | + freq = float(path.split("_")[-1]) |
| 97 | + D = pygrt.utils.read_statsfile(path) |
| 98 | + ax.plot(D['k'], np.real(D['SS_q']), 'k-', lw=1, label='Real') |
| 99 | + ax.plot(D['k'], np.imag(D['SS_q']), 'k--', lw=1, label='Imag') |
| 100 | + ax.text(0.98, 0.9, f"{freq:.1e} Hz", transform=ax.transAxes, fontsize=10, ha='right', va='top', bbox=dict(lw=1, fc='w')) |
| 101 | + ax.grid() |
| 102 | + ax.set_xlim(xmax=D['k'][-1]) |
| 103 | + if i == 0: |
| 104 | + ax.legend(loc='lower center', ncols=2, bbox_to_anchor=(0.5, 1.05)) |
| 105 | + |
| 106 | +fig.savefig("kernels.png", dpi=300, bbox_inches='tight') |
| 107 | +# ================================================================= |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +# ================================================================= |
| 112 | +# 跳过频段,重新计算 |
| 113 | +st_grn3 = pymod.compute_grn(5000, nt=nt, dt=dt)[0] |
| 114 | + |
| 115 | +srctypes = ['EX', 'VF', 'HF', 'DD', 'DS', 'SS'] |
| 116 | +chlst = ['Z', 'R', 'T'] |
| 117 | +def plot_all_waves(st_grn:Stream): |
| 118 | + |
| 119 | + fig = plt.figure(figsize=(14, 2*len(srctypes))) |
| 120 | + gs = fig.add_gridspec(len(srctypes), 3, hspace=0.3, wspace=0.1) |
| 121 | + for ik, tr in enumerate(st_grn): |
| 122 | + channel = tr.stats.channel |
| 123 | + srctype = channel[:2] |
| 124 | + ch = channel[-1] |
| 125 | + |
| 126 | + ax = fig.add_subplot(gs[srctypes.index(srctype), chlst.index(ch)]) |
| 127 | + |
| 128 | + data = tr.data.copy() |
| 129 | + |
| 130 | + ax.plot(t, data, 'k-', lw=0.6) |
| 131 | + ax.grid() |
| 132 | + ax.text(0.1, 0.9, channel, transform=ax.transAxes, ha='center', va='top', bbox=dict(lw=1, fc='w')) |
| 133 | + ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) |
| 134 | + |
| 135 | + return fig |
| 136 | + |
| 137 | +fig = plot_all_waves(st_grn3.copy()) |
| 138 | +fig.savefig("grn3.png", dpi=100, bbox_inches='tight') |
| 139 | +# ================================================================= |
0 commit comments