def resampleSubmaps(submapList, resampleFactor=10): '''Gets a list of enmap submaps and resamples them by a constant factor Uses defaults in enmap.resample, fft, and order=3 ''' resampledSubmapList = [] for submap in submapList: newShape = resampleFactor * np.array(submap.shape, dtype=int) resampledSubmapList.append(enmap.resample(submap, newShape)) return resampledSubmapList
def get_anisotropic_noise_template(shape,wcs,template_file=None,tmin=0,tmax=100): """ This function reads in a 2D PS unredenned template and returns a full 2D noise PS. It doesn't use the template in the most sensible way though. """ if template_file is None: template_file = "data/anisotropy_template.fits" template = np.nan_to_num(enmap.read_map(template_file)) template[template<tmin] = tmin template[template>tmax] = tmax ops = enmap.enmap(enmap.resample(template,shape),wcs) # interpolate to new geometry return ops
def extractStamp(submap, ra_deg, dec_deg, r_ring_arcmin, repixelize=True, reprojection=True): extractedSubmap = submap.copy() if repixelize: extractedSubmap = enmap.resample(extractedSubmap, 10 * np.array(submap.shape)) if reprojection: extractedSubmap = reproject.postage_stamp(extractedSubmap, ra_deg, dec_deg, 2 * r_ring_arcmin, 0.5 / 5.)[0, :, :] else: # this was added for backwards compatibility assert len(extractedSubmap.shape) == 2 extractedSubmap = submapTools.getSubmap_originalPixelization( theMap=extractedSubmap, # noqa ra_deg=ra_deg, dec_deg=dec_deg, # noqa semiWidth_deg=r_ring_arcmin / 60.) # noqa return extractedSubmap
def get_aperturePhotometry(submap, ra_deg, dec_deg, r_ring_arcmin, r_disk_arcmin, repixelize=True, reprojection=True): '''Gets T_disk and T_ring for a given submap. Needs ra and dec of the center of the submap and the radius of the ring and disk for the aperture. Can repixelize by a factor of 10 finer resolution and reproject. submap: submap in the original pixelization ra, dec_deg: ra and dec in degrees of the center of the aperture r_ring/disk_arcmin: radius for the aperture photometry repixelize: True if you want to do it as enmap.resample by a factor of 10 reprojection: True if you want to extract the postage stamp by reprojection at the equator. submapsFilename: None if you dont want to save submaps. String with a hdf5 filename if you want to save the submaps.''' submapForPhotometry = submap.copy() if repixelize: submapForPhotometry = enmap.resample(submapForPhotometry, 10 * np.array(submap.shape)) if reprojection: try: submapForPhotometry = reproject.postage_stamp( submapForPhotometry, ra_deg, dec_deg, 3.0 * r_ring_arcmin, 0.5 / 10.)[0, :, :] except: # noqa T_disk, T_ring = np.nan, np.nan return 0, 0 r_arcmin = 60. * np.rad2deg(submapForPhotometry.modrmap()) sel_disk = r_arcmin < r_disk_arcmin sel_ring = np.logical_and(r_arcmin > r_disk_arcmin, r_arcmin < r_ring_arcmin) T_disk = submapForPhotometry[sel_disk].mean() T_ring = submapForPhotometry[sel_ring].mean() return T_disk, T_ring
def noise_average(n2d,dfact=(16,16),lmin=300,lmax=8000,wnoise_annulus=500,bin_annulus=20, lknee_guess=3000,alpha_guess=-4,nparams=None,modlmap=None, verbose=False,method="fft",radial_fit=True, oshape=None,upsample=True,fill_lmax=None,fill_lmax_width=100): """Find the empirical mean noise binned in blocks of dfact[0] x dfact[1] . Preserves noise anisotropy. Most arguments are for the radial fitting part. A radial fit is divided out before downsampling (by default by FFT) and then multplied back with the radial fit. Watch for ringing in the final output. n2d noise power """ assert np.all(np.isfinite(n2d)) shape,wcs = n2d.shape,n2d.wcs minell = maps.minimum_ell(shape,wcs) if modlmap is None: modlmap = enmap.modlmap(shape,wcs) Ny,Nx = shape[-2:] if radial_fit: if nparams is None: if verbose: print("Radial fitting...") nparams = fit_noise_1d(n2d,lmin=lmin,lmax=lmax,wnoise_annulus=wnoise_annulus, bin_annulus=bin_annulus,lknee_guess=lknee_guess,alpha_guess=alpha_guess) wfit,lfit,afit = nparams nfitted = rednoise(modlmap,wfit,lfit,afit) else: nparams = None nfitted = 1. nflat = enmap.enmap(np.nan_to_num(n2d/nfitted),wcs) # flattened 2d noise power if fill_lmax is not None: fill_avg = nflat[np.logical_and(modlmap>(fill_lmax-fill_lmax_width),modlmap<=fill_lmax)].mean() nflat[modlmap>fill_lmax] = fill_avg if oshape is None: oshape = (Ny//dfact[0],Nx//dfact[1]) if verbose: print("Resampling...") nint = enmap.resample(enmap.enmap(nflat,wcs), oshape, method=method) if not(upsample): if radial_fit: nshape,nwcs = nint.shape,nint.wcs modlmap = enmap.modlmap(nshape,nwcs) nfitted = rednoise(modlmap,wfit,lfit,afit) ndown = nint else: ndown = enmap.enmap(enmap.resample(nint,shape,method=method),wcs) outcov = ndown*nfitted outcov[modlmap<minell] = 0 #np.inf if fill_lmax is not None: outcov[modlmap>fill_lmax] = 0 # res,_ = curve_fit(ntemplatefunc,cents,dn1d,p0=[lknee_guess,alpha_guess],bounds=([lknee_min,alpha_min],[lknee_max,alpha_max])) # bad_ells = modlmap[np.isnan(outcov)] # for ell in bad_ells: # print(ell) # print(maps.minimum_ell(shape,wcs)) # from orphics import io # # io.hplot(enmap.enmap(np.fft.fftshift(np.log(n2d)),wcs),"fitnoise_npower.png") # # io.hplot(enmap.enmap(np.fft.fftshift(np.log(outcov)),wcs),"fitnoise_ndown.png") # io.plot_img(enmap.enmap(np.fft.fftshift(np.log(n2d)),wcs),"fitnoise_npower_lowres.png",aspect='auto')#,lim=[-20,-16]) # io.plot_img(enmap.enmap(np.fft.fftshift(np.log(ndown)),wcs),"fitnoise_ndown_lowres.png",aspect='auto')#,lim=[-20,-16]) # io.plot_img(enmap.enmap(np.fft.fftshift(np.log(outcov)),wcs),"fitnoise_outcov_lowres.png",aspect='auto')#,lim=[-20,-16]) # import time # t = time.time() # fbin_edges = np.arange(lmin,lmax,bin_annulus) # fbinner = stats.bin2D(modlmap,fbin_edges) # cents, n1d = fbinner.bin(n2d) # cents,dn1d = fbinner.bin(outcov) # pl = io.Plotter(xyscale='linlog',xlabel='l',ylabel='D',scalefn=lambda x: x**2./2./np.pi) # pl.add(cents,n1d) # pl.add(cents,dn1d,ls="--") # pl.done(os.environ['WORK']+"/fitnoise2_%s.png" % t) # sys.exit() assert not(np.any(np.isnan(outcov))) return outcov,nfitted,nparams