def sim_bunch_lc_evt(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 (TSTART, TSTOP, OBSID, exptime) = epoch_file if type(TSTART) == type(1.2): print('Single exposure') TSTART = [TSTART] TSTOP = [TSTOP] OBSID = [OBSID] exptime = [exptime] with open( outpath + 'CR_{0}_P_{1}_REJ1034_simpleway.txt'.format( "%.0e" % CR, str(int(period))), 'a+') as f: k_trial = 0 while k_trial < num_trials: ev_all = EventList() # lc = make_lc_from_psd(psd=psd_model, cts_rate=CR / 2 * 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(exptime)): cts_rate = 0.5 * CR * lenbin # 实际的cts-rate应为这个的2倍 num_bins = int(exptime[i] / dt) sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=lenbin) w = np.arange(1 / exptime[i], 0.5 / lenbin, 1 / exptime[i]) psd_model = build_psd(x=w, p=[2.3e-3, 3.4, 0., 4.3e-4], x_2=w, p_2=[qpo_f, qpo_f / 16, 100, 2], type='bendp+lorentz') # psd_model = build_psd(x=w, p=[2.3e-3, 3.4, 0., 4.3e-4], # type='bendp') spectrum = psd_model lc_cut = sim.simulate(spectrum) lc_cut.counts += cts_rate lc_cut.counts[np.where(lc_cut.counts < 0)] = 0 lc_cut.counts = np.random.poisson(lc_cut.counts) # print(np.sum(lc_cut.counts)) ev = EventList() ev.time = func.sim_evtlist(lc_cut) + TSTART[i] ev_all = ev_all.join(ev) lc_long = ev_all.to_lc(dt=dt, tstart=ev_all.time[0] - 0.5 * dt, tseg=ev_all.time[-1] - ev_all.time[0]) 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()
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)))
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()
def LSP_band(srcevt, bkgevt, epoch, band, epochnum=[0, 31]): bin_len = 100 if len(epoch) == 0 or len(srcevt) < 4: return (1, 0) if len(np.shape(epoch)) == 1: epoch = np.array([epoch]) if len(bkgevt) < 2: bkgevt = [] else: useid = epoch[:, 2][epochnum[0]:epochnum[1]] time = [] bkg_time = [] energy = [] bkg_energy = [] srcevt[:, -1] = srcevt[:, -1].astype('int') bkgevt[:, -1] = bkgevt[:, -1].astype('int') for k in range(len(useid)): time = np.concatenate( (time, srcevt[:, 0][np.where(srcevt[:, -1] == useid[k])])) energy = np.concatenate( (energy, srcevt[:, 1][np.where(srcevt[:, -1] == useid[k])])) bkg_time = np.concatenate( (bkg_time, bkgevt[:, 0][np.where(bkgevt[:, -1] == useid[k])])) bkg_energy = np.concatenate( (bkg_energy, bkgevt[:, 1][np.where(bkgevt[:, -1] == useid[k])])) ## 看一下不同波段的lc如何 ## (time, energy) = func.filter_energy(time, energy, band) (bkg_time, bkg_energy) = func.filter_energy(bkg_time, bkg_energy, band) src_cts = len(time) bkg_cts = len(bkg_time) if len(time) < 4: return (1, 0) if len(bkg_time) < 2: bkg_time = [] T_tot = time[-1] - time[0] freq = np.arange(1 / T_tot, 0.5 / bin_len - 0.0002, 1 / (5 * T_tot)) freq = freq[np.where(freq > 1 / 20000.)] lc = func.get_hist_withbkg(time, bkg_time, bin_len) counts = np.sum(lc.counts) # cts_rate=(counts/exptime) x = lc.time flux = lc.counts (FP, out_period, max_NormLSP) = func.get_LS(x, flux, freq) return (FP, out_period)