Exemple #1
0
 def curvelens(cmb, phi, nside):
     alm_cmb = hp.map2alm(cmb)
     alm_phi = hp.map2alm(phi)
     grad_alm_cmb = hp.alm2map_der1(alm_cmb, nside)
     grad_alm_phi = hp.alm2map_der1(alm_phi, nside)
     lensedmap = cmb + grad_alm_phi * grad_alm_cmb
     return lensedmap
Exemple #2
0
def map_derivatives(m):
    """
    Compute the derivatives of a map
    """
    npix = m.size
    nside = hp.npix2nside(npix)

    # Take the spherical Fourier transform of the map
    alm = hp.map2alm(m, iter=1)

    # Convert back to real space and also get the first derivatives
    m_map, der_theta_map, der_phi_map = hp.alm2map_der1(alm, nside)

    # Fourier transform the first derivatives
    der_theta_alm = hp.map2alm(der_theta_map, iter=1)
    der_phi_alm = hp.map2alm(der_phi_map, iter=1)

    # Convert the first derivatives back to real space and also get the second derivatives
    der_theta_map2, der2_theta2_map, der2_theta_phi_map = hp.alm2map_der1(
        der_theta_alm, nside)
    der_phi_map2, der2_phi_theta_map, der2_phi2_map = hp.alm2map_der1(
        der_phi_alm, nside)

    # return all
    return [
        der_theta_map, der_phi_map, der2_theta2_map, der2_phi2_map,
        der2_theta_phi_map
    ]
Exemple #3
0
    def test_der1(self):
        """ compare output of alm2map_der1 with the equivalent spin-1 transform using alm2map_spin """
        
        m, dt_der1, dp_der1 = hp.alm2map_der1( self.almg, self.nside )

        alm_spin            = hp.almxfl( self.almg, np.array( [np.sqrt(l*(l+1.)) for l in six.moves.xrange(0,self.lmax+1)] ), inplace=False )
        dt_spin, dp_spin    = hp.alm2map_spin( [alm_spin, alm_spin*0.], self.nside, 1, self.lmax )

        np.testing.assert_array_almost_equal( dt_der1, dt_spin, decimal=8)
        np.testing.assert_array_almost_equal( dp_der1, dp_spin, decimal=8)
Exemple #4
0
    def evaluate_gradient(self):
        """Evaluate the 2D gradient of the CMB map.
		
		Evaluate the 2D gradient of the original input map across 
		the sky. This method uses the ``healpy`` function that returns 
		d/dtheta and d/dphi/sin(theta).

		"""
        alm = hp.map2alm(self.map_in)
        m, self.map_dtheta, self.map_dphi = hp.alm2map_der1(
            alm, self._NSIDE_small)
        del m, alm
Exemple #5
0
    def compute_intensity_derivatives(self, fromalm=False):
        """
        Compute derivatives of the input temperature map (healpix).
        Not updated for dichroic for the moment.

        Parameters
        ----------
        fromalm : bool, optional
            If True, loads alm file from disk instead of fourier
            transform the input map. Automatically turns True if you input
            alm files. False otherwise.

        Examples
        ----------
        >>> filename = 's4cmb/data/test_data_set_lensedCls.dat'
        >>> hpmap = HealpixFitsMap(input_filename=filename, fwhm_in=3.5,
        ...     nside_in=16, compute_derivatives=True, map_seed=489237)
        >>> hasattr(hpmap, 'dIdp')
        True

        """
        if fromalm:
            alm = hp.read_alm(self.input_filename[0])
        else:
            alm = hp.map2alm(self.I, self.lmax)

        # lmax = hp.Alm.getlmax(alm.size)
        if "T1" in self.derivatives_type:
            junk, self.dIdt, self.dIdp = hp.alm2map_der1(
                alm, self.nside_in, self.lmax)
        else:
            # computes first and second derivative as derivatives of spin-1
            # transform of a scalar field with _1Elm=sqrt(l(l+1))Ilm _1Blm=0
            l = np.arange(self.lmax + 1)
            grad = np.sqrt(l * (l + 1))
            curl = np.zeros_like(alm)
            dervs = alm2map_spin_der1([hp.almxfl(alm, grad), curl],
                                      self.nside_in, 1)
            self.dIdt = dervs[0][0]
            self.dIdp = dervs[0][1]
            self.d2Id2t = dervs[1][0]
            self.d2Id2p = dervs[2][1]
            self.d2Idpdt = dervs[2][0]
Exemple #6
0
 def gmfs(self, input_map):
     """calculates mask-edge-trimed MFs for a single field"""
     _alm = hp.map2alm(input_map)
     _map, _Dthe, _Dphi = hp.alm2map_der1(_alm, self._nside)
     #_Dthethe = self.d11(_Dthe)
     #_Dthephi = self.d12(_Dphi)
     #_Dphiphi = self.d22(_Dthe,_Dphi)
     #v2_num = (2.*_Dthe*_Dphi*_Dthephi-(_Dthe**2)*_Dphiphi-(_Dphi**2)*_Dthethe)
     v2_denom = (_Dthe**2 + _Dphi**2)
     v0 = np.zeros(self._nlv)
     #v1 = np.zeros_like(v0)
     #v2 = np.zeros_like(v0)
     for i in range(self._nlv):
         tmp_sigmoid = self.sigmoid(input_map - self._levels[i],
                                    self._width)
         v0[i] = np.sum(tmp_sigmoid[self._mask]) / self._neff
         #tmp_delta = self.dirac(input_map-self._levels[i],self._width)
         #v1[i] = 0.25*np.sum( (tmp_delta*np.sqrt(v2_denom))[self._mask>0] )/self._neff
         #v2[i] = (0.5/np.pi)*np.sum( (tmp_delta*v2_num/v2_denom)[self._mask>0] )/self._neff
     return v0  #(v0, v1, v2)
  pix = hp.vec2pix(NSIDE, x_red, y_red, z_red)

  # Transverse momenta for each halo in chi-bin:
  theta, phi = hp.vec2ang(np.column_stack((x_red, y_red, z_red))) # in radians
  v_theta = np.cos(theta)*np.cos(phi)*vx + np.cos(theta)*np.sin(phi)*vy - np.sin(theta)*vz
  v_phi = -np.sin(phi)*vx + np.cos(phi)*vy

  # Map of transverse momentum, velocity fields
  p_theta_map = np.zeros((hp.nside2npix(NSIDE)), dtype=np.float)
  np.add.at(p_theta_map, pix, M/M_avg*v_theta)

  p_phi_map = np.zeros((hp.nside2npix(NSIDE)), dtype=np.float)
  np.add.at(p_phi_map, pix, M/M_avg*v_phi)

  # Angular gradient of transverse momentum field
  dthetaPtheta = hp.alm2map_der1(hp.map2alm(p_theta_map), NSIDE)[1]
  dphiPphi = hp.alm2map_der1(hp.map2alm(p_phi_map), NSIDE)[2]
  dP = dthetaPtheta + dphiPphi # \grad*P = "dP"
  # Inverse angular laplacian of transverse momentum ~ 1/grad * P
  dPlm = hp.map2alm(dP)
  lmax = hp.Alm.getlmax(dPlm.size)
  ls = np.arange(lmax)
  invlap = 1.0 / ls / (ls + 1.0)
  invlap[0] = 0.0 # zero monopole (?)
  dPlm = hp.almxfl(dPlm, invlap)
  
  # contribution to map from each radial bin
  print("Computing dT_in_bin.")
  # total map is this integrated in all bins * geometric factors...
  chi_bin = (bin_lower_chi+bin_upper_chi)/2.0
  z_bin = zofchi(chi_bin)
Exemple #8
0
pyplot.savefig("dif_pixwin.png")

#%%
"""
STEP 5: Spherical harmonic transforms: alms --> map + 1st derivatives
"""

mapa1 = hp.synfast(Cls,
                   512,
                   alm=True,
                   pol=False,
                   pixwin=False,
                   fwhm=np.deg2rad(0.))
alms = mapa1[1]

mapa2 = hp.alm2map_der1(alms, 512)

len(mapa2)

hp.mollview(mapa1[0], title="mapa1[0]")
pyplot.savefig("mapa1_ampl_col0.png")

hp.mollview(mapa2[0], title="mapa2[0]")
pyplot.savefig("mapa2_ampl_col0.png")

hp.mollview(mapa2[1], title="der. theta")
pyplot.savefig("mapa2_derTheta_col1.png")

hp.mollview(mapa2[2], title="der. phi")
pyplot.savefig("mapa2_derPhi_col2.png")
Exemple #9
0
    def gentemp(self):
        """Let's fit the pair diff map with the healpix map. """

        print('Sampling off template map...')
        sys.stdout.flush()

        # Get regression template, which is the gnomonic projection of the input
        # healpix map around every pixel center
        nt = self.ra.size

        for k in range(nt):

            # Construct T template
            #xs = [4.0, 40.0] # x size in degrees
            #ys = [4.0, 40.0] # y size in degrees
            #res = [0.1, 0.25] # resolution in degrees
            xs = 20.0
            ys = 20.0
            res = 0.1
            xx, yy, ra, dec, tempT = gnomproj(self.hmaptemp[0],
                                              self.ra[k],
                                              self.dec[k],
                                              xs,
                                              ys,
                                              res,
                                              rot=self.alpha[k],
                                              Full=True)
            tempT = np.ravel(tempT)

            if k == 0:
                XT = np.zeros((nt, tempT.size))

            XT[k] = tempT

        # Construct pairdiff predictor from Q and U template maps
        tempQ = hp.get_interp_val(self.hmaptemp[1],
                                  self.ra,
                                  self.dec,
                                  lonlat=True)
        tempU = hp.get_interp_val(self.hmaptemp[2],
                                  self.ra,
                                  self.dec,
                                  lonlat=True)
        chiA = self.alpha
        chiB = chiA + 90
        siga = tempQ * cosd(2 * chiA) + tempU * sind(2 * chiA)
        sigb = tempQ * cosd(2 * chiB) + tempU * sind(2 * chiB)
        pairdiff = (siga - sigb) / 2.
        pairdiff = pairdiff.reshape(nt, 1)
        nQU = 1

        # Deriv map templates
        Tsm = hp.smoothing(self.hmaptemp[0], fwhm=30.0 / 60. * np.pi / 180)
        alm = hp.map2alm(Tsm)
        dum, dth, dph = hp.alm2map_der1(alm, self.tempNside)
        dth_alm = hp.map2alm(dth)
        dph_alm = hp.map2alm(dph)
        dth, dth2, dthdph = hp.alm2map_der1(dth_alm, self.tempNside)
        dum, dum, dph2 = hp.alm2map_der1(dph_alm, self.tempNside)

        # Sample off deriv map templates
        Tsm = hp.get_interp_val(Tsm, self.ra, self.dec, lonlat=True)
        dth = hp.get_interp_val(dth, self.ra, self.dec, lonlat=True)
        dph = hp.get_interp_val(dph, self.ra, self.dec, lonlat=True)
        dth2 = hp.get_interp_val(dth2, self.ra, self.dec, lonlat=True)
        dph2 = hp.get_interp_val(dph2, self.ra, self.dec, lonlat=True)
        dthdph = hp.get_interp_val(dthdph, self.ra, self.dec, lonlat=True)

        Xderiv = np.zeros((nt, 6))
        Xderiv[:, 0] = Tsm  # Gain
        Xderiv[:, 1] = dth  # Diff point x
        Xderiv[:, 2] = dph  # Diff point y
        Xderiv[:, 3] = dph2 + dth2  # Diff bw
        Xderiv[:, 4] = dph2 - dth2  # Diff plus ellip
        Xderiv[:, 5] = 2 * dthdph  # Cross elip

        # Initialize regressor matrix
        self.X = np.concatenate((XT, Xderiv, pairdiff), axis=1)
        self.nT = tempT.size  # Number of T regressors
        self.tempxx = xx
        self.tempyy = yy