Пример #1
0
    def to_pixel(self, Nside=None, Npix=None, dtype=None, out=None):
        """
        ::
        
            >>> insh = harmonic_sphere_map(0, 0, 4)
            >>> insh.to_pixel(8)
            Pixel sphere map (ring-ordered, Nside=8, Npix=768)

        Convert a uniform map::        
            >>> shmap = harmonic_sphere_map([[23], [0, 0], [0, 0, 0]])
            >>> pixmap = shmap.to_pixel(8); pixmap
            Pixel sphere map (ring-ordered, Nside=8, Npix=768)
            >>> np.allclose(pixmap, 23.0 / np.sqrt(4*np.pi))
            True

        One can convert multiple maps at once::

            >>> M = shmap
            >>> M = np.hstack((M[:,None], M[:,None], M[:,None], M[:,None]))
            >>> M = np.dstack((M, M))
            >>> M = harmonic_sphere_map(M, 0, 2); M
            (4, 2)-stack of brief complex harmonic sphere maps with l=0..2 (6 coefficients)
            >>> x = M.to_pixel(4); x
            (4, 2)-stack of pixel sphere maps (ring-ordered, Nside=4, Npix=192)
            >>> np.allclose(x, 23.0 / np.sqrt(4*np.pi))
            True

        """
        self._check_not_pretending()
        if self.format is not COMPLEX_BRIEF:
            return self.to_complex().to_pixel(Nside, Npix, dtype, out)
        if Nside is None:
            if Npix is None: raise ValueError("Provide Nside or Npix")
            Nside = npix2nside(Npix)
        if Npix is None:
            Npix = nside2npix(Nside)
        if dtype is None:
            dtype = self.dtype
        dtype = to_real_dtype(dtype)
        # for now, only double...
        assert dtype == np.float64
        
        size = self.shape[1:]
        if out is None:
            out = np.empty((Npix,) + size, dtype, order='F')

        # Todo: Optimize -- have lmin in complexpacked2complexmatrix
        if self.lmin == 0:
            source = self
        else:
            source = np.empty((l_to_lm(self.lmax + 1),) + size, complex_dtype, order='F')
            source[:l_to_lm(self.lmin)] = 0
            source[l_to_lm(self.lmin):] = self

        for mapidx in np.ndindex(*size):
            idx = (slice(None),) + mapidx
            alm_matrix = mapdatautils.alm_complexpacked2complexmatrix(source[idx])
            mapdatautils.alm2map(Nside, alm_matrix, out=out[idx])

        return _PixelSphereMap(out, pixel_order='ring')
Пример #2
0
def simulate_pixel_sphere_maps(Nside, size=None, pixel_order='ring', state=np.random):
    """
    Simulates standard normal maps in pixel space.
    
    INPUT:
    
     - ``Nside`` - Size of maps
     - size (optional)

    EXAMPLES::

    >>> simulate_pixel_sphere_maps(4)
    Pixel sphere map (ring-ordered, Nside=4, Npix=192)
    >>> simulate_pixel_sphere_maps(4, (2,3,4))
    (2, 3, 4)-stack of pixel sphere maps (ring-ordered, Nside=4, Npix=192)

    
    """
    Npix = nside2npix(Nside)
    if size is None:
        size = ()
    elif not isinstance(size, tuple):
        size = (size,)
    # A bit complicated in order to simulate Fortran-ordered array;
    # simulate the reverse shape and then transpose the result
    size = list(size)
    size.reverse()
    size = tuple(size) + (Npix,)
    return _PixelSphereMap(state.standard_normal(size).T, pixel_order=pixel_order)
Пример #3
0
 def change_resolution(self, Nside):
     """
     >>> M = pixel_sphere_map(np.arange(3072)); M
     Pixel sphere map (ring-ordered, Nside=16, Npix=3072)
     >>> np.mean(M)
     1535.5
     >>> P = M.change_resolution(32); P
     Pixel sphere map (ring-ordered, Nside=32, Npix=12288)
     >>> np.mean(P)
     1535.5
     """
     if self.Nside == Nside:
         return self.copy()
     elif self.pixel_order == 'ring':
         return self.to_nested().change_resolution(Nside).to_ring()
     else:
         assert self.pixel_order == 'nested'
         size = self.shape[1:]
         Npix_new = nside2npix(Nside)
         buf = np.empty((Npix_new,) + size, order='F', dtype=np.double)
         for mapidx in np.ndindex(*size):
             idx = (slice(None),) + mapidx
             healpix.sub_udgrade_nest(self[idx], buf[idx])
         return _PixelSphereMap(buf, pixel_order='nested')
if options.PSD == None:
    parser.error("You must specify the PSD!")

if len(args) < 1:
    parser.error("I need the output file!")

# get the name of the output file

if options.nproc == 1:
    outputfilenames = [args[0]]
else:
    outputfilenames = [re.sub('.xml','',args[0]) + ('-%s.xml' % str(i)) for i in range(options.nproc)]

outputfiles = [lisaxml.lisaXML(filename,author='M. Vallisneri') for filename in outputfilenames]

npixels = healpix.nside2npix(2**options.refine)

random.seed(options.seed)

PSD = options.PSD * (1.0 + random.uniform(-options.randomizePSD,options.randomizePSD))

whichfile = 0

for i in range(npixels):
    mysystem = Stochastic.Stochastic(options.sourceName + ' pseudosource #%s' % i)

    mysystem.EclipticLatitude, mysystem.EclipticLongitude = healpix.pix2ang_ring(2**options.refine,i)
    mysystem.Polarization = 0

    # Check normalization: divide by the number of pixels, or by solid angle / (4 pi)?
    # The 2 in the denominator is for the two polarizations
Пример #5
0
 def set_properties(self, pixel_order):
     if pixel_order not in ('ring', 'nested'):
         raise ValueError('invalid pixel order')
     self.pixel_order = pixel_order
     if nside2npix(npix2nside(self.shape[0])) != self.shape[0]:
         raise ValueError('invalid shape (must be valid Npix)')
Пример #6
0
def pixel_sphere_map(data, Nside=None, shape=None, pixel_order=None,
                     nested=None, ring=None):
    """

    

    INPUT:
    
     - ``Nside`` - The resolution of the map, given as the
       :math:`N_{side}` argument. The number of pixels is
       ``12*Nside**2``. Can be given as None to infer from data.
     
     - ``data`` - (default 0) The data. Can be a scalar 
     
     - ``pixel_order`` - (default 'ring') whether the data should be
       ring-ordered.  Set to 'nested' for the nested scheme.
    
    EXAMPLES::

        >>> pixel_sphere_map(0, 4)
        Pixel sphere map (ring-ordered, Nside=4, Npix=192)

        >>> M = pixel_sphere_map(np.arange(0, 49152)); M
        Pixel sphere map (ring-ordered, Nside=64, Npix=49152)
        >>> M.dtype
        dtype('float64')
        >>> M[23]
        23.0

        # Disable this for now >>> M[:,None,None]

        >>> np.sin(M) + 34
        Pixel sphere map (ring-ordered, Nside=64, Npix=49152)

        >>> M2 = pixel_sphere_map(np.hstack((M[:,None], M[:,None], M[:,None]))); M2
        (3,)-stack of pixel sphere maps (ring-ordered, Nside=64, Npix=49152)
        >>> M3 = pixel_sphere_map(np.dstack((M2[:,:,None], M2[:,:,None], M2[:,:,None]))); M3
        (3, 3)-stack of pixel sphere maps (ring-ordered, Nside=64, Npix=49152)


        >>> pixel_sphere_map(1)
        Traceback (most recent call last):
           ...
        ValueError: Cannot infer Nside from data

    """
    if (ring or nested) and pixel_order:
        raise ValueError()
    if nested:
        pixel_order = 'nested'
    elif ring or pixel_order is None:
        pixel_order = 'ring'
        
    if Nside not in (None, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192):
        raise ValueError('Invalid Nside (must be None or power of 2 <= 8192)')
    if np.isscalar(data):
        if shape is None:
            shape = ()
        elif np.isscalar(shape):
            shape = (shape,)
        if Nside is None:
            raise ValueError('Cannot infer Nside from data')
        Npix = nside2npix(Nside)
        data = np.ones((Npix,) + shape, dtype=real_dtype, order='F') * float(data)
    else:
        if shape is not None:
            raise ValueError()
        data = np.asfortranarray(data, dtype=real_dtype)
        Npix = data.shape[0]
        if Nside is not None and nside2npix(Nside) != Npix:
            raise ValueError('Nside given but mismatches data size')
    return _PixelSphereMap(data, pixel_order=pixel_order)