def easy_baseline(x, y, result=''): if (result != ''): if (os.path.exists(result) == False): os.mkdir(result) else: result = os.getcwd() dir_d = os.path.join(result, 'in_xy.txt') data_d = [x, y] zhf.printdatatofile(dir_d, data_d) x = np.array(x, np.double) y = np.array(y, np.double) os.system( 'Rscript /home/laojin/software/idl_my_lib/zbbidl/baseline_r_2015.r ' + dir_d + ' ' + result + '>>/dev/null') if (os.path.exists(os.path.join(result, 'back.txt'))): back = np.array(zhf.readcol(os.path.join(result, 'back.txt'))[0]) net = y - back x = decimal_for_array(x, 20) y = decimal_for_array(y, 20) net = decimal_for_array(net, 20) back = decimal_for_array(back, 20) zhf.printdatatofile(os.path.join(result, 'xout.txt'), [x]) zhf.printdatatofile(os.path.join(result, 'net.txt'), [net]) zhf.printdatatofile(os.path.join(result, 'back.txt'), [back]) zhf.printdatatofile(os.path.join(result, 'summary_t_obs_net_back.txt'), [x, y, net, back]) return x, y, net, back else: print('failed!!') print('I can not do the following!!') return False
def r_baseline(x, y, result='', lamb=1, hwi=30, it=20, int=200): if (result != ''): if (os.path.exists(result) == False): os.mkdir(result) else: result = os.getcwd() dir_d = os.path.join(result, 'in_xy.txt') data_d = [x, y] zhf.printdatatofile(dir_d, data_d) x = np.array(x, np.double) y = np.array(y, np.double) os.system('Rscript /home/laojin/my_work/my_r/zzh_baseline_r.r ' + dir_d + ' ' + result + ' ' + str(lamb) + ' ' + str(hwi) + ' ' + str(it) + ' ' + str(int) + '>>/dev/null') if (os.path.exists(os.path.join(result, 'back.txt'))): back = np.array(zhf.readcol(os.path.join(result, 'back.txt'))[0]) net = y - back x = decimal_for_array(x, 20) y = decimal_for_array(y, 20) net = decimal_for_array(net, 20) back = decimal_for_array(back, 20) zhf.printdatatofile(os.path.join(result, 'xout.txt'), [x]) zhf.printdatatofile(os.path.join(result, 'net.txt'), [net]) zhf.printdatatofile(os.path.join(result, 'back.txt'), [back]) zhf.printdatatofile(os.path.join(result, 'summary_t_obs_net_back.txt'), [x, y, net, back]) return x, y, net, back else: print('failed!!') print('I can not do the following!!') return False
edges_stop[i], binsize=binsize) #make_phaI_with_events(bnname,bgo,data_topdir,savedir,edges_start[i],edges_stop[i]) plt.savefig(save_all + 'A_' + bnname + '_' + bgo + '_' + str(i) + '.png') plt.close() print('---------------------------------------') try: value, value_arr1, value_arr2, flux_list = xspec_fit_kernel( filelist, savedir, makedirs + 'Z_' + str(i)) plt.savefig(save_all2 + 'A_' + bnname + '_' + str(i) + '.png') plt.close() #copy_rspI(savedir+'foldedspec.png',save_all2 + 'A_'+bnname+'_'+str(i)+'.png') zhf.printdatatofile(makedirs + 'Z_' + str(i) + '_Flux.txt', data=flux_list) zhf.printdatatofile(makedirs + 'Z_' + str(i) + '_Epec.txt', data=[[value], [value_arr1], [value_arr2]]) except: print(bnname + ' ' + str(i) + '出现错误,跳过拟合!') continue except (IndexError): print(bnname + '文件内容错误,跳过拟合!') continue else: print(bnname + '探头数量不足') else: print(bnname + '信息不完整。')
def run_f(year): localdir = '/media/laojin/Elements/trigdata/' resultlocaldir = '/home/laojin/results/results_GBM/' NaI = [ 'n0', 'n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'n9', 'na', 'nb' ] BGO = ['b0', 'b1'] time_start = -100 time_stop = 300 ch_top = 110 ch_buttum = 8 g_ch_top = 110 g_ch_buttum = 7 plot_light_curve = True topdir = localdir + year + '/' resultdir = resultlocaldir + year + '/' dirlist1 = os.listdir(topdir) print(year + ' have :', len(dirlist1)) dirlist = [] for dirl in dirlist1: if (os.path.isdir(topdir + dirl)): dirlist.append(dirl) print('The number of sample is ', len(dirlist)) dirlist = np.sort(dirlist) one_pulse = [] two_pulse = [] three_pulse = [] complex_pulse = [] for name in dirlist: filedir = topdir + name + '/' if (plot_light_curve): data_c = {} ni_max = [] ni_good = [] print('function analysis doing---') print(name) for ni in NaI: print(ni, end='\r') filem = 'glg_tte_' + ni + '_' + name + '_' filename = zhf.findfile(filedir, filem + '*') if filename == False: continue else: filename = filename[0] data = fits.open(filedir + filename) data_c[ni] = data trigtime = data[0].header['TRIGTIME'] time = data[2].data.field(0) ch = data[2].data.field(1) t = time - trigtime ch_index = np.where((ch >= ch_buttum) & (ch <= ch_top)) t = t[ch_index] t_index = np.where((t >= time_start) & (t <= time_stop)) t = t[t_index] tif, rif = easy_histogram(t, binsize=1) t_r, cs, bs = r_baseline(tif, rif) ev = cs + bs.sum() / bs.size if (rif[np.where(rif == 0)].size > 1000): ni_good.append(False) else: ni_good.append(True) ni_max.append(ev.max()) print('next.....') index_ni = np.argsort(ni_max) nai_sort = np.array(NaI)[index_ni] max_ni_name = [ nai_sort[-1], nai_sort[-2], nai_sort[-3], nai_sort[-4] ] for index, ni in enumerate(max_ni_name): print(ni, end='\r') data = data_c[ni] trigtime = data[0].header['TRIGTIME'] time = data[2].data.field(0) ch = data[2].data.field(1) ch_n = data[1].data.field(0) e1 = data[1].data.field(1) e2 = data[1].data.field(2) t = time - trigtime t_index = np.where((t >= time_start) & (t <= time_stop)) t = t[t_index] ch = ch[t_index] ch_index = np.where((ch >= ch_buttum) & (ch <= ch_top)) tk = t[ch_index] t_h, rate = easy_histogram(tk, binsize=0.01) t_r, cs, bs = r_baseline(t_h, rate) in_list = [t_r, rate, cs, bs] new_time, new_ch = r_remv_photo(t, ch, ch_n, 1) t_, e_ = get_all_energy(new_time, new_ch, ch_n, e1, e2) found_duration_dir = resultdir + name + '/found_duration_check_2/' start_edges, stop_edges = get_pulse_duration( tk, savedir=found_duration_dir, gamma=np.exp(-13), r=3) plt.figure(figsize=(20, 20)) plt.subplot(2, 2, 1) plt.tick_params(labelsize=20) plt.title('light curve', size=20) plt.plot(t_h, rate, linestyle='steps', color='k') plt.xlabel('time (s)', size=20) plt.ylabel('counts rate /s', size=20) plt.xlim([time_start, time_stop]) plt.subplot(2, 2, 2) plt.tick_params(labelsize=20) plt.title('point map', size=20) plt.plot(t_, e_, ',', color='k') plt.xlabel('time (s)', size=20) plt.ylabel('energy (kev)', size=20) plt.yscale('log') plt.xlim([time_start, time_stop]) plt.ylim([5, 9.1e2]) print('estimating the edges .....') if (len(start_edges) > 0): savedir = resultdir + name + '/' if (os.path.exists(savedir) == False): os.makedirs(savedir) edge_list = [start_edges, stop_edges] zhf.printdatatofile(savedir + 'pulse_edges_1.txt', edge_list) in_list.append(edge_list) if (len(start_edges) == 1): one_pulse.append([name, ni]) elif (len(start_edges) == 2): two_pulse.append([name, ni]) elif (len(start_edges) == 3): three_pulse.append([name, ni]) else: complex_pulse.append([name, ni]) plt.subplot(2, 2, 3) plt.tick_params(labelsize=20) plt.plot(t_h, rate, linestyle='steps', color='k') if (len(start_edges) > 0): for i in range(len(start_edges)): plt.axvline(x=start_edges[i], color='r') plt.axvline(x=stop_edges[i], color='g') plt.xlabel('time (s)', size=20) plt.ylabel('counts rate /s', size=20) plt.xlim([time_start, time_stop]) plt.subplot(2, 2, 4) plt.tick_params(labelsize=20) plt.plot(t_, e_, ',', color='k') if (len(start_edges) > 0): for i in range(len(start_edges)): plt.axvline(x=start_edges[i], color='r') plt.axvline(x=stop_edges[i], color='g') plt.xlabel('time (s)', size=20) plt.ylabel('energy (kev)', size=20) plt.yscale('log') plt.xlim([time_start, time_stop]) plt.ylim([5, 9.1e2]) savedir = resultdir + name + '/' if (os.path.exists(savedir) == False): os.makedirs(savedir) plt.savefig(savedir + 'Z_plot_1_2.png') savealldir = resultdir + 'A_plot_1_2_2/' if (os.path.exists(savealldir) == False): os.makedirs(savealldir) plt.savefig(savealldir + name + '_plot_1_2.png') plt.close() if (len(start_edges) > 0): break if (len(dirlist) >= 10): savedir_all = resultdir + 'A_plot_1_2_2/' if (os.path.exists(savedir_all) == False): os.makedirs(savedir_all) zhf.printdatatofile(savedir_all + 'Z_one_pulse_sample_list.txt', zhf.transpose(one_pulse)) zhf.printdatatofile(savedir_all + 'Z_two_pulse_sample_list.txt', zhf.transpose(two_pulse)) zhf.printdatatofile(savedir_all + 'Z_three_pulse_sample_list.txt', zhf.transpose(three_pulse)) zhf.printdatatofile(savedir_all + 'Z_complex_pulse_sample_list.txt', zhf.transpose(complex_pulse))
t_r, cs, bs = r_baseline(t_h, rate) in_list = [t_r, rate, cs, bs] for num, max_name in enumerate(max_ni_name): if ((ni == max_name) & (ni_good[index])): print('estimating the edges .....') found_duration_dir = resultdir + name + '/NaI_found_duration_check/' start_edges, stop_edges = get_pulse_duration( t, savedir=found_duration_dir) if (len(start_edges) > 0): edge_list = [start_edges, stop_edges] savedir = resultdir + name + '/' if (os.path.exists(savedir) == False): os.makedirs(savedir) zhf.printdatatofile( savedir + str(num) + '_pulse_edges.txt', edge_list) in_list.append(edge_list) in_list_num.append(num) in_list_name.append(max_name) if (ni == max_ni_name[0]): if (len(start_edges) == 1): one_pulse.append([name, ni]) elif (len(start_edges) == 2): two_pulse.append([name, ni]) elif (len(start_edges) == 3): three_pulse.append([name, ni]) else: complex_pulse.append([name, ni]) print('finish estimating .')
from zjh_fermi_daily_geometry import Fermi_daily_geometry import numpy as np timelist = zhf.readcol('/home/laojin/shiyan/result_time.txt') posistion = [] for i in timelist[0]: print(i) qsj,pos,sc = Fermi_daily_geometry(i).find_right_list() if(sc[1]>180): sc[1] = sc[1]-360 posistion.append(sc) posistion = np.array(posistion).T der = posistion[0] ra = posistion[1] #index = np.argsort(ra) #ra = ra[index] #der = der[index] zhf.printdatatofile('/home/laojin/shiyan/SAA_point.txt',[ra,der])
bin_c, bin_n = zhifang(t, t.min(), t.max(), bin) return bin_c, bin_n detectors = [ 'n0', 'n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'n9', 'na', 'nb' ] bin_c, bin_n = read() timelist = finding(bin_c, bin_n) #print(timelist) resulttime = [] for i in timelist: sum0 = 0 #print(i) for j in detectors: d_time = preice(i, j) d_time = d_time[len(d_time) - 1] sum0 = sum0 + d_time resulttime.append(GBMtime.met_to_utc(sum0 / len(detectors)).fits) fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) ax.plot(bin_c, bin_n, ',') ax.set_xlabel('Time') ax.set_ylabel('Number of Photo') fig.savefig('/home/laojin/shiyan/for_all_time.png') zhf.printdatatofile('/home/laojin/shiyan/result_time.txt', [resulttime])
print(epec_file_name) epec, epec_er1, epec_er2 = zhf.readcol(epec_file_name) print(flux_file_list[index]) flux, flux_p, flux_a, p_n, p_n_p, p_n_a = zhf.readcol( flux_file_list[index]) epec_value = epec[0] epec_err = (epec_er2[0] - epec_er1[0]) * 0.5 flux_value = np.mean(flux) flux_err = (np.mean(flux_a) - np.mean(flux_p)) * 0.5 Epec.append(epec_value) Epec_err.append(epec_err) Flux.append(flux_value) Flux_err.append(flux_err) print('----------------------------------------') zhf.printdatatofile(savetop + 'epec_flux.txt', data=[Epec, Epec_err, Flux, Flux_err]) plt.figure(figsize=(10, 10)) plt.subplot(1, 1, 1) plt.title('The correlation of Epec and Flux of single pulse') plt.errorbar(Epec, Flux, xerr=Epec_err, yerr=Flux_err, zorder=1, fmt='o', mfc='r', ecolor='g') plt.xscale('log') plt.yscale('log') plt.xlabel(r'Epec ${KeV}$')
plt.plot(t_, e_, ',', color='k') plt.xlabel('time (s)', size=20) plt.ylabel('energy (kev)', size=20) plt.yscale('log') plt.xlim([time_start, time_stop]) #plt.ylim([5, 9.1e2]) plt.ylim([120, 3e4]) print('estimating the edges .....') if (len(start_edges) > 0): savedir = resultdir + name + '/' if (os.path.exists(savedir) == False): os.makedirs(savedir) edge_list = [start_edges, stop_edges] zhf.printdatatofile(savedir + 'pulse_edges_xxx.txt', edge_list) in_list.append(edge_list) zhf.printdatatofile(resultlocaldir + 'A_add.txt', [[name], [detector]]) plt.subplot(2, 2, 3) plt.tick_params(labelsize=20) plt.plot(t_h, rate, linestyle='steps', color='k') if (len(start_edges) > 0): for i in range(len(start_edges)): plt.axvline(x=start_edges[i], color='r') plt.axvline(x=stop_edges[i], color='g') plt.xlabel('time (s)', size=20) plt.ylabel('counts rate /s', size=20) plt.xlim([time_start, time_stop]) plt.subplot(2, 2, 4) plt.tick_params(labelsize=20)