def compute_fg(self, dust_model, presets_dust=None, NSIDE_PATCH=4): #print("\nYou're computing thermal dust with model {}\n".format(dust_model)) dustmaps = np.zeros((self.nfreqs, 3, self.npix)) if dust_model == 'd0': sync_model = 's0' else: sync_model = 's1' settings = [dust_model, sync_model] self.preset_fg(dustmodel=dust_model, dict_dust=presets_dust) sky = pysm3.Sky(nside=self.nside, preset_strings=settings) if dust_model == 'd0' or dust_model == 'd1' or dust_model == 'd2' or dust_model == 'd3' or dust_model == 'd6': sky.components[0].mbb_temperature = 20 * sky.components[ 0].mbb_temperature.unit # fix temp at 20 K across the sky #print('Downgrade pixelization of spectral indices at nside = {}'.format(NSIDE_PATCH)) if dust_model != 'd0': #print('Downgrade pixelization of spectral indices at nside = {}'.format(NSIDE_PATCH)) for spectral_param in [ sky.components[0].mbb_index, sky.components[1].pl_index ]: spectral_param[:] = hp.ud_grade( hp.ud_grade(spectral_param.value, NSIDE_PATCH), self.nside) * spectral_param.unit for j in range(self.nfreqs): dustmaps[j] = np.array( sky.get_emission(self.nus[j] * u.GHz) * utils.bandpass_unit_conversion(self.nus[j] * u.GHz, None, u.uK_CMB)) return dustmaps
def get_nhits(nside_out=64): """ Generates an Nhits map in Galactic coordinates. Parameters ---------- nside_out : int Output resolution. Returns ------- ndarray Hits map. """ try: # Read in a hits map that has already been calculated # in galactic coordinates mp_G = hp.read_map(_SO_HIT_MAP, verbose=False) except FileNotFoundError: # if reading in galactic map fails, read in # celestial coordinates map and rotate it. mp_C = hp.read_map(_SO_HIT_MAP, verbose=False) nside_l = hp.get_nside(mp_C) nside_h = 512 ipixG = np.arange(hp.nside2npix(nside_h)) thG, phiG = hp.pix2ang(nside_h, ipixG) r = hp.Rotator(coord=['G', 'C']) thC, phiC = r(thG, phiG) ipixC = hp.ang2pix(nside_l, thC, phiC) mp_G = hp.ud_grade(mp_C[ipixC], nside_out=nside_l) return hp.ud_grade(mp_G, nside_out=nside_out)
def _my_ud_grade(map_in, nside_out, **kwargs): # As healpy.ud_grade, but it accepts map_in of nside = 0 and nside_out = 0, # which in this module means a single float or lenght-1 array if nside_out == 0: try: # Both input and output have nside = 0 return np.array([float(map_in)]) except TypeError: # This is really clunky... # 1) Downgrade to nside 1 # 2) put the 12 values in the pixels of a nside 4 map that belong to # the same nside 1 pixels # 3) Downgrade to nside 1 # 4) pick the value of the pixel in which the 12 values were placed map_in = hp.ud_grade(map_in, 1, **kwargs) out = np.full(hp.nside2npix(4), hp.UNSEEN) ids = hp.ud_grade(np.arange(12), 4, **kwargs) out[np.where(ids == 0)[0][:12]] = map_in kwargs['pess'] = False res = hp.ud_grade(out, 1, **kwargs) return res[:1] try: return hp.ud_grade(np.full(12, map_in.item()), nside_out, **kwargs) except ValueError: return hp.ud_grade(map_in, nside_out, **kwargs)
def loadmaps(fn,Nside=512,downgrade=False): """Load healpix maps of filenames and return as three Nmap x Npix numpy arrays, T, Q and U""" if downgrade: Nside = downgrade else: Nside = 512 nmap=len(fn) npix=hp.nside2npix(Nside) T=np.zeros((nmap,npix)) Q=np.zeros((nmap,npix)) U=np.zeros((nmap,npix)) for k,val in enumerate(fn): hmap=hp.read_map(val,field=(0,1,2)) if downgrade: T[k] = hp.ud_grade(hmap[0], Nside) Q[k] = hp.ud_grade(hmap[1], Nside) U[k] = hp.ud_grade(hmap[2], Nside) else: T[k]=hmap[0] Q[k]=hmap[1] U[k]=hmap[2] return T,Q,U
def get_mask_gal(percentage_keep=40, nside_out=512, coordinates='eq', quick=True): import pyfits from astropy.coordinates import FK5 from astropy import units as u savename = datadir+'mask_GAL0%i_%i_'%(percentage_keep,nside_out) savename += coordinates+'_.pkl' if quick: return pickle.load(open(savename,'r')) pp = pyfits.open(datadir+'HFI_Mask_GalPlane_2048_R1.10.fits') mask_gal = pp[1].data['GAL0%i'%percentage_keep] mask_gal = hp.reorder(mask_gal, out='RING', inp='NESTED') if coordinates=='gal': mask_out = hp.ud_grade(mask_gal, nside_out) if coordinates=='eq': nside_up = nside_out*2 mask_gal = hp.ud_grade(mask_gal, nside_up) # Find the indices in an up-sampled *galactic* map that belong to these # *equatorial* coordinates. theta, phi = hp.pix2ang(nside_up, np.arange(hp.nside2npix(nside_up))) ra = phi dec = np.pi/2.-theta coord = FK5(ra=ra, dec=dec, unit=(u.rad, u.rad)) l_gal = coord.galactic.l.rad b_gal = coord.galactic.b.rad phi = l_gal theta = np.pi/2.-b_gal ind_up = hp.ang2pix(nside_up, theta, phi) mask_up_eq = mask_gal[ind_up] mask_out = hp.ud_grade(mask_up_eq, nside_out) pickle.dump(mask_out, open(savename,'w')) return mask_out
def load_one_mask_forallfreq(self, nside_out=None): def _multi(a, b): return a * b def read_single(mask_path, mask_filename): return hp.read_map('{mask_path}{mask_filename}'.format( mask_path=mask_path, mask_filename=mask_filename), dtype=np.bool) if nside_out == None: nside_out = self.csu.nside_out maskset = self.csu.cf['pa']['mskset'] if maskset == None: tmask = np.ones(shape=hp.nside2npix(nside_out[1])) pmask = np.ones(shape=hp.nside2npix(nside_out[1])) else: mch = self.csu.mch pmask_path = self.csu.cf[mch][maskset]['pmask']["ap"] pmask_filename = self.csu.cf[mch][maskset]['pmask']['filename'] print('loading mask {}'.format(pmask_filename)) pmasks = [read_single(pmask_path, a) for a in pmask_filename] pmask = functools.reduce(_multi, [a for a in pmasks]) tmask_path = self.csu.cf[mch][maskset]['tmask']["ap"] tmask_filename = self.csu.cf[mch][maskset]['tmask']['filename'] tmask = read_single(tmask_path, tmask_filename) if nside_out is not None: tmask = hp.ud_grade(tmask, nside_out=nside_out[1]) pmask = hp.ud_grade(pmask, nside_out=nside_out[1]) tdict = {FREQ: tmask for FREQ in self.csu.PLANCKMAPFREQ_f} pdict = {FREQ: pmask for FREQ in self.csu.PLANCKMAPFREQ_f} return tdict, pdict, pdict
def __init__( self, threshold, sig, freq, nside, LMAX, CMB_file_name='/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits' ): self.threshold = threshold self.sig = sig self.freq = freq #check that nside value is valid if not np.all(hp.isnsideok(nside)): raise ValueError( "%s is not a valid nside parameter (must be a power of 2, less than 2**30)" % str(nside)) self.nside = nside self.LMAX = LMAX self.CMB_file_name = CMB_file_name #can read and downgrade cmb in same step #use nside instead of 512. change both cmb and gsm to nside self.CMB_map = hp.ud_grade(hp.read_map(CMB_file_name), nside) #power spectrum of downgraded CMB map self.CMB_PS = hp.anafast(self.CMB_map, lmax=LMAX) self.ell = np.arange(len(self.CMB_PS)) self.signal = np.sqrt((np.sum( (self.ell * (self.ell + 1) * self.CMB_PS)**2)) / (len(self.CMB_PS))) self.GSM_map = hp.ud_grade(gsm.generate(self.freq), nside) self.GSM_PS = hp.anafast(self.GSM_map, lmax=LMAX)
def get_haslam(nside_out=512, coordinates='eq', quick=True): savename = datadir+'haslam_'+coordinates+'_%i.pkl'%nside_out if quick: return pickle.load(open(savename,'r')) radio = hp.read_map(datadir+'lambda_haslam408_dsds.fits') if coordinates=='gal': radio_out = hp.ud_grade(radio, nside_out) if coordinates=='eq': from astropy.coordinates import FK5 from astropy import units as u # Up-sample and convert from galactic to equatorial. nside_up = nside_out*2 radio_up_gal = hp.ud_grade(radio, nside_up) # Find the indices in an up-sampled *galactic* map that belong to these # *equatorial* coordinates. theta, phi = hp.pix2ang(nside_up, np.arange(hp.nside2npix(nside_up))) ra = phi dec = np.pi/2.-theta coord = FK5(ra=ra, dec=dec, unit=(u.rad, u.rad)) l_gal = coord.galactic.l.rad b_gal = coord.galactic.b.rad phi = l_gal theta = np.pi/2.-b_gal ind_up = hp.ang2pix(nside_up, theta, phi) radio_up_eq = radio_up_gal[ind_up] radio_out = hp.ud_grade(radio_up_eq, nside_out) mask_out = np.ones_like(radio_out) pickle.dump((radio_out, mask_out), open(savename,'w')) return radio_out, mask_out
def load_Planck_mask(skycoverage=70, nside=2048, local=False): """ Load one of the Planck-provided masks """ if local: maskroot = "/Users/susanclark/Dropbox/Planck/" else: maskroot = "/data/seclark/BetterForegrounds/data/masks/" maskhdu = fits.open(maskroot + "HFI_Mask_GalPlane-apo0_2048_R2.00.fits") maskstr = "GAL{0:0=3d}".format(skycoverage) masknest = maskhdu[1].data[maskstr] maskring = hp.pixelfunc.reorder(masknest, n2r=True) if nside != 2048: # going straight from 2048 to 64 breaks things, so: if nside <= 64: maskring_lr = hp.ud_grade(maskring, 512) maskring = hp.ud_grade(maskring_lr, nside) print("successfully downgraded to nside {}".format(nside)) else: maskring = hp.ud_grade(maskring, nside) print("successfully downgraded to nside {}".format(nside)) return maskring
def broadcast_dust_polarization(self): root = self.id_dust_pol self.dust_pol_fwhm = self.comm.bcast(self.dust_pol_fwhm, root=root) self.dust_pol_nside = self.comm.bcast(self.dust_pol_nside, root=root) self.dust_pol_nu_ref = self.comm.bcast(self.dust_pol_nu_ref, root=root) self.dust_temp_pol = self.comm.bcast(self.dust_temp_pol, root=root) my_pix = self.get_my_pix(self.dust_pol_nside) # amplitude self.dust_Ad_Q = self.cache.put( 'dust_Ad_Q', self.comm.bcast(self.dust_Ad_Q, root=root)[my_pix]) self.dust_Ad_U = self.cache.put( 'dust_Ad_U', self.comm.bcast(self.dust_Ad_U, root=root)[my_pix]) # temperature if self.dust_temp_pol is None: # Old dust inputs self.dust_temp_pol = hp.ud_grade(self.dust_temp, self.dust_pol_nside) self.dust_temp_pol = self.cache.put('dust_temp_pol', self.dust_temp_pol[my_pix]) # beta self.dust_beta_pol = self.comm.bcast(self.dust_beta_pol, root=root) if self.dust_beta_pol is None: # Old dust inputs self.dust_beta_pol = hp.ud_grade(self.dust_beta, self.dust_pol_nside) self.dust_beta_pol = self.cache.put('dust_beta_pol', self.dust_beta_pol[my_pix]) # Only now, can we extract the local portion of dust # temperature and beta. my_pix = self.get_my_pix(self.dust_nside) self.dust_temp = self.cache.put('dust_temp', self.dust_temp[my_pix]) self.dust_beta = self.cache.put('dust_beta', self.dust_beta[my_pix]) return
def read_and_diff_files_fast(f1,f2,nside=256,tmask=None,return_map=False): #assume tmask input is already degraded mm1=hp.read_map(f1,[0,1,2],verbose=False) mm2=hp.read_map(f2,[0,1,2],verbose=False) mmm1=[] mmm2=[] for m1,m2 in zip(mm1,mm2): m1=hp.ud_grade(hp.ma(m1),nside_out=nside) m2=hp.ud_grade(hp.ma(m2),nside_out=nside) tmask=m1.mask | m2.mask | tmask mmm1.append(m1) mmm2.append(m2) diff=[] for m1,m2 in zip(mmm1,mmm2): d=m1-m2 d.mask=tmask diff.append(d) skyfrac=1-float(tmask.sum())/len(tmask) cldata=hp.anafast(diff) cldata_out=[] for cl in cldata: cldata_out.append(cl/skyfrac) if return_map is False: return cldata_out if return_map is True: return cldata_out,diff
def simulate_polarized_emission(self, I_map): """ Add polarized emission by means of: * an overall constant polarization fraction, * a depolarization map to mimick the line of sigth depolarization effect at low Galactic latitudes * a polarization angle map coming from a dust template (we exploit the observed correlation between polarized dust and molecular emission in star forming regions). """ polangle = self.read_map( self.remote_data.get("co/psimap_dust90_{}.fits".format(self.template_nside)) ).value depolmap = self.read_map( self.remote_data.get("co/gmap_dust90_{}.fits".format(self.template_nside)) ).value if hp.get_nside(depolmap) != self.target_nside: polangle = hp.ud_grade(map_in=polangle, nside_out=self.target_nside) depolmap = hp.ud_grade(map_in=depolmap, nside_out=self.target_nside) cospolangle = np.cos(2.0 * polangle) sinpolangle = np.sin(2.0 * polangle) P_map = self.polarization_fraction * depolmap * I_map Q_map = P_map * cospolangle U_map = P_map * sinpolangle return Q_map, U_map
def surv_sum_diff(surveydict,ss=1,nside=128,freq=70,mask_ps=True,fwhm=10.0): """ function to genreate smoothed difference between chosen survey and sum of the other 5 surveys from andreas interactive destriper/binner. uses common masks smooth to 10 degrees fwhm is in degrees, if zero or negative don't smooth at all, default 10 degrees """ sumlist=surveydict.keys() sumlist.remove(ss) nsum=len(sumlist) m=hp.ma(np.array(surveydict[sumlist[0]]))/nsum totalmask=m.mask if mask_ps==True: psmask = np.logical_not(np.floor(hp.ud_grade(hp.read_map(glob('/project/projectdirs/planck/data/mission/DPC_maps/dx8/lfi/DX8_MASKs/' + 'mask_ps_%dGHz_*.fits' % freq)[0]), nside,order_out='NEST'))) totalmask=m.mask | psmask for ss in sumlist[1:]: m1=hp.ma(np.array(surveydict[ss])) m=m+m1/nsum totalmask=m1.mask | totalmask m.mask=totalmask m.mask |= np.isnan(m) m1=hp.ma(np.array(surveydict[ss])) totalmask=totalmask | m1.mask d=(m1-m)/2. d=hp.ud_grade(d,nside,order_in='NEST',order_out='RING') if fwhm>0: dsm=hp.ma(hp.smoothing(d.filled(),fwhm*np.pi/180.)) dsm.mask=m.mask if fwhm<=0: dsm=d hp.mollview(dsm,min=-1e-5,max=1e-5,title='SS'+np.str(ss)+ ' - sum of others') return dsm
def slowRotatorGtoC(hpMap, nside, verbose=True): if verbose: print("Upgrading map...") nsideUp = nside * 2 hpUp = hp.ud_grade(hpMap, nsideUp) array_1d_double = npct.ndpointer(dtype=np.double, ndim=1, flags='C_CONTIGUOUS') libcd = npct.load_library( "/astro/u/msyriac/repos/orphics/orphics/tools/deg2hp.so", ".") libcd.RotateMapGtoC.restype = None libcd.RotateMapGtoC.argtypes = [ array_1d_double, array_1d_double, ctypes.c_long ] retMap = hpUp.copy() * 0. if verbose: print("Rotating ...") ret = libcd.RotateMapGtoC(hpUp, retMap, nsideUp) if verbose: print("Downgrading map...") return hp.ud_grade(retMap, nside)
def extend_indices(indices, nside_in, nside_out, nest=True): """ Minimally extends a set of indices such that it can be reduced to nside_out in a healpy fashion, always four pixels reduce naturally to a higher order pixel. Note that this function supports the ring ordering, however, since almost no other function does so, nest ordering is strongly recommended. :param indices: 1d array of integer pixel ids :param nside_in: nside of the input :param nside_out: nside of the output :param nest: indices are ordered in the "NEST" ordering scheme :return: returns a set of indices in the same ordering as the input. """ # figire out the ordering if nest: ordering = "NEST" else: ordering = "RING" # get the map to reduce m_in = np.zeros(hp.nside2npix(nside_in)) m_in[indices] = 1.0 # reduce m_in = hp.ud_grade(map_in=m_in, nside_out=nside_out, order_in=ordering, order_out=ordering) # expand m_in = hp.ud_grade(map_in=m_in, nside_out=nside_in, order_in=ordering, order_out=ordering) # get the new indices return np.arange(hp.nside2npix(nside_in))[m_in > 1e-12]
def udgrade_maps(fground_maps, noise, new_nside, nf_recon, nreals, return_with_noise = False): """ Upgrade or Degrade foreground maps. It returns foreground maps UD-graded, std (useless) and noise maps UD-graded for each noise realization """ npix_ud = 12 * new_nside **2 fgr_map_ud = np.zeros((len(fground_maps), nf_recon, npix_ud, 3)) if return_with_noise: noise_ud_i = np.zeros((len(fground_maps), nreals, nf_recon, npix_ud, 3)) maps_ud_i = np.zeros_like(noise_ud_i) for bandreg in range(len(fground_maps)): for irec in range(nf_recon): fgr_map_ud[bandreg, irec] = hp.ud_grade(fground_maps[bandreg, irec].T, new_nside).T if return_with_noise: for ireal in range(nreals): noise_ud_i[bandreg, ireal, irec] = hp.ud_grade(noise[bandreg, ireal, irec].T, new_nside).T maps_ud_i[bandreg, ireal, ...] = noise_ud_i[bandreg, ireal, ...] + fgr_map_ud[bandreg] # if return_with_noise: maps_ud, std_ud = np.mean(maps_ud_i, axis = 1), np.std(maps_ud_i, axis = 1) return maps_ud, std_ud, fgr_map_ud, noise_ud_i else: return fgr_map_ud
def des_main(): T = Timer() sigma_rad = np.radians(SMOOTHING / 60) E = fitsio.read(DES_FILE)['kE'] E = hp.smoothing(E, sigma=sigma_rad) E = hp.ud_grade(E, nside_out=NSIDE) T.mark("prepared_des_map") mask = fitsio.read(DES_MASK)['mask'] mask = hp.ud_grade(mask, nside_out=NSIDE) mask_fraction = mask.sum() / mask.size T.mark("prepared_des_mask") E *= mask nfull = TOTAL_POINTS_FULL_SKY nmasked = int(nfull * mask_fraction) sample_info = prepare_sampling(NSIDE) T.mark("prepared_sampling_info") des_points = simulate_points(E, nfull, sample_info, scaling=DES_SCALING) T.mark("simulated_des_points") save_results('des', des_points, full=False, noisy=True)
def sim(): #####Add PS models ##NDI f_NDI.init_ps_king(hp.ud_grade(list_nfw_NDI[1],nside,power=-2),theta_ps_disk_NDI,f_NDI.fcore_arr,f_NDI.score_arr,f_NDI.gcore_arr,f_NDI.stail_arr,f_NDI.gtail_arr,f_NDI.SpE_arr,n_ps = n_ps_max,smin=0.5,nside_detail_factor=4) f_NDI.init_ps_king(hp.ud_grade(list_disk_NDI[1],nside,power=-2),theta_ps_disk_NDI,f_NDI.fcore_arr,f_NDI.score_arr,f_NDI.gcore_arr,f_NDI.stail_arr,f_NDI.gtail_arr,f_NDI.SpE_arr,n_ps = n_ps_max,smin=0.5,nside_detail_factor=4) #f_NDI.init_ps_king(hp.ud_grade(list_iso_NDI[1],nside,power=-2),theta_ps_iso_NDI,f_NDI.fcore_arr,f_NDI.score_arr,f_NDI.gcore_arr,f_NDI.stail_arr,f_NDI.gtail_arr,f_NDI.SpE_arr,n_ps = n_ps_max,smin=0.5,nside_detail_factor=4) ##DI f_DI.init_ps_king(hp.ud_grade(list_disk_DI[1],nside,power=-2),theta_ps_disk_DI,f_NDI.fcore_arr,f_NDI.score_arr,f_NDI.gcore_arr,f_NDI.stail_arr,f_NDI.gtail_arr,f_NDI.SpE_arr,n_ps = n_ps_max,smin=0.5,nside_detail_factor=4) #f_DI.init_ps_king(hp.ud_grade(list_iso_DI[1],nside,power=-2),theta_ps_iso_DI,f_NDI.fcore_arr,f_NDI.score_arr,f_NDI.gcore_arr,f_NDI.stail_arr,f_NDI.gtail_arr,f_NDI.SpE_arr,n_ps = n_ps_max,smin=0.5,nside_detail_factor=4) #NB: Isotropic commented out because it only contributed a single PS and that causes sim code to crash ##################### ######Make Fake Data f_P.make_fake_data() f_NDI.make_fake_data() f_DI.make_fake_data() ##################### #######Save Fake Data # if not os.path.exists(work_dir+ 'data/fake_data'): # os.mkdir(work_dir+ 'data/fake_data') f_P.save_fake_data(P_plots_dir + 'FD'+fake_tag+'.txt.gz') f_NDI.save_fake_data(NDI_plots_dir + 'FD'+fake_tag+'.txt.gz') f_DI.save_fake_data(DI_plots_dir + 'FD'+fake_tag+'.txt.gz') f_P.save_fake_data_key(P_plots_dir + 'FD_key'+fake_tag+'.txt.gz') f_NDI.save_fake_data_key(NDI_plots_dir + 'FD_key'+fake_tag+'.txt.gz') f_DI.save_fake_data_key(DI_plots_dir + 'FD_key'+fake_tag+'.txt.gz')
def degrade(self, nside): if nside == self.nside: return self else: marge_maps_t = [] n_marge_maps_t = len( self.templates_t) - (self.marge_monopole + self.marge_dipole) if (n_marge_maps_t > 0): marge_maps_t = [ hp.ud_grade(ti.map, nside) for ti in self.templates_t[0:n_marge_maps_t] ] marge_maps_p = [] n_marge_maps_p = len(self.templates_p) if (n_marge_maps_p > 0): marge_maps_p = [ hp.ud_grade(ti.map, nside) for ti in self.templates_p[0:n_marge_maps_p] ] return alm_filter_ninv( [hp.ud_grade(n, nside, power=-2) for n in self.n_inv], self.b_transf, self.marge_monopole, self.marge_dipole, marge_maps_t, marge_maps_p)
def __init__(self, temps, norms, S_arys=None, dNdS_arys=None, ps_temps=None, psf_r=None, nside=128): self.temps = temps self.norms = norms self.nside = nside self.S_arys = S_arys self.dNdS_arys = dNdS_arys self.ps_temps = ps_temps self.psf_r = psf_r # ud_grade templates for i_temp in range(len(self.temps)): self.temps[i_temp] = hp.ud_grade(self.temps[i_temp], nside_out=self.nside, power=-2) for i_temp in range(len(self.ps_temps)): self.ps_temps[i_temp] = hp.ud_grade(self.ps_temps[i_temp], nside_out=self.nside, power=-2)
def __init__(self, map1, map2=None, nside=2048, lmax=2048, nside_jk=4): ''' Parameters: ----------- map1: REAL SPACE Healpix map. map2: REAL SPACE Healpix map, or None. - Compute cross correlation if given, otherwise just uses the first map. nside: nside of real space maps. Default 2048. lmax: lmax of output Cls and alms used for computation. Default 2048 nside_jk: nside of jackknife portion map, whose pixels will be used for masking. - If 4, compute 12*4*4 = 192 Jackknifes. - If 2, compute 12*2*2 = 48 Jackknifes. - Default is 4. ''' self.nside = nside self.lmax = lmax self.njk = hp.nside2npix( nside_jk) # Number of jacknifes = 12 * jacknife_nside^2 # The larger this nside, the more jackknifes to compute. self.fsky = 1. - 1 / self.njk # Area normalization based on jackknife. ### Cross Correlation maps. self.map1 = hp.ud_grade(map1, nside_out=self.nside) self.map2 = (hp.ud_grade(map2, nside_out=self.nside) if map2 is not None else None) self.result = np.arange(0, lmax + 1, 1)
def smooth_pol(self, fwhm_in, nside_in, maps_in): if fwhm_in > .9 * self.fwhm and nside_in == self.nside: return maps_in if fwhm_in > .9 * self.fwhm and self.nside < nside_in: # Simple ud_grade if self.global_rank == 0 and self.verbose: print('Downgrading Nside {} -> {}' ''.format(nside_in, self.nside), flush=True) maps_out = [] npix_in = hp.nside2npix(nside_in) my_pix_in = self.get_my_pix(nside_in) for (qmap, umap) in maps_in: my_mapout = np.zeros(npix_in, dtype=np.float) qmapout = np.zeros(npix_in, dtype=np.float) umapout = np.zeros(npix_in, dtype=np.float) my_mapout[my_pix_in] = qmap self.comm.Allreduce(my_mapout, qmapout) my_mapout[my_pix_in] = umap self.comm.Allreduce(my_mapout, umapout) del my_mapout maps_out.append((hp.ud_grade(qmapout, self.nside)[self.my_pix], hp.ud_grade(umapout, self.nside)[self.my_pix])) else: # Full smoothing lmax = self.optimal_lmax(fwhm_in, nside_in) total_beam = self.total_beam(fwhm_in, lmax, pol=True) if self.global_rank == 0 and self.verbose: print('Smoothing {} -> {}. lmax = {}. Nside {} -> {}' ''.format(fwhm_in, self.fwhm, lmax, nside_in, self.nside), flush=True) local_m = np.arange(self.rank, lmax + 1, self.ntask, dtype=np.int32) alminfo = packed_real_order(lmax, ms=local_m) grid_in = self.get_grid(nside_in) grid_out = self.get_grid(self.nside) maps_out = [] for (local_map_Q, local_map_U) in maps_in: map_P = np.ascontiguousarray(np.vstack( [local_map_Q, local_map_U]).reshape((1, 2, -1)), dtype=np.float64) alm_P = analysis(grid_in, alminfo, map_P, spin=2, comm=self.comm) if total_beam is not None: alminfo.almxfl(alm_P, total_beam) map_P = synthesis(grid_out, alminfo, alm_P, spin=2, comm=self.comm)[0] maps_out.append(map_P) return maps_out
def test_degrade_map_recarray(self): """ Test HealSparse.degrade functionality with recarray quantities """ random.seed(seed=12345) nside_coverage = 32 nside_map = 1024 nside_new = 256 dtype = [('col1', 'f8'), ('col2', 'f8'), ('col3', 'i4')] sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, dtype, primary='col1') pixel = np.arange(20000) values = np.zeros_like(pixel, dtype=dtype) values['col1'] = random.random(size=pixel.size) values['col2'] = random.random(size=pixel.size) values['col3'] = random.poisson(size=pixel.size, lam=2) sparse_map.update_values_pix(pixel, values) ra, dec = hpg.pixel_to_angle(nside_map, pixel) # Make the test values hpmap_col1 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN hpmap_col2 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN hpmap_col3 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN hpmap_col1[pixel] = values['col1'] hpmap_col2[pixel] = values['col2'] hpmap_col3[pixel] = values['col3'] # Degrade healpix maps hpmap_col1 = hp.ud_grade(hpmap_col1, nside_out=nside_new, order_in='NESTED', order_out='NESTED') hpmap_col2 = hp.ud_grade(hpmap_col2, nside_out=nside_new, order_in='NESTED', order_out='NESTED') hpmap_col3 = hp.ud_grade(hpmap_col3, nside_out=nside_new, order_in='NESTED', order_out='NESTED') ipnest_test = hpg.angle_to_pixel(nside_new, ra, dec) # Degrade the old map new_map = sparse_map.degrade(nside_out=nside_new) testing.assert_almost_equal(new_map.get_values_pos(ra, dec, lonlat=True)['col1'], hpmap_col1[ipnest_test]) testing.assert_almost_equal(new_map.get_values_pos(ra, dec, lonlat=True)['col2'], hpmap_col2[ipnest_test]) testing.assert_almost_equal(new_map.get_values_pos(ra, dec, lonlat=True)['col3'], hpmap_col3[ipnest_test]) # Test degrade-on-read self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestHealSparse-') fname = os.path.join(self.test_dir, 'test_recarray_degrade.hs') sparse_map.write(fname) new_map2 = healsparse.HealSparseMap.read(fname, degrade_nside=nside_new) testing.assert_almost_equal(new_map2.get_values_pos(ra, dec, lonlat=True)['col1'], hpmap_col1[ipnest_test]) testing.assert_almost_equal(new_map2.get_values_pos(ra, dec, lonlat=True)['col2'], hpmap_col2[ipnest_test]) testing.assert_almost_equal(new_map2.get_values_pos(ra, dec, lonlat=True)['col3'], hpmap_col3[ipnest_test])
def test_upgrade_map_recarray(self): """ Test upgrade functionality with a recarray. """ random.seed(seed=12345) nside_coverage = 32 nside_map = 256 nside_new = 1024 dtype = [('col1', 'f8'), ('col2', 'f8'), ('col3', 'i4')] sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, dtype, primary='col1') pixel = np.arange(20000) values = np.zeros_like(pixel, dtype=dtype) values['col1'] = random.random(size=pixel.size) values['col2'] = random.random(size=pixel.size) values['col3'] = random.poisson(size=pixel.size, lam=2) sparse_map.update_values_pix(pixel, values) ra, dec = hpg.pixel_to_angle(nside_map, pixel) # Make the test values hpmap_col1 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN hpmap_col2 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN hpmap_col3 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN hpmap_col1[pixel] = values['col1'] hpmap_col2[pixel] = values['col2'] hpmap_col3[pixel] = values['col3'] # Upgrade healpix maps hpmap_col1 = hp.ud_grade(hpmap_col1, nside_out=nside_new, order_in='NESTED', order_out='NESTED') hpmap_col2 = hp.ud_grade(hpmap_col2, nside_out=nside_new, order_in='NESTED', order_out='NESTED') hpmap_col3 = hp.ud_grade(hpmap_col3, nside_out=nside_new, order_in='NESTED', order_out='NESTED') ipnest_test = hpg.angle_to_pixel(nside_new, ra, dec) # Upgrade the old map new_map = sparse_map.upgrade(nside_out=nside_new) testing.assert_almost_equal( new_map.get_values_pos(ra, dec, lonlat=True)['col1'], hpmap_col1[ipnest_test]) testing.assert_almost_equal( new_map.get_values_pos(ra, dec, lonlat=True)['col2'], hpmap_col2[ipnest_test]) testing.assert_almost_equal( new_map.get_values_pos(ra, dec, lonlat=True)['col3'], hpmap_col3[ipnest_test])
def load_wmap_maps_QU(fnames, n2r=True, Nside=None): """ Load WMAP-format polarization maps from file (i.e. from LAMBDA). Returns an n x Npix array, where n = len(`fnames`) is the number of maps and Npix is the number of pixels in each map. All maps read in at one time must have the same length (Npix). If `fnames` is a single file and not a list, reads and returns the single specified map. If `n2r` is True, converts from NEST format (the WMAP standard) to RING format (the healpy standard). Scales the map to the specified `Nside`. If None, does not rescale the map. All input maps must have the same base Nside. """ colname = 'BESTFIT' try: Nbands = np.shape(fnames)[0] except IndexError: # Single-map case. Just read and return the map. with fits.open(fnames) as f: a = f[1].data[colname] base_Nside = f[1].header['NSIDE'] if (Nside is not None) and (Nside != base_Nside): a = hp.ud_grade(a, Nside, order_in='NESTED', order_out='NESTED') if n2r: a = hp.reorder(a, n2r=True) return a fname0 = fnames[0] # Initialize array and populate first band with fits.open(fname0) as f0: Npix = f0[1].header['NAXIS2'] base_Nside = f0[1].header['NSIDE'] a = np.empty([Nbands, Npix]) a[0] = f0[1].data[colname] for i, fname in enumerate(fnames[1:]): with fits.open(fname) as f: a[i + 1] = f[1].data[colname] if (Nside is not None) and (Nside != base_Nside): b = np.empty([a.shape[0], hp.nside2npix(Nside)]) for i in range(a.shape[0]): b[i] = hp.ud_grade(a[i], Nside, order_in='NESTED', order_out='NESTED') a = b if n2r: for i in range(a.shape[0]): a[i] = hp.reorder(a[i], n2r=True) return a
def load_template_from_file(self,filename,alttempdir='False'): #should already be an exposure-corrected counts map # By default load templates from template_dir: if alttempdir == 'False': fits_file_list = fits.open(self.template_dir + filename)[0].data return np.array([hp.ud_grade(fits_file,self.nside,power=-2) for fits_file in fits_file_list])[self.CTB_en_min:self.CTB_en_max] else: # This option is currently only setup for a single file fits_file = fits.open(alttempdir + filename)[0].data return np.array([hp.ud_grade(fits_file,self.nside,power=-2)])
def gentempmap(self): """Make template map""" print('preparing template map...') sys.stdout.flush() # First, template is input map at lower res self.hmaptemp = np.array(hp.ud_grade(self.hmap, self.tempNside)) ################### # T noise if self.Ttemptype == 'planck': fn = 'planckmaps/mc_noise/143/ffp8_noise_143_full_map_mc_{:05d}.fits'.format( self.rlz) hmapn = 1e6 * np.array(hp.read_map(fn, field=0)) hmapn = np.array(hp.ud_grade(hmapn, self.tempNside, pess=False)) hmapn[np.abs(hmapn) > 1e10] = 0 else: hmapn = 0 # Add T noise self.hmaptemp[0] += hmapn ################### # Q/U noise if self.QUtemptype == 'planck': # Add Planck noise fn = 'planckmaps/mc_noise/143/ffp8_noise_143_full_map_mc_{:05d}.fits'.format( self.rlz) hmapn = 1e6 * np.array(hp.read_map(fn, field=(1, 2))) elif (self.QUtemptype == 'spt'): fn = 'input_maps/SPT_noise_map_r{:04d}.fits'.format(self.rlz) hmapn = hp.read_map(fn, field=(1, 2)) elif (self.QUtemptype == 's4'): fn = 'input_maps/S4_noise_map_r{:04d}.fits'.format(self.rlz) hmapn = hp.read_map(fn, field=(1, 2)) elif self.QUtemptype == 'noiseless': hmapn = np.zeros_like(self.hmaptemp[1:]) else: raise ValueError('QU template type not recognized') # Add noise hmapn = np.array(hp.ud_grade(hmapn, self.tempNside, pess=False)) hmapn[np.abs(hmapn) > 1e10] = 0 self.hmaptemp[1:] += hmapn # Smooth Q/U templates fwhm = 30.0 # fwhm in arcmin self.hmaptemp[1] = hp.smoothing(self.hmaptemp[1], fwhm=fwhm / 60. * np.pi / 180) self.hmaptemp[2] = hp.smoothing(self.hmaptemp[2], fwhm=fwhm / 60. * np.pi / 180)
def getFilePixels(self, r): """Given a healpix cell and radius for a given nside, figure out which lightcone pixels we need to read Parameters ---------- r : int radial bin to read Returns ------- pix_file : list List of the lightcone file pixels that need to be read peano_idx : list List of the peano indices of particles that need to be read """ partpath = self.nbody.partpath[self.nbody.boxnum] nside = self.nbody.domain.nside pix = self.nbody.domain.pix header_fmt = ['Np', 'nside_index', 'nside_file', 'box_rmin', 'box_rmax', 'void', 'Lbox', 'Mpart', 'Omega_m', 'Omega_l', 'h'] f = '{}/snapshot_Lightcone_{}_0'.format(partpath, r) hdr, idx = read_radial_bin(f) hdr = dict(zip(header_fmt, hdr)) self.part_mass = hdr['Mpart'] * 1e10 if not self.nbody.domain.nest: pix = hp.ring2nest(nside, pix) # this assumes that nside < nside_index which should always be true idxmap = hp.ud_grade(np.arange(12 * nside**2), hdr['nside_index'], order_in='NESTED', order_out='NESTED') # get peano cells corresponding to pix # first get nside=nside_index, nest ordered cells corresponding to pix peano_idx = nest2peano(np.where(idxmap == pix)[0], int(np.log2(hdr['nside_index']))) if nside < hdr['nside_file']: udmap = hp.ud_grade(np.arange(12 * nside**2), hdr['nside_file'], order_in='NESTED', order_out='NESTED') pix_file, = np.where(udmap == pix) elif nside > hdr['nside_file']: udmap = hp.ud_grade(np.arange(12 * hdr['nside_file']**2), nside, order_in='NESTED', order_out='NESTED') pix_file = [udmap[pix]] else: pix_file = [pix] return pix_file, peano_idx
def smoothing_data(self, downgrade_to): hpmap_10, hpmap_408, hpmap_22, hpmap_45_old, hpmap_35, hpmap_38, hpmap_40, hpmap_45, hpmap_50, hpmap_60, hpmap_70, hpmap_74, hpmap_80 = self.read_file( ) pix_number_45_old = [] for dec in np.arange(70, 90, 0.1): for ra in np.arange(0, 360, 0.1): pix_num = self.DeclRaToIndex(dec, ra, downgrade_to) pix_number_45_old.append(pix_num) pix_number_45 = [] for dec in np.arange(-90, -40, 0.1): for ra in np.arange(0, 360, 0.1): pix_num = self.DeclRaToIndex(dec, ra, downgrade_to) pix_number_45.append(pix_num) Dict = { 'hpmap_10': hpmap_10, 'hpmap_408': hpmap_408, 'hpmap_22': hpmap_22, 'hpmap_45_old': hpmap_45_old, 'hpmap_35': hpmap_35, 'hpmap_38': hpmap_38, 'hpmap_40': hpmap_40, 'hpmap_45': hpmap_45, 'hpmap_50': hpmap_50, 'hpmap_60': hpmap_60, 'hpmap_70': hpmap_70, 'hpmap_74': hpmap_74, 'hpmap_80': hpmap_80 } for key, X in Dict.items(): X = self.change_coord(X, ['C', 'G']) nans, x = self.nan_helper(X) X[nans] = np.interp(x(nans), x(~nans), X[~nans]) X = self.smooth(X) X = hp.ud_grade(X, downgrade_to) if key == 'hpmap_45_old': X = self.change_coord(X, ['G', 'C']) X[pix_number_45_old] = 0. X = self.change_coord(X, ['C', 'G']) if key == 'hpmap_35' or 'hpmap_38' or 'hpmap_40' or 'hpmap_45' or 'hpmap_50' or 'hpmap_60' or 'hpmap_70' or 'hpmap_74' or 'hpmap_80': X = self.change_coord(X, ['G', 'C']) X[pix_number_45] = 0. X = self.change_coord(X, ['C', 'G']) #print key,np.min(X),np.max(X) if key == 'hpmap_35': Mask_missing_region_lwa = nans.copy() Mask_missing_region_lwa = hp.ud_grade(Mask_missing_region_lwa, downgrade_to) Dict[key] = X #the output map with coordinate of galaxy return Dict, Mask_missing_region_lwa
def __init__(self, band, scene, true_sky=None, factor=1, fwhm=0, seed=None, comm=None): """ Parameters ---------- band : int The band 150 or 220. scene : Scene The acquisition scene. true_sky : array of shape (npixel,) or (npixel, 3) The true CMB sky (temperature or polarized). The Planck observation will be this true sky plus a random independent gaussian noise realization. factor : 1 or 3 floats, optional The factor by which the Planck standard deviation is multiplied. fwhm : float, optional, !not used! The fwhm of the Gaussian used to smooth the map [radians]. seed : integer, optional. The seed used to generate Planck noise. comm : mpi4py.MPI.Comm The acquisition's MPI communicator. """ if band not in (150, 220): raise ValueError("Invalid band '{}'.".format(band)) if true_sky is None: raise ValueError('The Planck Q & U maps are not released yet.') if scene.kind == 'IQU' and true_sky.shape[-1] != 3: raise TypeError('The Planck sky shape is not (npix, 3).') true_sky = np.array(hp.ud_grade(true_sky.T, nside_out=scene.nside), copy=False).T if scene.kind == 'IQU' and true_sky.shape[-1] != 3: raise TypeError('The Planck sky shape is not (npix, 3).') self.scene = scene self.fwhm = fwhm self._true_sky = true_sky if band == 150: filename = 'Variance_Planck143GHz_Kcmb2_ns256.fits' else: filename = 'Variance_Planck217GHz_Kcmb2_ns256.fits' sigma = 1e6 * factor * np.sqrt(FitsArray(PATH + filename)) if scene.kind == 'I': sigma = sigma[:, 0] elif scene.kind == 'QU': sigma = sigma[:, :2] if self.scene.nside != 256: sigma = np.array(hp.ud_grade(sigma.T, self.scene.nside, power=2), copy=False).T self.sigma = sigma if comm is None: comm = MPI.COMM_WORLD self.comm = comm if seed is not None and seed != comm.bcast(seed, root=0): raise ValueError('The specified seed is not the same on all the nodes.') self.seed = seed
def _get_hm_maps(self): if self.hm1_map is None: hm1_map = hp.read_map(self.file_hm1) hm1_map = rotate_map(hm1_map, self.rot) self.hm1_map = [hp.ud_grade(hm1_map, nside_out=self.nside)] if self.hm2_map is None: hm2_map = hp.read_map(self.file_hm2) hm2_map = rotate_map(hm2_map, self.rot) self.hm2_map = [hp.ud_grade(hm2_map, nside_out=self.nside)] return self.hm1_map, self.hm2_map
def get_fg_maps(): fgmps = np.zeros([2, hp.nside2npix(NSIDE_DEFAULT), 6]) for i in np.arange(6): t, q, u = hp.read_map('maps_FG/sim_' + FG_CODE + '_band%d.fits' % (i + 1), field=[0, 1, 2], verbose=False) fgmps[0, :, i] = hp.ud_grade(q, nside_out=NSIDE_DEFAULT) fgmps[1, :, i] = hp.ud_grade(u, nside_out=NSIDE_DEFAULT) return fgmps
def degrade(self, nside): if nside == self.nside: return self else: marge_maps_p = [] n_marge_maps_p = len(self.templates_p) if ( n_marge_maps_p > 0 ): marge_maps_p = [hp.ud_grade(ti.map, nside) for ti in self.templates_p[0:n_marge_maps_p]] return alm_filter_ninv( [hp.ud_grade(n, nside, power=-2) for n in self.n_inv], self.b_transf, marge_maps_p )
def read_skymap(params,is3D=False): filename = params["skymap"] map_struct = {} if is3D: healpix_data = hp.read_map(filename, field=(0,1,2,3), verbose=False) distmu_data = healpix_data[1] distsigma_data = healpix_data[2] prob_data = healpix_data[0] norm_data = healpix_data[3] map_struct["distmu"] = distmu_data / params["DScale"] map_struct["distsigma"] = distsigma_data / params["DScale"] map_struct["prob"] = prob_data map_struct["distnorm"] = norm_data else: prob_data = hp.read_map(filename, field=0, verbose=False) prob_data = prob_data / np.sum(prob_data) map_struct["prob"] = prob_data nside = hp.pixelfunc.get_nside(prob_data) nside = params["nside"] map_struct["prob"] = hp.ud_grade(map_struct["prob"],nside,power=-2) if is3D: map_struct["distmu"] = hp.ud_grade(map_struct["distmu"],nside,power=-2) map_struct["distsigma"] = hp.ud_grade(map_struct["distsigma"],nside,power=-2) map_struct["distnorm"] = hp.ud_grade(map_struct["distnorm"],nside,power=-2) npix = hp.nside2npix(nside) theta, phi = hp.pix2ang(nside, np.arange(npix)) ra = np.rad2deg(phi) dec = np.rad2deg(0.5*np.pi - theta) map_struct["ra"] = ra map_struct["dec"] = dec sort_idx = np.argsort(prob_data)[::-1] csm = np.empty(len(prob_data)) csm[sort_idx] = np.cumsum(prob_data[sort_idx]) map_struct["cumprob"] = csm pixarea = hp.nside2pixarea(nside) pixarea_deg2 = hp.nside2pixarea(nside, degrees=True) map_struct["nside"] = nside map_struct["npix"] = npix map_struct["pixarea"] = pixarea map_struct["pixarea_deg2"] = pixarea_deg2 return map_struct
def ud_grade(self, nside_new): nmap = hp.ud_grade(self.data.copy(), nside_out=nside_new, order_in=self.check_order(), order_out=self.check_order()) nmask = hp.ud_grade(self.mask.copy(), nside_out=nside_new, order_in=self.check_order(), order_out=self.check_order()) self.header['NSIDE'] = nside_new return Fitsmap(nmap, nmask, self.header.copy(), self.check_order())
def __init__(self, band, scene, true_sky=None, factor=1, fwhm=0, mask=None, convolution_operator=None): """ Parameters ---------- band : int The band 150 or 220. scene : Scene The acquisition scene. true_sky : array of shape (npixel,) or (npixel, 3) The true CMB sky (temperature or polarized). The Planck observation will be this true sky plus a random independent gaussian noise realization. factor : 1 or 3 floats, optional The factor by which the Planck standard deviation is multiplied. fwhm : float, optional, !not used! The fwhm of the Gaussian used to smooth the map [radians]. mask : array of shape (npixel, ) or None The boolean mask, which is equal True at pixels where we need Planck, that is outside the QUBIC field. """ if band not in (150, 220): raise ValueError("Invalid band '{}'.".format(band)) if true_sky is None: raise ValueError('The Planck Q & U maps are not released yet.') if scene.kind == 'IQU' and true_sky.shape[-1] != 3: raise TypeError('The Planck sky shape is not (npix, 3).') true_sky = np.array(hp.ud_grade(true_sky.T, nside_out=scene.nside), copy=False).T if scene.kind == 'IQU' and true_sky.shape[-1] != 3: raise TypeError('The Planck sky shape is not (npix, 3).') self.scene = scene self.fwhm = fwhm self._true_sky = true_sky if mask is not None: self.mask = mask else: self.mask = np.ones(scene.npixel, dtype=np.bool) if band == 150: filename = 'Variance_Planck143GHz_Kcmb2_ns256.fits' else: filename = 'Variance_Planck217GHz_Kcmb2_ns256.fits' sigma = 1e6 * factor * np.sqrt(FitsArray(PATH + filename)) if scene.kind == 'I': sigma = sigma[:, 0] elif scene.kind == 'QU': sigma = sigma[:, :2] if self.scene.nside != 256: sigma = np.array(hp.ud_grade(sigma.T, self.scene.nside, power=2), copy=False).T self.sigma = sigma if convolution_operator is None: self.C = IdentityOperator() else: self.C = convolution_operator
def OrderingConvert( self, orderingin=None, orderingout=None, inmap=None ) : ''' ''' orderingin, nestin, orderingout = self._OrderingCheck(orderingin, orderingout)[:3] if (self.verbose and jp.SysFrame(0,2)[-1][-2]=='') : strfont = '' else : strfont = ' ' if (self.verbose) : print strfont+'Tmap2Vis.OrderingConvert' print strfont+strfont+"orderingin='"+orderingin+"', orderingout='"+orderingout+"'" if (self.verbose and jp.SysFrame(0,2)[-1][-2]=='') : print if (inmap is None) : self.Tmap = hp.ud_grade(self.Tmap, self.nsidein, order_in=orderingin, order_out=orderingout) else : return hp.ud_grade(inmap, hp.get_nside(inmap), order_in=orderingin, order_out=orderingout)
def initialize(self, maps, exposure, nwalkers=DEFAULT_NUM_WALKERS): ### resample maps, exposure to the nside for this RoModel nside = self.params['ro_nside'] maps = np.sum(hp.ud_grade(m, nside, power=-2) for m in maps) exposure = hp.ud_grade(exposure, nside, power=-2) npix = hp.nside2npix(nside) state = np.empty((nwalkers, npix), dtype=float) for pix in xrange(npix): state[:, i] = gamma.rvs(maps[i] + 1, size=nwalkers) / exposure[i] return state
def __init__(self,fname_nz,fname_map,fname_mask,label): mp=hp.ud_grade(hp.read_map(fname_map,verbose=False),nside_out=512) msk=hp.ud_grade(hp.read_map(fname_mask,verbose=False),nside_out=512) self.label=label self.z,self.pz=np.loadtxt(fname_nz,unpack=True) fsky=np.mean(msk) num=np.sum(mp*msk) area=4*np.pi*fsky*(180/np.pi)**2 dens=num/area/100 print(label,np.sum(self.pz*self.z)/np.sum(self.pz),dens*100) self.nz=dens*self.pz/np.sum(self.pz*np.mean(np.diff(self.z)))
def plug_holes(m, verbose=False, in_place=True, nest=False): """ Use simple downgrading to derive estimates of the missing pixel values """ autotimer = timing.auto_timer() nbad_start = np.sum(np.isclose(m, hp.UNSEEN)) if nbad_start == m.size: if verbose: print('plug_holes: All map pixels are empty. Cannot plug holes', flush=True) return if nbad_start == 0: return nside = hp.get_nside(m) npix = m.size if nest: mnest = m.copy() else: mnest = hp.reorder(m, r2n=True) lowres = mnest nside_lowres = nside bad = np.isclose(mnest, hp.UNSEEN) while np.any(bad) and nside_lowres > 1: nside_lowres //= 2 lowres = hp.ud_grade(lowres, nside_lowres, order_in='NESTED') hires = hp.ud_grade(lowres, nside, order_in='NESTED') bad = np.isclose(mnest, hp.UNSEEN) mnest[bad] = hires[bad] nbad_end = np.sum(bad) if nbad_end != 0: mn = np.mean(mnest[np.logical_not(bad)]) mnest[bad] = mn if not in_place: m = m.copy() if nest: m[:] = mnest else: m[:] = hp.reorder(mnest, n2r=True) if verbose and nbad_start != 0: print('plug_holes: Filled {} missing pixels ({:.2f}%), lowest ' 'resolution was Nside={}.'.format( nbad_start, (100.*nbad_start) // npix, nside_lowres)) del autotimer return m
def __init__(self, nside, name, mask_id, fname_mask, fname_map, fname_dndz, field_mask=0, field_map=0, is_ndens=True, syst_list=None): self.name = name self.nside = nside self.mask_id = mask_id self.fname_mask = fname_mask self.is_ndens = is_ndens # True if this is a delta_gal map # Read mask self.mask = hp.ud_grade(hp.read_map(fname_mask, verbose=False, field=field_mask), nside_out=nside) # Read map self.map0 = hp.ud_grade(hp.read_map(fname_map, verbose=False, field=field_map), nside_out=nside) mask_bn = np.ones_like(self.mask) mask_bn[self.mask <= 0] = 0 # Binary mask self.map0 *= mask_bn # Remove masked pixels if is_ndens: # Compute delta if this is a number density map # Mean number of galaxies per pixel. mean_g = np.sum(self.map0*self.mask) / np.sum(self.mask) # Transform to number density self.ndens = mean_g * hp.nside2npix(self.nside) / (4*np.pi) # Compute delta self.map = self.mask*(self.map0 / mean_g - 1.) # Read redshift distribution self.dndz = fname_dndz self.z, self.nz = np.loadtxt(self.dndz, unpack=True) # Compute redshift range z_inrange = self.z[(self.nz > 0.005 * np.amax(self.nz))] self.zrange = np.array([z_inrange[0], z_inrange[-1]]) else: # Nothing to do otherwise self.ndens = 0 self.map = self.map0 self.z = None self.dndz = None # Load contaminant templates self.temp = None if syst_list is not None: for sname in syst_list: if os.path.isfile(sname): if self.temp is None: self.temp = [] t = hp.ud_grade(hp.read_map(sname, verbose=False), nside_out=nside) t_mean = np.sum(t * self.mask)/np.sum(self.mask) self.temp.append([mask_bn * (t- t_mean)]) # Generate NmtField self.field = nmt.NmtField(self.mask, [self.map], templates=self.temp)
def getmapsready(file, newns, pixok): maps = read_map(file) newmaps_I = hp.ud_grade(maps[:,0], nside_out=newns) newmaps_Q = hp.ud_grade(maps[:,1], nside_out=newns) newmaps_U = hp.ud_grade(maps[:,2], nside_out=newns) newmaps_I[~pixok] = np.nan newmaps_Q[~pixok] = np.nan newmaps_U[~pixok] = np.nan #newmaps_I[pixok] -= np.mean(newmaps_I[pixok]) #newmaps_Q[pixok] -= np.mean(newmaps_Q[pixok]) #newmaps_U[pixok] -= np.mean(newmaps_U[pixok]) newmaps = np.array([newmaps_I, newmaps_Q, newmaps_U]).T return newmaps
def thermal_dust(frequencies, nside, model_template, spectral_index_model, temp_model): """Galactic thermal dust cube: it uses HEALPIX pixelization and is in mK. """ ### Models: template, spectral index, and temperature if model_template == 'gnilc_353': td_0 = hp.read_map( 'data/COM_CompMap_Dust-GNILC-F353_2048_R2.00_small_scales.fits') td_0 = 261.20305067644796 * td_0 # from 'MJy/sr' to 'muK_RJ' freq_in = 353.0 # GHz else: sys.exit("Not a valid model!!!") if spectral_index_model == 'gnilc_353': specind_0 = hp.read_map( 'data/COM_CompMap_Dust-GNILC-Model-Spectral-Index_2048_R2.00.fits') else: sys.exit("Not a valid model!!!") if temp_model == 'gnilc_353': temp_0 = hp.read_map( 'data/COM_CompMap_Dust-GNILC-Model-Temperature_2048_R2.00.fits' ) # K else: sys.exit("Not a valid model!!!") ### Maps nchannels = frequencies.size td = hp.ud_grade(td_0, nside_out=nside) td_0 = 0 specind = hp.ud_grade(specind_0, nside_out=nside) specind_0 = 0 temp = hp.ud_grade(temp_0, nside_out=nside) temp_0 = 0 maps = np.zeros((nchannels, hp.nside2npix(nside))) ### Frequency scaling: modified blackbody -- see Planck Collaboration XI et al. 2013 gamma = h_planck / (k_bol * temp) for i in range(0, nchannels): maps[i, :] = 1.e-3 * td * (frequencies[i] / freq_in)**(specind + 1) * ( (np.exp(gamma * (freq_in * 1.e9)) - 1.) / (np.exp(gamma * (frequencies[i] * 1.e9)) - 1.)) return maps # mK
def process_healpix(filename, out_nside): print 'Process {0} on {1}'.format(MP.current_process().name, filename) pixval = HP.read_map(filename, verbose=False) nside = HP.npix2nside(pixval.size) out_pixres = HP.nside2resol(out_nside) print 'Before: ', pixval.min(), pixval.max(), NP.mean(pixval), NP.std(pixval) if nside > out_nside: if nside/out_nside > 4: pixval = HP.ud_grade(pixval, 4*out_nside) nside = 4*out_nside pix_smoothed = HP.smoothing(pixval, fwhm=out_pixres, regression=False, verbose=False) pix_resampled = HP.ud_grade(pix_smoothed, out_nside) print 'After: ', pixval.min(), pixval.max(), NP.mean(pix_resampled), NP.std(pix_resampled) return pix_resampled
def _interpolate_level(m): """Recursive multi-resolution interpolation. Modifies `m` in place.""" # Determine resolution. npix = len(m) if npix > 12: # Determine which pixels comprise multi-pixel tiles. ipix = np.flatnonzero( (m[0::4] == m[1::4]) & (m[0::4] == m[2::4]) & (m[0::4] == m[3::4])) if len(ipix): ipix = (4 * ipix + np.expand_dims(np.arange(4, dtype=np.intp), 1)).T.ravel() nside = hp.npix2nside(npix) # Downsample. m_lores = hp.ud_grade( m, nside // 2, order_in='NESTED', order_out='NESTED') # Interpolate recursively. _interpolate_level(m_lores) # Record interpolated multi-pixel tiles. m[ipix] = hp.get_interp_val( m_lores, *hp.pix2ang(nside, ipix, nest=True), nest=True)
def create_cmb(nside, polon, freq='0'): print "creating map" if freq == '0': cmbfname = "planck_data/cmb_map512.fits" cmb = hp.read_map(cmbfname, field=(0,1,2)) cmb = hp.ud_grade(cmb, nside) #cmb = hp.sphtfunc.smoothing(cmb, fwhm=beam, pol=True) cmbnew = np.array(cmb) norm = 10**6 cmbnew[0] *= norm cmbnew[1] *= norm cmbnew[2] *= norm if polon: return cmbnew else: return cmbnew[0] else: cmbfname = 'planck_data/ebex_'+freq+'.fits' T = hp.read_map(cmbfname) if polon: if freq == '150': Q = hp.read_map('planck_data/ebex_150Q.fits') U = hp.read_map('planck_data/ebex_150U.fits') if freq == '250': Q = hp.read_map('planck_data/ebex_250Q.fits') U = hp.read_map('planck_data/ebex_250U.fits') return [T, Q, U] else: return T
def to_healpix(self, n_side=32): """ Convert beam pattern to a healpix Returns ------- hmap: np.array Numpy array representing healpix map. Array is in observer frame, i.e. zenith aligned with equatorial coordinate system. """ beam_azel = self.to_azel() el = np.linspace(0, np.pi, beam_azel.shape[1]) az = np.linspace(-np.pi, np.pi, beam_azel.shape[0])[:, None] # Generate beam pattern with n_side = 32 (for gridding reasons) hmap = hp.ma(np.zeros(hp.nside2npix(32))) pix = hp.ang2pix(32, el, az) hmap[pix] = beam_azel # Upsample if required if n_side != 32: hmap = hp.ud_grade(hmap, nside_out=n_side) # Apply mask n_pix = hp.nside2npix(n_side) theta, phi = hp.pix2ang(n_side, np.arange(n_pix)) mask1 = phi + np.pi/2 > 2 * np.pi mask2 = phi < np.pi / 2 hmap.mask = np.invert(np.logical_or(mask1, mask2)) return hmap
def setup_b_instance(ebin_min): global b, f, run_tag run_tag=run_tag_base + '-'+str(ebin_min) #set the run tag using ebin_min f = copy.deepcopy(f_total) #copy f_total and reduce to energy subset f.reduce_to_energy_subset(ebin_min,ebin_min+1) if fake_data: print 'Using fake data!' data = np.loadtxt(fake_data_path) # Data is often in a different nside - convert it here data = np.array([hp.ud_grade(dat,nside,power=-2) for dat in data]) else: print 'Using real data!' data = f.CTB_count_maps b = bsm.bayesian_scan_NPTF(tag=tag,nside=nside,work_dir=work_dir,psf_dir=psf_dir) b.load_external_data(f.CTB_en_bins,data,f.CTB_exposure_maps) b.make_mask_total(band_mask_range = band_mask_range,mask_ring = mask_ring,outer=outer,ps_mask_array = f.ps_mask_array) b.add_new_template(f.template_dict) b.rebin_external_data(1) b.add_poiss_model('nfw','$A_{nfw}$',[-6,6],True) b.add_poiss_model('p6','$A_{p6}$',[0,2],False) b.add_poiss_model('bubs','$A_{bubs}$',[0,2],False) b.add_poiss_model('iso','$A_{iso}$',[-2,2],False) b.add_poiss_model('ps_model','$A_{ps-model}$',[0,3],False) b.initiate_poissonian_edep()
def read_hpx_maps(fns): '''Read in one or more healpix maps and add them together. Must input an array of strings even if only inputting a single map. Parameters ---------- fns : list of strings The filenames for the healpix maps to read in. Returns ------- hpx_map: array-like A healpix map that is the sum of the Healpix maps in the input files. Notes ----- The nside of the output map will be the nside of the file map in the list. Every other map will be upgraded or downgraded that that nside value. ''' hpx_map = H.read_map(fns[0], verbose=False) nside = H.npix2nside(len(hpx_map)) for fn_tmp in fns[1:]: tmp_map = H.read_map(fn_tmp, verbose=False) hpx_map += H.ud_grade(tmp_map, nside) return hpx_map
def make_ps_template(self): the_map = np.zeros(self.npixupbin) the_map[self.pix_num] = 1 # Smooth using a Gaussian self.sgp = swp.smooth_gaussian_psf(self.psf,the_map) # Downbin for the final map self.smooth_ps_map = hp.ud_grade(self.sgp.smooth_the_pixel(self.pix_num),self.nside,power=-2)
def G_plus_C_cross_C_four(freq,scale): """plot of fourier of GSM(scale)+CMB crossed with CMB compared to CMB only User defines GSM freq in MHz User defines scale of GSM such as 0.1 or 0.01 WARNING: takes some time to run """ gsm=GlobalSkyModel() #create the GSM model gsmdata=gsm.generate(freq) #rather than upgrading GSM, downgrade the CMB to 512 cmbdata=hp.read_map('/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits') cmbmap=hp.ud_grade(cmbdata,512) LMAX=1024 both=(gsmdata*scale)+cmbmap #cross cmb+gsm with the cmb c_plus_g_cross_c=hp.sphtfunc.anafast(both,map2=cmbmap,lmax=LMAX) ell=np.arange(len(c_plus_g_cross_c)) plt.figure() #format variables as text for labels freq_str=str(freq) scale_str=str(scale) plt.plot(ell,ell*(ell+1)*c_plus_g_cross_c,color='b',label='GSM('+scale_str+') at '+freq_str+' MHZ+CMB cross CMB') cmbmaponly=hp.anafast(cmbdata,lmax=LMAX) ell=np.arange(len(cmbmaponly)) plt.plot(ell,ell*(ell+1)*cmbmaponly,color='r',label='CMB') #delete this line? #hp.write_cmbmaponly('cmbmaponly.fits',cmbmaponly) plt.xlabel('ell');plt.ylabel('ell(ell+1)');plt.grid() #delete this line? #hp.write_cp_plus_g_cross_c('c_plus_g_cross_c.fits',c_plus_g_cross_c) plt.legend(loc='upper right') plt.show()
def make_ps_template(self): the_map = np.zeros(self.npixupbin) the_map[self.pix_num] = 1 # Smooth using a king function self.skf = swk.smooth_king_psf(self.maps_dir,the_map,self.Ebin,self.eventclass,self.eventtype) # Downbin for the final map self.smooth_ps_map = hp.ud_grade(self.skf.smooth_the_pixel(self.pix_num),self.nside,power=-2)
def return_diff_p8_um(fits_file ,CTB_en_bins,CTB_en_min,CTB_en_max,NSIDE=128): # Designed to replace function below just for p8 diffuse models NPIX = hp.nside2npix(NSIDE) diff_fits = fits.open(fits_file) diff_master_p8 = diff_fits[0].data difftoreturn = [hp.ud_grade(diff_master_p8[:,i],NSIDE)*(CTB_en_bins[i+1-CTB_en_min]-CTB_en_bins[i-CTB_en_min])*4*np.pi/NPIX*10**(3) for i in range(CTB_en_min,CTB_en_max)] return difftoreturn
def G_plus_C_four(freq): """plots fourier of GSM and CMB added together User defines GSM frequency in MHz WARNING:takes some times to run Note: GSM default shape is 512 CMB default shape is 2048 GSM and CMB shape must match to add arrays GSM has been cast to a higher nside of 2048 """ gsm=GlobalSkyModel() #creates a model of the GSM gsmdata=gsm.generate(freq) #upgrades the GSM to 2048 gsmdata=hp.ud_grade(gsmdata,2048) cmbdata=hp.read_map('/home/amber/data/cmb/COM_CMB_IQU-commander-field-Int_2048_R2.01_full.fits') LMAX=1024 bothmap=hp.anafast(gsmdata+cmbdata,lmax=LMAX) ell=np.arange(len(bothmap)) plt.figure() freq_str=str(freq) plt.plot(ell,ell*(ell+1)*bothmap,color='b',label='GSM at '+freq_str+' MHz +CMB') plt.xlabel('ell');plt.ylabel('ell(ell+1)');plt.grid() #delete this line? #hp.write_bothmap('bothmap.fits',bothmap) plt.legend(loc='upper right') plt.show()
def ascii2fits(file_1,nside_1,file_2,nside_2): """ A function for converting ascii map to fits with a mask. @param file_1 input ascii file with useful pixel numbers @param nside_1 nside of the input ascii file @param file_2 output file path @param nside_2 nside of the output map """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) if(nside_2 > nside_1): raise RuntimeError("nside of the input should be greater than the output") logger.info("Reading data from "+file_1) pixs = np.loadtxt(file_1,skiprows=1) logger.info("Creating healpix mask with nside "+str(nside_2)) mask = np.zeros(hp.nside2npix(nside_1)) for pix in pixs: mask[int(pix)] = 1 ud_mask = hp.ud_grade(mask,nside_2) logger.info("Writing output to "+file_2) hp.write_map(file_2,ud_mask)
def read_masks(self, freq): result = [] filenames = [get_filename(self.config.get("Templates", mask_type).format(frequency=freq)) for mask_type in ["ps_mask", "spectra_mask"]] for file_name in filenames: result.append(np.logical_not(np.floor(hp.ud_grade(hp.read_map(file_name), self.nside)).astype(np.bool))) return tuple(result)
def read_data(filename, i_from, i_to, nside, BaselineLength, comm, pol=False, maskdestripe="mask.fits"): """Read data serially with h5py a cython version using HDF5 parallel I/O is available, but complicated to build and less flexible""" l.info("Proc %d: Read %s %d-%d" % (comm.MyPID, filename, i_from, i_to)) with h5py.File(filename, mode="r") as h5file: data = h5file["data"][i_from:i_to] pix = hp.ang2pix(nside,data['THETA'],data['PHI']) if maskdestripe: mask = hp.ud_grade(hp.read_map(maskdestripe), nside) data["FLAG"][mask[pix] == 0] = 1 good_data = data["FLAG"] == 0 BaselineLengths = np.sum((good_data).reshape(-1, BaselineLength), axis = 1) l.info("Proc %d: Total data %d, good data %d" % (comm.MyPID, len(data), (data['FLAG'] == 0).sum())) data = data [good_data] pix = pix [good_data] if pol: d = {'Q':data['Q'].copy(), 'U':data['U'].copy()} d['q_channel_w'], d['u_channel_w'] = get_qu_weights(np.radians(data['PSI'])) d['PSI'] = data['PSI'].copy() else: d = {'T':data['TEMP'].copy()} return pix, d, BaselineLengths
def _load_data(self): print "Loading data for galaxy simulation." _haslam_file = join(_datadir, "haslam.fits") _sp_ind_file = join(_datadir, "spectral.hdf5") if not exists(_haslam_file): print "Needed data files missing! Try and fetch the Haslam map from LAMBDA [y/n]?" choice = raw_input().lower() if choice == "y": import urllib _haslam_url = "http://lambda.gsfc.nasa.gov/data/foregrounds/haslam/lambda_haslam408_dsds.fits" print "Downloading %s ...." % _haslam_url urllib.urlretrieve(_haslam_url, _haslam_file) print "Done." elif choice == "n": raise Exception("No Haslam map found. Can not continue.") else: print "Please respond with 'y' or 'n'." self._haslam = healpy.read_map(join(_datadir, "haslam.fits")) self._sp_ind = h5py.File(_sp_ind_file)["spectral_index"][:] # Upgrade the map resolution to the same as the Healpix map (nside=512). self._sp_ind = healpy.smoothing(healpy.ud_grade(self._sp_ind, 512), degree=True, sigma=1.0)
def read_mask(filename, nside): return np.logical_not( np.floor( hp.ud_grade( hp.read_map(filename), nside_out=nside ) ).astype(np.bool) )
def NsideConvert( self, nsideout=None, orderingin=None, inmap=None ) : ''' ''' nsideout = self._NsideCheck(nsideout) orderingin = self._OrderingCheck(orderingin)[0] if (self.verbose and jp.SysFrame(0,2)[-1][-2]=='') : strfont = '' else : strfont = ' ' if (self.verbose) : print strfont+'Tmap2Vis.NsideConvert' nsideinstr = '' if (inmap is None) : print strfont+strfont+"nsidein="+str(hp.get_nside(self.Tmap))+", nsideout="+str(nsideout)+", orderingin='"+orderingin+"'" if(self.verbose and jp.SysFrame(0,2)[-1][-2]==''): print self.Tmap = hp.ud_grade(self.Tmap, nsideout, order_in=orderingin) else : print strfont+strfont+"nsidein="+str(hp.get_nside(inmap))+", nsideout="+str(nsideout)+", orderingin='"+orderingin+"'" if(self.verbose and jp.SysFrame(0,2)[-1][-2]==''): print return hp.ud_grade(inmap, nsideout, order_in=orderingin)