Exemple #1
0
def test_somefunc():
    lenbin = 100
    T_exp = 590000
    CR = 5e-3
    epoch_89 = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/epoch_src_test_single.txt'
    (TSTART, TSTOP, OBSID, exptime) = func.read_epoch(epoch_89)
    epoch_89 = (TSTART, TSTOP, OBSID, exptime)
    w = make_freq_range(dt=lenbin, epoch_file=epoch_89)
    # psd_model=build_psd(x=w,p=[2.3e-3, 3.4, 0., 4.3e-4],x_2=w,
    #                     p_2=[4.01e-4, 4.01e-4 / 16, 100, 2],type='bendp+lorentz')
    p_1 = [4.3e-4, 3.4, 0., 2e-2]
    p_2 = [2.67e-4, 16, 0.15, 2]
    psd_model = sim.build_psd(x=w, p=p_1, x_2=w, p_2=p_2, type='bendp+lorentz')

    frms = integrate.quad(func.bendp_lorentz, w[0], w[-1], args=(p_1, p_2))[0]
    frms = np.sqrt(frms)

    plt.loglog()
    plt.plot(w, psd_model)
    plt.xlabel('Frequency (Hz)', font2)
    plt.ylabel('Power', font2)
    plt.tick_params(labelsize=16)
    plt.show()
    lc = make_lc_from_psd(psd=psd_model,
                          cts_rate=CR * lenbin,
                          dt=lenbin,
                          epoch_file=epoch_89,
                          frms=frms)
    ps_org = plot_psd(lc)
    print('counts={0}'.format(np.sum(lc.counts)))
    lc_evt = Lightcurve(time=lc.time, counts=lc.counts, dt=lc.dt, gti=lc.gti)
    lc_evt.counts = np.random.poisson(lc_evt.counts)
    # ev_all = EventList()
    # ev_all.time = func.sim_evtlist(lc)
    # lc_evt = ev_all.to_lc(dt=lenbin, tstart=ev_all.time[0] - 0.5 * lenbin,
    #                       tseg=ev_all.time[-1] - ev_all.time[0])
    ps_real = plot_psd(lc_evt)
    freq = np.arange(1 / T_exp, 0.5 / lenbin, 1 / (5 * T_exp))
    freq = freq[np.where(freq > 1 / 10000.)]
    temp = func.get_LS(lc_evt.time, lc_evt.counts, freq=freq)

    # plt.subplot(121)
    plt.xlabel('Time', font2)
    plt.ylabel('Counts/bin', font2)
    plt.tick_params(labelsize=16)
    plt.plot(lc.time, lc.counts, color='red')
    # plt.subplot(122)
    plt.show()
    plt.plot(lc_evt.time, lc_evt.counts, color='green')
    print('counts={0}'.format(np.sum(lc_evt.counts)))
    plt.xlabel('Time', font2)
    plt.ylabel('Counts/bin', font2)
    plt.tick_params(labelsize=16)
    plt.show()
    evt = EventList()
    EventList.simulate_times(evt, lc=lc)
    print('counts={0}'.format(len(evt.time)))
Exemple #2
0
def sim_bunch_lc(CR, dt, period, epoch_file, outpath, num_trials=100):
    lenbin = dt
    # epoch_89 = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/epoch_src_89.txt'
    w = make_freq_range(dt=lenbin, epoch_file=epoch_file)
    qpo_f = 1. / period

    p_1 = [4.3e-4, 3.4, 0., 2.3e-3]
    p_2 = [qpo_f, 16, 0.05, 2]
    psd_model = build_psd(x=w, p=p_1, x_2=w, p_2=p_2, type='bendp+lorentz')

    # frms=integrate.quad(func.bendp_lorentz,w[0],w[-1],args=(p_1,p_2))[0]
    # frms=np.sqrt(frms)
    # psd_model=build_psd(x=w,p=[4.3e-4, 3.4, 0., 2.e-2],x_2=w,
    #                     p_2=[qpo_f, 16, 0.15, 2],type='bendp+lorentz')
    # psd_model = build_psd(x=w, p=[2.3e-3, 3.4, 0., 4.3e-4],
    #                       type='bendp')
    k_trial = 0
    (TSTART, TSTOP, OBSID, exptime) = epoch_file
    with open(
            outpath +
            'CR_{0}_P_{1}_REJ1034.txt'.format("%.0e" % CR, "%.0d" % period),
            'a+') as f:
        # with open(outpath+'CR_{0}_P_{1}_REJ1034_fake4.txt'.format("%.0e" %CR ,"%.0d" %period),'a+') as f:
        # with open(outpath + 'CR_{0}_P_{1}_A002_R15_fake4.txt'.format("%.0e" % CR, "%.0d" % period), 'a+') as f:
        # with open(outpath+'CR_{0}_noQPO_REJ1034_R50.txt'.format("%.0e"%CR),'a+') as f:
        while k_trial < num_trials:
            lc = make_lc_from_psd(psd=psd_model,
                                  cts_rate=CR * lenbin,
                                  dt=lenbin,
                                  epoch_file=epoch_file)
            print('trial' + ':  ' + str(k_trial))
            lc_evt = Lightcurve(time=lc.time,
                                counts=lc.counts,
                                dt=lc.dt,
                                gti=lc.gti)
            lc_evt.counts = np.random.poisson(lc_evt.counts)
            T_tot = TSTOP[-1] - TSTART[0]
            for i in range(len(OBSID)):
                lc_cut = lc_evt.truncate(start=TSTART[i],
                                         stop=TSTOP[i],
                                         method='time')
                if i == 0: lc_long = lc_cut
                else:
                    lc_long = lc_long.join(lc_cut)
            print('counts={0}'.format(np.sum(lc_long.counts)))
            T_exp = lc_long.time[-1] - lc_long.time[0]
            freq = np.arange(1 / T_exp, 0.5 / lenbin, 1 / (5 * T_exp))
            freq = freq[np.where(freq > 1 / 20000.)]
            # print(T_tot)
            # print(T_exp)
            temp = func.get_LS(lc_long.time, lc_long.counts, freq=freq)
            f.writelines((str(temp[0]) + '        ' + str(temp[1]) +
                          '        ' + str(temp[2]) + '  ' + '\n'))
            k_trial += 1
    f.close()