Exemplo n.º 1
0
def angular_distance_from_mask(mask):
    """
    For each pixel of a Healpix map, return the smallest angular distance
    to a set of masked pixels (of value True), in degrees.

    Parameter
    ---------
    maskok : boolean Healpix map
        The Healpix mask that defines the set of masked pixels (of value True)
        whose smallest angular distance to each pixel of a Healpix map of same
        nside is computed.

    """
    nside = hp.npix2nside(len(mask))

    # get the list of pixels on the external border of the mask
    ip = np.arange(12*nside**2)[~mask]
    neigh = hp.get_all_neighbours(nside, ip)
    nn = np.unique(neigh.ravel())
    if nn[0] == -1:
        nn = nn[1:]
    nn = nn[mask[nn]]

    # get unit vectors for border and inner pixels
    vecpix_inner = np.array(hp.pix2vec(nside, ip))
    vecpix_outer = np.array(hp.pix2vec(nside, nn))

    # get angles between the border pixels and inner pixels
    cosang = np.dot(vecpix_inner.T, vecpix_outer)
    mapang = np.zeros(12*nside**2)
    mapang[~mask] = np.degrees(np.arccos(np.max(cosang, axis=1)))
    return mapang
Exemplo n.º 2
0
    def correlation(self, k, skymap, skymask):
        #{{{
        # en la version paralela hace un solo centro cada vez
        # que estra a esta funcion
        import numpy as np
        import healpy as hp
        import astropy.units as u
        import time
        import random

        random.seed((k + 42) * 3)
        # draw a set of nran pixel pairs
        x = np.array([random.random() for _ in range(self.nran * 2)])
        x = x * self.N_ma_IDs
        x = x.astype(int)
        x = x.reshape((-1, 2))

        # compute frequency
        coso = np.zeros(self.nran)
        tt = np.zeros(self.nran)
        for j, idxs in enumerate(x):
            v1 = hp.pix2vec(self.nside, self.masked_indices[idxs[0]])
            v2 = hp.pix2vec(self.nside, self.masked_indices[idxs[1]])
            coso[j] = np.dot(v1, v2)
            tt[j] = skymap.data[idxs[0]] * skymap.data[idxs[1]]

        H = np.histogram(coso, bins=self.breaks, weights=tt, density=False)[0]
        K = np.histogram(coso, bins=self.breaks, density=False)[0]

        return (H, K)
Exemplo n.º 3
0
def angular_distance_from_mask(mask):
    """
    For each pixel of a Healpix map, return the smallest angular distance
    to a set of masked pixels (of value True), in degrees.

    Parameter
    ---------
    maskok : boolean Healpix map
        The Healpix mask that defines the set of masked pixels (of value True)
        whose smallest angular distance to each pixel of a Healpix map of same
        nside is computed.

    """
    nside = hp.npix2nside(len(mask))

    # get the list of pixels on the external border of the mask
    ip = np.arange(12 * nside**2)[~mask]
    neigh = hp.get_all_neighbours(nside, ip)
    nn = np.unique(neigh.ravel())
    if nn[0] == -1:
        nn = nn[1:]
    nn = nn[mask[nn]]

    # get unit vectors for border and inner pixels
    vecpix_inner = np.array(hp.pix2vec(nside, ip))
    vecpix_outer = np.array(hp.pix2vec(nside, nn))

    # get angles between the border pixels and inner pixels
    cosang = np.dot(vecpix_inner.T, vecpix_outer)
    mapang = np.zeros(12 * nside**2)
    mapang[~mask] = np.degrees(np.arccos(np.max(cosang, axis=1)))
    return mapang
Exemplo n.º 4
0
def main_loop(ar_map, ar_map_unc, max_bin_angle, max_angular_separation, outer_disc_mean, outer_disc):
    ar_product = np.zeros(shape=num_bins)
    ar_weights = np.zeros(shape=num_bins)
    ar_counts = np.zeros(shape=num_bins)
    chosen_indices = np.random.choice(outer_disc, size=100, replace=False)
    for index in chosen_indices:
        vec_a = hp.pix2vec(ar_map_nside, index)
        inner_disc = hp.query_disc(ar_map_nside, vec=vec_a, radius=max_angular_separation.to(u.rad).value)
        if enable_intersection:
            inner_disc = np.intersect1d(inner_disc, outer_disc, assume_unique=True)
        vec_b = hp.pix2vec(ar_map_nside, inner_disc)
        ar_ang_dist_with_zero = hp.rotator.angdist(vec_b, vec_a)
        a = index
        b = inner_disc[ar_ang_dist_with_zero > 0]
        ar_ang_dist = ar_ang_dist_with_zero[ar_ang_dist_with_zero > 0]
        ar_bins_float = ar_ang_dist / max_bin_angle * num_bins  # type: np.ndarray
        ar_bins = ar_bins_float.astype(int)
        # TODO: filter NaNs earlier so that they don't decrease the correlation
        ar_pair_product = np.nan_to_num((ar_map[a] - outer_disc_mean) * (ar_map[b] - outer_disc_mean))
        ar_pair_weights = (ar_map_unc[a] * ar_map_unc[b]) ** (-2)  # type: np.ndarray
        ar_product += np.bincount(ar_bins, weights=ar_pair_product * ar_pair_weights, minlength=num_bins)
        ar_weights += np.bincount(ar_bins, weights=ar_pair_weights, minlength=num_bins)
        ar_counts += np.bincount(ar_bins, minlength=num_bins)

    return ar_product, ar_weights, ar_counts
Exemplo n.º 5
0
    def compute_luminosity(self, SZ_map):

        self.compute_FWHM_in_degrees(SZ_map)
        NSIDE = hp.get_nside(SZ_map)

        pixels_in_disk = hp.query_disc(NSIDE,
                                       hp.pix2vec(NSIDE, self.peak_index),
                                       np.radians(self.FWHM * 1.5))
        nb_pixels_disk = len(pixels_in_disk)
        vania_luminosity = 0

        for pixel in pixels_in_disk:
            vania_luminosity += SZ_map[pixel]

        inner_ring = hp.query_disc(NSIDE, hp.pix2vec(NSIDE, self.peak_index),
                                   np.radians(self.FWHM * 3.5))
        outer_ring = hp.query_disc(NSIDE, hp.pix2vec(NSIDE, self.peak_index),
                                   np.radians(self.FWHM * 5.5))
        pixels_values_ring = list()
        nb_pixels_background = len(outer_ring) - len(inner_ring)

        for pixel in outer_ring:
            if not (pixel in inner_ring):
                pixels_values_ring.append(SZ_map[pixel])

        self.luminosity_error = np.std(pixels_values_ring)
        local_noise_ring = sum(pixels_values_ring)
        self.luminosity = vania_luminosity - local_noise_ring * (
            nb_pixels_disk / nb_pixels_background)
        print 'luminosity = %s \pm %s' % (self.luminosity,
                                          self.luminosity_error)
Exemplo n.º 6
0
def alpha_mat():
    npix = hp.nside2npix(8)
    alphas = np.zeros((npix, npix))
    for i in range(npix):
        for j in range(i):
            d1 = hp.pix2vec(8, i)
            d2 = hp.pix2vec(8, j)
            alphas[i, j] = get_alpha(d1, d2)
            alphas[j, i] = -alphas[i, j]
    return alphas
Exemplo n.º 7
0
def make_ring_mask(inner, outer, ring_b, ring_l, nside):
    """ Masks outside inner < r < outer, of a ring centred at (ring_b,ring_l)
    """
    mask_none = np.arange(hp.nside2npix(nside))
    return np.logical_not(
        (np.cos(np.radians(inner)) >= np.dot(
            hp.ang2vec(np.radians(90 - ring_b), np.radians(ring_l)),
            hp.pix2vec(nside, mask_none))) *
        (np.dot(hp.ang2vec(np.radians(90 - ring_b), np.radians(ring_l)),
                hp.pix2vec(nside, mask_none)) >= np.cos(np.radians(outer))))
Exemplo n.º 8
0
def alpha_test():
    d1 = hp.pix2vec(8, 700)
    d2 = hp.pix2vec(8, 600)
    alpha = get_alpha(d1, d2)
    print(alpha)

    alphas = alpha_mat()
    plt.imshow(alphas)
    plt.colorbar()
    plt.show()
    return
Exemplo n.º 9
0
def create_pixel_list(nside,
                      area_center_nside=None,
                      area_center_pixel=None,
                      area_num_pixels=None):
    if (area_center_nside is not None or area_center_pixel is not None or area_num_pixels is not None) and \
       (area_center_nside is None or area_center_pixel is None or area_num_pixels is None):
        raise RuntimeError(
            "You have to either set none of the three options area_center_nside,area_center_pixel,area_num_pixels or all of them"
        )

    npixel_max = healpy.nside2npix(nside)

    # just return all pixels if no specific area is requested or the area covers the entire map
    if (area_center_nside is None) or (area_num_pixels >= npixel_max):
        return range(npixel_max)

    # otherwise, build the area iteratively
    pixel_area_sqdeg = healpy.nside2pixarea(nside, degrees=True)
    area_for_requested_pixels_sqdeg = pixel_area_sqdeg * float(area_num_pixels)
    approx_radius = numpy.sqrt(area_for_requested_pixels_sqdeg) / numpy.pi
    print(
        "Building healpix pixel list for nside {} with an area of {} (out of {}) pixels (=={:.2f}sqdeg; radius={:.2f}deg)"
        .format(nside, area_num_pixels, npixel_max,
                area_for_requested_pixels_sqdeg, approx_radius))

    # get the center coordinate
    c_x, c_y, c_z = healpy.pix2vec(area_center_nside, area_center_pixel)
    start_pixel = healpy.vec2pix(nside, c_x, c_y, c_z)
    c_x, c_y, c_z = healpy.pix2vec(nside, start_pixel)
    pixel_set = set([start_pixel])

    print("start pixel:", start_pixel)

    # Create a full list of pixels ordered so that the center pixel is first and the distance to the center is growing.
    # Then crop the list to return the number of requested pixels. This makes sure that we can extend the list later.

    pixels = numpy.array(range(npixel_max))

    p_x, p_y, p_z = healpy.pix2vec(nside, pixels)
    pixel_space_angles = numpy.arccos(
        numpy.clip(c_x * p_x + c_y * p_y + c_z * p_z, -1., 1.))
    pixel_num = numpy.array(range(len(pixel_space_angles)), dtype=numpy.float)

    # pixels, sorted by distance from the requested pixel; secondary sort key is just the healpix pixel index
    pixel_list_sorted = pixels[numpy.lexsort(
        (pixel_num, pixel_space_angles))].tolist()

    return_list = pixel_list_sorted[:area_num_pixels]

    print("Pixel set created. It has {} entries (requested entries were {})".
          format(len(return_list), area_num_pixels))

    return return_list
Exemplo n.º 10
0
def _get_mask(tag, nside):
    if tag == 'nomask':
        return np.zeros(hp.nside2npix(nside)).astype(bool)
    elif 'gal' in tag:
        gal_cut = int(tag.split('gal')[-1])
        pix = np.arange(hp.nside2npix(nside))
        return np.abs(hp.pix2vec(nside, pix)[2]) < np.sin(np.radians(gal_cut))
    elif 'pole' in tag:
        pix = np.arange(hp.nside2npix(nside))
        return hp.pix2vec(nside, pix)[2] < 0
    else:
        raise ValueError('Unsupported tag: %s' % tag)
Exemplo n.º 11
0
def my_query_disk(nside, x0, radius):
    # Avoid the stupid nside limit of powers of two.
    ret = []
    npix = hp.nside2npix(nside)
    all_pixels = np.array(range(npix))
    all_vec = hp.pix2vec(nside, all_pixels)

    for i in all_pixels:
        x1 = hp.pix2vec(nside, i)
        angle = np.arccos(np.dot(x1, x0))
        if angle <= radius:
            ret.append(i)

    return np.array(ret)
Exemplo n.º 12
0
        def _setup(nside):

            self.cov = np.diag([self.unc_x**2, self.unc_y**2, self.unc_z**2])

            # compute pdf for each pixel
            self.nside = nside
            self._n_order = self._nside2norder()
            self.npix = hp.nside2npix(nside)
            self.dir_x_s, self.dir_y_s, self.dir_z_s = \
                hp.pix2vec(nside, range(self.npix))

            self.neg_llh_values = -self.log_prob_dir(
                self.dir_x_s, self.dir_y_s, self.dir_z_s)

            # sort directions according to neg llh
            sorted_indices = np.argsort(self.neg_llh_values)
            self.dir_x_s = self.dir_x_s[sorted_indices]
            self.dir_y_s = self.dir_y_s[sorted_indices]
            self.dir_z_s = self.dir_z_s[sorted_indices]
            self.neg_llh_values = self.neg_llh_values[sorted_indices]
            self.ipix_list = sorted_indices

            # get zenith and azimuth
            self.zenith_s, self.azimuth_s = self.get_zenith_azimuth(
                self.dir_x_s, self.dir_y_s, self.dir_z_s)

            # get normalized probabilities and cdf
            prob = np.exp(-self.neg_llh_values)
            self.prob_values = prob / np.sum(prob)
            self.cdf_values = np.cumsum(self.prob_values)
Exemplo n.º 13
0
    def contour_area(self, levels, nside):
        """Get the area inside a contour of a given level in square degrees.

        Parameters
        ----------
        levels : float or array_like
            The level or levels for which to compute the contained area.
            Must be a value in (0, 1).
        nside : int
            The nside parameter of the HEALpix.
            The higher nside, the more accurate, but also slower to compute.

        Returns
        -------
        array_like
            The contained area in square degrees for each specified level
        """
        if isinstance(levels, float):
            levels = [levels]

        # area of one HEALPix with given nside in square degrees
        pix_area = hp.nside2pixarea(nside, degrees=True)
        npix = hp.nside2npix(nside)
        cdf_values = self.cdf_dir(*hp.pix2vec(nside, range(npix)))

        # number of HEALPix pixels within contour
        contour_areas = []
        for level in levels:
            num_pixels_inside = np.sum(cdf_values <= level)
            contour_areas.append(num_pixels_inside * pix_area)
        return contour_areas
Exemplo n.º 14
0
    def as_healpix(self, nside, nest=True):
        r"""Returns a healpix map with the mean and standard deviations
        of :math:`d` for any pixel containing at least one posterior
        sample. 

        """
        from lalinference.bayestar import distance

        npix = hp.nside2npix(nside)
        datasets = [kde.dataset for kde in self.kdes]
        inverse_covariances = [kde.inv_cov for kde in self.kdes]
        weights = self.weights

        # Compute marginal probability, conditional mean, and conditional
        # standard deviation in all directions.
        prob, mean, std = np.transpose([distance.cartesian_kde_to_moments(
            np.asarray(hp.pix2vec(nside, ipix, nest=nest)),
            datasets, inverse_covariances, weights)
            for ipix in range(npix)])

        # Normalize marginal probability...
        # just to be safe. It should be marginalized already.
        prob /= prob.sum()

        # Apply method of moments to find location parameter, scale parameter,
        # and normalization.
        distmu, distsigma, distnorm = distance.moments_to_parameters(mean, std)

        # Done!
        return prob, distmu, distsigma, distnorm
Exemplo n.º 15
0
def distance_to_mask(mask, maxdist=None, deg=True):

    nside = hp.npix2nside(len(mask))

    if maxdist == None:
        if deg:
            maxdist = 180.
        else:
            maxdist = np.pi

    p0, p1 = border_of_mask(mask)

    dist = maxdist * mask

    if len(p0):
        for p in range(len(p0)):
            vec = np.array(hp.pix2vec(nside, p0[p]))
            listpix, d = query_dist(nside, vec, maxdist, deg=deg)
            if len(listpix):
                tmpdist = dist[listpix]
                wh, = np.where(d < tmpdist)
                tmpdist[wh] = d[wh]
                dist[listpix] = tmpdist

    return dist * mask
Exemplo n.º 16
0
def rotate_QU_frame(q, u, rot):
    """
    Rotate coordinate from of QU angles on a HEALPIX map

    Parameters
    ----------
    q, u: healpy map for stoke parameters Q and U
    rot: Rotator object

    """
    nside = int(np.sqrt(q.size/12.))
    pix = np.arange(12*nside**2).astype(int)
    vecs = np.array(hp.pix2vec(nside, pix))

    # First rotate the map pixels
    qp = rot.rotate_map_pixel(q)
    up = rot.rotate_map_pixel(u)
    qp[qp < -1e25] = hp.UNSEEN
    up[up < -1e25] = hp.UNSEEN

    # Then caculate the reference angles in the rotated frame
    vecs_r = rot(vecs,inv=True)
    angles = rot.angle_ref(vecs_r)

    L_map = (qp + 1j * up)*np.exp(1j*2*angles)

    return np.real(L_map), np.imag(L_map)
Exemplo n.º 17
0
def min_all_cos_dtheta_fast(pix, nside, nest=False, safe=False):
    """
	computes the maximum angular separation between any two pixels within pix=[ipix,ipix,...]
	does this with a boarder-to-boarder search after checking some other things
	this could be in error up to the pixel size (hopefully small)

	This algorithm is due in part to Antonios Kontos, who helped Reed Essick think through the details
	"""
    Npix = len(pix)
    ### check to see if more than half the sky is filled
    if Npix * hp.nside2pixarea(nside) > 2 * np.pi:
        return -1
    ### check to see if the antipode of any point is in the set
    npix = hp.nside2npix(nside)
    selected = np.zeros(npix, dtype=bool)
    selected[pix] = True
    antipodes = np.zeros_like(selected)
    vec = -np.array(hp.pix2vec(nside, pix, nest=nest))  ### reflect vectors
    antipodes[hp.vec2pix(nside, vec[0], vec[1], vec[2],
                         nest=nest)] = True  ### reflection -> antipode
    ### reflection accomplished in cartesian coords
    if np.sum(selected *
              antipodes):  ### point and it's antipode are in the set
        return -1  ### could be in error by the pixel size...

    ### boarder-to-boarder search
    boarder_pix = []
    for bpix in __into_boarders(nside, pix, nest=nest):
        boarder_pix += bpix
    return min_all_cos_dtheta(boarder_pix, nside, nest=nest, safe=False)
Exemplo n.º 18
0
def cartesian_gaussian_to_skymap(level, mean, cov):
    """Convert a 3D Cartesian Gaussian to a 3D sky map."""
    # Set up HEALPix grid.
    nside = 2**level
    npix = ah.nside_to_npix(nside)
    ipix = np.arange(npix)
    coords = np.column_stack(hp.pix2vec(nside, ipix, nest=True))

    # Create sky map using same method as KDE to convert from Cartesian
    # to spherical representation. This is just a special case where there
    # is only a single cluster and a single KDE sample.
    means = mean[np.newaxis, ..., np.newaxis]
    inv_covs = np.linalg.inv(cov)[np.newaxis, ...]
    weights = np.ones(1)
    probdensity, distmean, diststd = np.transpose([
        cartesian_kde_to_moments(n, means, inv_covs, weights) for n in coords
    ])

    # Create 3D, multi-order sky map.
    uniq = nest2uniq(level, ipix)
    distmu, distsigma, distnorm = moments_to_parameters(distmean, diststd)
    return Table({
        'UNIQ': uniq,
        'PROBDENSITY': probdensity,
        'DISTMU': distmu,
        'DISTSIGMA': distsigma,
        'DISTNORM': distnorm
    })
Exemplo n.º 19
0
    def get_npix(self, rbins, src_ind, out_edge, tol=1):

        '''
        Return the number of sources within each r bins around a source.

        Input: rbins: r bins specified by the user. Need to be an int or an array
               src_ind: index of source;
               out_edge: radius of the disk that is taken into account. in arcmin;
               tol: tolerance between 0 and 1 indicating a threshold of fraction of
                    unmasked pixels in a disk
        Output: number of sources within each r bins around a source.
        '''
        mask = self.mask
        pos_src = self.pos_src_all[src_ind]
        list_out_unmasked, list_in_unmasked = self.get_disk(mask, pos_src,
                                                            out_edge, 0,
                                                            tol)
        if np.asarray(list_out_unmasked).sum() == 0:
            return np.zeros(rbins.size-1)
        Ns = hp.npix2nside(mask.size)
        vector_disc = np.array(hp.pix2vec(Ns, list_out_unmasked))
        # Get the unit vector of each pixels within the out disc
        innprod = np.round(np.dot(pos_src, vector_disc), 15)
        innprod[innprod > 1] = 1
        rtheta = np.degrees(np.arccos(innprod)) * 60.

        normal = np.histogram(rtheta, bins=rbins)[0]
        return normal
Exemplo n.º 20
0
def healpix_graph(nside=16,
                  nest=True,
                  lap_type='normalized',
                  indexes=None,
                  use_4=False,
                  dtype=np.float32):
    """Build a healpix graph using the pygsp from NSIDE."""
    from pygsp import graphs

    if indexes is None:
        indexes = range(nside**2 * 12)

    # 1) get the coordinates
    npix = hp.nside2npix(nside)  # number of pixels: 12 * nside**2
    pix = range(npix)
    x, y, z = hp.pix2vec(nside, pix, nest=nest)
    coords = np.vstack([x, y, z]).transpose()[indexes]
    # 2) computing the weight matrix
    if use_4:
        raise NotImplementedError()
        W = build_matrix_4_neighboors(nside, indexes, nest=nest, dtype=dtype)
    else:
        W = healpix_weightmatrix(nside=nside,
                                 nest=nest,
                                 indexes=indexes,
                                 dtype=dtype)
    # 3) building the graph
    G = graphs.Graph(W, lap_type=lap_type, coords=coords)
    return G
Exemplo n.º 21
0
    def calc_azza(self, Nside, center, return_inds=False):
        """
        Set the az/za arrays.
            Center = lon/lat in degrees
            radius = selection radius in degrees
            return_inds = Return the healpix indices too
        """
        if self.fov is None:
            raise AttributeError("Need to set a field of view in degrees")
        radius = self.fov * np.pi / 180. * 1 / 2.
        cvec = hp.ang2vec(center[0], center[1], lonlat=True)
        pix = hp.query_disc(Nside, cvec, radius)
        vecs = hp.pix2vec(Nside, pix)
        vecs = np.array(vecs).T  # Shape (Npix_use, 3)

        colat = np.radians(90. - center[1])  # Colatitude, radians.
        xvec = [-cvec[1], cvec[0], 0] * 1 / np.sin(colat)  # From cross product
        yvec = np.cross(cvec, xvec)
        sdotx = np.tensordot(vecs, xvec, 1)
        sdotz = np.tensordot(vecs, cvec, 1)
        sdoty = np.tensordot(vecs, yvec, 1)
        za_arr = np.arccos(sdotz)
        az_arr = (np.arctan2(sdotx, sdoty) + np.pi) % (
            2 * np.pi
        )  # xy plane is tangent. Increasing azimuthal angle eastward, zero at North (y axis)
        if return_inds:
            return za_arr, az_arr, pix
        return za_arr, az_arr
Exemplo n.º 22
0
def match_hpx_pixel(nside, nest, nside_pix, ipix_ring):
    """
    """
    ipix_in = np.arange(12 * nside * nside)
    vecs = hp.pix2vec(nside, ipix_in, nest)
    pix_match = hp.vec2pix(nside_pix, vecs[0], vecs[1], vecs[2]) == ipix_ring
    return ipix_in[pix_match]
Exemplo n.º 23
0
    def _sample_from_ipix(self, ipix, nest=False):
        """
        Sample vectors from a uniform distribution within a HEALpixel.

        Credit goes to
        https://git.rwth-aachen.de/astro/astrotools/blob/master/
        astrotools/healpytools.py

        :param ipix: pixel number(s)
        :param nest: set True in case you work with healpy's nested scheme
        :return: vectors containing events from the pixel(s) specified in ipix

        Parameters
        ----------
        ipix : int, list of int
            Healpy pixels.
        nest : bool, optional
            Set to True in case healpy's nested scheme is used.

        Returns
        -------
        np.array, np.array, np.array
            The sampled direction vector components.
        """
        if not nest:
            ipix = hp.ring2nest(self.nside, ipix=ipix)

        n_up = 29 - self._n_order
        i_up = ipix * 4**n_up
        i_up += self._random_state.randint(0, 4**n_up, size=np.size(ipix))
        return hp.pix2vec(nside=2**29, ipix=i_up, nest=True)
def get_vertices(m):
    m = np.copy(m)
    top_npix = len(m)
    top_nside = hp.npix2nside(top_npix)
    top_order = int(np.log2(top_nside))
    for order in range(top_order + 1):
        nside = 1 << order
        npix = hp.nside2npix(nside)
        stride = 1 << (2 * (top_order - order))
        keep = (hp.pix2vec(nside, np.arange(npix), nest=True) *
                np.expand_dims(xyz0, 1)).sum(0) >= np.cos(np.deg2rad(30))
        if order < top_order:
            mm = m.reshape((-1, stride))
            keep &= (mm[:, :-1] == mm[:, 1:]).all(axis=1)
            m += hp.ud_grade(np.where(keep, np.nan, 0),
                             nside_out=top_nside,
                             order_in='NEST',
                             order_out='NEST')
        else:
            keep &= ~np.isnan(m)
        for ipix in np.flatnonzero(keep):
            boundaries = hp.boundaries(nside, ipix, nest=True, step=1).T
            theta, phi = hp.vec2ang(boundaries)
            ra = np.rad2deg(phi)
            dec = np.rad2deg(0.5 * np.pi - theta)
            vertices = np.column_stack((ra, dec))
            yield vertices
Exemplo n.º 25
0
def calc_beam(cal, nside, inttime, freq):
    """Calculate Effective Omega from Beam integral."""
    from scipy.interpolate import interp1d
    from capo import fringe
    aa1 = ap.cal.get_aa(cal, np.array([freq]))
    npix = hp.nside2npix(nside)
    pix = np.arange(npix)
    theta, phi = hp.pix2ang(nside, pix)
    # only keep points above the horizon
    phi = phi[theta < np.pi/2.]
    theta = theta[theta < np.pi/2.]
    intpix = hp.ang2pix(nside, theta, phi)
    x, y, z = hp.pix2vec(nside, intpix)
    # need xyz in eq coords to make fringe weights
    xyz_eq = np.dot(np.linalg.inv(aa1._eq2zen), np.array([x, y, z]))
    # A is defined as the power
    A = aa1[0].bm_response((x, y, z), pol=args.pol)[0]**2

    # calculate the fringe weights for FRF'd data
    bins = fringe.gen_frbins(inttime)
    frp, bins = fringe.aa_to_fr_profile(aa1, (1, 4), 0, bins=bins)
    bl = aa1.get_baseline(1, 4, 'r') * freq
    fng = fringe.mk_fng(bl, xyz_eq)
    wgts = interp1d(bins, frp, kind='linear')
    fng_wgt = wgts(fng)
    fng_bm = A * fng_wgt
    return A, fng_wgt
Exemplo n.º 26
0
def mask_src(cat_file, MASK_S_RAD, NSIDE):
    """Returns the 'bad pixels' defined by the position of a source and a 
       certain radius away from that point.

       SOURCE_CAT: str
           opened fits file with the sorce catalog
       MASK_S_RAD: float
           radius around each source definig bad pixels to mask
       NSIDE: int
           healpix nside parameter
    """
    logger.info('Mask for sources activated')
    src_cat = pf.open(cat_file)
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    CAT = src_cat['LAT_Point_Source_Catalog']
    BAD_PIX_SRC = []
    SOURCES = CAT.data
    RADrad = MASK_S_RAD * np.pi / 180.
    for i in range(0, len(SOURCES) - 1):
        GLON = SOURCES[i][3]
        GLAT = SOURCES[i][4]
        x, y, z = hp.rotator.dir2vec(GLON, GLAT, lonlat=True)
        b_pix = hp.pixelfunc.vec2pix(NSIDE, x, y, z)
        BAD_PIX_SRC.append(b_pix)
    BAD_PIX_inrad = []
    for bn in BAD_PIX_SRC:
        pixVec = hp.pix2vec(NSIDE, bn)
        radintpix = hp.query_disc(NSIDE, pixVec, RADrad)
        BAD_PIX_inrad.extend(radintpix)
    BAD_PIX_SRC.extend(BAD_PIX_inrad)
    src_cat.close()
    return BAD_PIX_SRC
Exemplo n.º 27
0
 def get_mask(self):
     if self.mask is None:
         if self.fsky >= 1:
             self.mask = np.ones(hp.nside2npix(self.nside))
         else:
             # This generates a correctly-apodized mask
             v0 = np.array([np.sin(np.radians(90-self.dec0)) *
                            np.cos(np.radians(self.ra0)),
                            np.sin(np.radians(90-self.dec0)) *
                            np.sin(np.radians(self.ra0)),
                            np.cos(np.radians(90-self.dec0))])
             vv = np.array(hp.pix2vec(self.nside,
                                      np.arange(hp.nside2npix(self.nside))))
             cth = np.sum(v0[:, None]*vv, axis=0)
             th = np.arccos(cth)
             th0 = np.arccos(1-2*self.fsky)
             th_apo = np.radians(self.aposize)
             id0 = np.where(th >= th0)[0]
             id1 = np.where(th <= th0-th_apo)[0]
             idb = np.where((th > th0-th_apo) & (th < th0))[0]
             x = np.sqrt((1 - np.cos(th[idb] - th0)) / (1 - np.cos(th_apo)))
             mask_apo = np.zeros(hp.nside2npix(self.nside))
             mask_apo[id0] = 0.
             mask_apo[id1] = 1.
             mask_apo[idb] = x-np.sin(2 * np.pi * x) / (2 * np.pi)
             self.mask = mask_apo
     return self.mask
Exemplo n.º 28
0
def main(args):
    log = logging.getLogger('root')
    hdlr = logging.StreamHandler(sys.stdout)
    log.addHandler(hdlr)
    log.setLevel(logging.getLevelName(args.loglevel.upper()))
    pyfftw.interfaces.cache.enable()
    vol1 = mrc.read(args.volume1, inc_header=False, compat="relion")
    vol2 = mrc.read(args.volume2, inc_header=False, compat="relion")
    if args.mask is not None:
        mask = mrc.read(args.mask, inc_header=False, compat="relion")
        vol1 *= mask
        vol2 *= mask
    f3d1 = fft.rfftn(vol1, threads=args.threads)
    f3d2 = fft.rfftn(vol2, threads=args.threads)
    nside = 2**args.healpix_order
    x, y, z = pix2vec(nside, np.arange(12 * nside ** 2))
    xhalf = x >= 0
    hp = np.column_stack([x[xhalf], y[xhalf], z[xhalf]])
    t0 = time.time()
    fcor = calc_dfsc(f3d1, f3d2, hp, np.deg2rad(args.arc))
    log.info("Computed CFSC in %0.2f s" % (time.time() - t0))
    fsc = calc_fsc(f3d1, f3d2)
    t0 = time.time()
    log.info("Computed GFSC in %0.2f s" % (time.time() - t0))
    freqs = np.fft.rfftfreq(f3d1.shape[0])
    np.save(args.output, np.row_stack([freqs, fsc, fcor]))
    return 0
Exemplo n.º 29
0
def gsr_files(infilenames,
              pixel,
              nside,
              n,
              angDeg=5.5,
              g=0.4,
              gdot=0.0,
              cluster_id_dict={}):
    """ This function was supposed to help create a meta cluster plot, but the healpix vector
    format was too difficult to manipulate in the time we had, so this file is in misc.py"""
    hp_dicts = []
    for infn in infilenames:
        hp_dicts.append(util.get_hpdict(infn))

    vec = hp.pix2vec(nside, pixel, nest=True)
    neighbors = hp.query_disc(nside,
                              vec,
                              angDeg * np.pi / 180.,
                              inclusive=True,
                              nest=True)
    lines = []
    for pix in neighbors:
        for hp_dict in hp_dicts:
            for line in hp_dict[pix]:
                lines.append(line)

    outfilename = 'demo_train/training_quiver_meta'
    if len(lines) > 0:
        transform_to_arrows(util.lunation_center(n), [(g, gdot)],
                            vec,
                            lines,
                            outfilename,
                            makefiles=True,
                            cluster_id_dict=cluster_id_dict)
Exemplo n.º 30
0
def mask_src(cat_file, MASK_S_RAD, NSIDE):
    """Returns the 'bad pixels' defined by the position of a source and a 
       certain radius away from that point.

       cat_file: str
           .fits file of the sorce catalog
       MASK_S_RAD: float
           radius around each source definig bad pixels to mask
       NSIDE: int
           healpix nside parameter
    """
    logger.info('Mask for sources activated')
    src_cat = pf.open(cat_file)
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    CAT = src_cat['LAT_Point_Source_Catalog']
    BAD_PIX_SRC = []
    SOURCES = CAT.data
    RADrad = MASK_S_RAD*np.pi/180.
    for i in range (0,len(SOURCES)-1):
        GLON = SOURCES.field('GLON')[i]
        GLAT = SOURCES.field('GLAT')[i]
        x, y, z = hp.rotator.dir2vec(GLON,GLAT,lonlat=True)
        b_pix= hp.pixelfunc.vec2pix(NSIDE, x, y, z)
        BAD_PIX_SRC.append(b_pix) 
    BAD_PIX_inrad = []
    for bn in BAD_PIX_SRC:
        pixVec = hp.pix2vec(NSIDE,bn)
        radintpix = hp.query_disc(NSIDE, pixVec, RADrad)
        BAD_PIX_inrad.extend(radintpix)  
    BAD_PIX_SRC.extend(BAD_PIX_inrad)
    src_cat.close()
    return BAD_PIX_SRC
Exemplo n.º 31
0
def build_adjacency_from_heat_kernel(nside,
                                     comm,
                                     stopping_threshold=1e-7,
                                     KS_weighted=False,
                                     Q=None,
                                     alpha=0.5):

    if comm is None:
        rank = 0
        nprocs = 1
    else:
        rank = comm.Get_rank()
        nprocs = comm.Get_size()

    p = np.arange(hp.nside2npix(nside))
    V = np.array(hp.pix2vec(ipix=p, nside=hp.get_nside(p))).T
    scalprod = minmaxrescale(V.dot(V.T), a=-1, b=1)

    if KS_weighted:
        Theta = np.arccos(scalprod)
        Psi = Theta + alpha * (1 - Q) * np.pi / 2
        scalprod = np.cos(Psi)

    lmax, sigmabeam = get_lmax(nside, stopping_threshold)

    Gloc = np.zeros_like(scalprod)

    start_ell, stop_ell = split_data_among_processors(lmax, rank, nprocs)
    for l in np.arange(start_ell, stop_ell):
        Gloc += heat_kernel(scalprod, l, sigmabeam)
    Gloc = comm.allreduce(Gloc, op=MPI.SUM)

    return Gloc
Exemplo n.º 32
0
def match_hpx_pixel(nside, nest, nside_pix, ipix_ring):
    """
    """
    ipix_in = np.arange(12 * nside * nside)
    vecs = hp.pix2vec(nside, ipix_in, nest)
    pix_match = hp.vec2pix(nside_pix, vecs[0], vecs[1], vecs[2]) == ipix_ring
    return ipix_in[pix_match]
Exemplo n.º 33
0
def map_ang_from_edges(maskok):
	print('Calculating Angle Mask Map')
	nsmask = hp.npix2nside(len(maskok))
	### Get the list of pixels on the external border of the mask
	ip = np.arange(12*nsmask**2)
	neigh = hp.get_all_neighbours(nsmask, ip[maskok])
	nn = np.unique(np.sort(neigh.ravel()))
	nn = nn[maskok[nn] == False]
	### Get unit vectors for border and inner pixels
	vecpixarray_inner = np.array(hp.pix2vec(nsmask,ip[maskok]))
	vecpixarray_outer = np.array(hp.pix2vec(nsmask,nn))
	### get angles between the border pixels and inner pixels
	cosang = np.dot(np.transpose(vecpixarray_inner),vecpixarray_outer)
	mapang = np.zeros(12*nsmask**2)
	mapang[maskok] = np.degrees(np.arccos(np.max(cosang,axis=1)))
	return mapang
Exemplo n.º 34
0
def make_window(window, mask):
    nside = hp.npix2nside(len(mask))
    
    valid_sky = np.where(mask==1)[0]
    edge = get_edge(mask)
    interior = np.setdiff1d(valid_sky, edge)
    
    distance = np.zeros(len(mask))
    i = 0
    for pixel in interior:
#         min_distance = 1000
#         vec = hp.pix2vec(nside, pixel)
#         for edge_pixel in edge:
#             dist = np.arccos(np.dot(vec, hp.pix2vec(nside, edge_pixel)))
#             if dist < min_distance:
#                 min_distance = dist
#         distance[pixel] = min_distance
        vec = hp.pix2vec(nside, pixel)
        distance[pixel] = 47.41*np.pi/180.0 - np.arccos(np.dot(vec, hp.ang2vec(np.pi/2, 0.0)))
        #i = i + 1
        #print "%d/%d\r" % (i, len(interior)),
    
    max_distance = np.max(distance)
    
    map_window = np.zeros(len(mask))
    for pixel in interior:
        arg = int((distance[pixel] / (2.0 * max_distance)) * len(window))
        map_window[pixel] = window[arg]
    return map_window
Exemplo n.º 35
0
    def exec(self, data):
        """
        Create the gradient timestreams.

        This pixelizes each detector's pointing and then assigns a
        timestream value based on the cartesian Z coordinate of the pixel
        center.

        Args:
            data (toast.Data): The distributed data.
        """
        autotimer = timing.auto_timer(type(self).__name__)
        comm = data.comm

        zaxis = np.array([0, 0, 1], dtype=np.float64)
        nullquat = np.array([0, 0, 0, 1], dtype=np.float64)

        range = self._max - self._min

        for obs in data.obs:
            tod = obs['tod']

            offset, nsamp = tod.local_samples

            common = tod.local_common_flags() & self._common_flag_mask

            for det in tod.local_dets:
                flags = tod.local_flags(det) & self._flag_mask
                totflags = (flags | common)
                del flags

                pdata = tod.local_pointing(det).copy()
                pdata[totflags != 0, :] = nullquat

                dir = qa.rotate(pdata, zaxis)
                pixels = hp.vec2pix(self._nside,
                                    dir[:, 0],
                                    dir[:, 1],
                                    dir[:, 2],
                                    nest=self._nest)
                x, y, z = hp.pix2vec(self._nside, pixels, nest=self._nest)
                z += 1.0
                z *= 0.5
                z *= range
                z += self._min
                z[totflags != 0] = 0.0

                cachename = "{}_{}".format(self._out, det)
                if not tod.cache.exists(cachename):
                    tod.cache.create(cachename, np.float64, (nsamp, ))
                ref = tod.cache.reference(cachename)
                ref[:] += z
                del ref

                if not self._keep_quats:
                    cachename = 'quat_{}'.format(det)
                    tod.cache.destroy(cachename)

            del common
        return
Exemplo n.º 36
0
def calc_beam(cal, nside, inttime, freq):
    """Calculate Effective Omega from Beam integral."""
    from scipy.interpolate import interp1d
    from capo import fringe
    aa1 = ap.cal.get_aa(cal, np.array([freq]))
    npix = hp.nside2npix(nside)
    pix = np.arange(npix)
    theta, phi = hp.pix2ang(nside, pix)
    # only keep points above the horizon
    phi = phi[theta < np.pi / 2.]
    theta = theta[theta < np.pi / 2.]
    intpix = hp.ang2pix(nside, theta, phi)
    x, y, z = hp.pix2vec(nside, intpix)
    # need xyz in eq coords to make fringe weights
    xyz_eq = np.dot(np.linalg.inv(aa1._eq2zen), np.array([x, y, z]))
    # A is defined as the power
    A = aa1[0].bm_response((x, y, z), pol=args.pol)[0]**2

    # calculate the fringe weights for FRF'd data
    bins = fringe.gen_frbins(inttime)
    frp, bins = fringe.aa_to_fr_profile(aa1, (1, 4), 0, bins=bins)
    bl = aa1.get_baseline(1, 4, 'r') * freq
    fng = fringe.mk_fng(bl, xyz_eq)
    wgts = interp1d(bins, frp, kind='linear')
    fng_wgt = wgts(fng)
    fng_bm = A * fng_wgt
    return A, fng_wgt
Exemplo n.º 37
0
def compute_ds_dcb_line_par(map,mask,ellmin,ellmax,fwhmrad,nthreads):
    npix=np.size(np.where(mask))
    ellbins=ellmin.size
    ellval=(ellmin+ellmax)/2
    print('    Calculating dS/dCb')
    pixwin=hp.pixwin(hp.npix2nside(len(map)))
    maxell=np.max(ellmax)
    ll=np.arange(int(maxell)+1)
    bl=np.exp(-0.5*ll**2*(fwhmrad/2.35)**2)
    
    nside=np.sqrt(map.size/12)
    iprings=np.arange(map.size)
    vecs=hp.pix2vec(int(nside),iprings[mask])
    cosangles=np.dot(np.transpose(vecs),vecs)

    nshots=npix//nthreads+1
    ntot=nshots*nthreads
    indices=np.arange(ntot)
    indices[indices>npix-1]=npix-1
    lines=np.reshape(indices,(nshots,nthreads))
    
    ds_dcb=np.zeros((ellbins,npix,npix))

    for num in np.arange(nshots):
        # initiate multithreading
        tasks=multiprocessing.JoinableQueue()
        results = multiprocessing.Queue()
        # start consumers
        num_consumers = nthreads
        #print 'Creating %d consumers' % num_consumers
        consumers = [ Consumer(tasks, results)
                for i in xrange(num_consumers) ]
        for w in consumers:
            w.start()
        # Enqueue jobs
        num_jobs = nthreads
        for i in np.arange(num_jobs):
            #print('Calling task ',lines[num,i])
            tasks.put(Task(cosangles[lines[num,i],:],lines[num,i],ll,bl,pixwin,ellmin,ellmax,ellval))
        # poison them when finished
        for i in np.arange(num_jobs):
            #print 'Killing Task %d ' % i
            tasks.put(None)
            
        #print('****** Now ready to join tasks ******')
        tasks.join()
        #print('###### Tasks were joined ######')

        while num_jobs:
            #print('Getting result for task %d' % num_jobs)
            result = results.get()
            bla=result[0]
            num=result[1]
            ds_dcb[:,num,num:]=bla[:,num:]
            ds_dcb[:,num:,num]=bla[:,num:]
            num_jobs -= 1
    
    return(ds_dcb)
Exemplo n.º 38
0
def solar_system_dipole_map(nside=16, nest=False, dipole_vector_galactic=None):
    pix = np.arange(healpy.nside2npix(nside),dtype=np.int)
    vec = np.zeros([len(pix),3])
    vec[:,0], vec[:,1], vec[:,2] = healpy.pix2vec(nside, pix, nest=nest)
    dip = Dipole(type='solar_system', coord='G', lowmem=False, dipole_vector_galactic=dipole_vector_galactic)
    dipole_tod = dip.get(None, vec)
    m = np.zeros(len(pix))
    m[pix] = dipole_tod
    return m
Exemplo n.º 39
0
def sample_dpgmm(args):
    print 'sampling component ..'
    # parse the arguments
    ((w, p), (d_grid, npix)) = args
    # get the number of pixels for the healpy nside
    # calculate the log probablity at the cartesian coordinates
    logp = np.array([[p.logProb(d*np.array(hp.pix2vec(np.int(nside), pix, nest=True))) for pix in xrange(npix)] for d in d_grid])
    # multiply by weight of component and return
    return np.log(w) + logp
Exemplo n.º 40
0
def get_disc(pix, disc_size, nside):
    vec = hp.pix2vec(nside, pix, nest=False)
    in_disc = hp.query_disc(
        nside=nside,
        vec=vec,
        radius=np.deg2rad(disc_size),
        nest=False)

    return in_disc
Exemplo n.º 41
0
    def exec(self, data):
        """
        Create the gradient timestreams.

        This pixelizes each detector's pointing and then assigns a
        timestream value based on the cartesian Z coordinate of the pixel
        center.

        Args:
            data (toast.Data): The distributed data.
        """
        autotimer = timing.auto_timer(type(self).__name__)
        comm = data.comm

        zaxis = np.array([0, 0, 1], dtype=np.float64)
        nullquat = np.array([0, 0, 0, 1], dtype=np.float64)

        range = self._max - self._min

        for obs in data.obs:
            tod = obs['tod']

            offset, nsamp = tod.local_samples

            common = tod.local_common_flags() & self._common_flag_mask

            for det in tod.local_dets:
                flags = tod.local_flags(det) & self._flag_mask
                totflags = (flags | common)
                del flags

                pdata = tod.local_pointing(det).copy()
                pdata[totflags != 0, :] = nullquat

                dir = qa.rotate(pdata, zaxis)
                pixels = hp.vec2pix(self._nside, dir[:, 0], dir[:, 1], dir[:, 2],
                                    nest=self._nest)
                x, y, z = hp.pix2vec(self._nside, pixels, nest=self._nest)
                z += 1.0
                z *= 0.5
                z *= range
                z += self._min
                z[totflags != 0] = 0.0

                cachename = "{}_{}".format(self._out, det)
                if not tod.cache.exists(cachename):
                    tod.cache.create(cachename, np.float64, (nsamp,))
                ref = tod.cache.reference(cachename)
                ref[:] += z
                del ref

                if not self._keep_quats:
                    cachename = 'quat_{}'.format(det)
                    tod.cache.destroy(cachename)

            del common
        return
Exemplo n.º 42
0
def make_gauss_map(nside=64,fwhm=1.0,cpix=0):
    """
    produce healpix map nside=nside with gaussian fwhm=fwhm (degrees) centered on cpix
    """
    npix=hp.nside2npix(nside)
    gmap=np.zeros(npix)
    vcen=hp.pix2vec(nside,cpix)
    sigma=np.radians(fwhm)/(2*np.sqrt(2*log(2.)))
    twosigmasquared=2*sigma**2
    print fwhm/sigma

    for p in arange(npix):
        v=hp.pix2vec(nside,p)
        ang=np.arccos(np.dot(vcen,v))
        gmap[p]=exp(-ang**2/twosigmasquared)
        
    return gmap
    
    
Exemplo n.º 43
0
def add_doppler(map1, A, dvec):
    """ return a map that has the doppler dipole added to the temperature map given by the amplitude A and direction (angle); the amplitdue A should inlclude the frequency dependent boost factor b_nu.
    """
    NPIX=len(map1)
    NSIDE=hp.npix2nside(NPIX)
    newmap=np.array([0.]*NPIX)
    for i in range(NPIX):
        pixvec=hp.pix2vec(NSIDE, i)
        newmap[i]=map1[i]*(1+A*np.dot(pixvec, dvec))
    return newmap
def weight(time):
    """
    Weight of pixel at (lat_r, lon_r) assuming Lambertian surface
    """
    EO_vec = latlon2cart(LAT_O, LON_O-OMEGA*time/deg2rad)
    ES_vec = latlon2cart(LAT_S, LON_S-OMEGA*time/deg2rad)
    ER_vec_array = np.array(hp.pix2vec(N_SIDE, np.arange(hp.nside2npix(N_SIDE))))
    cosTH0_array = np.dot(ES_vec, ER_vec_array)
    cosTH1_array = np.dot(EO_vec, ER_vec_array)
    return np.clip(cosTH0_array, 0., 1.)*np.clip(cosTH1_array, 0., 1.)
Exemplo n.º 45
0
 def px2crd(self, px, ncrd=3):
     """Convert a 1 dimensional input array of pixel numbers to the type of coordinates specified by ncrd.  If ncrd=3 (default), the returned array will have (x,y,z) for each pixel.  Otherwise if ncrd=2, the returned array will have (theta,phi) for each pixel."""
     is_nest = (self._scheme == 'NEST')
     assert(ncrd in (2,3))
     if ncrd == 2: # th/phi mode
         th,phi = healpy.pix2ang(self._nside, px, nest=is_nest)
         return th, phi
     else: # ncrd == 3 -> vec mode
         x,y,z = healpy.pix2vec(self._nside, px, nest=is_nest)
         return x,y,z
Exemplo n.º 46
0
 def __init__(self, radius_mpc=r_cmb_mpc, n_side=2**4):
     # FYI: n_side = 2**4 corresponds to
     # 0.064 radians resolution = ~0.9 Mpc at z~1000.
     from healpy import nside2npix, pix2vec
     self.n_pix = nside2npix(n_side)
     x, y, z = pix2vec(n_side, range(self.n_pix))
     self.radius_mpc = radius_mpc
     self.x = self.radius_mpc*x
     self.y = self.radius_mpc*y
     self.z = self.radius_mpc*z
     self.update_xyz()
Exemplo n.º 47
0
def weight_2(time, n_side, param_geometry):
    """
    Weight of pixel at (lat_r, lon_r) assuming Lambertian surface
    """
    lat_o, lon_o, lat_s, lon_s, omega = param_geometry
    EO_vec = latlon2cart(lat_o, lon_o-omega*time/deg2rad)[0]
    ES_vec = latlon2cart(lat_s, lon_s-omega*time/deg2rad)[0]
    ER_vec_array = np.array(hp.pix2vec(n_side, np.arange(hp.nside2npix(n_side))))
    cosTH0_array = np.dot(ES_vec, ER_vec_array)
    cosTH1_array = np.dot(EO_vec, ER_vec_array)
    return np.clip(cosTH0_array, 0., 1.)*np.clip(cosTH1_array, 0., 1.)
Exemplo n.º 48
0
def map_generator():

	#Constructing maps...............................
	#if not os.path.exists('./map'):
	#	os.makedirs('./map')

	#Star map
	if(map_list['star'] == True): map['star'] = hp.read_map(star_map_file)[mpix2hpix]

	#Galaxy map
	if((map_list['gal'] == True) or (map_list['odds'] == True)):
		for bin in zbin:
			map['gal'][zbintag(bin)] = {}
			i_eff = 0
			for od in od_cut:
				gal_map = np.zeros(N_mpix)	
				mask = cut(bin, od, cat['p']['val']['zp'], cat['p']['val']['od'])		
				mpix = cat['p']['val']['mpix'][mask]
				for i in mpix: 
					if(i >= 0): gal_map[i] += 1
				map['gal'][zbintag(bin)][odtag(eff_cut[i_eff])] = gal_map 
				
				if(map_list['gal'] == True): hp.write_map(folder_out + 'map/map_gal' + nside_tag + '_' + zbintag(bin) + '_' + odtag(eff_cut[i_eff]) + '.fits', np.append(gal_map, 0)[hpix2mpix])
				i_eff += 1

	#Odds map
	if(map_list['odds'] == True):
		for bin in zbin:
			mask = cut(bin, 0., cat['p']['val']['zp'], cat['p']['val']['od'])	
			od_map = np.zeros(N_mpix)	
			n_map = map['gal'][zbintag(bin)][odtag(0.0)]
			mpix = cat['p']['val']['mpix'][mask]
			odds = cat['p']['val']['od'][mask]
			for i in range(len(mpix)): 
				if(mpix[i] >= 0): od_map[mpix[i]] += odds[i] 
			for i in range(N_mpix):
				if(n_map[i] != 0): od_map[i] /= n_map[i]

			od_map_corr = np.copy(od_map)
			for i in range(N_mpix):
				if(n_map[i] == 0):
					i_vec = hp.pix2vec(nside, mpix2hpix[i])
					nest = hpix2mpix[hp.query_disc(nside, i_vec, 1 * (np.pi/ 180))]
					nest = nest[nest >= 0]
					o = 0
					n = 0
					for j in nest:
						o += od_map[j]
						if (od_map[j] > 0.00000001): n += 1
					o /= float(n)
					od_map_corr[i] = o
			
			map['odds'][zbintag(bin)] = od_map_corr
			hp.write_map(folder_out + 'Map/od_map' + nside_tag + zbintag(bin) + '.fits', np.append(od_map_corr,0)[hpix2mpix])
Exemplo n.º 49
0
def subpixel(superpix, nside_superpix, nside_subpix):
    """
    Return the indices of sub-pixels (resolution nside_subpix) within the super-pixel with (resolution nside_superpix).
    """
    if nside_superpix == nside_subpix:
        return superpix
    vec = healpy.pix2vec(nside_superpix, superpix)
    radius = np.degrees(2.0 * healpy.max_pixrad(nside_superpix))
    subpix = query_disc(nside_subpix, vec, radius)
    pix_for_subpix = superpixel(subpix, nside_subpix, nside_superpix)
    # Might be able to speed up array indexing...
    return subpix[pix_for_subpix == superpix]
Exemplo n.º 50
0
    def labelHealpix(pixels, values, nside, threshold=0, xsize=1000):
        """
        Label contiguous regions of a (sparse) HEALPix map. Works by mapping 
        HEALPix array to a Mollweide projection and applying scipy.ndimage.label
     
        Assumes non-nested HEALPix map.
        
        Parameters:
        pixels    : Pixel values associated to (sparse) HEALPix array
        values    : (Sparse) HEALPix array of data values
        nside     : HEALPix dimensionality
        threshold : Threshold value for object detection
        xsize     : Size of Mollweide projection
        
        Returns:
        labels, nlabels
        """
        proj = healpy.projector.MollweideProj(xsize=xsize)
        vec = healpy.pix2vec(nside,pixels)
        xy = proj.vec2xy(vec)
        ij = proj.xy2ij(xy)
        xx,yy = proj.ij2xy()
     
        # Convert to Mollweide
        searchims = []
        if values.ndim < 2: iterate = [values]
        else:               iterate = values.T
        for i,value in enumerate(iterate):
            logger.debug("Labeling slice %i...")
            searchim = numpy.zeros(xx.shape,dtype=bool)
            select = (value > threshold)
            yidx = ij[0][select]; xidx = ij[1][select]
            searchim[yidx,xidx] |= True
            searchims.append( searchim )
        searchims = numpy.array(searchims)

        # Full binary structure
        s = ndimage.generate_binary_structure(searchims.ndim,searchims.ndim)
     
        ### # Dilate in the z-direction
        logger.info("  Dilating image...")
        searchims = ndimage.binary_dilation(searchims,s,1)
        
        # Do the labeling
        logger.info("  Labeling image...")
        labels,nlabels = ndimage.label(searchims,structure=s)

        # Convert back to healpix
        pix_labels = labels[:,ij[0],ij[1]].T
        pix_labels = pix_labels.reshape(values.shape)
        pix_labels *= (values > threshold) # re-trim

        return pix_labels, nlabels
Exemplo n.º 51
0
def galmap2eqmap(map):
    """
    function to rotate galactic coord map (healpix) to equatorial
    """
    nside=np.int(np.sqrt(len(map)/12))
    grot=hp.Rotator(coord='GC')
    pixlist=range(len(map))
    veclist=hp.pix2vec(nside,pixlist)
    rveclist=grot(veclist)
    rpixlist=hp.vec2pix(nside,rveclist[0,:],rveclist[1,:],rveclist[2,:])
    outmap=np.zeros(len(map))
    outmap[rpixlist]=map[pixlist]
    return outmap
Exemplo n.º 52
0
 def sigmap(self):
     """
     (array): Return the underlying signal map (full map on all processes).
     """
     autotimer = timing.auto_timer(type(self).__name__)
     range = self._max - self._min
     pix = np.arange(0, 12 * self._nside * self._nside, dtype=np.int64)
     x, y, z = hp.pix2vec(self._nside, pix, nest=self._nest)
     z += 1.0
     z *= 0.5
     z *= range
     z += self._min
     return z
Exemplo n.º 53
0
def local_mean_map(map1, mask1, deg):
    """ return the local mean map for map1 with mask mask1
    """
    mp1=hp.ma(map1)
    mp1.mask=np.logical_not(mask1)
    NSIDE1=hp.npix2nside(len(map1))
    NSIDE2=nside(deg)
    NPIX2=hp.nside2npix(NSIDE2)
    mp2=np.array([0.]*NPIX2)
    for pix2 in range(0, NPIX2):
        disc1=hp.query_disc(nside=NSIDE1, vec=hp.pix2vec(NSIDE2, pix2), radius=deg2rad(deg))
        mp2[pix2]=np.mean(mp1[disc1])
    return mp2
Exemplo n.º 54
0
def principal_axes(prob, distmu, distsigma, nest=False):
    npix = len(prob)
    nside = hp.npix2nside(npix)
    good = np.isfinite(prob) & np.isfinite(distmu) & np.isfinite(distsigma)
    ipix = np.flatnonzero(good)
    distmean, diststd, _ = parameters_to_moments(distmu[good], distsigma[good])
    mass = prob[good] * (np.square(diststd) + np.square(distmean))
    xyz = np.asarray(hp.pix2vec(nside, ipix, nest=nest))
    cov = np.dot(xyz * mass, xyz.T)
    L, V = np.linalg.eigh(cov)
    if np.linalg.det(V) < 0:
        V = -V
    return V
Exemplo n.º 55
0
def principal_axes(prob, distmu, distsigma, nest=False):
    npix = len(prob)
    nside = hp.npix2nside(npix)
    bad = ~(np.isfinite(prob) & np.isfinite(distmu) & np.isfinite(distsigma))
    distmean, diststd, _ = parameters_to_moments(distmu, distsigma)
    mass = prob * (np.square(diststd) + np.square(distmean))
    mass[bad] = 0.0
    xyz = np.asarray(hp.pix2vec(nside, np.arange(npix), nest=nest))
    cov = np.dot(xyz * mass, xyz.T)
    L, V = np.linalg.eigh(cov)
    if np.linalg.det(V) < 0:
        V = -V
    return V
Exemplo n.º 56
0
def aipy_beam(freq,theta,phi,efield=False):
    # really only defined between 120 and 180 MHz
    # theta, phi in radians
    nside = 256
    bm = prms['beam'](np.array([freq]),nside=nside,lmax=20,mmax=20,deg=7)
    bm.set_params(prms['bm_prms'])
    px = hp.ang2pix(nside,theta,phi)
    xyz = hp.pix2vec(nside,px)
    poly = np.array([h.map[px] for h in bm.hmap])
    Axx = np.polyval(poly,freq)
    Axx = np.where(xyz[-1] >= 0, Axx, 0)
    Axx /= Axx.max()
    if (not efield):
        Axx = Axx*Axx
    return Axx
Exemplo n.º 57
0
def smoothMap(map, smooth=5.):

    if smooth == 0:
        return map

    npix = len(map)
    nside = hp.npix2nside(npix)
    smooth_rad = smooth * np.pi/180.
    smooth_map = np.zeros(map.shape)

    vec = np.transpose(hp.pix2vec(nside, np.arange(npix)))
    for i in range(npix):
        neighbors = hp.query_disc(nside, vec[i], smooth_rad)
        smooth_map[i] += np.sum(map[neighbors], axis=0)

    return smooth_map
Exemplo n.º 58
0
def hp_interpolator(map_, el, az, n_pix=4):
    NSide = hp.pixelfunc.get_nside(map_)
    direction = np.array([np.pi/2.-el.to_rad(),az.to_rad()])
    steplength = hp.pixelfunc.max_pixrad(NSide)
    for i, r in enumerate(np.arange(steplength, np.pi, steplength)):
        pixels = np.array(hp.query_disc(NSide,hp.ang2vec(direction[0], direction[1]), r))
        filled = np.where(map_[pixels] > -1.)[0]
        l = len(filled)
        if l >= n_pix:
            # print(i, l)
            filled_pixel = pixels[filled]
            filled_pixel_directions = hp.pix2vec(NSide, filled_pixel)
            angular_distance = hp.rotator.angdist(direction, filled_pixel_directions)
            if angular_distance.min() == 0.: # do we really want this?
                return map_[filled_pixel[angular_distance.argmin()]]
            return np.average(map_[filled_pixel], weights=np.power(1./angular_distance, 2))
Exemplo n.º 59
0
def local_variance_map(map1, mask1, deg):
    """ return the local variance map for map1 with mask mask1, given the error tolerance tol for mask2
    """
    mp1=hp.ma(map1)
    mp1.mask=np.logical_not(mask1)

    NSIDE1=hp.npix2nside(len(map1))
    NSIDE2=nside(deg)
    NPIX2=hp.nside2npix(NSIDE2)
    #mask2=np.round(hp.ud_grade(mask1, nside_out=NSIDE2)+tol)
    mp2=np.array([0.]*NPIX2)
    for pix2 in range(0, NPIX2):
        disc1=hp.query_disc(nside=NSIDE1, vec=hp.pix2vec(NSIDE2, pix2), radius=deg2rad(deg))
        mp2[pix2]=np.var(mp1[disc1])
    #varmp2.mask=np.logical_not(mask2)
    return mp2
Exemplo n.º 60
0
def mask_hemi_west(NSIDE):
    """Returns the 'bad pixels' in the westhern hemisphere.

       NSIDE: int
           healpix nside parameter
    """
    logger.info('Masking westhern hemisphere...')
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    BAD_PIX_HEMI_W = []
    iii = range(NPIX)
    x,y,z = hp.pix2vec(NSIDE,iii)
    lon,lat = hp.rotator.vec2dir(x,y,z,lonlat=True)
    for i,b in enumerate(lon):
        if 0 <= b <= 180:
            BAD_PIX_HEMI_W.append(iii[i])
    return BAD_PIX_HEMI_W