示例#1
0
 def setUp(self):
     nf = 100
     start, end = 10, 1e5
     wl = np.exp(np.linspace(np.log(start), np.log(end), nf))
     freq = ps.rf.val_to_hz(wl, inp="A")
     # freq = freq[np.argsort(freq)]  # ascending order
     flux = np.ones(nf)
     self.sp = ps.Spectrum('uniform', freq=freq, flux=flux)
     self.distance = 10  # pc
示例#2
0
    def test_series_redshift2obs(self):
        import matplotlib.pyplot as plt
        x = np.linspace(-10, 10, 10)
        y = 10. - x**2
        # norm
        freq = x - np.min(x)
        flux = y + np.min(y)
        times = [10]
        ss = ps.SeriesSpectrum("test_SeriesSpectrum")

        for k, t in enumerate(times):
            s = ps.Spectrum(freq=freq, flux=flux, name='test_spectrum')
            ss.add(t, s)

        z = 1.
        series = ss
        series_z = series.redshift2obs(z)

        # plot
        for t, sp in series:
            plt.plot(sp.Freq,
                     sp.Flux,
                     marker='',
                     ls=':',
                     label='origin t={:f}'.format(t))

        for t, sp in series_z:
            plt.plot(sp.Freq,
                     sp.Flux,
                     marker='',
                     ls=':',
                     label='   z t={:f}'.format(t))

        plt.xlabel('Freq')
        plt.ylabel('Flux')
        plt.legend(loc='best')
        plt.show()
示例#3
0
def plot_spec_wl(times, series, tt, wl_ab, **kwargs):
    font_size = kwargs.get('font_size', 12)
    nrow = np.math.ceil(len(times) / 2.)
    ncol = 2
    fig = plt.figure(figsize=(12, nrow * 4))
    plt.matplotlib.rcParams.update({'font.size': font_size})

    series_cut = series.copy(wl_ab=wl_ab)
    # radiuses = np.interp(times, tt['time'], tt['Rph'], 0, 0)
    radiuses = np.interp(times, tt['time'], tt['rbb'], 0, 0)
    Tbbes = np.interp(times, tt['time'], tt['Tbb'], 0, 0)
    marker_style = dict(linestyle=':', markersize=5)

    for i, t in enumerate(times):
        ax = fig.add_subplot(nrow, ncol, i + 1)
        spec = series.get_spec_by_time(t)
        spec.cut_flux(max(spec.Flux) * 1e-6)  # cut flux
        R = radiuses[i]

        star_bb = ps.Star("bb", spec)
        star_bb.set_distance(R)

        spec_cut = series_cut.get_spec_by_time(t)
        star_cut = ps.Star("bb_cut", spec_cut)
        star_cut.set_distance(R)

        # spectrum
        ax.semilogy(star_bb.Wl * ps.phys.cm_to_angs,
                    star_bb.FluxWlObs,
                    label="Spec Ph")
        # Tcolor
        spec_obs = ps.Spectrum('wbb', star_cut.Freq, star_cut.FluxObs)
        Tcol = spec_obs.T_color
        T_wien = spec_obs.T_wien
        zeta = (T_wien / Tcol)**4
        wbb = ps.SpectrumDilutePlanck(spec.Freq, Tcol, zeta)
        ax.semilogy(wbb.Wl * ps.phys.cm_to_angs,
                    wbb.FluxWl,
                    label="Tcol={:.0f} W={:.2f}".format(Tcol, zeta),
                    marker='<',
                    **marker_style)
        # diluted
        Tdil, W = spec_obs.T_color_zeta()
        dil = ps.SpectrumDilutePlanck(spec.Freq, Tdil, W)
        ax.semilogy(dil.Wl * ps.phys.cm_to_angs,
                    dil.FluxWl,
                    label="Tdil={:.0f} W={:.2f}".format(Tdil, W),
                    marker='>',
                    **marker_style)
        # Tbb
        Tbb = Tbbes[i]
        bb = ps.SpectrumPlanck(spec.Freq, Tbb)
        ax.semilogy(bb.Wl * ps.phys.cm_to_angs,
                    bb.FluxWl,
                    label="Tbb={:.0f}".format(Tbb),
                    marker='d',
                    **marker_style)

        # wl range
        if wl_ab is not None:
            for xc in wl_ab:
                plt.axvline(x=xc, color="grey", linestyle='--')
        ax.legend(loc="best", prop={'size': 9})
        ax.text(0.01,
                0.05,
                "$t_d: {:.1f}$".format(t),
                horizontalalignment='left',
                transform=ax.transAxes,
                bbox=dict(facecolor='green', alpha=0.3))
        xlim = ax.get_xlim()
        ax.set_xlim(xlim[0], min(xlim[1], 2e4))

    # fig.subplots_adjust(wspace=0, hspace=0)
    # fig.subplots_adjust(wspace=0, hspace=0, left=0.07, right=0.96, top=0.97, bottom=0.06)
    plt.subplots_adjust(left=0.07, right=0.96, top=0.97, bottom=0.06)
    return fig
示例#4
0
def plot_fit_wl(model, series, wl_ab, times=None, fsave=None):
    tt = model.get_tt().load()
    series_cut = series.copy(wl_ab=wl_ab)

    time = series.Time
    radius = np.interp(time, tt['time'], tt['rbb'])

    if True:
        Tcol, Twien, zeta = [], [], []
        Tdil, Wdil = [], []
        for i, t in enumerate(time):
            sp = series_cut.get_spec(i)
            # sp = series_cut.get_spec_by_time(t)
            R = radius[i]
            star_cut = ps.Star("bb_cut", sp)
            star_cut.set_distance(R)
            sp_obs = ps.Spectrum('cut', star_cut.Freq, star_cut.FluxObs)
            Tc = sp_obs.T_color
            Tw = sp_obs.T_wien
            w = np.power(Tw / Tc, 4)
            Td, W_d = sp_obs.T_color_zeta()

            Tcol.append(Tc)
            Twien.append(Tw)
            zeta.append(w)
            Tdil.append(Td)
            Wdil.append(W_d)
    else:
        Tcol = series_cut.get_T_color()
        Twien = series_cut.get_T_wien()
        zeta = np.power(Twien / Tcol, 4)

        res = series_cut.get_T_color_zeta()
        Tdil, Wdil = res[:, 0], res[:, 1]

    print("{:>8}".format("Time") + ' '.join("{:>12s}".format(s)
                                            for s in ("T_dil", "W_dil")))
    for t, *p in zip(time, Tdil, Wdil):
        print("{:8.2f}".format(t) + ' '.join("{0:12.2f}".format(s) for s in p))
        # print("%10.2f  %12.6f  %12.6f  %12.6f  " % p)

    # save
    if fsave is not None:
        save_fit_wl(fsave, model.Name, time, Tdil, Wdil, wl_ab=wl_ab)
        return

    # plot
    fig, ax = plt.subplots()
    marker_style = dict(linestyle=':', color='cornflowerblue', markersize=10)

    ax.semilogy(time,
                Tcol,
                'ks-',
                markerfacecolor='white',
                markersize=3,
                label="Tcolor")
    ax.semilogy(time, Twien, 'r:', label="Twien")
    ax.semilogy(time, Tdil, 'ys-', markersize=3, label="T_dil")
    ax.semilogy(tt['time'], tt['Tbb'], 'g:', label="tt Tbb")
    # ax.semilogy(tt['time'], tt['Teff'], 'y:', label="tt Teff")
    # ax.semilogy(results['time'], tt['T'], 'm:', label="tt Teff")
    ax.legend()
    # wl range

    if times is not None:
        for xc in times:
            ax.axvline(x=xc, color="grey", linestyle='--')

        fig = plot_spec_wl(times, series, tt, wl_ab)
    else:
        pass
    return fig