def mapmake_full(self, nside, mapname): def _make_alm(mi): print("Making %i" % mi) mmode = self.mmode(mi) sphmode = self.beamtransfer.project_vector_telescope_to_sky(mi, mmode) return sphmode alm_list = mpiutil.parallel_map(_make_alm, list(range(self.telescope.mmax + 1))) if mpiutil.rank0: alm = np.zeros( ( self.telescope.nfreq, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1, ), dtype=np.complex128, ) for mi in range(self.telescope.mmax + 1): alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(self.output_directory + "/" + mapname, "w") as f: f.create_dataset("/map", data=skymap) mpiutil.barrier()
def mapmake_kl(self, nside, mapname, wiener=False): mapfile = self.output_directory + "/" + mapname if os.path.exists(mapfile): if mpiutil.rank0: print("File %s exists. Skipping...") return kl = self.manager.kltransforms[self.klname] if not kl.inverse: raise Exception("Need the inverse to make a meaningful map.") def _make_alm(mi): print("Making %i" % mi) klmode = self.mmode_kl(mi) if wiener: evals = kl.evals_m(mi, self.klthreshold) if evals is not None: klmode *= evals / (1.0 + evals) isvdmode = kl.project_vector_kl_to_svd( mi, klmode, threshold=self.klthreshold ) sphmode = self.beamtransfer.project_vector_svd_to_sky(mi, isvdmode) return sphmode alm_list = mpiutil.parallel_map(_make_alm, list(range(self.telescope.mmax + 1))) if mpiutil.rank0: alm = np.zeros( ( self.telescope.nfreq, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1, ), dtype=np.complex128, ) # Determine whether to use m=0 or not mlist = list(range(1 if self.no_m_zero else 0, self.telescope.mmax + 1)) for mi in mlist: alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(mapfile, "w") as f: f.create_dataset("/map", data=skymap) mpiutil.barrier()
def mapmake_svd(self, nside, mapname): self.generate_mmodes_svd() def _make_alm(mi): svdmode = self.mmode_svd(mi) sphmode = self.beamtransfer.project_vector_svd_to_sky(mi, svdmode) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1)) if mpiutil.rank0: alm = np.zeros((self.telescope.nfreq, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) for mi in range(self.telescope.mmax + 1): alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(self.output_directory + '/' + mapname, 'w') as f: f.create_dataset('/map', data=skymap) f.attrs['frequency'] = self.beamtransfer.telescope.frequencies f.attrs['polarization'] = np.array( ['I', 'Q', 'U', 'V'])[:self.beamtransfer.telescope.num_pol_sky] mpiutil.barrier()
def mapmake_svd(self, nside, mapname): self.generate_mmodes_svd() def _make_alm(mi): svdmode = self.mmode_svd(mi) sphmode = self.beamtransfer.project_vector_svd_to_sky(mi, svdmode) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1)) if mpiutil.rank0: alm = np.zeros((self.telescope.nfreq, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) for mi in range(self.telescope.mmax + 1): alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(self.output_directory + '/' + mapname, 'w') as f: f.create_dataset('/map', data=skymap) mpiutil.barrier()
def generate_map(args): import os import numpy as np import h5py import healpy from cora.util import hputil with h5py.File(args.in_file, 'r') as f: in_alm = f['alm'][...] lmax = in_alm.shape[-2] - 1 mmax = in_alm.shape[-1] - 1 print 'lmax = %d\nmmax = %d' % (lmax, mmax) lmin_cut = max(args.lmin, 0) lmax_cut = min(args.lmax, lmax) if args.lmax is not None else lmax mmin_cut = max(args.mmin, 0) mmax_cut = min(args.mmax, mmax) if args.mmax is not None else mmax if args.include: print 'Include lmin_cut = %d, lmax_cut = %d, mmin_cut = %d, mmax_cut = %d' % (lmin_cut, lmax_cut, mmin_cut, mmax_cut) temp_alm = np.zeros_like(in_alm, dtype=in_alm.dtype) temp_alm[:, :, lmin_cut:(lmax_cut+1), mmin_cut:(mmax_cut+1)] = in_alm[:, :, lmin_cut:(lmax_cut+1), mmin_cut:(mmax_cut+1)] else: print 'Exclude lmin_cut = %d, lmax_cut = %d, mmin_cut = %d, mmax_cut = %d' % (lmin_cut, lmax_cut, mmin_cut, mmax_cut) temp_alm = in_alm temp_alm[:, :, lmin_cut:(lmax_cut+1), mmin_cut:(mmax_cut+1)] = 0.0 out_map = hputil.sphtrans_inv_sky(temp_alm, args.nside) incl = 'incl' if args.include else 'excl' out_file = args.out_file or ('lm_cut_%d_%d_%d_%d_%s_' % (lmin_cut, lmax_cut, mmin_cut, mmax_cut, incl) + os.path.basename(args.in_file).replace('alm', 'map')) with h5py.File(out_file, 'w') as f: f.create_dataset('map', data=out_map)
def mapmake_kl(self, nside, mapname, wiener=False): mapfile = self.output_directory + '/' + mapname if os.path.exists(mapfile): if mpiutil.rank0: print "File %s exists. Skipping..." return kl = self.manager.kltransforms[self.klname] if not kl.inverse: raise Exception("Need the inverse to make a meaningful map.") def _make_alm(mi): print "Making %i" % mi klmode = self.mmode_kl(mi) if wiener: evals = kl.evals_m(mi, self.klthreshold) if evals is not None: klmode *= (evals / (1.0 + evals)) isvdmode = kl.project_vector_kl_to_svd(mi, klmode, threshold=self.klthreshold) sphmode = self.beamtransfer.project_vector_svd_to_sky(mi, isvdmode) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1)) if mpiutil.rank0: alm = np.zeros((self.telescope.nfreq, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) # Determine whether to use m=0 or not mlist = range(1 if self.no_m_zero else 0, self.telescope.mmax + 1) for mi in mlist: alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(mapfile, 'w') as f: f.create_dataset('/map', data=skymap) f.attrs['frequency'] = self.beamtransfer.telescope.frequencies f.attrs['polarization'] = np.array(['I', 'Q', 'U', 'V'])[:self.beamtransfer.telescope.num_pol_sky] mpiutil.barrier()
def mapmake_kl(self, nside, mapname, wiener=False): mapfile = self.output_directory + '/' + mapname if os.path.exists(mapfile): if mpiutil.rank0: print "File %s exists. Skipping..." return kl = self.manager.kltransforms[self.klname] if not kl.inverse: raise Exception("Need the inverse to make a meaningful map.") def _make_alm(mi): print "Making %i" % mi klmode = self.mmode_kl(mi) if wiener: evals = kl.evals_m(mi, self.klthreshold) if evals is not None: klmode *= (evals / (1.0 + evals)) isvdmode = kl.project_vector_kl_to_svd(mi, klmode, threshold=self.klthreshold) sphmode = self.beamtransfer.project_vector_svd_to_sky(mi, isvdmode) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1), root=0, method='rand') if mpiutil.rank0: alm = np.zeros((self.telescope.nfreq, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) # Determine whether to use m=0 or not mlist = range(1 if self.no_m_zero else 0, self.telescope.mmax + 1) for mi in mlist: alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(mapfile, 'w') as f: f.create_dataset('/map', data=skymap) f.attrs['frequency'] = self.beamtransfer.telescope.frequencies f.attrs['polarization'] = np.array(['I', 'Q', 'U', 'V'])[:self.beamtransfer.telescope.num_pol_sky] mpiutil.barrier()
def mapmake_full(self, nside, mapname, nbin=None, dirty=False, method='svd', normalize=True, threshold=1.0e3): nfreq = self.telescope.nfreq if nbin is not None and (nbin <= 0 or nbin >= nfreq): # invalid nbin nbin = None def _make_alm(mi): print "Making %i" % mi mmode = self.mmode(mi) if dirty: sphmode = self.beamtransfer.project_vector_backward_dirty(mi, mmode, nbin, normalize, threshold) else: if method == 'svd': sphmode = self.beamtransfer.project_vector_telescope_to_sky(mi, mmode, nbin) elif method == 'tk': sphmode = self.beamtransfer.project_vector_telescope_to_sky_tk(mi, mmode, nbin) else: raise ValueError('Unknown map-making method %s' % method) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1)) if mpiutil.rank0: # get center freq of each bin if nbin is not None: n, s, e = mpiutil.split_m(nfreq, nbin) cfreqs = np.array([ self.beamtransfer.telescope.frequencies[(s[i]+e[i])/2] for i in range(nbin) ]) else: nbin = nfreq cfreqs = self.beamtransfer.telescope.frequencies alm = np.zeros((nbin, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) for mi in range(self.telescope.mmax + 1): alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(self.output_directory + '/' + mapname, 'w') as f: f.create_dataset('/map', data=skymap) f.attrs['frequency'] = cfreqs f.attrs['polarization'] = np.array(['I', 'Q', 'U', 'V'])[:self.beamtransfer.telescope.num_pol_sky] mpiutil.barrier()
def _write_map_from_almarray(almp, filename, attrs=None): if mpiutil.rank0: almp = np.squeeze(np.transpose(almp, axes=(2, 1, 3, 0))) almf = np.zeros((almp.shape[0], almp.shape[1], almp.shape[1]), dtype=np.complex128) almf[:, :, :almp.shape[2]] = almp pmap = hputil.sphtrans_inv_sky(almf, self.nside) f = h5py.File(filename, 'w') if attrs is not None: for key, val in attrs.items(): f.attrs[repr(key)] = val f.create_dataset('/map', data=pmap) f.close()
def mkfullsky(corr, nside, alms=False): """Construct a set of correlated Healpix maps. Make a set of full sky gaussian random fields, given the correlation structure. Useful for constructing a set of different redshift slices. Parameters ---------- corr : np.ndarray (lmax+1, numz, numz) The correlation matrix :math:`C_l(z, z')`. nside : integer The resolution of the Healpix maps. alms : boolean, optional If True return the alms instead of the sky maps. Returns ------- hpmaps : np.ndarray (numz, npix) The Healpix maps. hpmaps[i] is the i'th map. """ numz = corr.shape[1] maxl = corr.shape[0] - 1 if corr.shape[2] != numz: raise Exception("Correlation matrix is incorrect shape.") alm_array = np.zeros((numz, 1, maxl + 1, maxl + 1), dtype=np.complex128) # Generate gaussian deviates and transform to have correct correlation # structure for l in range(maxl + 1): # Add in a small diagonal to try and ensure positive definiteness cmax = corr[l].diagonal().max() * 1e-14 corrm = corr[l] + np.identity(numz) * cmax trans = nputil.matrix_root_manynull(corrm, truncate=False) gaussvars = nputil.complex_std_normal((numz, l + 1)) alm_array[:, 0, l, :(l + 1)] = np.dot(trans, gaussvars) if alms: return alm_array # Perform the spherical harmonic transform for each z sky = hputil.sphtrans_inv_sky(alm_array, nside) sky = sky[:, 0] return sky
def _write_map_from_almarray(almp, filename, attrs=None): if mpiutil.rank0: almp = np.squeeze(np.transpose(almp, axes=(2, 1, 3, 0))) almf = np.zeros( (almp.shape[0], almp.shape[1], almp.shape[1]), dtype=np.complex128) almf[:, :, :almp.shape[2]] = almp pmap = hputil.sphtrans_inv_sky(almf, self.nside) f = h5py.File(filename, 'w') if attrs is not None: for key, val in attrs.items(): f.attrs[repr(key)] = val f.create_dataset('/map', data=pmap) f.close()
def gaussianfg(ctx): """Generate a full-sky Gaussian random field for synchrotron emission. """ import numpy as np from cora.core import skysim from cora.util import hputil from cora.foreground import galaxy fsyn = galaxy.FullSkySynchrotron() fpol = galaxy.FullSkyPolarisedSynchrotron() # Set frequency parameters fsyn.frequencies = ctx.obj.freq nfreq = len(fsyn.frequencies) nside = ctx.obj.nside lmax = 3 * nside npol = 4 if ctx.obj.full_pol else 1 cv_fg = np.zeros((lmax + 1, npol, nfreq, npol, nfreq)) cv_fg[:, 0, :, 0, :] = skysim.clarray(fsyn.angular_powerspectrum, lmax, fsyn.nu_pixels) if ctx.obj.full_pol: cv_fg[:, 1, :, 1, :] = skysim.clarray(fpol.angular_powerspectrum, lmax, fsyn.nu_pixels) cv_fg[:, 2, :, 2, :] = skysim.clarray(fpol.angular_powerspectrum, lmax, fsyn.nu_pixels) cv_fg = cv_fg.reshape(lmax + 1, npol * nfreq, npol * nfreq) alms = skysim.mkfullsky(cv_fg, nside, alms=True).reshape(npol, nfreq, lmax + 1, lmax + 1) alms = alms.transpose((1, 0, 2, 3)) maps = hputil.sphtrans_inv_sky(alms, nside) write_map(ctx.obj.filename, maps, fsyn.frequencies, ctx.obj.freq_width, ctx.obj.include_pol)
def generate_map(args): import numpy as np import h5py import healpy from cora.util import hputil with h5py.File(args.in_map, 'r') as f: in_map = f['map'][...] nside = healpy.pixelfunc.get_nside(in_map[0]) in_alm = hputil.sphtrans_sky(in_map) # lmax = in_alm.shape[-2] - 1 mmax = in_alm.shape[-1] - 1 print 'mmax = %d' % mmax # l_cut = min(args.l_cut, lmax) # m_cut = min(args.m_cut, mmax) m_index = min(args.m_index, mmax) temp_alm = np.zeros_like(in_alm, dtype=in_alm.dtype) temp_alm[:, :, :, m_index] = in_alm[:, :, :, m_index] out_map = hputil.sphtrans_inv_sky(temp_alm, nside) out_file = args.out_file or ('m_slice_%d_' % m_index + args.in_map) with h5py.File(out_file, 'w') as f: f.create_dataset('map', data=out_map)
def generate_map(args): import os import numpy as np import h5py import healpy from cora.util import hputil with h5py.File(args.in_map, 'r') as f: in_map = f['map'][...] nside = healpy.pixelfunc.get_nside(in_map[0]) in_alm = hputil.sphtrans_sky(in_map, lmax=args.maxl) lmax = in_alm.shape[-2] - 1 mmax = in_alm.shape[-1] - 1 print 'lmax = %d\nmmax = %d' % (lmax, mmax) lmin_cut = max(args.lmin, 0) lmax_cut = min(args.lmax, lmax) if args.lmax is not None else lmax mmin_cut = max(args.mmin, 0) mmax_cut = min(args.mmax, mmax) if args.mmax is not None else mmax temp_alm = np.zeros_like(in_alm, dtype=in_alm.dtype) temp_alm[:, :, lmin_cut:(lmax_cut+1), mmin_cut:(mmax_cut+1)] = in_alm[:, :, lmin_cut:(lmax_cut+1), mmin_cut:(mmax_cut+1)] out_map = hputil.sphtrans_inv_sky(temp_alm, nside) out_file = args.out_file or ('lm_cut_%d_%d_%d_%d_' % (lmin_cut, lmax_cut, mmin_cut, mmax_cut) + os.path.basename(args.in_map)) with h5py.File(out_file, 'w') as f: f.create_dataset('map', data=out_map)
def mapmake_full(self, nside, mapname, nbin=None, dirty=False, method='svd', normalize=True, threshold=1.0e3): nfreq = self.telescope.nfreq if nbin is not None and (nbin <= 0 or nbin >= nfreq): # invalid nbin nbin = None def _make_alm(mi): print "Making %i" % mi mmode = self.mmode(mi) if dirty: sphmode = self.beamtransfer.project_vector_backward_dirty( mi, mmode, nbin, normalize, threshold) else: if method == 'svd': sphmode = self.beamtransfer.project_vector_telescope_to_sky( mi, mmode, nbin) elif method == 'tk': sphmode = self.beamtransfer.project_vector_telescope_to_sky_tk( mi, mmode, nbin) else: raise ValueError('Unknown map-making method %s' % method) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1)) if mpiutil.rank0: # get center freq of each bin if nbin is not None: n, s, e = mpiutil.split_m(nfreq, nbin) cfreqs = np.array([ self.beamtransfer.telescope.frequencies[(s[i] + e[i]) / 2] for i in range(nbin) ]) else: nbin = nfreq cfreqs = self.beamtransfer.telescope.frequencies alm = np.zeros((nbin, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) for mi in range(self.telescope.mmax + 1): alm[..., mi] = alm_list[mi] skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(self.output_directory + '/' + mapname, 'w') as f: f.create_dataset('/map', data=skymap) f.attrs['frequency'] = cfreqs f.attrs['polarization'] = np.array( ['I', 'Q', 'U', 'V'])[:self.beamtransfer.telescope.num_pol_sky] mpiutil.barrier()
print "Gather results onto root process" p_all = mpiutil.world.gather(mproj, root=0) # Save out results if mpiutil.rank0: nalm = np.zeros((cyl.nfreq, cyl.lmax+1, cyl.lmax+1), dtype=np.complex128) print "Combining results." for p_process in p_all: for mi, proj in p_process: if proj is None: continue nalm[:, :, mi] = proj.reshape(nalm.shape[:-1]) print "Transforming onto sky." noise_map = hputil.sphtrans_inv_sky(nalm, nside) print "Saving file." f = h5py.File(outfile, 'w') f.create_dataset('/map', data=noise_map) f.close()
def process(self, mmodes): """Make a map from the given m-modes. Parameters ---------- mmodes : containers.MModes Returns ------- map : containers.Map """ from cora.util import hputil # Fetch various properties bt = self.beamtransfer lmax = bt.telescope.lmax mmax = min(bt.telescope.mmax, len(mmodes.index_map['m']) - 1) nfreq = len(mmodes.index_map['freq']) # bt.telescope.nfreq def find_key(key_list, key): try: return map(tuple, list(key_list)).index(tuple(key)) except TypeError: return list(key_list).index(key) except ValueError: return None # Figure out mapping between the frequencies bt_freq = self.beamtransfer.telescope.frequencies mm_freq = mmodes.index_map['freq']['centre'] freq_ind = [find_key(bt_freq, mf) for mf in mm_freq] # Trim off excess m-modes mmodes.redistribute('freq') m_array = mmodes.vis[:(mmax + 1)] m_array = m_array.redistribute(axis=0) m_weight = mmodes.weight[:(mmax + 1)] m_weight = m_weight.redistribute(axis=0) # Create array to store alms in. alm = mpiarray.MPIArray((nfreq, 4, lmax + 1, mmax + 1), axis=3, dtype=np.complex128, comm=mmodes.comm) alm[:] = 0.0 # Loop over all m's and solve from m-mode visibilities to alms. for mi, m in m_array.enumerate(axis=0): for fi in range(nfreq): v = m_array[mi, :, fi].view(np.ndarray) a = alm[fi, ..., mi].view(np.ndarray) Ni = m_weight[mi, :, fi].view(np.ndarray) a[:] = self._solve_m(m, fi, v, Ni) # Redistribute back over frequency alm = alm.redistribute(axis=0) # Copy into square alm array for transform almt = mpiarray.MPIArray((nfreq, 4, lmax + 1, lmax + 1), dtype=np.complex128, axis=0, comm=mmodes.comm) almt[..., :(mmax + 1)] = alm alm = almt # Perform spherical harmonic transform to map space maps = hputil.sphtrans_inv_sky(alm, self.nside) maps = mpiarray.MPIArray.wrap(maps, axis=0) m = containers.Map(nside=self.nside, axes_from=mmodes, comm=mmodes.comm) m.map[:] = maps return m
# Save out results if mpiutil.rank0: balm = np.zeros_like(alm) dalm = np.zeros_like(alm) print "Combining results." for p_process in p_all: for mi, proj in p_process: if proj == None: continue dalm[:, :, mi] = proj[0].reshape(dalm.shape[:-1]) balm[:, :, mi] = proj[1].reshape(balm.shape[:-1]) print "Transforming onto sky." beamp_map = hputil.sphtrans_inv_sky(balm, nside) dirty_map = hputil.sphtrans_inv_sky(dalm, nside) print "Saving file." f = h5py.File(args.outfile, 'w') f.create_dataset('/beamproj', data=beamp_map) f.create_dataset('/dirtymap', data=dirty_map) f.close()
if mpiutil.rank0: print("Gather results onto root process") p_all = mpiutil.world.gather(mproj, root=0) # Save out results if mpiutil.rank0: palm = np.zeros_like(alm) print("Combining results.") for p_process in p_all: for mi, proj in p_process: if proj is None: continue palm[:, :, mi] = proj.reshape(palm.shape[:-1]) print("Transforming onto sky.") proj_map = hputil.sphtrans_inv_sky(palm, nside) print("Saving file.") f = h5py.File(args.outfile, "w") f.attrs["threshold"] = cut f.create_dataset("/klproj", data=proj_map) f.close()
if mpiutil.rank0: print("Gather results onto root process") p_all = mpiutil.world.gather(mproj, root=0) # Save out results if mpiutil.rank0: nalm = np.zeros((cyl.nfreq, cyl.lmax + 1, cyl.lmax + 1), dtype=np.complex128) print("Combining results.") for p_process in p_all: for mi, proj in p_process: if proj is None: continue nalm[:, :, mi] = proj.reshape(nalm.shape[:-1]) print("Transforming onto sky.") noise_map = hputil.sphtrans_inv_sky(nalm, nside) print("Saving file.") f = h5py.File(outfile, "w") f.create_dataset("/map", data=noise_map) f.close()
def mapmake_full(self, nside, mapname, nbin=None, dirty=False, method='svd', normalize=True, threshold=1.0e3, eps=0.01, correct_order=0, prior_map_file=None): nfreq = self.telescope.nfreq if nbin is None: nbin = nfreq else: if (nbin < 1 or nbin > nfreq): # invalid nbin nbin = nfreq else: nbin = int(nbin) if prior_map_file is not None: # read in the prior sky map with h5py.File(prior_map_file, 'r') as f: prior_map = f['map'][:] # shape (nbin, npol, npix) # alm of the prior map alm0 = hputil.sphtrans_sky(prior_map, lmax=self.telescope.lmax).reshape(nbin, self.telescope.num_pol_sky, self.telescope.lmax+1, self.telescope.lmax+1) # shape (nbin, npol, lmax+1, lmax+1) else: alm0 = None def _make_alm(mi): print "Making %i" % mi mmode = self.mmode(mi) if dirty: sphmode = self.beamtransfer.project_vector_backward_dirty(mi, mmode, nbin, normalize, threshold) else: if method == 'svd': sphmode = self.beamtransfer.project_vector_telescope_to_sky(mi, mmode, nbin) elif method == 'tk': # sphmode = self.beamtransfer.project_vector_telescope_to_sky_tk(mi, mmode, nbin, eps=eps) mmode0 = alm0[:, :, :, mi] if alm0 is not None else None sphmode = self.beamtransfer.project_vector_telescope_to_sky_tk(mi, mmode, nbin, eps=eps, correct_order=correct_order, mmode0=mmode0) else: raise ValueError('Unknown map-making method %s' % method) return sphmode alm_list = mpiutil.parallel_map(_make_alm, range(self.telescope.mmax + 1), root=0, method='rand') if mpiutil.rank0: # get center freq of each bin n, s, e = mpiutil.split_m(nfreq, nbin) cfreqs = np.array([ self.beamtransfer.telescope.frequencies[(s[i]+e[i])/2] for i in range(nbin) ]) alm = np.zeros((nbin, self.telescope.num_pol_sky, self.telescope.lmax + 1, self.telescope.lmax + 1), dtype=np.complex128) mlist = range(1 if self.no_m_zero else 0, self.telescope.mmax + 1) for mi in mlist: alm[..., mi] = alm_list[mi] alm[:, :, 100:, 1] = 0 skymap = hputil.sphtrans_inv_sky(alm, nside) with h5py.File(self.output_directory + '/' + mapname, 'w') as f: f.create_dataset('/map', data=skymap) f.attrs['frequency'] = cfreqs f.attrs['polarization'] = np.array(['I', 'Q', 'U', 'V'])[:self.beamtransfer.telescope.num_pol_sky] mpiutil.barrier()
print "Gather results onto root process" p_all = mpiutil.world.gather(mproj, root=0) # Save out results if mpiutil.rank0: palm = np.zeros_like(alm) print "Combining results." for p_process in p_all: for mi, proj in p_process: if proj == None: continue palm[:, :, mi] = proj.reshape(palm.shape[:-1]) print "Transforming onto sky." proj_map = hputil.sphtrans_inv_sky(palm, nside) print "Saving file." f = h5py.File(args.outfile, 'w') f.attrs['threshold'] = cut f.create_dataset('/klproj', data=proj_map) f.close()
def process(self, mmodes): """Make a map from the given m-modes. Parameters ---------- mmodes : containers.MModes Returns ------- map : containers.Map """ from cora.util import hputil # Fetch various properties bt = self.beamtransfer lmax = bt.telescope.lmax mmax = min(bt.telescope.mmax, len(mmodes.index_map["m"]) - 1) nfreq = len(mmodes.index_map["freq"]) # bt.telescope.nfreq # Figure out mapping between the frequencies bt_freq = self.beamtransfer.telescope.frequencies mm_freq = mmodes.index_map["freq"]["centre"] freq_ind = tools.find_keys(bt_freq, mm_freq, require_match=True) # Trim off excess m-modes mmodes.redistribute("freq") m_array = mmodes.vis[:(mmax + 1)] m_array = m_array.redistribute(axis=0) m_weight = mmodes.weight[:(mmax + 1)] m_weight = m_weight.redistribute(axis=0) # Create array to store alms in. alm = mpiarray.MPIArray( (nfreq, 4, lmax + 1, mmax + 1), axis=3, dtype=np.complex128, comm=mmodes.comm, ) alm[:] = 0.0 # Loop over all m's and solve from m-mode visibilities to alms. for mi, m in m_array.enumerate(axis=0): self.log.debug("Processing m=%i (local %i/%i)", m, mi + 1, m_array.local_shape[0]) # Get and cache the beam transfer matrix, but trim off any l < m. # if self.bt_cache is None: # self.bt_cache = (m, bt.beam_m(m)) # self.log.debug("Cached beamtransfer for m=%i", m) for fi in range(nfreq): v = m_array[mi, :, fi].view(np.ndarray) a = alm[fi, ..., mi].view(np.ndarray) Ni = m_weight[mi, :, fi].view(np.ndarray) a[:] = self._solve_m(m, freq_ind[fi], v, Ni) self.bt_cache = None # Redistribute back over frequency alm = alm.redistribute(axis=0) # Copy into square alm array for transform almt = mpiarray.MPIArray( (nfreq, 4, lmax + 1, lmax + 1), dtype=np.complex128, axis=0, comm=mmodes.comm, ) almt[..., :(mmax + 1)] = alm alm = almt # Perform spherical harmonic transform to map space maps = hputil.sphtrans_inv_sky(alm, self.nside) maps = mpiarray.MPIArray.wrap(maps, axis=0) m = containers.Map(nside=self.nside, axes_from=mmodes, comm=mmodes.comm) m.map[:] = maps return m
p_all = mpiutil.world.gather(mproj, root=0) # Save out results if mpiutil.rank0: balm = np.zeros_like(alm) dalm = np.zeros_like(alm) print("Combining results.") for p_process in p_all: for mi, proj in p_process: if proj is None: continue dalm[:, :, mi] = proj[0].reshape(dalm.shape[:-1]) balm[:, :, mi] = proj[1].reshape(balm.shape[:-1]) print("Transforming onto sky.") beamp_map = hputil.sphtrans_inv_sky(balm, nside) dirty_map = hputil.sphtrans_inv_sky(dalm, nside) print("Saving file.") f = h5py.File(args.outfile, "w") f.create_dataset("/beamproj", data=beamp_map) f.create_dataset("/dirtymap", data=dirty_map) f.close()