예제 #1
0
파일: imaging.py 프로젝트: iceseismic/qopen
def plot_energies(energies,
                  bulk_window=None, coda_window=None, downsample_to=None,
                  xlim_lin=None, xlim_log=None,
                  figsize=None, **kwargs):
    gs = gridspec.GridSpec(2 * len(energies), 2)
    gs.update(wspace=0.05)
    fig = plt.figure(figsize=figsize)
    sax1 = sax3 = None
    for i, tr in enumerate(energies):
        pair = get_pair(tr)
        otime = tr.stats.origintime
        if downsample_to is None:
            d = 1
        else:
            d = tr.stats.sampling_rate // downsample_to
        ts = np.arange(len(tr)) * tr.stats.delta
        ts = ts - (otime - tr.stats.starttime)
        c = 'k'
        ax2 = plt.subplot(gs[2 * i + 1, 0], sharex=sax1, sharey=sax1)
        ax1 = plt.subplot(gs[2 * i, 0], sharex=ax2)
        ax3 = plt.subplot(gs[2 * i:2 * i + 2, 1], sharex=sax3, sharey=sax3)
        ax1.annotate('%s' % pair[1], (1, 0.5), (-10, 0), 'axes fraction',
                     'offset points', size='small', ha='right', va='center')
        ax3.annotate('%s' % pair[0], (0, 1), (10, -5), 'axes fraction',
                     'offset points', size='small', ha='left', va='top')
        ax1.plot(ts[::d], tr.data[::d], color=c)
        ax2.semilogy(ts[::d], tr.data[::d], color=c)
        ax3.loglog(ts[::d], tr.data[::d], color=c)
        for ax in (ax1, ax2, ax3):
            plt.setp(ax.get_xticklabels(), visible=False)
            ax.set_yticklabels([])
            if 'ponset' in tr.stats:
                tponset = tr.stats.ponset - otime
                ax.axvline(tponset, color='green', alpha=0.5)
            if 'sonset' in tr.stats:
                tsonset = tr.stats.sonset - otime
                ax.axvline(tsonset, color='b', alpha=0.5)
        for ax in (ax2, ax3):
            if bulk_window and coda_window:
                c = ('b', 'k')
                wins = (bulk_window[pair], coda_window[pair])
                for i, win in enumerate(wins):
                    ax.axvspan(win[0] - otime, win[1] - otime,
                               0.05, 0.08, color=c[i], alpha=0.5)

        if sax1 is None:
            sax1 = ax2
            sax3 = ax3
    if xlim_lin:
        ax1.set_xlim(xlim_lin)
    if xlim_log:
        ax3.set_xlim(xlim_log)
    loglocator = mpl.ticker.LogLocator(base=100)
    ax2.yaxis.set_major_locator(loglocator)
    ax3.yaxis.set_major_locator(loglocator)
    ax2.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    ax3.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    plt.setp(ax2.get_xticklabels(), visible=True)
    plt.setp(ax3.get_xticklabels(), visible=True)
    _savefig(fig, **kwargs)
예제 #2
0
def plot_energies(energies,
                  bulk_window=None, coda_window=None, downsample_to=None,
                  xlim_lin=None, xlim_log=None,
                  figsize=None, **kwargs):
    gs = gridspec.GridSpec(2 * len(energies), 2)
    gs.update(wspace=0.05)
    fig = plt.figure(figsize=figsize)
    sax1 = sax3 = None
    for i, tr in enumerate(energies):
        pair = get_pair(tr)
        otime = tr.stats.origintime
        if downsample_to is None:
            d = 1
        else:
            d = tr.stats.sampling_rate // downsample_to
        ts = np.arange(len(tr)) * tr.stats.delta
        ts = ts - (otime - tr.stats.starttime)
        c = 'k'
        ax2 = plt.subplot(gs[2 * i + 1, 0], sharex=sax1, sharey=sax1)
        ax1 = plt.subplot(gs[2 * i, 0], sharex=ax2)
        ax3 = plt.subplot(gs[2 * i:2 * i + 2, 1], sharex=sax3, sharey=sax3)
        ax1.annotate('%s' % pair[1], (1, 0.5), (-10, 0), 'axes fraction',
                     'offset points', size='small', ha='right', va='center')
        ax3.annotate('%s' % pair[0], (0, 1), (10, -5), 'axes fraction',
                     'offset points', size='small', ha='left', va='top')
        ax1.plot(ts[::d], tr.data[::d], color=c)
        ax2.semilogy(ts[::d], tr.data[::d], color=c)
        ax3.loglog(ts[::d], tr.data[::d], color=c)
        for ax in (ax1, ax2, ax3):
            plt.setp(ax.get_xticklabels(), visible=False)
            ax.set_yticklabels([])
            if 'ponset' in tr.stats:
                tponset = tr.stats.ponset - otime
                ax.axvline(tponset, color='green', alpha=0.5)
            if 'sonset' in tr.stats:
                tsonset = tr.stats.sonset - otime
                ax.axvline(tsonset, color='b', alpha=0.5)
        for ax in (ax2, ax3):
            if bulk_window and coda_window:
                c = ('b', 'k')
                wins = (bulk_window[pair], coda_window[pair])
                for i, win in enumerate(wins):
                    ax.axvspan(win[0] - otime, win[1] - otime,
                               0.05, 0.08, color=c[i], alpha=0.5)

        if sax1 is None:
            sax1 = ax2
            sax3 = ax3
    if xlim_lin:
        ax1.set_xlim(xlim_lin)
    if xlim_log:
        ax3.set_xlim(xlim_log)
    loglocator = mpl.ticker.LogLocator(base=100)
    ax2.yaxis.set_major_locator(loglocator)
    ax3.yaxis.set_major_locator(loglocator)
    ax2.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    ax3.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    plt.setp(ax2.get_xticklabels(), visible=True)
    plt.setp(ax3.get_xticklabels(), visible=True)
    _savefig(fig, **kwargs)
예제 #3
0
파일: imaging.py 프로젝트: iceseismic/qopen
def plot_fits(energies, g0, b, W, R, v0, info, G_func,
              smooth=None, smooth_window='bartlett',
              xlim=None, ylim=None, fname=None, title=None, figsize=None,
              **kwargs):
    tcoda, tbulk, Ecoda, Ebulk, Gcoda, Gbulk = info
    N = len(energies)
    n = int(np.ceil(np.sqrt(N)))
    fig = plt.figure(figsize=figsize)
    gs = gridspec.GridSpec(n, n)
    share = None
    if b is None:
        b = 0
    tmaxs = []
    ymaxs = []
    ymins = []
    c1 = 'mediumblue'
    c2 = 'darkred'
    c1l = '#8181CD'  # 37.25% #'#8686CD'
    c2l = '#8B6969'  # 25%  #'#8B6161'
    for i, energy in enumerate(energies):
        evid, station = get_pair(energy)
        ax = plt.subplot(gs[i // n, i % n], sharex=share, sharey=share)
        plot = ax.semilogy

        def get_Emod(G, t):
            #return FS * R[station] * W[evid] * G * np.exp(-b * t)
            return R[station] * W[evid] * G * np.exp(-b * t)
        st = energy.stats
        r = st.distance
        #ax.axvline(st.starttime - st.origintime + r / v0, ls = '--', c='gray')
        #ax.axvline(r / v0, ls='--', c='gray')
        t = _get_times(energy) + r / v0 - (st.sonset - st.origintime)

        if smooth:
            plot(t, energy.data_unsmoothed, color='0.7')
        plot(t, energy.data, color=c1l)
        G_of_t = lambda tt: G_func(r, tt, v0, g0)
        G_ = smooth_func(G_of_t, t, smooth, window=smooth_window)
        Emod = get_Emod(G_, t)
        index = np.argwhere(Emod<1e-30)[-1]
        Emod[index] = 1e-30
        plot(t, Emod, color=c2l)

        plot(tcoda[i], Ecoda[i], color=c1)
        Emodcoda = get_Emod(Gcoda[i], tcoda[i])
        plot(tcoda[i], Emodcoda, color=c2)

        if tbulk and len(tbulk) > 0:
            plot(tbulk[i], Ebulk[i], 'o', color=c1, mec=c1, ms=MS)
            Emodbulk = get_Emod(Gbulk[i], tbulk[i])
            plot(tbulk[i], Emodbulk, 'o', ms=MS - 1,
                 color=c2, mec=c2)

        l = '%s\n%s' % (evid, station)
        l = l + '\nr=%dkm' % (r / 1000)
        ax.annotate(l, (1, 1), (-5, -5), 'axes fraction',
                    'offset points', ha='right', va='top', size='x-small')
        _set_gridlabels(ax, i, n, n, N, xlabel='time (s)',
                       ylabel=r'E (Jm$^{-3}$Hz$^{-1}$)')
        tmaxs.append(t[-1])
        ymaxs.append(max(np.max(Emod), np.max(energy.data)))
        ymins.append(min(np.min(Emodcoda), np.max(Ecoda[i])))
        if share is None:
            share = ax
#        if True:
#            save = {'t': t, 'data': energy.stats.orig_data,
#                    'Eobs': energy.data, 'Emod': Emod,
#                    'tcoda': tcoda[i], 'Eobscoda': Ecoda[i],
#                    'Emodcoda': Emodcoda}
#            if tbulk:
#                save.update({'tbulk': tbulk[i], 'Eobsbulk': Ebulk[i],
#                             'Emodbulk': Emodbulk})
#            np.savez(fname.replace('png', '') + station + '.npz', **save)
    ax.locator_params(axis='x', nbins=5, prune='upper')
    loglocator = mpl.ticker.LogLocator(base=100)
    ax.yaxis.set_major_locator(loglocator)
    ax.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    ax.set_xlim(xlim or (0, max(tmaxs)))
    ax.set_ylim(ylim or (0.1 * min(ymins), 1.5 * max(ymaxs)))
    _savefig(fig, fname=fname, title=title, **kwargs)
예제 #4
0
def plot_fits(energies,
              g0,
              b,
              W,
              R,
              v0,
              info,
              G_func,
              smooth=None,
              smooth_window='bartlett',
              xlim=None,
              ylim=None,
              fname=None,
              title=None,
              figsize=None,
              **kwargs):
    """Plot fits of spectral energy densities"""
    tcoda, tbulk, Ecoda, Ebulk, Gcoda, Gbulk = info
    N = len(energies)
    n = int(np.ceil(np.sqrt(N)))
    fig = plt.figure(figsize=figsize)
    gs = gridspec.GridSpec(n, n)
    share = None
    if b is None:
        b = 0
    tmaxs = []
    ymaxs = []
    ymins = []
    c1 = 'mediumblue'
    c2 = 'darkred'
    c1l = '#8181CD'  # 37.25% #'#8686CD'
    c2l = '#8B6969'  # 25%  #'#8B6161'
    for i, energy in enumerate(energies):
        evid, station = get_pair(energy)
        ax = plt.subplot(gs[i // n, i % n], sharex=share, sharey=share)
        plot = ax.semilogy

        def get_Emod(G, t):
            return R[station] * W[evid] * G * np.exp(-b * t)
#            return FS * R[station] * W[evid] * G * np.exp(-b * t)

        st = energy.stats
        r = st.distance
        #        ax.axvline(st.starttime - st.origintime + r / v0, ls = '--', c='gray')
        #        ax.axvline(r / v0, ls='--', c='gray')
        t = _get_times(energy) + r / v0 - (st.sonset - st.origintime)

        if smooth:
            plot(t, energy.data_unsmoothed, color='0.7')
        plot(t, energy.data, color=c1l)
        G_ = smooth_func(lambda t_: G_func(r, t_, v0, g0),
                         t,
                         smooth,
                         window=smooth_window)
        Emod = get_Emod(G_, t)
        index = np.argwhere(Emod < 1e-30)[-1]
        Emod[index] = 1e-30
        plot(t, Emod, color=c2l)

        plot(tcoda[i], Ecoda[i], color=c1)
        Emodcoda = get_Emod(Gcoda[i], tcoda[i])
        plot(tcoda[i], Emodcoda, color=c2)

        if tbulk and len(tbulk) > 0:
            plot(tbulk[i], Ebulk[i], 'o', color=c1, mec=c1, ms=MS)
            Emodbulk = get_Emod(Gbulk[i], tbulk[i])
            plot(tbulk[i], Emodbulk, 'o', ms=MS - 1, color=c2, mec=c2)

        l = '%s\n%s' % (evid, station)
        l = l + '\nr=%dkm' % (r / 1000)
        ax.annotate(l, (1, 1), (-5, -5),
                    'axes fraction',
                    'offset points',
                    ha='right',
                    va='top',
                    size='x-small')
        _set_gridlabels(ax,
                        i,
                        n,
                        n,
                        N,
                        xlabel='time (s)',
                        ylabel=r'E (Jm$^{-3}$Hz$^{-1}$)')
        tmaxs.append(t[-1])
        ymaxs.append(max(np.max(Emod), np.max(energy.data)))
        ymins.append(min(np.min(Emodcoda), np.max(Ecoda[i])))
        if share is None:
            share = ax


#        if True:
#            save = {'t': t, 'data': energy.stats.orig_data,
#                    'Eobs': energy.data, 'Emod': Emod,
#                    'tcoda': tcoda[i], 'Eobscoda': Ecoda[i],
#                    'Emodcoda': Emodcoda}
#            if tbulk:
#                save.update({'tbulk': tbulk[i], 'Eobsbulk': Ebulk[i],
#                             'Emodbulk': Emodbulk})
#            np.savez(fname.replace('png', '') + station + '.npz', **save)
    ax.locator_params(axis='x', nbins=5, prune='upper')
    loglocator = mpl.ticker.LogLocator(base=100)
    ax.yaxis.set_major_locator(loglocator)
    ax.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    ax.set_xlim(xlim or (0, max(tmaxs)))
    ax.set_ylim(ylim or (0.1 * min(ymins), 1.5 * max(ymaxs)))
    _savefig(fig, fname=fname, title=title, **kwargs)
예제 #5
0
def plot_fits(energies,
              g0,
              b,
              W,
              R,
              v0,
              info,
              smooth=None,
              smooth_window='bartlett',
              title=None,
              mag=None):
    fs = 150 / 25.4
    plt.figure(figsize=(fs, 0.6 * fs))
    tcoda, tbulk, Ecoda, Ebulk, Gcoda, Gbulk = info
    N = len(energies)
    nx, ny = 3, 2
    gs = gridspec.GridSpec(ny, nx, wspace=0.05, hspace=0.05)
    share = None
    if b is None:
        b = 0
    tmaxs = []
    ymaxs = []
    ymins = []
    c1 = 'mediumblue'
    c2 = 'darkred'
    c1l = '#8181CD'
    c2l = '#8B6969'
    for i, energy in enumerate(energies):
        evid, station = get_pair(energy)
        ax = plt.subplot(gs[i // nx, i % nx], sharex=share, sharey=share)
        plot = ax.semilogy

        def get_Emod(G, t):
            return R[station] * W[evid] * G * np.exp(-b * t)

        st = energy.stats
        r = st.distance
        t = _get_times(energy) + r / v0 - (st.sonset - st.origintime)

        if smooth:
            plot(t, energy.data_unsmoothed, color='0.7')
        plot(t, energy.data, color=c1l)
        G_ = Gsmooth(G_func,
                     r,
                     t,
                     v0,
                     g0,
                     smooth=smooth,
                     smooth_window=smooth_window)
        Emod = get_Emod(G_, t)
        index = np.argwhere(Emod < 1e-30)[-1]
        Emod[index] = 1e-30

        plot(t, Emod, color=c2l)

        plot(tcoda[i], Ecoda[i], color=c1)
        Emodcoda = get_Emod(Gcoda[i], tcoda[i])
        plot(tcoda[i], Emodcoda, color=c2)

        if tbulk and len(tbulk) > 0:
            plot(tbulk[i], Ebulk[i], 'o', color=c1, mec=c1, ms=4)
            Emodbulk = get_Emod(Gbulk[i], tbulk[i])
            plot(tbulk[i], Emodbulk, 'o', ms=3, color=c2, mec=c2)

        l = '%s\nr=%dkm' % (station, r / 1000)
        ax.annotate(l, (1, 1), (-5, -5),
                    'axes fraction',
                    'offset points',
                    ha='right',
                    va='top',
                    size='x-small')
        set_gridlabels(ax,
                       i,
                       nx,
                       N,
                       xlabel='time (s)',
                       ylabel=r'E (Jm$^{-3}$Hz$^{-1}$)')
        kw = dict(color='darkgreen', alpha=0.5, lw=0, zorder=10000)
        ax.axvspan(tcoda[i][0] - 10, tcoda[i][0] - 0.8, 0.05, 0.08, **kw)
        ax.axvspan(tcoda[i][0] + 0.8, tcoda[i][-1], 0.05, 0.08, **kw)
        tmaxs.append(t[-1])
        ymaxs.append(max(np.max(Emod), np.max(energy.data)))
        ymins.append(min(np.min(Emodcoda), np.max(Ecoda[i])))
        if share is None:
            share = ax
    f = 6
    lmag = (r'  $M_{\mathrm{R}}%.1f$' % mag) if mag else ''
    l = ('id %s' + lmag + '\n'
         r'$Q^{-1}_{\mathrm{sc}} = %.1f\times 10^{%d}$' + '\n'
         r'$Q^{-1}_{\mathrm{i}} = %.1f\times 10^{%d}$')
    l = l % (evid, g0 * v0 / (2 * np.pi * f) * 1e3, -3, b /
             (2 * np.pi * f) * 1e3, -3)
    ax.annotate(title, (0.715, 0.43),
                xycoords='figure fraction',
                ha='left',
                va='top',
                annotation_clip=False,
                size='large')
    ax.annotate(l, (0.715, 0.34),
                xycoords='figure fraction',
                ha='left',
                va='top',
                annotation_clip=False)
    ax.yaxis.set_minor_locator(mpl.ticker.NullLocator())
    ax.set_yticks(10.**np.arange(-18, -7, 2))
    ax.set_xlim((-5, 120))
    ax.set_ylim((1e-15 / 1.5, 1e-7 * 1.5))