Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 3
0
def in_ring(nside, iz, phi0, dphi, nest=False):
    """Compute the list of pixels in ring number iz in phi interval [phi0,phi0+dphi]

	Input:
	 - nside: a power of 2
	 - iz: ring number
	 - phi0: the starting longitude
	 - dphi: interval of longitude
	Keyword:
	 - nested: if True, return pixel number in nested scheme. Default: False (RING)
	Return:
	 - list of pixel numbers
	"""
    from numpy import pi, arange, concatenate, hstack, fabs, round
    from pixelfunc import nside2npix, ring2nest

    npix = nside2npix(nside)
    take_all = 0
    to_top = 0
    twopi = 2.0 * pi
    ncap = 2 * nside * (nside - 1)
    listir = -1
    nir = 0

    phi_low = (phi0 - dphi) % twopi
    if (phi_low < 0): phi_low = phi_low + twopi

    phi_hi = (phi0 + dphi) % twopi

    if (phi_hi < 0): phi_hi = phi_hi + twopi
    if (fabs(dphi - pi) < 1e-6): take_all = 1

    # equatorial region
    if ((iz >= nside) & (iz <= 3 * nside)):
        ir = iz - nside + 1
        ipix1 = ncap + 4 * nside * (ir - 1)
        ipix2 = ipix1 + 4 * nside - 1
        kshift = ir % 2
        nr = nside * 4
    else:
        if (iz < nside):
            ir = iz
            ipix1 = 2 * ir * (ir - 1)
            ipix2 = ipix1 + 4 * ir - 1
        else:
            ir = 4 * nside - iz
            ipix1 = npix - 2 * ir * (ir + 1)
            ipix2 = ipix1 + 4 * ir - 1
        nr = ir * 4
        kshift = 1

    if (take_all == 1):
        nir = ipix2 - ipix1 + 1
        listir = arange(ipix1, ipix2 + 1, 1)
    if (take_all == 0):
        shift = kshift * .5
        ip_low = int(round(nr * phi_low / twopi - shift))
        ip_hi = int(round(nr * phi_hi / twopi - shift))
        ip_low = ip_low % nr
        ip_hi = ip_hi % nr

        if (ip_low > ip_hi): to_top = 1

        ip_low = ip_low + ipix1
        ip_hi = ip_hi + ipix1

        if (to_top == 1):
            nir1 = ipix2 - ip_low + 1
            nir2 = ip_hi - ipix1 + 1
            nir = nir1 + nir2
            if ((nir1 > 0) & (nir2 > 0)):
                #listir   = concatenate(arange(0,nir2,1)+ipix1, arange(0,nir1,1)+ip_low)
                list1 = arange(0, nir1, 1) + ip_low
                list2 = arange(0, nir2, 1) + ipix1
                listir = concatenate((list1, list2))
            else:
                if (nir1 == 0): listir = arange(0, nir2, 1) + ipix1
                if (nir2 == 0): listir = arange(0, nir1, 1) + ip_low
        else:
            nir = ip_hi - ip_low + 1
            listir = arange(0, nir, 1) + ip_low

    if (nest): listir = ring2nest(nside, listir)

    return listir
Ejemplo n.º 4
0
def in_ring(nside,iz,phi0,dphi,nest=False):
	"""Compute the list of pixels in ring number iz in phi interval [phi0,phi0+dphi]

	Input:
	 - nside: a power of 2
	 - iz: ring number
	 - phi0: the starting longitude
	 - dphi: interval of longitude
	Keyword:
	 - nested: if True, return pixel number in nested scheme. Default: False (RING)
	Return:
	 - list of pixel numbers
	"""
	from numpy import pi,arange,concatenate,hstack,fabs,round
	from pixelfunc import nside2npix,ring2nest
	
	npix = nside2npix(nside)
	take_all = 0
	to_top = 0
	twopi = 2.0 * pi
	ncap  = 2*nside*(nside-1)
	listir = -1
	nir = 0

	phi_low = (phi0 - dphi) % twopi
	if (phi_low < 0): phi_low = phi_low + twopi
	
	phi_hi  = (phi0 + dphi) % twopi
	
	if (phi_hi < 0): phi_hi  = phi_hi  + twopi
	if (fabs(dphi-pi) < 1e-6): take_all = 1

	
	# equatorial region
	if ((iz >= nside) & (iz <= 3*nside)):
    		ir = iz - nside + 1
    		ipix1 = ncap + 4*nside*(ir-1)
    		ipix2 = ipix1 + 4*nside - 1
    		kshift = ir % 2
    		nr = nside*4
	else:
    		if (iz < nside):
        		ir = iz
        		ipix1 = 2*ir*(ir-1)
        		ipix2 = ipix1 + 4*ir - 1
		else:
        		ir = 4*nside - iz
        		ipix1 = npix - 2*ir*(ir+1)
        		ipix2 = ipix1 + 4*ir - 1
    		nr = ir*4
    		kshift = 1

	if (take_all == 1):
    		nir    = ipix2 - ipix1 + 1
		listir = arange(ipix1,ipix2+1,1)
	if (take_all == 0):
		shift = kshift * .5
    		ip_low = int(round (nr * phi_low / twopi - shift))
    		ip_hi  = int(round(nr * phi_hi  / twopi - shift))
    		ip_low = ip_low % nr     
    		ip_hi  = ip_hi  % nr      

		if (ip_low > ip_hi): to_top = 1
		
		ip_low = ip_low + ipix1
		ip_hi  = ip_hi  + ipix1
		

		if (to_top == 1):
		    	nir1 = ipix2 - ip_low + 1
    			nir2 = ip_hi - ipix1  + 1
    			nir  = nir1 + nir2
 			if ((nir1 > 0) & (nir2 > 0)):
        			#listir   = concatenate(arange(0,nir2,1)+ipix1, arange(0,nir1,1)+ip_low)
				list1 = arange(0,nir1,1)+ip_low
				list2 = arange(0,nir2,1)+ipix1
				listir = concatenate((list1,list2))
			else:
        			if (nir1 == 0) : listir = arange(0,nir2,1)+ipix1
				if (nir2 == 0) : listir = arange(0,nir1,1)+ip_low
      		else:
			nir = ip_hi - ip_low + 1
			listir = arange(0,nir,1)+ip_low  

	if (nest): listir = ring2nest(nside,listir)

	return listir
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)