Пример #1
0
def synfast(cls,nside,lmax=-1,mmax=-1,alm=False,
            pixwin=False,fwhm=0.0,sigma=None,degree=False,
            arcmin=False):
    """Create a map(s) from cl(s).

    Input:
      - cls: an array of cl or a list of cls (either 4 or 6, see synalm)
      - nside: the nside of the output map(s)
    Parameters:
      - lmax, mmax: maximum l and m for alm. Default: 3*nside-1
      - alm : if True, return also alm(s). Default: False.
      - pixwin: convolve the alm by the pixel window function. Default: False.
      - fwhm,sigma,degree,arcmin: see smoothalm. Convolve the map(s)
        by a symmetric Gaussian beam
    Output:
      - if alm==False: return a map or a tuple of maps
      - if alm==True: return a tuple of map(s) and alm(s)
    """
    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside value (must be a power of two).")
    if lmax < 0:
        lmax = 3*nside-1
    alms = synalm(cls,lmax,mmax)
    maps = alm2map(alms,nside,lmax,mmax,pixwin=pixwin,
                   fwhm=fwhm,sigma=sigma,degree=degree,
                   arcmin=arcmin)
    if alm: return maps,alms
    else: return maps
Пример #2
0
def pixwin(nside, pol=False):
    """Return the pixel window function for the given nside.

    Input:
      - nside
    Parameters:
      - pol: if True, return also the polar pixel window. Default: False
    Return:
      - the temperature pixel window function, or a tuple with both
        temperature and polarisation pixel window functions.
    """
    datapath = DATAPATH
    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside value (must be a power of two).")
    fname = os.path.join(datapath, 'pixel_window_n%04d.fits' % nside)
    if not os.path.isfile(fname):
        raise ValueError("No pixel window for this nside "
                         "or data files missing")
    # return hfitslib._pixwin(nside,datapath,pol)  ## BROKEN -> seg fault...
    try:
        import pyfits
    except ImportError:
        print "*********************************************************"
        print "**   You need to install pyfits to use this function   **"
        print "*********************************************************"
        raise
    pw = pyfits.getdata(fname)
    pw_temp, pw_pol = pw.field(0), pw.field(1)
    if pol:
        return pw_temp, pw_pol
    else:
        return pw_temp
Пример #3
0
def pixwin(nside,pol=False):
    """Return the pixel window function for the given nside.

    Input:
      - nside
    Parameters:
      - pol: if True, return also the polar pixel window. Default: False
    Return:
      - the temperature pixel window function, or a tuple with both
        temperature and polarisation pixel window functions.
    """
    datapath = DATAPATH
    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside value (must be a power of two).")
    fname = os.path.join(datapath, 'pixel_window_n%04d.fits'%nside)
    if not os.path.isfile(fname):
        raise ValueError("No pixel window for this nside "
                         "or data files missing")
    # return hfitslib._pixwin(nside,datapath,pol)  ## BROKEN -> seg fault...
    try:
        import pyfits
    except ImportError:
        print "*********************************************************"
        print "**   You need to install pyfits to use this function   **"
        print "*********************************************************"
        raise
    pw = pyfits.getdata(fname)
    pw_temp, pw_pol = pw.field(0), pw.field(1)
    if pol:
        return pw_temp, pw_pol
    else:
        return pw_temp
Пример #4
0
def synfast(cls,
            nside,
            lmax=-1,
            mmax=-1,
            alm=False,
            pixwin=False,
            fwhm=0.0,
            sigma=None,
            degree=False,
            arcmin=False):
    """Create a map(s) from cl(s).

    Parameters
    ----------
    cls : array or tuple of array
      A cl or a list of cl (either 4 or 6, see :func:`synalm`)
    nside : int, scalar
      The nside of the output map(s)
    lmax : int, scalar, optional
      Maximum l for alm. Default: 3*nside-1
    mmax : int, scalar, optional
      Maximum m for alm. Default: 3*nside-1
    alm : bool, scalar, optional
      If True, return also alm(s). Default: False.
    pixwin : bool, scalar, optional
      If True, convolve the alm by the pixel window function. Default: False.
    fwhm : float, scalar, optional
      The fwhm of the Gaussian used to smooth the map (applied on alm)
    sigma : float, scalar, optional
      The sigma of the Gaussian used to smooth the map (applied on alm)
    degree : bool, scalar, optional
      If True, unit of sigma or fwhm is degree, otherwise it is radian
    arcmin : bool, scalar, optional
      If True, unit of sigma or fwhm is arcmin, otherwise it is radian

    Returns
    -------
    map : array or tuple of arrays
      The output map (possibly list of maps if polarized input).
      or, if alm is True, a tuple of (map,alm)
      (alm possibly a list of alm if polarized input)
    """
    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside value (must be a power of two).")
    if lmax < 0:
        lmax = 3 * nside - 1
    alms = synalm(cls, lmax, mmax)
    maps = alm2map(alms,
                   nside,
                   lmax,
                   mmax,
                   pixwin=pixwin,
                   fwhm=fwhm,
                   sigma=sigma,
                   degree=degree,
                   arcmin=arcmin)
    if alm: return maps, alms
    else: return maps
Пример #5
0
def synfast(cls,nside,lmax=-1,mmax=-1,alm=False,
            pixwin=False,fwhm=0.0,sigma=None,degree=False,
            arcmin=False):
    """Create a map(s) from cl(s).

    Parameters
    ----------
    cls : array or tuple of array
      A cl or a list of cl (either 4 or 6, see :func:`synalm`)
    nside : int, scalar
      The nside of the output map(s)
    lmax : int, scalar, optional
      Maximum l for alm. Default: 3*nside-1
    mmax : int, scalar, optional
      Maximum m for alm. Default: 3*nside-1
    alm : bool, scalar, optional
      If True, return also alm(s). Default: False.
    pixwin : bool, scalar, optional
      If True, convolve the alm by the pixel window function. Default: False.
    fwhm : float, scalar, optional
      The fwhm of the Gaussian used to smooth the map (applied on alm)
    sigma : float, scalar, optional
      The sigma of the Gaussian used to smooth the map (applied on alm)
    degree : bool, scalar, optional
      If True, unit of sigma or fwhm is degree, otherwise it is radian
    arcmin : bool, scalar, optional
      If True, unit of sigma or fwhm is arcmin, otherwise it is radian

    Returns
    -------
    map : array or tuple of arrays
      The output map (possibly list of maps if polarized input).
      or, if alm is True, a tuple of (map,alm)
      (alm possibly a list of alm if polarized input)
    """
    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside value (must be a power of two).")
    if lmax < 0:
        lmax = 3*nside-1
    alms = synalm(cls,lmax,mmax)
    maps = alm2map(alms,nside,lmax,mmax,pixwin=pixwin,
                   fwhm=fwhm,sigma=sigma,degree=degree,
                   arcmin=arcmin)
    if alm: return maps,alms
    else: return maps
Пример #6
0
def synfast(cls,
            nside,
            lmax=-1,
            mmax=-1,
            alm=False,
            pixwin=False,
            fwhm=0.0,
            sigma=None,
            degree=False,
            arcmin=False):
    """Create a map(s) from cl(s).

    Input:
      - cls: an array of cl or a list of cls (either 4 or 6, see synalm)
      - nside: the nside of the output map(s)
    Parameters:
      - lmax, mmax: maximum l and m for alm. Default: 3*nside-1
      - alm : if True, return also alm(s). Default: False.
      - pixwin: convolve the alm by the pixel window function. Default: False.
      - fwhm,sigma,degree,arcmin: see smoothalm. Convolve the map(s)
        by a symmetric Gaussian beam
    Output:
      - if alm==False: return a map or a tuple of maps
      - if alm==True: return a tuple of map(s) and alm(s)
    """
    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside value (must be a power of two).")
    if lmax < 0:
        lmax = 3 * nside - 1
    alms = synalm(cls, lmax, mmax)
    maps = alm2map(alms,
                   nside,
                   lmax,
                   mmax,
                   pixwin=pixwin,
                   fwhm=fwhm,
                   sigma=sigma,
                   degree=degree,
                   arcmin=arcmin)
    if alm: return maps, alms
    else: return maps
Пример #7
0
def read_map(filename,field=0,dtype=np.float64,nest=False,hdu=1,h=False,
             verbose=False):
    """Read an healpix map from a fits file.

    Parameters
    ----------
    filename : str
      the fits file name
    field : int or tuple of int, optional
      The column to read. Default: 0.
      By convention 0 is temperature, 1 is Q, 2 is U.
      Field can be a tuple to read multiple columns (0,1,2)
    dtype : data type, optional
      Force the conversion to some type. Default: np.float64
    nest : bool, optional
      If True return the map in NEST ordering, otherwise in RING ordering; 
      use fits keyword ORDERING to decide whether conversion is needed or not
      If None, no conversion is performed.
    hdu : int, optional
      the header number to look at (start at 0)
    h : boo, optional
      If True, return also the header. Default: False.
    verbose : bool, optional
      If True, print a number of diagnostic messages

    Returns
    -------
    m | (m0, m1, ...) [, header] : array or a tuple of arrays, optionally with header appended
      The map(s) read from the file, and the header if *h* is True.
    """
    hdulist=pf.open(filename)
    #print hdulist[1].header
    nside = hdulist[hdu].header.get('NSIDE')
    if nside is None:
        warnings.warn("No NSIDE in the header file : will use length of array",
                      HealpixFitsWarning)
    else:
        nside = int(nside)
    if verbose: print 'NSIDE = %d'%nside

    if not pixelfunc.isnsideok(nside):
        raise ValueError('Wrong nside parameter.')
    ordering = hdulist[hdu].header.get('ORDERING','UNDEF').strip()
    if ordering == 'UNDEF':
        ordering = (nest and 'NESTED' or 'RING')
        warnings.warn("No ORDERING keyword in header file : "
                      "assume %s"%ordering)
    if verbose: print 'ORDERING = %s in fits file'%ordering

    sz=pixelfunc.nside2npix(nside)
    if not hasattr(field, '__len__'):
        field = (field,)
    ret = []

    for ff in field:
        m=hdulist[hdu].data.field(ff).astype(dtype).ravel()
        if (not pixelfunc.isnpixok(m.size) or (sz>0 and sz != m.size)) and verbose:
            print 'nside=%d, sz=%d, m.size=%d'%(nside,sz,m.size)
            raise ValueError('Wrong nside parameter.')
        if not nest is None: # no conversion with None
            if nest and ordering == 'RING':
                idx = pixelfunc.nest2ring(nside,np.arange(m.size,dtype=np.int32))
                m = m[idx]
                if verbose: print 'Ordering converted to NEST'
            elif (not nest) and ordering == 'NESTED':
                idx = pixelfunc.ring2nest(nside,np.arange(m.size,dtype=np.int32))
                m = m[idx]
                if verbose: print 'Ordering converted to RING'
        try:
            m[pixelfunc.mask_bad(m)] = UNSEEN
        except OverflowError, e:
            pass
        ret.append(m)
Пример #8
0
def read_map(filename, field=0, dtype=npy.float64, nest=False, hdu=1, h=False, verbose=False):
    """Read an healpix map from a fits file.

    Input:
      - filename: the fits file name
    Parameters:
      - field: the column to read Default: 0
               by convention 0 is temperature, 1 is Q, 2 is U
               field can be a tuple to read multiple columns (0,1,2)
      - dtype: force the conversion to some type. Default: npy.float64
      - nest=False: if True return the map in NEST ordering, otherwise in RING ordering; 
                    use fits keyword ORDERING to decide whether conversion is needed or not
                    if None, no conversion is performed
      - hdu=1: the header number to look at (start at 0)
      - h=False: if True, return also the header
      - verbose=False: if True, print a number of diagnostic messages
    Return:
      - an array, a tuple of array, possibly with the header at the end if h
        is True
    """
    hdulist = pyf.open(filename)
    # print hdulist[1].header
    nside = int(hdulist[hdu].header.get("NSIDE"))
    if nside is None:
        warnings.warn("No NSIDE in the header file : will use length of array", HealpixFitsWarning)
    if verbose:
        print "NSIDE = %d" % nside

    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside parameter.")
    ordering = hdulist[hdu].header.get("ORDERING", "UNDEF").strip()
    if ordering == "UNDEF":
        ordering = nest and "NESTED" or "RING"
        warnings.warn("No ORDERING keyword in header file : " "assume %s" % ordering)
    if verbose:
        print "ORDERING = %s in fits file" % ordering

    sz = pixelfunc.nside2npix(nside)
    if not hasattr(field, "__len__"):
        field = (field,)
    ret = []

    for ff in field:
        m = hdulist[hdu].data.field(ff).astype(dtype).ravel()
        if (not pixelfunc.isnpixok(m.size) or (sz > 0 and sz != m.size)) and verbose:
            print "nside=%d, sz=%d, m.size=%d" % (nside, sz, m.size)
            raise ValueError("Wrong nside parameter.")
        if nest != None:  # no conversion with None
            if nest and ordering == "RING":
                idx = pixelfunc.nest2ring(nside, npy.arange(m.size, dtype=npy.int32))
                m = m[idx]
                if verbose:
                    print "Ordering converted to NEST"
            elif (not nest) and ordering == "NESTED":
                idx = pixelfunc.ring2nest(nside, npy.arange(m.size, dtype=npy.int32))
                m = m[idx]
                if verbose:
                    print "Ordering converted to RING"
        try:
            m[pixelfunc.mask_bad(m)] = UNSEEN
        except OverflowError, e:
            pass
        ret.append(m)
Пример #9
0
def read_map(filename,
             field=0,
             dtype=npy.float64,
             nest=False,
             hdu=1,
             h=False,
             verbose=False):
    """Read an healpix map from a fits file.

    Input:
      - filename: the fits file name
    Parameters:
      - field: the column to read Default: 0
               by convention 0 is temperature, 1 is Q, 2 is U
               field can be a tuple to read multiple columns (0,1,2)
      - dtype: force the conversion to some type. Default: npy.float64
      - nest=False: if True return the map in NEST ordering, otherwise in RING ordering; 
                    use fits keyword ORDERING to decide whether conversion is needed or not
                    if None, no conversion is performed
      - hdu=1: the header number to look at (start at 0)
      - h=False: if True, return also the header
      - verbose=False: if True, print a number of diagnostic messages
    Return:
      - an array, a tuple of array, possibly with the header at the end if h
        is True
    """
    hdulist = pyf.open(filename)
    #print hdulist[1].header
    nside = int(hdulist[hdu].header.get('NSIDE'))
    if nside is None:
        warnings.warn("No NSIDE in the header file : will use length of array",
                      HealpixFitsWarning)
    if verbose: print 'NSIDE = %d' % nside

    if not pixelfunc.isnsideok(nside):
        raise ValueError('Wrong nside parameter.')
    ordering = hdulist[hdu].header.get('ORDERING', 'UNDEF').strip()
    if ordering == 'UNDEF':
        ordering = (nest and 'NESTED' or 'RING')
        warnings.warn("No ORDERING keyword in header file : "
                      "assume %s" % ordering)
    if verbose: print 'ORDERING = %s in fits file' % ordering

    sz = pixelfunc.nside2npix(nside)
    if not hasattr(field, '__len__'):
        field = (field, )
    ret = []

    for ff in field:
        m = hdulist[hdu].data.field(ff).astype(dtype).ravel()
        if (not pixelfunc.isnpixok(m.size) or
            (sz > 0 and sz != m.size)) and verbose:
            print 'nside=%d, sz=%d, m.size=%d' % (nside, sz, m.size)
            raise ValueError('Wrong nside parameter.')
        if nest != None:  # no conversion with None
            if nest and ordering == 'RING':
                idx = pixelfunc.nest2ring(nside,
                                          npy.arange(m.size, dtype=npy.int32))
                m = m[idx]
                if verbose: print 'Ordering converted to NEST'
            elif (not nest) and ordering == 'NESTED':
                idx = pixelfunc.ring2nest(nside,
                                          npy.arange(m.size, dtype=npy.int32))
                m = m[idx]
                if verbose: print 'Ordering converted to RING'
        try:
            m[pixelfunc.mask_bad(m)] = UNSEEN
        except OverflowError, e:
            pass
        ret.append(m)
Пример #10
0
def read_map(filename,field=0,dtype=npy.float64,nest=False,hdu=1,h=False):
    """Read an healpix map from a fits file.

    Input:
      - filename: the fits file name
    Parameters:
      - field: the column to read Default: 0
               by convention 0 is temperature, 1 is Q, 2 is U
               field can be a tuple to read multiple columns (0,1,2)
      - dtype: force the conversion to some type. Default: npy.float64
      - nest=False: if True return the map in NEST ordering, otherwise in RING ordering; 
                    use fits keyword ORDERING to decide whether conversion is needed or not
                    if None, no conversion is performed
      - hdu=1: the header number to look at (start at 0)
      - h=False: if True, return also the header
    Return:
      - an array, a tuple of array, possibly with the header at the end if h
        is True
    """
    hdulist=pyf.open(filename)
    #print hdulist[1].header
    nside = int(hdulist[hdu].header.get('NSIDE'))
    if nside is None:
        warnings.warn("No NSIDE in the header file : will use length of array",
                      HealpixFitsWarning)
    print 'NSIDE = %d'%nside
    if not pixelfunc.isnsideok(nside):
        raise ValueError('Wrong nside parameter.')
    ordering = hdulist[hdu].header.get('ORDERING','UNDEF').strip()
    if ordering == 'UNDEF':
        ordering = (nest and 'NESTED' or 'RING')
        warnings.warn("No ORDERING keyword in header file : "
                      "assume %s"%ordering)
    print 'ORDERING = %s in fits file'%ordering
    sz=pixelfunc.nside2npix(nside)
    if not hasattr(field, '__len__'):
        field = (field,)
    ret = []

    for ff in field:
        m=hdulist[hdu].data.field(ff).astype(dtype).ravel()
        if not pixelfunc.isnpixok(m.size) or (sz>0 and sz != m.size):
            print 'nside=%d, sz=%d, m.size=%d'%(nside,sz,m.size)
            raise ValueError('Wrong nside parameter.')
        if nest != None: # no conversion with None
            if nest and ordering == 'RING':
                idx = pixelfunc.nest2ring(nside,npy.arange(m.size,dtype=npy.int32))
                m = m[idx]
                print 'Ordering converted to NEST'
            elif (not nest) and ordering == 'NESTED':
                idx = pixelfunc.ring2nest(nside,npy.arange(m.size,dtype=npy.int32))
                m = m[idx]
                print 'Ordering converted to RING'
        m[m<-1.637e30] = UNSEEN
        ret.append(m)
    
    if len(ret) == 1:
        if h:
            return ret[0],hdulist[hdu].header.items()
        else:
            return ret[0]
    else:
        if h:
            ret.append(hdulist[hdu].header.items())
            return tuple(ret)
        else:
            return tuple(ret)