def mkForeCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL from GRATools.utils.gFTools import get_cl_param cl_param_file = os.path.join( GRATOOLS_OUT, '%s_%s_parameters.txt' % (in_label, binning_label)) _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): mask_file = data.MASK_FILE if type(mask_file) == list: mask_file = mask_file[i] mask = hp.read_map(mask_file) logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, _emean[i])) l_max = 1000 _l = np.arange(l_max) flux_map_name = in_label + '_fore_%i-%i.fits' % (emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FORE, flux_map_name)) flux_map_masked = hp.ma(flux_map) flux_map_masked.mask = np.logical_not(mask) fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\ float(len(flux_map))) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f' % fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \ iter=5) _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label))))
def mkForeCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL from GRATools.utils.gFTools import get_cl_param cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' %(in_label, binning_label)) _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): mask_file = data.MASK_FILE if type(mask_file) == list: mask_file = mask_file[i] mask = hp.read_map(mask_file) logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) l_max= 1000 _l = np.arange(l_max) flux_map_name = in_label+'_fore_%i-%i.fits'%(emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FORE, flux_map_name)) flux_map_masked = hp.ma(flux_map) flux_map_masked.mask = np.logical_not(mask) fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\ float(len(flux_map))) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \ iter=5) _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label))))
FLUX_LABELS = ['UCV (t56) w/o Foreground sub', 'UCV (t56) w/ Foreground sub'] #OUT_LABEL = 'Flux_t56_srcmask2_Rm-Rp' #OUT_LABEL = 'Flux_types_srcmask2' #OUT_LABEL = 'Flux_t56_srcmask2-1p5-weighted' #OUT_LABEL = 'Flux_t56_maskweight_north-south' #OUT_LABEL = 'Flux_t56_maskweight_east-west' OUT_LABEL = 'Flux_t56_maskweight' plt.figure(figsize=(10, 7), dpi=80) from GRATools.utils.gDrawRef import ref_igrb_band from GRATools.utils.gDrawRef import ref_igrb_noFGsub leg, lab = ref_igrb_band() igrb, lab_igrb = ref_igrb_noFGsub() flux = [] for f in FLUX_FILES: from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, fsky = get_cl_param(f) spec = plt.errorbar(_emean, _f*_emean, fmt='o', markersize=3, \ elinewidth=1, xerr=(_emax-_emin)/2, yerr=_ferr*_emean) label = os.path.basename(f).replace('_parameters.txt', '') flux.append(spec) plt.xscale("log") plt.yscale("log") plt.xlabel('Energy [MeV]') plt.ylabel('E$^{2}$ $\cdot$ Flux [MeV cm$^{-2}$ s$^{-1}$ sr$^{-1}$]') plt.title(' Energy Spectrum') plt.legend([igrb, leg]+flux, [lab_igrb, lab]+FLUX_LABELS, loc=3) #overlay_tag() save_current_figure(OUT_LABEL+'_ESpec.png')
def mkCsi(**kwargs): """ """ get_var_from_file(kwargs['config']) ncores = kwargs['ncores'] psf_file = data.PSF_REF_FILE p = multiprocessing.Pool(processes=ncores) logger.info('Starting Csi analysis...') in_label = data.IN_LABEL out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) csi_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \ %(out_label, binning_label)), 'w') psf_ref = get_psf_ref(psf_file) #psf_ref.plot(show=False) #plt.xscale('log') #plt.yscale('log') #plt.show() for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) cont_ang = np.radians(psf_ref(_emean[i])) csi_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) flux_map_name = in_label+'_flux_%i-%i.fits'%(emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name)) flux_map = udgrade_as_psf(flux_map, cont_ang) R = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name)) R = udgrade_as_psf(R, cont_ang) fsky = 1.-(len(np.where(flux_map == hp.UNSEEN)[0])/\ float(len(flux_map))) logger.info('fsky = %f'%fsky) npix = len(flux_map) nside = hp.npix2nside(npix) _unmask = np.where(flux_map != hp.UNSEEN)[0] npix_unmask = len(_unmask) Imean = _f[i] dI = flux_map - Imean dR = R - Imean R = permute_unmasked_pix(R) dR = permute_unmasked_pix(dR) th_bins = data.TH_BINNING theta = [] for thmin, thmax in zip(th_bins[:-1], th_bins[1:]): th_mean = np.sqrt(thmin*thmax) theta.append(th_mean) theta = np.array(theta) logger.info('Computing Csi...') diri = hp.pixelfunc.pix2ang(nside, _unmask) veci = hp.rotator.dir2vec(diri) xyz = np.array([(veci[0][i], veci[1][i], veci[2][i]) for i in range(0, len(veci[0]))]) args = zip(_unmask, xyz, [dI]*npix_unmask, [dR]*npix_unmask, [nside]*npix_unmask) #args = zip(_unmask, xyz, [flux_map]*npix_unmask, [R]*npix_unmask, # [nside]*npix_unmask) a = np.array(p.map(csi_compute, args)) SUMij_list = a[:, 0] SUMf_list = a[:, 1] SUMR_list = a[:, 2] SUMij_th = [] SUMf_th = [] SUMR_th = [] for i, s in enumerate(SUMij_list[0]): SUMij_th.append(np.sum(SUMij_list[:, i])) SUMf_th.append(np.sum(SUMf_list[:, i])) SUMR_th.append(np.sum(SUMR_list[:, i])) csi = (np.array(SUMij_th))/np.array(SUMf_th)#-Imean**2 r = (np.array(SUMR_th))/np.array(SUMf_th)#-Imean**2 csi_txt.write('THETA\t%s\n'%str(list(theta)).replace('[',''). \ replace(']','').replace(', ', ' ')) csi_txt.write('CSI\t%s\n'%str(list(csi)).replace('[',''). \ replace(']','').replace(', ', ' ')) csi_txt.write('R\t%s\n'%str(list(r)).replace('[',''). \ replace(']','').replace(', ', ' ')) csi_txt.close() p.close() p.join() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \ %(out_label, binning_label))))
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL mask_label = data.MASK_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt' % out_wb_label) if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) _l = np.arange(0, 1000) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: from GRATools.utils.gWindowFunc import get_wbeam wb = get_wbeam(out_wb_txt) save_current_figure('Wbeam_%s.png' % out_wb_label, clear=True) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL in_label = in_label + '_' + mask_label out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE mask = hp.read_map(mask_file) cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_cls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) gamma = data.WEIGHT_SPEC_INDEX Im = (1 / (1 - gamma)) * (emax**(1 - gamma) - emin**(1 - gamma)) / (emax - emin) eweightedmean = np.power(1 / Im, 1 / gamma) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, eweightedmean)) l_max = 1000 _l = np.arange(l_max) wb_en = wb.hslice(eweightedmean)(_l) flux_map_name = in_label + '_flux_%i-%i.fits' % (emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name)) flux_map_masked = hp.ma(flux_map) flux_map_masked.mask = np.logical_not(mask) fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\ float(len(flux_map))) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f' % fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \ iter=5) _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4) cn_fit = np.average(_cl_fit[-500:-100] / fsky) / len( _cl_fit[-500:-100]) print 'cn fit = ', cn_fit print 'cn poisson = ', _cn[i] cn = _cn[i] wl = wb_en * wpix _cl = (_cl / fsky - cn) / (wl**2) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) _cl_err = np.sqrt(2. / ((2 * _l + 1) * fsky)) * (_cl + (cn / wl**2)) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_cls.txt' \ %(out_label, binning_label))))
def mkCross(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label) if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) _l = np.arange(0, 1000) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: from GRATools.utils.gWindowFunc import get_wbeam wb = get_wbeam(out_wb_txt) save_current_figure('Wbeam_%s.png'%out_wb_label, clear=True) logger.info('Starting Cl analysis...') in_label1 = data.IN_LABEL1 in_label2 = data.IN_LABEL2 out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE mask = hp.read_map(mask_file) cl_param_file1 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label1, binning_label)) cl_param_file2 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label2, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file1) _emin2, _emax2, _emean2, _f2, _ferr2, _cn2, _fsky2 = get_cl_param(cl_param_file2) cross_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_cross.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) gamma = data.WEIGHT_SPEC_INDEX Im = (1/(1-gamma))*(emax**(1-gamma)-emin**(1-gamma))/(emax-emin) eweightedmean = np.power(1/Im, 1/gamma) cross_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, eweightedmean)) l_max= 1000 _l = np.arange(l_max) wb_en = wb.hslice(eweightedmean)(_l) flux_map_name1 = in_label1+'_flux_%i-%i.fits'%(emin, emax) flux_map_name2 = in_label2+'_flux_%i-%i.fits'%(emin, emax) flux_map1 = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name1)) flux_map2 = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name2)) flux_map_masked1 = hp.ma(flux_map1) flux_map_masked1.mask = np.logical_not(mask) flux_map_masked2 = hp.ma(flux_map2) flux_map_masked2.mask = np.logical_not(mask) fsky1 = 1.-(len(np.where(flux_map_masked1.filled() == hp.UNSEEN)[0])/\ float(len(flux_map1))) fsky2 = 1.-(len(np.where(flux_map_masked2.filled() == hp.UNSEEN)[0])/\ float(len(flux_map2))) if kwargs['show'] == True: hp.mollview(flux_map_masked1.filled(), title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky1, fsky2 fsky = np.sqrt(fsky1*fsky2) nside1 = hp.npix2nside(len(flux_map1)) nside2 = hp.npix2nside(len(flux_map1)) wpix1 = hp.sphtfunc.pixwin(nside1)[:l_max] wpix2 = hp.sphtfunc.pixwin(nside2)[:l_max] _cl_cross = hp.sphtfunc.anafast(flux_map_masked1.filled(), flux_map_masked2.filled(), lmax=l_max-1, \ iter=5) wl2 = wb_en*wb_en*wpix1*wpix2 _cl_cross = (_cl_cross/fsky)/(wl2) cross_txt.write('Cl\t%s\n'%str(list(_cl_cross)).replace('[',''). \ replace(']','').replace(', ', ' ')) _cl_cross_err = np.sqrt(2./((2*_l+1)*fsky))*(_cl_cross) cross_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_cross_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cross_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_cross.txt' \ %(out_label, binning_label))))
def mkCross(**kwargs): """ """ get_var_from_file(kwargs['config']) lmax = data.LMAX _l = np.arange(0, lmax) gamma = data.WEIGHT_SPEC_INDEX out_label = data.OUT_LABEL logger.info('Calculating energy spectrum...') from GRATools.utils.gWindowFunc import get_powerlaw_spline energy_spec = get_powerlaw_spline(gamma) logger.info('Calculating PSF with gtpsf 1...') dict_gtpsf1 = data.DICT_GTPSF1 logger.info('Calculating Wbeam Function1...') out_wb_label1 = data.OUT_W_LABEL1 out_wb_txt1 = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label1) in_label1 = data.IN_LABEL1 if not os.path.exists(out_wb_txt1): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf1) from GRATools.utils.gWindowFunc import get_psf psf_file1 = data.PSF_FILE1 psf1 = get_psf(psf_file1) from GRATools.utils.gWindowFunc import build_wbeam wb1 = build_wbeam(psf1, _l, out_wb_txt1) else: pass logger.info('Calculating PSF with gtpsf 2...') dict_gtpsf2 = data.DICT_GTPSF2 logger.info('Calculating Wbeam Function2...') out_wb_label2 = data.OUT_W_LABEL2 out_wb_txt2 = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label2) in_label2 = data.IN_LABEL2 if not os.path.exists(out_wb_txt2): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf2) from GRATools.utils.gWindowFunc import get_psf psf_file2 = data.PSF_FILE2 psf2 = get_psf(psf_file2) from GRATools.utils.gWindowFunc import build_wbeam wb2 = build_wbeam(psf2, _l, out_wb_txt2) else: pass logger.info('Starting Cross analysis...') mask_label1 = data.MASK_LABEL1 mask_label2 = data.MASK_LABEL2 in_label1 = in_label1 + '_' + mask_label1 in_label2 = in_label2 + '_' + mask_label2 binning_label1 = data.BINNING_LABEL1 binning_label2 = data.BINNING_LABEL2 mask_file1 = data.MASK_FILE1 mask_file2 = data.MASK_FILE2 cl_param_file1 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label1, binning_label1)) cl_param_file2 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label2, binning_label2)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f1, _ferr1, _cn1, _fsky1 = \ get_cl_param(cl_param_file1) _emin, _emax, _emean, _f2, _ferr2, _cn2, _fsky2 = \ get_cl_param(cl_param_file2) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_polspicecross.txt' \ %(out_label, binning_label1)), 'w') from GRATools.utils.gWindowFunc import get_integral_wbeam for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) mask_f1 = mask_file1 mask_f2 = mask_file2 if type(mask_file1) == list: mask_f1 = mask_file1[i] if type(mask_file2) == list: mask_f2 = mask_file2[i] mask1 = hp.read_map(mask_f1) mask2 = hp.read_map(mask_f2) wb1_en = get_integral_wbeam(out_wb_txt1, energy_spec, emin, emax) wb2_en = get_integral_wbeam(out_wb_txt2, energy_spec, emin, emax) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) l_max = lmax _l = np.arange(l_max) wb1_en = wb1_en(_l) wb2_en = wb2_en(_l) flux_map_name1 = in_label1+'_flux_%i-%i.fits'%(emin, emax) flux_map_name2 = in_label2+'_flux_%i-%i.fits'%(emin, emax) flux_map_f1 = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name1) flux_map_f2 = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name2) flux_map_f_mdclean1 = remove_monopole_dipole(flux_map_f1) flux_map_f_mdclean2 = remove_monopole_dipole(flux_map_f2) flux_map1 = hp.read_map(flux_map_f_mdclean1) flux_map2 = hp.read_map(flux_map_f_mdclean2) fsky1 = _fsky1[i] fsky2 = _fsky2[i] cn = 0. if kwargs['show'] == True: hp.mollview(flux_map1*mask1, title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') hp.mollview(flux_map2*mask2, title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') plt.show() logger.info('fsky1 = %.3f'%fsky1) logger.info('fsky2 = %.3f'%fsky2) nside1 = hp.npix2nside(len(flux_map1)) nside2 = hp.npix2nside(len(flux_map2)) wpix1 = hp.sphtfunc.pixwin(nside1)[:l_max] wpix2 = hp.sphtfunc.pixwin(nside2)[:l_max] out_name = '%s_%i-%i' %(out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') logger.info('cn poisson = %e'%cn) wl = np.sqrt(wb1_en*wpix1*wb2_en*wpix2) if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder,'%s_cl.txt'%out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder,'%s_clraw.txt'%out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder,'%s_cov.fits'%out_name) if key == 'mapfile': pol_dict[key] = flux_map_f_mdclean1 if key == 'maskfile': pol_dict[key] = mask_f1 if key == 'mapfile2': pol_dict[key] = flux_map_f_mdclean2 if key == 'maskfile2': pol_dict[key] = mask_f2 config_file_name = 'pol_%s'%(out_name) if os.path.exists(os.path.join(out_folder,'%s_cl.txt'%out_name)) and \ os.path.exists(os.path.join(out_folder,'%s_cov.fits'%out_name)): logger.info('ATT: Retriving power spectrum...') _l, _cl, _clerr = pol_cl_parse(os.path.join(out_folder, '%s_cl.txt'%out_name), os.path.join(out_folder, '%s_cov.fits'%out_name), raw_corr=(cn, wl), rebin=True) logger.info('... and covariance matrix.') _cov = pol_cov_parse(os.path.join(out_folder, '%s_cov.fits'%out_name), wl_array=wl, rebin=True, show=True) else: _l, _cl, _clerr, _cov = pol_cl_calculation(pol_dict, config_file_name, raw_corr=(cn, wl), rebin=True,show=True) cl_txt.write('multipole\t%s\n'%str(list(_l)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_clerr)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT,'%s_%s_polspicecls.txt' %(out_label, binning_label1))))
def mkCsi(**kwargs): """ """ get_var_from_file(kwargs['config']) ncores = kwargs['ncores'] psf_file = data.PSF_REF_FILE p = multiprocessing.Pool(processes=ncores) logger.info('Starting Csi analysis...') in_label = data.IN_LABEL out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) csi_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \ %(out_label, binning_label)), 'w') psf_ref = get_psf_ref(psf_file) #psf_ref.plot(show=False) #plt.xscale('log') #plt.yscale('log') #plt.show() for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) cont_ang = np.radians(psf_ref(_emean[i])) csi_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, _emean[i])) flux_map_name = in_label + '_flux_%i-%i.fits' % (emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name)) flux_map = udgrade_as_psf(flux_map, cont_ang) R = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name)) R = udgrade_as_psf(R, cont_ang) fsky = 1.-(len(np.where(flux_map == hp.UNSEEN)[0])/\ float(len(flux_map))) logger.info('fsky = %f' % fsky) npix = len(flux_map) nside = hp.npix2nside(npix) _unmask = np.where(flux_map != hp.UNSEEN)[0] npix_unmask = len(_unmask) Imean = _f[i] dI = flux_map - Imean dR = R - Imean R = permute_unmasked_pix(R) dR = permute_unmasked_pix(dR) th_bins = data.TH_BINNING theta = [] for thmin, thmax in zip(th_bins[:-1], th_bins[1:]): th_mean = np.sqrt(thmin * thmax) theta.append(th_mean) theta = np.array(theta) logger.info('Computing Csi...') diri = hp.pixelfunc.pix2ang(nside, _unmask) veci = hp.rotator.dir2vec(diri) xyz = np.array([(veci[0][i], veci[1][i], veci[2][i]) for i in range(0, len(veci[0]))]) args = zip(_unmask, xyz, [dI] * npix_unmask, [dR] * npix_unmask, [nside] * npix_unmask) #args = zip(_unmask, xyz, [flux_map]*npix_unmask, [R]*npix_unmask, # [nside]*npix_unmask) a = np.array(p.map(csi_compute, args)) SUMij_list = a[:, 0] SUMf_list = a[:, 1] SUMR_list = a[:, 2] SUMij_th = [] SUMf_th = [] SUMR_th = [] for i, s in enumerate(SUMij_list[0]): SUMij_th.append(np.sum(SUMij_list[:, i])) SUMf_th.append(np.sum(SUMf_list[:, i])) SUMR_th.append(np.sum(SUMR_list[:, i])) csi = (np.array(SUMij_th)) / np.array(SUMf_th) #-Imean**2 r = (np.array(SUMR_th)) / np.array(SUMf_th) #-Imean**2 csi_txt.write('THETA\t%s\n'%str(list(theta)).replace('[',''). \ replace(']','').replace(', ', ' ')) csi_txt.write('CSI\t%s\n'%str(list(csi)).replace('[',''). \ replace(']','').replace(', ', ' ')) csi_txt.write('R\t%s\n'%str(list(r)).replace('[',''). \ replace(']','').replace(', ', ' ')) csi_txt.close() p.close() p.join() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \ %(out_label, binning_label))))
FLUX_LABELS = ['UCV (t56) w/o Foreground sub', 'UCV (t56) w/ Foreground sub'] #OUT_LABEL = 'Flux_t56_srcmask2_Rm-Rp' #OUT_LABEL = 'Flux_types_srcmask2' #OUT_LABEL = 'Flux_t56_srcmask2-1p5-weighted' #OUT_LABEL = 'Flux_t56_maskweight_north-south' #OUT_LABEL = 'Flux_t56_maskweight_east-west' OUT_LABEL = 'Flux_t56_maskweight' plt.figure(figsize=(10, 7), dpi=80) from GRATools.utils.gDrawRef import ref_igrb_band from GRATools.utils.gDrawRef import ref_igrb_noFGsub leg, lab = ref_igrb_band() igrb, lab_igrb = ref_igrb_noFGsub() flux = [] for f in FLUX_FILES: from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, fsky = get_cl_param(f) spec = plt.errorbar(_emean, _f*_emean, fmt='o', markersize=3, \ elinewidth=1, xerr=(_emax-_emin)/2, yerr=_ferr*_emean) label = os.path.basename(f).replace('_parameters.txt', '') flux.append(spec) plt.xscale("log") plt.yscale("log") plt.xlabel('Energy [MeV]') plt.ylabel('E$^{2}$ $\cdot$ Flux [MeV cm$^{-2}$ s$^{-1}$ sr$^{-1}$]') plt.title(' Energy Spectrum') plt.legend([igrb, leg] + flux, [lab_igrb, lab] + FLUX_LABELS, loc=3) #overlay_tag() save_current_figure(OUT_LABEL + '_ESpec.png')
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt' % out_wb_label) if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) _l = np.arange(0, 1000) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: from GRATools.utils.gWindowFunc import get_wbeam wb = get_wbeam(out_wb_txt) save_current_figure('Wbeam_%s.png' % out_wb_label, clear=True) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL mask_label = data.MASK_LABEL in_label = in_label + '_' + mask_label out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_polspicecls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) mask_f = mask_file if type(mask_file) == list: mask_f = mask_file[i] gamma = data.WEIGHT_SPEC_INDEX Im = (1 / (1 - gamma)) * (emax**(1 - gamma) - emin**(1 - gamma)) / (emax - emin) eweightedmean = np.power(1 / Im, 1 / gamma) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, eweightedmean)) l_max = 1000 _l = np.arange(l_max) wb_en = wb.hslice(eweightedmean)(_l) flux_map_name = in_label + '_flux_%i-%i.fits' % (emin, emax) flux_map_f = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name) flux_map = hp.read_map(flux_map_f) fsky = _fsky[i] if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f' % fsky, min=1e-7, max=1e-4, norm='log') plt.show() logger.info('fsky = ' % fsky) nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] out_name = '%s_%i-%i' % (out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder, '%s_cl.txt' % out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder, '%s_clraw.txt' % out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder, '%s_cov.fits' % out_name) if key == 'mapfile': pol_dict[key] = flux_map_f if key == 'maskfile': pol_dict[key] = mask_f config_file_name = 'pol_%s' % (out_name) _l, _cl, _cl_err = pol_cl_calculation(pol_dict, config_file_name) logger.info('cn poisson = %e' % _cn[i]) cn = _cn[i] wl = wb_en * wpix _cl = (_cl[:l_max] - cn) / (wl**2) _cl_err = _cl_err[:l_max] / (wl**2) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) #_cl_err = np.sqrt(2./((2*_l+1)*fsky))*(_cl+(cn/wl**2)) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info( 'Created %s' % (os.path.join(GRATOOLS_OUT, '%s_%s_polspicecls.txt' % (out_label, binning_label))))
#os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t56_srcmask1p5_13bins_parameters.txt'), #os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t56_maskweighted_13bins_parameters.txt'), #os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t0_13bins_parameters.txt'), #os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t56_Rm_13bins_parameters.txt'), #os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t56_Rp_13bins_parameters.txt') os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t4_srcmask2_13bins_parameters.txt'), os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t32_srcmask2_13bins_parameters.txt'), os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t1_srcmask2_13bins_parameters.txt'), os.path.join(GRATOOLS_OUT, 'Allyrs_UCV_t2_srcmask2_13bins_parameters.txt') ] FLUX_LABELS = ['UCV (t56) PSF0', 'UCV (t56) PSF3', 'UCV (t56) FRONT', 'UCV (t56) BACK'] #OUT_LABEL = 'Flux_evtypes' plt.figure(figsize=(10, 7), dpi=80) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f_ref, _ferr_ref, _cn_ref, fsky_ref = get_cl_param(FLUX_REF) plt.plot((_emin[0], _emax[-1]), (0, 0), '--', color='gray') plt.plot((100, 1000000), (0.1, 0.1), '-', color='silver', linewidth=1.0) plt.plot((100, 1000000), (-0.1, -0.1), '-', color='silver', linewidth=1.0) flux, flux_label = [], [] plt.title('Flux Residuals - Ref.: %s'%FLUX_REF_LABEL) for f in FLUX_FILES: _emin, _emax, _emean, _f, _ferr, _cn, fsky = get_cl_param(f) #print _ferr _res = (_f_ref - _f)/_f_ref _res_err = np.sqrt(((_f/_f_ref**2)*_ferr_ref)**2+(_ferr/_f_ref)**2) spec = plt.errorbar(_emean, _res, fmt='o', markersize=3, \ elinewidth=1, xerr=(_emax-_emin)/2, yerr=_res_err) flux.append(spec) plt.xscale("log") #plt.yscale("log")
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) lmax = data.LMAX _l = np.arange(0, lmax) gamma = data.WEIGHT_SPEC_INDEX out_label = data.OUT_LABEL logger.info('Calculating energy spectrum...') from GRATools.utils.gWindowFunc import get_powerlaw_spline energy_spec = get_powerlaw_spline(gamma) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label) in_label = data.IN_LABEL if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) #_l = np.arange(0, 1500) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: pass logger.info('Starting Cl analysis...') mask_label = data.MASK_LABEL in_label = in_label + '_' + mask_label binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_polspicecls.txt' \ %(out_label, binning_label)), 'w') monopoles, monopoles_err = [], [] dipol_vectors = [] from GRATools.utils.gWindowFunc import get_integral_wbeam for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) mask_f = mask_file if type(mask_file) == list: mask_f = mask_file[i] #mask = hp.read_map(mask_f) wb_en = get_integral_wbeam(out_wb_txt, energy_spec, emin, emax) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) l_max= lmax _l = np.arange(l_max) wb_en = wb_en(_l) # monopole and dipole cleaning from MASKED map, bad value = UNSEEN flux_map_name = in_label+'_fluxmasked_%i-%i.fits'%(emin, emax) flux_map_f = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name) flux_map_f_mdclean, mono, dipo = remove_monopole_dipole(flux_map_f) monopoles.append(mono) mono_err = np.sqrt(2/_fsky[i])*(mono+_cn[i]) monopoles_err.append(mono_err) dipol_vectors.append(dipo) logger.info('Monopole_term = %e +- %e'%(mono, mono_err)) logger.info('Dipole vector = %s' %(str(dipo))) # Up to l=n cleaning from MASKED map (correction for fsky discarded) if kwargs['multiclean'] is not None: flux_map_f_multiclean = remove_multipole(flux_map_f_mdclean, lmax=kwargs['multiclean']) flux_map = hp.read_map(flux_map_f_multiclean) else: flux_map = hp.read_map(flux_map_f_mdclean) fsky = _fsky[i] cn = _cn[i] if kwargs['show'] == True: hp.mollview(flux_map, title='f$_{sky}$ = %.3f'%fsky) plt.show() logger.info('fsky = '%fsky) nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] out_name = '%s_%i-%i' %(out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') logger.info('cn poisson = %e'%cn) wl = wb_en*wpix if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder,'%s_cl.txt'%out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder,'%s_clraw.txt'%out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder,'%s_cov.fits'%out_name) if key == 'mapfile': pol_dict[key] = flux_map_f_mdclean if key == 'maskfile': pol_dict[key] = mask_f config_file_name = 'pol_%s'%(out_name) if os.path.exists(os.path.join(out_folder,'%s_cl.txt'%out_name)) and \ os.path.exists(os.path.join(out_folder,'%s_cov.fits'%out_name)): logger.info('ATT: Retriving power spectrum...') _l, _cl, _clerr = pol_cl_parse(os.path.join(out_folder, '%s_cl.txt'%out_name), os.path.join(out_folder, '%s_cov.fits'%out_name), raw_corr=(cn, wl), rebin=True) logger.info('... and covariance matrix.') _cov = pol_cov_parse(os.path.join(out_folder, '%s_cov.fits'%out_name), wl_array=wl, rebin=True, show=True) else: _l, _cl, _clerr, _cov = pol_cl_calculation(pol_dict, config_file_name, raw_corr=(cn, wl), rebin=True,show=True) cl_txt.write('multipole\t%s\n'%str(list(_l)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_clerr)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Monopoles\t%s\n\n'%str(monopoles).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Monopoles_ERR\t%s\n\n'%str(monopoles_err).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT,'%s_%s_polspicecls.txt' %(out_label, binning_label))))