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 setup_class(cls): from ..simulator import Simulator dt = 0.01 cls.time_lag = 5. data = np.load(os.path.join(datadir, "sample_variable_lc.npy")) flux = data times = np.arange(data.size) * dt maxfreq = 0.25 / cls.time_lag roll_amount = int(cls.time_lag // dt) good = slice(roll_amount, roll_amount + int(200 // dt)) rolled_flux = np.array(np.roll(flux, roll_amount)) times, flux, rolled_flux = times[good], flux[good], rolled_flux[good] length = times[-1] - times[0] test_lc1 = Lightcurve(times, flux, err_dist="gauss", dt=dt, skip_checks=True) test_lc2 = Lightcurve(test_lc1.time, rolled_flux, err_dist=test_lc1.err_dist, dt=dt) test_ev1, test_ev2 = EventList(), EventList() test_ev1.simulate_times(test_lc1) test_ev2.simulate_times(test_lc2) test_ev1.energy = np.random.uniform(0.3, 9, len(test_ev1.time)) test_ev2.energy = np.random.uniform(9, 12, len(test_ev2.time)) cls.lag = LagEnergySpectrum( test_ev1, freq_interval=[0, maxfreq], energy_spec=(0.3, 9, 1, "lin"), ref_band=[9, 12], bin_time=dt / 2, segment_size=length, events2=test_ev2, ) # Make single event list test_ev = test_ev1.join(test_ev2) cls.lag_same = LagEnergySpectrum( test_ev, freq_interval=[0, maxfreq], energy_spec=(0.3, 9, 1, "lin"), ref_band=[9, 12], bin_time=dt / 2, segment_size=length, )
def get_lc_onesource(k,src_index,num_trials=2): k_trial =0 FP = []; period = []; cts_num=[]; peakP=[]; power_P=[] path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k)) tstart = epoch_file[:, 0];tstop = epoch_file[:, 1] ID=epoch_file[:,2];exptime = epoch_file[:, 3] evt_file = np.loadtxt(path + '{0}.txt'.format(src_index)) bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index)) for i in range(len(ID)): index = len(np.where(evt_file[:,2] == ID[i])[0]) index_b=len(np.where(bkgevt_file[:,2] == ID[i])[0]) cts_num.append(index-index_b/12.) dt = 100 T_exp = 11000154.981141508 freq=np.arange(1/T_exp,0.5/dt,1/(5*T_exp)) freq=freq[np.where(freq > 1 / 20000.)] if os.path.exists(path+'/simulation/{0}_LS_simP.csv'.format(src_index)): print('caution! file exists') return None with open(path + '/simulation/{0}_LS_simP.csv'.format(src_index), 'a+') as csvfile: header = freq header = header.astype('str') writer = csv.writer(csvfile) while k_trial <num_trials: ev_all = EventList() for i in range(len(exptime)): cts_rate = cts_num[i]/(2*exptime[i]) * dt # 实际的cts-rate应为这个的2倍 num_bins = int(exptime[i] / dt) sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt) w = np.arange(1 / exptime[i], 0.5 / dt, 1 / exptime[i]) spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w, [1.0518215e-3,1.0518215e-3/16,200,2]) lc = sim.simulate(spectrum) # lc.counts += cts_rate lc.counts[np.where(lc.counts<0)]=0 ps = Powerspectrum(lc, norm='abs') ev = EventList() ev.time = sim_evtlist(lc) + tstart[i] ev_all = ev_all.join(ev) # print(len(ev_all.time)) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0]-0.5*dt, tseg=ev_all.time[-1]-ev_all.time[0]) # T_exp=lc_new.time[-1]-lc_new.time[0] temp=get_LS(lc_new.time, lc_new.counts, freq=freq,trial=k_trial) writer.writerows([temp[-1]]) k_trial+=1
def slow_sim_const(k, src_index, num_trials=2): k_trial = 0 dt = 100 FP = [] period = [] cts_num = [] peakP = [] power_P = [] path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k)) tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] evt_file = np.loadtxt(path + '{0}.txt'.format(src_index)) bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index)) T_tot = tstop[-1] - tstart[0] cts_rate = len(evt_file) / (2 * np.sum(exptime)) * dt # 实际的cts-rate应为这个的2倍 num_bins = int(T_tot / dt) T_exp = T_tot freq = np.arange(1 / T_exp, 0.5 / dt, 1 / (5 * T_exp)) freq = freq[np.where(freq > 1 / 20000.)] with open(path + '/simulation/{0}_LSP_FP_const.txt'.format(src_index), 'a+') as f: while k_trial < num_trials: ev_all = EventList() for i in range(len(ID)): index = len(np.where(evt_file[:, 2] == ID[i])[0]) index_b = len(np.where(bkgevt_file[:, 2] == ID[i])[0]) temp = index - index_b / 12. if temp < 0: temp = 0 cts_num = np.random.poisson(temp) ev = EventList() ev.time = np.random.uniform(tstart[i] - dt / 2, tstop[i] + dt / 2, cts_num) ev_all = ev_all.join(ev) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0] - 0.5 * dt, tseg=ev_all.time[-1] - ev_all.time[0]) # T_exp=lc_new.time[-1]-lc_new.time[0] print(np.sum(lc_new)) temp = get_LS(lc_new.time, lc_new.counts, freq=freq) f.writelines((str(temp[0]) + ' ' + str(temp[1]) + '\n')) k_trial += 1 f.close()
def get_lc_byspec_1hr(k,j,num_trials=1000): k_trial=0 FP = []; period = []; while k_trial <num_trials: path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file=np.loadtxt(path+'CDFS_epoch_ep{0}.txt'.format(k)) # path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8/' # epoch_file = np.loadtxt(path + 'CDFS_epoch.txt') tstart=epoch_file[:,0];tstop=epoch_file[:,1];exptime=epoch_file[:,3] ev_all = EventList() for i in range(len(exptime)): dt=100 cts_rate=cr[j] *dt #实际的cts-rate应为这个的2倍 num_bins=int(exptime[i]/dt) sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt) w = np.arange(1 / exptime[i], 0.5 / dt, 1 / exptime[i]) # w = np.fft.rfftfreq(sim.N, d=sim.dt)[1:] # spectrum = smoothbknpo(w, [0.01, 2, 1e-2, 1e-3]) # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) ## 这是RE J1034+396 的参数 # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w, [6.68e-4 ,6.68e-4 /16,200,2]) spectrum = bending_po(w, [2.3e-3, 3.4, 0., 4.3e-4]) + generalized_lorentzian(w,[5.6e-4, 5.6e-4 / 16, 200,2]) # spectrum =powerlaw + generalized_lorentzian(w, [1 / 1000., 1 / 10000., 0.5*np.max(powerlaw), 2]) lc = sim.simulate(spectrum) lc.counts += cts_rate lc.counts[np.where(lc.counts<0)]=0 ps=Powerspectrum(lc,norm='abs') ev = EventList() # ev.simulate_times(use_spline=False,lc=lc,bin_time=dt) # ev.time+=tstart[i] ev.time=sim_evtlist(lc)+tstart[i] # print(ev.time)RX J1301.9+2747 ev_all=ev_all.join(ev) print('cts={0}'.format(len(ev_all.time))) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0]-0.5*dt, tseg=ev_all.time[-1]-ev_all.time[0]) T_exp=lc_new.time[-1]-lc_new.time[0] freq = np.arange(1 / T_exp, 0.5 / dt, 1 / (5 * T_exp)) freq=freq[np.where(freq > 1 / 20000.)] # print(len(freq)) temp=get_LS(lc_new.time, lc_new.counts, freq=freq) FP.append(temp[0]);period.append(temp[1]) k_trial+=1 result=np.column_stack((FP,period)) np.savetxt(path+'simulation/'+'trial_out_1hr_{0}_REJ1034+396_test_noC.txt'.format(cr_str[j]),result,fmt="%10.5f %10.5f") return ev_all
def get_lc_onesource_const(k,src_index,num_trials=100): dt=100 k_trial =0 FP = []; period = []; cts_num=[]; peakP=[]; power_P=[] dt = 100 T_exp = 11000154.981141508 freq=np.arange(1/T_exp,0.5/dt,1/(5*T_exp)) freq=freq[np.where(freq > 1 / 20000.)] path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k)) tstart = epoch_file[:, 0];tstop = epoch_file[:, 1] ID=epoch_file[:,2];exptime = epoch_file[:, 3] evt_file = np.loadtxt(path + '{0}.txt'.format(src_index)) bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index)) if os.path.exists(path+'/simulation/{0}_LS_simP_const.csv'.format(src_index)): print('caution! file exists') return None with open(path + '/simulation/{0}_LS_simP_const.csv'.format(src_index), 'a+') as csvfile: header = freq header = header.astype('str') writer = csv.writer(csvfile) while k_trial <num_trials: ev_all = EventList() for i in range(len(ID)): index = len(np.where(evt_file[:,2] == ID[i])[0]) index_b=len(np.where(bkgevt_file[:,2] == ID[i])[0]) temp=index-index_b/12. if temp<0:temp=0 cts_num=np.random.poisson(temp) ev=EventList() ev.time=np.random.uniform(tstart[i]-dt/2,tstop[i]+dt/2,cts_num) ev_all = ev_all.join(ev) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0] - 0.5 * dt, tseg=ev_all.time[-1] - ev_all.time[0]) # T_exp=lc_new.time[-1]-lc_new.time[0] temp = get_LS(lc_new.time, lc_new.counts, freq=freq, trial=k_trial) writer.writerows([temp[-1]]) k_trial += 1
def get_lc_byspec(k,j,num_trials=1000): k_trial=0 FP = []; period = []; while k_trial <num_trials: path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file=np.loadtxt(path+'CDFS_epoch_ep{0}.txt'.format(k)) # path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8/' # epoch_file = np.loadtxt(path + 'CDFS_epoch.txt') tstart=epoch_file[:,0];tstop=epoch_file[:,1];exptime=epoch_file[:,3] ev_all = EventList() for i in range(len(exptime)): dt=100 cts_rate=cr[j] *dt #实际的cts-rate应为这个的2倍 num_bins=int(exptime[i]/dt) sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt) w = np.arange(1 / exptime[i], 0.5 / dt, 1 / exptime[i]) # w = np.fft.rfftfreq(sim.N, d=sim.dt)[1:] # spectrum = smoothbknpo(w, [0.01, 2, 1e-2, 1e-3]) spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) ## 这是RE J1034+396 的参数 # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w, [6.68e-4 ,6.68e-4 /16,200,2]) # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w,[2.7e-4, 2.7e-4 / 16, 200,2]) # spectrum =powerlaw + generalized_lorentzian(w, [1 / 1000., 1 / 10000., 0.5*np.max(powerlaw), 2]) lc = sim.simulate(spectrum) lc.counts += cts_rate lc.counts[np.where(lc.counts<0)]=0 # plt.plot(w,spectrum) # plt.loglog() # plt.xlabel("Frequency (Hz)", fontproperties=font_prop) # plt.ylabel("Power (abs)", fontproperties=font_prop) # plt.show() ps=Powerspectrum(lc,norm='abs') # fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True) # ax1.plot(ps.freq, ps.power, lw=2, color='blue') # ax1.set_ylabel("Frequency (Hz)", fontproperties=font_prop) # ax1.set_ylabel("Power (raw)", fontproperties=font_prop) # # ax1.set_yscale('log') # ax1.tick_params(axis='x', labelsize=16) # ax1.tick_params(axis='y', labelsize=16) # ax1.tick_params(which='major', width=1.5, length=7) # ax1.tick_params(which='minor', width=1.5, length=4) # for axis in ['top', 'bottom', 'left', 'right']: # ax1.spines[axis].set_linewidth(1.5) # plt.loglog() # plt.show() ev = EventList() # ev.simulate_times(use_spline=False,lc=lc,bin_time=dt) # ev.time+=tstart[i] ev.time=sim_evtlist(lc)+tstart[i] # print(ev.time)RX J1301.9+2747 ev_all=ev_all.join(ev) # lc_temp = ev.to_lc(dt=dt, tstart=ev.time[0]-0.5*dt, tseg=ev.time[-1]-ev.time[0]) # ps_temp=Powerspectrum(lc_temp,norm='abs') # fig2, ax2 = plt.subplots(1, 1, figsize=(9, 6), sharex=True) # ax2.plot(ps_temp.freq, ps_temp.power, lw=2, color='blue') # ax2.set_ylabel("Frequency (Hz)", fontproperties=font_prop) # ax2.set_ylabel("Power (raw)", fontproperties=font_prop) # # ax1.set_yscale('log') # ax2.tick_params(axis='x', labelsize=16) # ax2.tick_params(axis='y', labelsize=16) # ax2.tick_params(which='major', width=1.5, length=7) # ax2.tick_params(which='minor', width=1.5, length=4) # for axis in ['top', 'bottom', 'left', 'right']: # ax2.spines[axis].set_linewidth(1.5) # plt.loglog() # plt.show() # # plt.figure(2) # plt.plot(lc.time,lc.counts,c='green') # plt.plot(lc_temp.time,lc_temp.counts,c='r') # plt.show() # plt.figure(3) # get_LS(lc.time,lc.counts,w) # print('cts={0}'.format(len(ev_all.time))) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0]-0.5*dt, tseg=ev_all.time[-1]-ev_all.time[0]) # plt.figure(2) # plt.plot(lc_new.time,lc_new.counts,c='r') # plt.show() # plt.figure(3) T_exp=lc_new.time[-1]-lc_new.time[0] freq=np.arange(1/T_exp,0.5/dt,1/(5*T_exp)) freq=freq[np.where(freq > 1 / 10000.)] # print(len(freq)) temp=get_LS(lc_new.time, lc_new.counts, freq=freq) FP.append(temp[0]);period.append(temp[1]) k_trial+=1 result=np.column_stack((FP,period)) np.savetxt(path+'simulation/'+'trial_out_{0}_REJ1034+396_noQPO.txt'.format(cr_str[j]),result,fmt="%10.5f %10.5f") return ev_all
def slow_sim_fixpds(k, src_index, num_trials=2): k_trial = 0 dt = 100 FP = [] period = [] cts_num = [] peakP = [] power_P = [] path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k)) tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] evt_file = np.loadtxt(path + '{0}.txt'.format(src_index)) bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index)) T_tot = tstop[-1] - tstart[0] cts_rate = len(evt_file) / (2 * np.sum(exptime)) * dt # 实际的cts-rate应为这个的2倍 num_bins = int(T_tot / dt) sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt) w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot) spectrum = bending_po( w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian( w, [2.7e-4, 2.7e-4 / 16, 200, 2]) lc = sim.simulate(spectrum) lc.time = lc.time + tstart[0] # lc.gti=[[lc.time[0],lc.time[-1]]] lc.counts += cts_rate lc.counts[np.where(lc.counts < 0)] = 0 ps = Powerspectrum(lc, norm='leahy') fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True) ax1.loglog() ax1.step(ps.freq, ps.power, lw=2, color='blue') ax1.plot([1 / 950.7, 1 / 950.7], [0, np.max(ps.power)], '--', linewidth=1) ax1.set_ylabel("Frequency (Hz)", fontproperties=font1) ax1.set_ylabel("Power (raw)", fontproperties=font1) ax1.set_yscale('log') ax1.tick_params(axis='x', labelsize=16) ax1.tick_params(axis='y', labelsize=16) ax1.tick_params(which='major', width=1.5, length=7) ax1.tick_params(which='minor', width=1.5, length=4) for axis in ['top', 'bottom', 'left', 'right']: ax1.spines[axis].set_linewidth(1.5) plt.show() T_exp = T_tot freq = np.arange(1 / T_exp, 0.5 / dt, 1 / (5 * T_exp)) freq = freq[np.where(freq > 1 / 20000.)] with open(path + '/simulation/{0}_max_LSP_fixpds.txt'.format(src_index), 'a+') as f: res_period = [] res_power = [] while k_trial < num_trials: ev_all = EventList() for i in range(len(exptime)): lc_cut = lc.truncate(start=tstart[i], stop=tstop[i], method='time') ev = EventList() ev.time = sim_evtlist(lc_cut) ev_all = ev_all.join(ev) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0] - 0.5 * dt, tseg=ev_all.time[-1] - ev_all.time[0]) temp = get_LS(lc_new.time, lc_new.counts, freq=freq, trial=k_trial) # res_period.append(temp[0]);res_power.append(temp[-1]) f.writelines((str(temp[0]) + ' ' + str(temp[1]) + '\n')) k_trial += 1 f.close()
def slow_sim_fixpds_qpo_chandra_epoch23(src_index, num_trials=2, dt=100): k_trial = 0 def make_lc(evtfile, bkgevtfile, epochfile, dt): epoch_file = np.loadtxt(epochfile) evt_file = np.loadtxt(evtfile) bkgevt_file = np.loadtxt(bkgevtfile) tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] T_tot = tstop[-1] - tstart[0] cts_rate = len(evt_file) / (2 * np.sum(exptime)) * dt # 实际的cts-rate应为这个的2倍 num_bins = int(T_tot / dt) sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt) w = np.arange(1 / T_tot, 0.5 / dt, 1 / (T_tot)) spectrum = bending_po( w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian( w, [4.01e-4, 4.01e-4 / 16, 200, 2]) lc = sim.simulate(spectrum) # lc=sim.simulate(pow) lc.time = lc.time + tstart[0] # lc.gti=[[lc.time[0],lc.time[-1]]] lc.counts += cts_rate lc.counts[np.where(lc.counts < 0)] = 0 return lc path2 = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep2/' epoch_file2 = path2 + 'epoch_src_{0}.txt'.format(src_index) path3 = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/' epoch_file3 = path3 + 'epoch_src_{0}.txt'.format(src_index) lc2 = make_lc(path2 + '{0}.txt'.format(src_index), path2 + '{0}_bkg.txt'.format(src_index), epoch_file2, dt) lc3 = make_lc(path3 + '{0}.txt'.format(src_index), path3 + '{0}_bkg.txt'.format(src_index), epoch_file3, dt) lc = lc3.join(lc2) bkgevt_file = np.loadtxt(path2 + '{0}_bkg_ep23.txt'.format(src_index)) epoch_file = np.loadtxt(path2 + 'epoch_src_{0}_ep23.txt'.format(src_index)) tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] T_tot = tstop[-1] - tstart[0] with open(path2 + 'simulation/{0}_LSP_FP_qpo_ep23.txt'.format(src_index), 'a+') as f: while k_trial < num_trials: ev_all = EventList() ev_all_bkg = EventList() for i in range(len(ID)): lc_cut = lc.truncate(start=tstart[i], stop=tstop[i], method='time') ev = EventList() ev.time = sim_evtlist(lc_cut) ev_all = ev_all.join(ev) index_b = len(np.where(bkgevt_file[:, 2] == ID[i])[0]) temp = index_b / 12. cts_num = np.random.poisson(temp) ev_bkg = EventList() ev_bkg.time = np.random.uniform(tstart[i] - dt / 2, tstop[i] + dt / 2, cts_num) ev_all_bkg = ev_all_bkg.join(ev_bkg) lc_src = ev_all.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot) lc_bkg = ev_all_bkg.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot) # T_exp=lc_new.time[-1]-lc_new.time[0] lc_new_ep2_src = lc_src.truncate(start=tstart[0], stop=tstop[1], method='time') lc_new_ep2_bkg = lc_bkg.truncate(start=tstart[0], stop=tstop[1], method='time') lc_new_ep2_src.counts -= lc_new_ep2_bkg.counts lc_new_ep3_src = lc_src.truncate(start=tstart[2], stop=tstop[-1], method='time') lc_new_ep3_bkg = lc_bkg.truncate(start=tstart[2], stop=tstop[-1], method='time') lc_new_ep3_src.counts -= lc_new_ep3_bkg.counts print(np.sum(lc_new_ep2_src.counts)) print(np.sum(lc_new_ep3_src.counts)) freq2 = np.arange(1 / (tstop[1] - tstart[0]), 0.5 / dt, 1 / (5 * (tstop[1] - tstart[0]))) freq2 = freq2[np.where(freq2 > 1 / 20000.)] freq3 = np.arange(1 / (tstop[-1] - tstart[2]), 0.5 / dt, 1 / (5 * (tstop[-1] - tstart[2]))) freq3 = freq3[np.where(freq3 > 1 / 20000.)] temp2 = get_LS(lc_new_ep2_src.time, lc_new_ep2_src.counts, freq=freq2) temp3 = get_LS(lc_new_ep3_src.time, lc_new_ep3_src.counts, freq=freq3) f.writelines( (str(temp2[0]) + ' ' + str(temp2[1]) + ' ' + str(temp2[2]) + ' ' + str(temp2[3]) + ' ' + str(temp3[0]) + ' ' + str(temp3[1]) + ' ' + str(temp3[2]) + ' ' + str(temp3[3]) + '\n')) k_trial += 1 f.close()
def slow_sim_fixpds_qpo_chandra(src_index, num_trials=2, dt=100): k_trial = 0 path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/' epoch_file = np.loadtxt(path + 'epoch_src_{0}.txt'.format(src_index)) # epoch_file=epoch_file[23:] tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] evt_file_origin = np.loadtxt(path + '{0}.txt'.format(src_index)) bkgevt_file_origin = np.loadtxt(path + '{0}_bkg.txt'.format(src_index)) # (evt_file,bkgevt_file)=filter_obs(evt_file_origin,bkgevt_file_origin,ID) evt_file = evt_file_origin bkgevt_file = bkgevt_file_origin T_tot = tstop[-1] - tstart[0] cts_rate = len(evt_file) / (2 * np.sum(exptime)) * dt # 实际的cts-rate应为这个的2倍 bkg_rate = len(bkgevt_file_origin) / (2 * np.sum(exptime)) * dt num_bins = int(T_tot / dt) freq = np.arange(1 / T_tot, 0.5 / dt, 1 / (5 * T_tot)) freq = freq[np.where(freq > 1 / 20000.)] sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt) w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot) spectrum = bending_po( w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian( w, [4.01e-4, 4.01e-4 / 16, 200, 2]) # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) lc = sim.simulate(spectrum) # lc=sim.simulate(pow) lc.time = lc.time + tstart[0] lc.gti = [[lc.time[0], lc.time[-1]]] lc.counts += cts_rate lc.counts[np.where(lc.counts < 0)] = 0 ps = Powerspectrum(lc, norm='frac') fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True) ax1.loglog() ax1.step(ps.freq, ps.power, lw=2, color='blue') ax1.plot([1 / 950.7, 1 / 950.7], [0, np.max(ps.power)], '--', linewidth=1) ax1.set_ylabel("Frequency (Hz)", fontproperties=font1) ax1.set_ylabel("Power (raw)", fontproperties=font1) ax1.set_yscale('log') ax1.tick_params(axis='x', labelsize=16) ax1.tick_params(axis='y', labelsize=16) ax1.tick_params(which='major', width=1.5, length=7) ax1.tick_params(which='minor', width=1.5, length=4) for axis in ['top', 'bottom', 'left', 'right']: ax1.spines[axis].set_linewidth(1.5) plt.show() with open(path + 'simulation/{0}_LSP_FP_qpo_nofix.txt'.format(src_index), 'a+') as f: while k_trial < num_trials: ev_all = EventList() ev_all_bkg = EventList() for i in range(len(ID)): lc_cut = lc.truncate(start=tstart[i], stop=tstop[i], method='time') ev = EventList() ev.time = sim_evtlist(lc_cut) ev_all = ev_all.join(ev) index_b = len(np.where(bkgevt_file[:, 2] == ID[i])[0]) temp = index_b / 12. cts_num = np.random.poisson(temp) ev_bkg = EventList() ev_bkg.time = np.random.uniform(tstart[i] - dt / 2, tstop[i] + dt / 2, cts_num) ev_all_bkg = ev_all_bkg.join(ev_bkg) lc_src = ev_all.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot) lc_bkg = ev_all_bkg.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot) print(np.sum(lc_src)) print(np.sum(lc_bkg)) # T_exp=lc_new.time[-1]-lc_new.time[0] lc_new = lc_src lc_new.counts -= lc_bkg.counts temp = get_LS(lc_new.time, lc_new.counts, freq=freq) # f.writelines((str(temp[0]) + ' ' + str(temp[1]) +' '+ str(temp[2])+' '+ str(temp[3])+'\n')) k_trial += 1 f.close()
def slow_sim_fixpds_qpo_XMM(src_index, num_trials=2): k_trial = 0 dt = 100 path = '/Users/baotong/Desktop/CDFS/xmm_CDFS/xmm_txt/' epoch_file = np.loadtxt(path + 'epoch6_xmmobs.txt') tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] # evt_file_origin =np.loadtxt(path + 'XID{0}_{1}_all_obs_10sec.txt'.format(source_id, det)) # bkgevt_file_origin = np.loadtxt(path + 'bkg_XID{0}_{1}_all_obs_10sec.txt'.format(source_id, det)) # (evt_file,bkgevt_file)=filter_obs(evt_file_origin,bkgevt_file_origin,ID) T_tot = tstop[-1] - tstart[0] # cts_rate = len(evt_file)/ (2 * np.sum(exptime)) * dt # 实际的cts-rate应为这个的2倍 cts_rate = 0.001 * 0.67 / 2 * dt ##这里cts_rate单位都是per bin bkg_cts_rate = 0.001 * 0.33 num_bins = int(T_tot / dt) freq = np.arange(1 / T_tot, 0.5 / dt, 1 / (5 * T_tot)) freq = freq[np.where(freq > 1 / 20000.)] sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt) w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot) spectrum = bending_po( w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian( w, [4.01e-4, 4.01e-4 / 16, 200, 2]) # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) lc = sim.simulate(spectrum) lc.time = lc.time + tstart[0] # lc.gti=[[lc.time[0],lc.time[-1]]] lc.counts += cts_rate lc.counts[np.where(lc.counts < 0)] = 0 with open(path + 'simulation/ep6_1e-3_SN3_LSP_qpo.txt', 'a+') as f: while k_trial < num_trials: ev_all = EventList() ev_all_bkg = EventList() for i in range(len(ID)): lc_cut = lc.truncate(start=tstart[i], stop=tstop[i], method='time') ev = EventList() ev.time = sim_evtlist(lc_cut) ev_all = ev_all.join(ev) index_b = (tstop[i] - tstart[i]) * bkg_cts_rate temp = index_b cts_num = np.random.poisson(temp) ev_bkg = EventList() ev_bkg.time = np.random.uniform(tstart[i] - dt / 2, tstop[i] + dt / 2, cts_num) ev_all_bkg = ev_all_bkg.join(ev_bkg) lc_src = ev_all.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot) lc_bkg = ev_all_bkg.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot) print(np.sum(lc_src)) print(np.sum(lc_bkg)) # T_exp=lc_new.time[-1]-lc_new.time[0] # lc_new=lc_src-lc_bkg lc_new = lc_src + lc_bkg temp = get_LS(lc_new.time, lc_new.counts, freq=freq) f.writelines( (str(temp[0]) + ' ' + str(temp[1]) + ' ' + str(temp[2]) + ' ' + str(temp[3]) + '\n')) k_trial += 1 f.close()
def slow_sim_const(k,src_index,num_trials=2): def pfold(time,period,binnumber=10): turns=time/period HIST=np.zeros(binnumber) phase=turns-turns.astype('int') for i in range(len(phase)): HIST[int(phase[i]*binnumber)]+=1 x = np.linspace(0, 1, binnumber + 1) x = x[:-1] x2 = np.concatenate((x, x + 1)); y2 = np.concatenate((HIST, HIST)) plt.figure(1,(9,6)) plt.step(x2,y2) k_trial =0 dt=100 FP = []; period = []; cts_num=[]; peakP=[]; power_P=[] path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k) epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k)) tstart = epoch_file[:, 0];tstop = epoch_file[:, 1] ID=epoch_file[:,2];exptime = epoch_file[:, 3] evt_file = np.loadtxt(path + '{0}.txt'.format(src_index)) bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index)) T_tot=tstop[-1]-tstart[0] cts_rate = len(evt_file)/ (2 * np.sum(exptime)) * dt # 实际的cts-rate应为这个的2倍 num_bins = int(T_tot / dt) T_exp = T_tot freq=np.arange(1/T_exp,0.5/dt,1/(5*T_exp)) freq=freq[np.where(freq > 1 / 20000.)] with open(path + '/simulation/{0}_max_LSP_const.txt'.format(src_index), 'a+') as f: while k_trial < num_trials: ev_all = EventList() for i in range(len(ID)): index = len(np.where(evt_file[:, 2] == ID[i])[0]) index_b = len(np.where(bkgevt_file[:, 2] == ID[i])[0]) temp = index - index_b / 12. if temp < 0: temp = 0 cts_num = np.random.poisson(temp) ev = EventList() ev.time = np.random.uniform(tstart[i] - dt / 2, tstop[i] + dt / 2, cts_num) ev_all = ev_all.join(ev) lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0] - 0.5 * dt, tseg=ev_all.time[-1] - ev_all.time[0]) # T_exp=lc_new.time[-1]-lc_new.time[0] temp = get_LS(lc_new.time, lc_new.counts, freq=freq, trial=k_trial) f.writelines((str(temp[0]) + ' ' + str(temp[1]) + '\n')) if temp[1]>18.36: print('caution') plt.savefig(path + '/simulation/LSP_{0}_trial_{1}.eps'.format(src_index,k_trial)) plt.close() np.savetxt(path + '/simulation/{0}_trial_{1}_event.txt'.format(src_index,k_trial),ev_all.time) pfold(ev_all.time,temp[0],10) plt.savefig(path + '/simulation/pfold_{0}_trial_{1}.eps'.format(src_index, k_trial)) break k_trial += 1 f.close()
def make_evt_nobkg(cts_rate, dt, epoch_file, lc_bin=100, fixpds=True, lc=0): tstart = epoch_file[:, 0] tstop = epoch_file[:, 1] ID = epoch_file[:, 2] exptime = epoch_file[:, 3] T_tot = tstop[-1] - tstart[0] cts_rate = cts_rate / 2 * dt ##这里cts_rate单位都是per bin num_bins = int(T_tot / dt) freq = np.arange(1 / T_tot, 0.5 / dt, 1 / (5 * T_tot)) freq = freq[np.where(freq > 1 / 20000.)] if fixpds: lc = lc else: print('not fix!') sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt) w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot) spectrum = bending_po( w, [2.3e-4, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian( w, [4.01e-4, 4.01e-4 / 16, 0.1, 2]) lc = sim.simulate(spectrum) lc.time = lc.time + tstart[0] lc.counts += cts_rate lc.counts[np.where(lc.counts < 0)] = 0 lc.gti = [[lc.time[0], lc.time[-1]]] plt.plot(w, spectrum) plt.loglog() plt.show() plt.plot(lc.time, lc.counts) plt.show() ps = Powerspectrum(lc, norm='abs') fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True) ax1.plot(ps.freq, ps.power, lw=2, color='blue') ax1.set_ylabel("Frequency (Hz)", fontproperties=font1) ax1.set_ylabel("Power (raw)", fontproperties=font1) # ax1.set_yscale('log') ax1.tick_params(axis='x', labelsize=16) ax1.tick_params(axis='y', labelsize=16) ax1.tick_params(which='major', width=1.5, length=7) ax1.tick_params(which='minor', width=1.5, length=4) for axis in ['top', 'bottom', 'left', 'right']: ax1.spines[axis].set_linewidth(1.5) plt.loglog() plt.show() ev_all = EventList() ev_all_bkg = EventList() for i in range(len(ID)): lc_cut = lc.truncate(start=tstart[i], stop=tstop[i], method='time') ev = EventList() ev.time = sim_evtlist(lc_cut) ev_all = ev_all.join(ev) lc_src = ev_all.to_lc(dt=lc_bin, tstart=tstart[0], tseg=T_tot) ps = Powerspectrum(lc_src, norm='abs') fig, ax2 = plt.subplots(1, 1, figsize=(9, 6), sharex=True) ax2.plot(ps.freq, ps.power, lw=2, color='blue') ax2.set_ylabel("Frequency (Hz)", fontproperties=font1) ax2.set_ylabel("Power (raw)", fontproperties=font1) # ax1.set_yscale('log') ax2.tick_params(axis='x', labelsize=16) ax2.tick_params(axis='y', labelsize=16) ax2.tick_params(which='major', width=1.5, length=7) ax2.tick_params(which='minor', width=1.5, length=4) for axis in ['top', 'bottom', 'left', 'right']: ax2.spines[axis].set_linewidth(1.5) plt.loglog() plt.show() return [ev_all.time, lc_src]