def make_clzz(self, pk): """Make an angular powerspectrum from the input matter powerspectrum. Uses the lmax and frequencies from the telescope object. Parameters ---------- pk : function, np.ndarray -> np.ndarray The input powerspectrum (must be vectorized). Returns ------- aps : np.ndarray[lmax+1, nfreq, nfreq] The angular powerspectrum. """ crt = corr21cm.Corr21cm(ps=pk, redshift=1.5) crt.ps_2d = True clzz = skymodel.im21cm_model( self.telescope.lmax, self.telescope.frequencies, self.telescope.num_pol_sky, cr=crt, temponly=True, ) print("Rank: %i - Finished making band." % mpiutil.rank) return clzz
def test_corr_signal(): """Test that the signal power spectrum is being calculated correctly. Correct here is referenced to a specific version believed to have no errors. """ cr = corr21cm.Corr21cm() aps1 = cr.angular_powerspectrum(np.arange(1000), 800.0, 800.0) assert len(aps1) == 1000 assert np.allclose( aps1.sum(), 1.5963772205823096e-09, rtol=1e-7 ) # Calculated for commit 02f4d1cd3f402d fa = np.linspace(400.0, 800.0, 64) aps2 = cr.angular_powerspectrum( np.arange(1000)[:, None, None], fa[None, :, None], fa[None, None, :] ) assert aps2.shape == (1000, 64, 64) # Calculated for commit 02f4d1cd3f402d v1 = 8.986790805379046e-13 # l=400, fi=40, fj=40 v2 = 1.1939298801340165e-18 # l=200, fi=10, fj=40 assert np.allclose(aps2[400, 40, 40], v1, rtol=1e-7) assert np.allclose(aps2[200, 10, 40], v2, rtol=1e-7)
def _21cm(fstate, nside, pol, filename, eor, oversample): """Generate a Gaussian simulation of the unresolved 21cm background.""" from cora.signal import corr21cm # Read in arguments. if eor: cr = corr21cm.EoR21cm() else: cr = corr21cm.Corr21cm() cr.nside = nside cr.frequencies = fstate.frequencies cr.oversample = oversample if oversample is not None else 3 # Generate signal realisation and save. sg_map = cr.getpolsky() if pol == "full" else cr.getsky() # Save map write_map(filename, sg_map, cr.frequencies, fstate.freq_width, pol != "none")
def _21cm(ctx, eor, oversample): """Generate a Gaussian simulation of the unresolved 21cm background. """ from cora.signal import corr21cm # Read in arguments. if eor: cr = corr21cm.EoR21cm() else: cr = corr21cm.Corr21cm() cr.nside = ctx.obj.nside cr.frequencies = ctx.obj.freq cr.oversample = oversample if oversample is not None else 3 # Generate signal realisation and save. sg_map = cr.getpolsky() if ctx.obj.full_pol else cr.getsky() # Save map write_map(ctx.obj.filename, sg_map, cr.frequencies, ctx.obj.freq_width, ctx.obj.include_pol)
def im21cm_model(lmax, frequencies, npol, cr=None, temponly=False): nfreq = frequencies.size if not cr: global _cr if not _cr: if not _reionisation: _cr = corr21cm.Corr21cm() else: _cr = corr21cm.EoR21cm() cr = _cr #cr._freq_window = np.abs(cr.cosmology.comoving_distance(frequencies[0]) - cr.cosmology.comoving_distance(frequencies[1])) cv_t = skysim.clarray(cr.angular_powerspectrum, lmax, frequencies) if temponly: return cv_t else: cv_sg = np.zeros((npol, npol, lmax + 1, nfreq, nfreq)) cv_sg[0, 0] = cv_t return cv_sg
def genbands(self): """Precompute the powerspectrum bands, including the P(k, mu) bands and the angular powerspectrum. """ print "Generating bands..." cr = corr21cm.Corr21cm() cr.ps_2d = False # Create different sets of bands depending on whether we're using polar bins or not. if self.bandtype == 'polar': # Create the array of band bounds self.theta_bands = np.linspace(0.0, np.pi / 2.0, self.num_theta + 1, endpoint=True) # Broadcast the bounds against each other to make the 2D array of bands kb, tb = np.broadcast_arrays(self.k_bands[np.newaxis, :], self.theta_bands[:, np.newaxis]) # Pull out the start, end and centre of the bands in k, mu directions self.k_start = kb[1:, :-1].flatten() self.k_end = kb[1:, 1:].flatten() self.k_center = 0.5 * (self.k_end + self.k_start) self.theta_start = tb[:-1, 1:].flatten() self.theta_end = tb[1:, 1:].flatten() self.theta_center = 0.5 * (self.theta_end + self.theta_start) bounds = zip(self.k_start, self.k_end, self.theta_start, self.theta_end) # Make a list of functions of the band window functions self.band_func = [ bandfunc_2d_polar(*bound) for bound in bounds ] # Create a list of functions of the band power functions if self.unit_bands: # Need slightly awkward double lambda because of loop closure scaling. self.band_pk = [ (lambda bandt: (lambda k, mu: cr.ps_vv(k) * bandt(k, mu)))(band) for band in self.band_func] self.band_power = np.ones_like(self.k_start) else: self.band_pk = self.band_func self.band_power = cr.ps_vv(self.k_center) elif self.bandtype == 'cartesian': # Broadcast the bounds against each other to make the 2D array of bands kparb, kperpb = np.broadcast_arrays(self.kpar_bands[np.newaxis, :], self.kperp_bands[:, np.newaxis]) # Pull out the start, end and centre of the bands in k, mu directions self.kpar_start = kparb[1:, :-1].flatten() self.kpar_end = kparb[1:, 1:].flatten() self.kpar_center = 0.5 * (self.kpar_end + self.kpar_start) self.kperp_start = kperpb[:-1, 1:].flatten() self.kperp_end = kperpb[1:, 1:].flatten() self.kperp_center = 0.5 * (self.kperp_end + self.kperp_start) bounds = zip(self.kpar_start, self.kpar_end, self.kperp_start, self.kperp_end) self.k_center = (self.kpar_center**2 + self.kperp_center**2)**0.5 # Make a list of functions of the band window functions self.band_func = [ bandfunc_2d_cart(*bound) for bound in bounds ] else: raise Exception('Bandtype %s is not supported.' % self.bandtype) # Create a list of functions of the band power functions if self.unit_bands: # Need slightly awkward double lambda because of loop closure scaling. self.band_pk = [ (lambda bandt: (lambda k, mu: cr.ps_vv(k) * bandt(k, mu)))(band) for band in self.band_func] self.band_power = np.ones_like(self.k_center) else: self.band_pk = self.band_func self.band_power = cr.ps_vv(self.k_center) # Use new parallel map to speed up computaiton of bands if self.clarray is None: self.make_clzz_array() print "Done."
def __init__(self): self.cr = corr21cm.Corr21cm()
import numpy as np from scipy.ndimage import gaussian_filter from scipy import constants as const import h5py from cora.signal import corr21cm import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt corr = corr21cm.Corr21cm() corr.x_width = 20.0 # degree corr.y_width = 20.0 # degree corr.x_num = 256 corr.y_num = 256 corr.nu_lower = 700.0 # MHz corr.nu_upper = 800.0 # MHz corr.nu_num = 256 freq0 = 1420.4 # MHz z1 = freq0 / corr.nu_upper - 1.0 # lower redshifts z2 = freq0 / corr.nu_lower - 1.0 # higher redshifts k = np.linspace(0.01, 2, 1000) # Mpc^-1 Pk1d = corr.powerspectrum_1D(k, z1, z2, 256) print Pk1d.shape # plot Pk1d plt.figure() plt.loglog(k, 1.0e6 * Pk1d) plt.savefig('cora_pk1d.png')
from astropy import units import pandas as pd from matplotlib.colors import LogNorm import IPython import os, sys import numpy as np import matplotlib.pyplot as plt from drift.core.manager import ProductManager import IPython from IPython import get_ipython cosmo = FlatLambdaCDM(H0=70, Om0=0.3) from cora.signal import corr21cm cr = corr21cm.Corr21cm() from drift.core.psestimation import decorrelate_ps_file def fg_wedge(kperp, z=1.33, theta=5 * units.deg): return theta.to('rad').value * (cosmo.comoving_distance(z) * cosmo.H(z) / consts.c / (1 + z)).to('').value * kperp def plot_power_spectrum(measured_fname, prod_fname, label=None): with h5py.File(measured_fname, 'r') as fil: pa = fil['powerspectrum'][()] err = fil['fisher'][()] with h5py.File(prod_fname, 'r') as fil: kpar = fil['kpar_center'][()]