예제 #1
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)
예제 #2
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)
예제 #3
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)
예제 #4
0
파일: fitsfunc.py 프로젝트: afarahi/XCat
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)