예제 #1
0
파일: utils.py 프로젝트: eschiappucci/code
def padproj(sptmap, padding=(4000, 4000), squaremap=True, proj0=True):
    sptmap = np.pad(np.asarray(sptmap), padding, mode='constant')

    if squaremap == True:
        sptmap = sptmap[:,
                        int((sptmap.shape[1] / 2) -
                            (sptmap.shape[0] / 2)):int((sptmap.shape[1] / 2) +
                                                       (sptmap.shape[0] / 2))]

    sptmap = maps.FlatSkyMap(
        sptmap.copy(order='C'),
        0.25 * core.G3Units.arcmin,
        weighted=False,
        proj=maps.MapProjection.Proj5,
        alpha_center=0 * core.G3Units.deg,
        delta_center=-57.5 * core.G3Units.deg,
        coord_ref=maps.MapCoordReference.Equatorial,
        units=core.G3TimestreamUnits.Tcmb,
        pol_type=maps.MapPolType.T,
    )
    if proj0 == True:
        MapProj0 = map_3g.Clone(False)
        MapProj0.proj = maps.MapProjection.Proj0
        maps.reproj_map(map_3g, MapProj0, interp=True)
        sptmap = MapProj0
    return sptmap
예제 #2
0
    def __call__(self, frame):

        if isinstance(frame,
                      core.G3Frame) and frame.type != core.G3FrameType.Map:
            return

        if "Q" in frame and self.stub.coord_ref != frame["Q"].coord_ref:
            raise RuntimeError(
                "Coordinate rotation of polarized maps is not implemented")

        for key in ["T", "Q", "U", "Wpol", "Wunpol"]:

            if key not in frame:
                continue

            m = frame.pop(key)

            if key in "TQU":
                mnew = self.stub.clone(False)
                maps.reproj_map(m, mnew, rebin=self.rebin, interp=self.interp)

            elif key in ["Wpol", "Wunpol"]:
                mnew = maps.G3SkyMapWeights(self.stub, key == "Wpol")
                for wkey in mnew.keys():
                    maps.reproj_map(m[wkey],
                                    mnew[wkey],
                                    rebin=self.rebin,
                                    interp=self.interp)

            frame[key] = mnew

        return frame
예제 #3
0
def healpix_to_flatsky(
    map_in, nest=False, map_stub=None, rebin=1, interp=False, **kwargs
):
    """
    Re-pixelize a map from Healpix to one of the flat sky projections.

    Parameters:
    -----------
    map_in: numpy array or HealpixSkyMap
        The array containing the input healpix map to reproject.

    nest[False]: bool
        Ordering of the healpix map, if the input is a numpy array.  Ring
        ordering is assumed by default.

    map_stub[None]: FlatSkyMap
        Stub output map object to be used to construct the output map.  If not
        supplied, one will be constructed using the remaining keyword arguments.

    rebin[1]: int
        If supplied and >1, account for sub-pixel structure by integrating
        over a sub-grid on each pixel of the given dimension.  This avoids
        aliasing of power at angular scales beyond the map resolution.

    interp[false]: bool
        If True, use bilinear interpolation to extract values from the input
        map.  Otherwise, the nearest-neighbor value is used.

    All additional keyword arguments are passed to FlatSkyMap to construct
    the output map object.  Required if `map_stub` is not supplied,
    otherwise ignored.

    Returns:
    --------
    output_map: FlatSkyMap
        The reprojected map
    """

    # Construct the output map
    if map_stub is None:
        if isinstance(map_in, maps.HealpixSkyMap):
            kwargs.setdefault("coord_ref", map_in.coord_ref)
            kwargs.setdefault("pol_type", map_in.pol_type)
            kwargs.setdefault("pol_conv", map_in.pol_conv)
        map_out = maps.FlatSkyMap(**kwargs)
    else:
        if not isinstance(map_stub, maps.FlatSkyMap):
            raise TypeError("Output stub must be a FlatSkyMap")
        map_out = map_stub.Clone(False)

    # Populate output map pixels with interpolation and rebinning
    if not isinstance(map_in, maps.HealpixSkyMap):
        map_in = maps.HealpixSkyMap(
            map_in,
            nested=nest,
            coord_ref=map_out.coord_ref,
            weighted=map_out.weighted,
            units=map_out.units,
            pol_type=map_out.pol_type,
            pol_conv=map_out.pol_conv,
        )
    maps.reproj_map(map_in, map_out, rebin=rebin, interp=interp)

    return map_out
예제 #4
0
def flatsky_to_healpix(
    map_in, map_stub=None, rebin=1, interp=False, fullsky=False, **kwargs
):
    """
    Re-pixelize a map to Healpix from one of the flat projections.

    Parameters:
    -----------
    map_in: FlatSkyMap
        The input map you want to reproject

    map_stub[None]: HealpixSkyMap
        Stub output map object to be used to construct the output map.  If not
        supplied, one will be constructed using the remaining keyword arguments.

    rebin[1]: int
        If supplied and >1, account for sub-pixel structure by integrating
        over a sub-grid on each pixel of the given dimension.  This avoids
        aliasing of power at angular scales beyond the map resolution.

    interp[false]: bool
        If True, use bilinear interpolation to extract values from the input
        map.  Otherwise, the nearest-neighbor value is used.

    fullsky[false]: bool
        If True a full-sky numpy array representation of the map is returned.
        Otherwise, a HealpixSkyMap instance is returned, containing only the
        pixels that overlap with the input map.

    All additional keyword arguments are passed to HealpixSkyMap to construct
    the output map object.  Required if `map_stub` is not supplied,
    otherwise ignored.

    Returns:
    --------
    output_map: numpy array or HealpixSkyMap
        The array containing the healpix map
        If `fullsky` is True, this is a numpy array, otherwise a
        HealpixSkyMap instance.
    """
    if not isinstance(map_in, maps.FlatSkyMap):
        raise TypeError("Input must be a FlatSkyMap")

    # Construct the output map
    if map_stub is None:
        kwargs.setdefault("coord_ref", map_in.coord_ref)
        kwargs.setdefault("pol_type", map_in.pol_type)
        kwargs.setdefault("pol_conv", map_in.pol_conv)
        map_out = maps.HealpixSkyMap(**kwargs)
    else:
        if not isinstance(map_stub, maps.HealpixSkyMap):
            raise TypeError("Output stub must be a HealpixSkyMap")
        map_out = map_stub.Clone(False)

    # optimize ringsparse storage
    a0 = map_in.alpha_center
    da = map_in.x_res * map_in.shape[1]
    circ = 2 * np.pi * core.G3Units.rad
    a0 = np.mod(a0 + circ, circ)
    amin = np.mod(a0 - da + circ, circ)
    amax = np.mod(a0 + da + circ, circ)
    amin_shift = np.mod(a0 - da + circ + circ / 2, circ)
    amax_shift = np.mod(a0 + da + circ + circ / 2, circ)
    map_out.shift_ra = bool(np.abs(amax - amin) > np.abs(amax_shift - amin_shift))

    # Populate output map pixels with interpolation and rebinning
    maps.reproj_map(map_in, map_out, rebin=rebin, interp=interp)

    if fullsky:
        map_out.dense = True
        return np.asarray(map_out)
    return map_out
예제 #5
0
def flatsky_to_healpix(map_in,
                       map_stub=None,
                       rebin=1,
                       interp=False,
                       fullsky=False,
                       **kwargs):
    """
    Re-pixelize a map to Healpix from one of the flat projections.

    Parameters:
    -----------
    map_in: FlatSkyMap
        The input map you want to reproject

    map_stub[None]: HealpixSkyMap
        Stub output map object to be used to construct the output map.  If not
        supplied, one will be constructed using the remaining keyword arguments.

    rebin[1]: int
        If supplied and >1, subdivide the output pixel by n x n with each 
        sub-pixel taking on the input map values at pixel center (with interp
        or nearest neighbor). The output pixel takes on the average of the sub-pixel
        values.
        In the case that the input map has higher resolution than the output
        map (and that the input map is not low-pass filtered to remove
        information above the Nyquist freq. of the output map pixel), this
        reduces aliasing compared with direct sampling. But there would still be
        aliased power from the input map from freq above the ouput map pixel's
        Nyquist. 

    interp[false]: bool
        If True, use bilinear interpolation to extract values from the input
        map.  Otherwise, the nearest-neighbor value is used.

    fullsky[false]: bool
        If True a full-sky numpy array representation of the map is returned.
        Otherwise, a HealpixSkyMap instance is returned, containing only the
        pixels that overlap with the input map.

    All additional keyword arguments are passed to HealpixSkyMap to construct
    the output map object.  Required if `map_stub` is not supplied,
    otherwise ignored.

    Maps can be rotated between Equatorial and Galactic coordinates, and/or
    change polarization convention between COSMO and IAU, by setting the
    appropriate attributes of the input and output maps.  Attributes not defined
    in the output map are assumed to be that of the input map.

    Returns:
    --------
    output_map: numpy array or HealpixSkyMap
        The array containing the healpix map.  If `fullsky` is True, this is a
        numpy array, otherwise a HealpixSkyMap instance.
    """
    if not isinstance(map_in, maps.FlatSkyMap):
        raise TypeError("Input must be a FlatSkyMap")

    # Construct the output map
    if map_stub is None:
        kwargs.setdefault("coord_ref", map_in.coord_ref)
        kwargs.setdefault("pol_type", map_in.pol_type)
        kwargs.setdefault("pol_conv", map_in.pol_conv)
        map_out = maps.HealpixSkyMap(**kwargs)
    else:
        if not isinstance(map_stub, maps.HealpixSkyMap):
            raise TypeError("Output stub must be a HealpixSkyMap")
        map_out = map_stub.clone(False)

    # optimize ringsparse storage
    a0 = map_in.alpha_center
    da = map_in.x_res * map_in.shape[1]
    circ = 2 * np.pi * core.G3Units.rad
    a0 = np.mod(a0 + circ, circ)
    amin = np.mod(a0 - da + circ, circ)
    amax = np.mod(a0 + da + circ, circ)
    amin_shift = np.mod(a0 - da + circ + circ / 2, circ)
    amax_shift = np.mod(a0 + da + circ + circ / 2, circ)
    map_out.shift_ra = bool(
        np.abs(amax - amin) > np.abs(amax_shift - amin_shift))

    # Populate output map pixels with interpolation and rebinning
    maps.reproj_map(map_in, map_out, rebin=rebin, interp=interp)

    if fullsky:
        map_out.dense = True
        return np.asarray(map_out)
    return map_out
예제 #6
0
def healpix_to_flatsky(map_in,
                       nest=False,
                       map_stub=None,
                       rebin=1,
                       interp=False,
                       **kwargs):
    """
    Re-pixelize a map from Healpix to one of the flat sky projections.

    Parameters:
    -----------
    map_in: numpy array or HealpixSkyMap
        The array containing the input healpix map to reproject.

    nest[False]: bool
        Ordering of the healpix map, if the input is a numpy array.  Ring
        ordering is assumed by default.

    map_stub[None]: FlatSkyMap
        Stub output map object to be used to construct the output map.  If not
        supplied, one will be constructed using the remaining keyword arguments.

    rebin[1]: int
        If supplied and >1, subdivide the output pixel by n x n with each 
        sub-pixel taking on the input map values at pixel center (with interp
        or nearest neighbor). The output pixel takes on the average of the sub-pixel
        values.
        In the case that the input map has higher resolution than the output
        map (and that the input map is not low-pass filtered to remove
        information above the Nyquist freq. of the output map pixel), this
        reduces aliasing compared with direct sampling. But there would still be
        aliased power from the input map from freq above the ouput map pixel's
        Nyquist. 

    interp[false]: bool
        If True, use bilinear interpolation to extract values from the input
        map.  Otherwise, the nearest-neighbor value is used.

    All additional keyword arguments are passed to FlatSkyMap to construct the
    output map object.  Required if `map_stub` is not supplied, otherwise
    ignored.

    Maps can be rotated between Equatorial and Galactic coordinates, and/or
    change polarization convention between COSMO and IAU, by setting the
    appropriate attributes of the input and output maps.  Note that if the input
    map is a numpy array representation of a healpix map, the coordinate system
    and polarization convention are assumed to be that of the output map.
    Conversely, attributes not defined in the output map are assumed to be
    that of the input map.

    Returns:
    --------
    output_map: FlatSkyMap
        The reprojected map
    """

    # Construct the output map
    if map_stub is None:
        if len(kwargs) == 0:
            raise ValueError(
                "Need to provide either a stub map or map parameters (projection, center, etc.)"
            )
        if isinstance(map_in, maps.HealpixSkyMap):
            kwargs.setdefault("coord_ref", map_in.coord_ref)
            kwargs.setdefault("pol_type", map_in.pol_type)
            kwargs.setdefault("pol_conv", map_in.pol_conv)
        map_out = maps.FlatSkyMap(**kwargs)
    else:
        if not isinstance(map_stub, maps.FlatSkyMap):
            raise TypeError("Output stub must be a FlatSkyMap")
        map_out = map_stub.clone(False)

    # Populate output map pixels with interpolation and rebinning
    if not isinstance(map_in, maps.HealpixSkyMap):
        map_in = maps.HealpixSkyMap(
            map_in,
            nested=nest,
            coord_ref=map_out.coord_ref,
            weighted=map_out.weighted,
            units=map_out.units,
            pol_type=map_out.pol_type,
            pol_conv=map_out.pol_conv,
        )
    maps.reproj_map(map_in, map_out, rebin=rebin, interp=interp)

    return map_out
예제 #7
0
)
fm = maps.maputils.healpix_to_flatsky(x, map_stub=fm_stub)
x2 = maps.maputils.flatsky_to_healpix(fm, map_stub=x.clone(False))

hitpix = np.asarray(x2) > 0
assert(np.allclose(np.asarray(x)[hitpix], np.asarray(x2)[hitpix]))


# Coordinate system rotations
a = np.arange(49152)
x = maps.HealpixSkyMap(a)
alpha, delta = maps.get_ra_dec_map(x)

# equatorial to galactic
xgal = x.clone(False)
x.coord_ref = maps.MapCoordReference.Equatorial
xgal.coord_ref = maps.MapCoordReference.Galactic
maps.reproj_map(x, xgal)
ra, dec = maps.azel.convert_gal_to_radec(alpha, delta)
pix = xgal.angles_to_pixels(ra, dec)
assert(np.allclose(np.asarray(pix), np.asarray(xgal)))

# ... and back
xeq = x.clone(False)
x.coord_ref = maps.MapCoordReference.Galactic
xeq.coord_ref = maps.MapCoordReference.Equatorial
maps.reproj_map(x, xeq)
lon, lat = maps.azel.convert_radec_to_gal(alpha, delta)
pix = xeq.angles_to_pixels(lon, lat)
assert(np.allclose(np.asarray(pix), np.asarray(xeq)))