(
    filename,
    field=0,
    dtype=None,
    nest=False,
    partial=False,
    hdu=1,
    h=False,
    verbose=True,
    memmap=False,
)
Read a healpix map from a fits file.  Partial-sky files,
    if properly identified, are expanded to full size and filled with UNSEEN.

    .. warning::
    Starting from healpy 1.15.0, if you do not specify `dtype`,
    the map will be read in memory with the same precision it is stored on disk.
    Previously, by default `healpy` wrote maps in `float32` and then upcast to
    `float64` when reading to memory. To reproduce the same behaviour of `healpy`
    1.14.0 and below, set `dtype=np.float64` in `read_map`.

    Parameters
    ----------
    filename : str or HDU or HDUList or pathlib.Path instance
      the fits file name
    field : int or tuple of int, or None, 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)
      If the fits file is a partial-sky file, field=0 corresponds to the
      first column after the pixel index column.
      If None, all columns are read in.
    dtype : data type or list of data types, optional
      Force the conversion to some type. Passing a list allows different
      types for each field. In that case, the length of the list must
      correspond to the length of the field parameter.
      If None, keep the dtype of the input FITS file
      Default: Preserve the data types in the file
    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.
    partial : bool, optional
      If True, fits file is assumed to be a partial-sky file with explicit indexing,
      if the indexing scheme cannot be determined from the header.
      If False, implicit indexing is assumed.  Default: False.
      A partial sky file is one in which OBJECT=PARTIAL and INDXSCHM=EXPLICIT,
      and the first column is then assumed to contain pixel indices.
      A full sky file is one in which OBJECT=FULLSKY and INDXSCHM=IMPLICIT.
      At least one of these keywords must be set for the indexing
      scheme to be properly identified.
    hdu : int, optional
      the header number to look at (start at 0)
    h : bool, optional
      If True, return also the header. Default: False.
    verbose : bool, deprecated
      It has no effect
    memmap : bool, optional
      Argument passed to astropy.io.fits.open, if True, the map is not read into memory,
      but only the required pixels are read when needed. Default: False.

    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.
Read a healpix map from a fits file.  Partial-sky files,
    if properly identified, are expanded to full size and filled with UNSEEN.
Read a healpix map from a fits file . Partial - sky files if properly identified are expanded to full size and filled with UNSEEN .
def read_map(
    filename,
    field=0,
    dtype=None,
    nest=False,
    partial=False,
    hdu=1,
    h=False,
    verbose=True,
    memmap=False,
):
    """Read a healpix map from a fits file.  Partial-sky files,
    if properly identified, are expanded to full size and filled with UNSEEN.

    .. warning::
    Starting from healpy 1.15.0, if you do not specify `dtype`,
    the map will be read in memory with the same precision it is stored on disk.
    Previously, by default `healpy` wrote maps in `float32` and then upcast to
    `float64` when reading to memory. To reproduce the same behaviour of `healpy`
    1.14.0 and below, set `dtype=np.float64` in `read_map`.

    Parameters
    ----------
    filename : str or HDU or HDUList or pathlib.Path instance
      the fits file name
    field : int or tuple of int, or None, 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)
      If the fits file is a partial-sky file, field=0 corresponds to the
      first column after the pixel index column.
      If None, all columns are read in.
    dtype : data type or list of data types, optional
      Force the conversion to some type. Passing a list allows different
      types for each field. In that case, the length of the list must
      correspond to the length of the field parameter.
      If None, keep the dtype of the input FITS file
      Default: Preserve the data types in the file
    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.
    partial : bool, optional
      If True, fits file is assumed to be a partial-sky file with explicit indexing,
      if the indexing scheme cannot be determined from the header.
      If False, implicit indexing is assumed.  Default: False.
      A partial sky file is one in which OBJECT=PARTIAL and INDXSCHM=EXPLICIT,
      and the first column is then assumed to contain pixel indices.
      A full sky file is one in which OBJECT=FULLSKY and INDXSCHM=IMPLICIT.
      At least one of these keywords must be set for the indexing
      scheme to be properly identified.
    hdu : int, optional
      the header number to look at (start at 0)
    h : bool, optional
      If True, return also the header. Default: False.
    verbose : bool, deprecated
      It has no effect
    memmap : bool, optional
      Argument passed to astropy.io.fits.open, if True, the map is not read into memory,
      but only the required pixels are read when needed. Default: False.

    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.
    """

    opened_file = False
    if isinstance(filename, allowed_paths):
        filename = pf.open(filename, memmap=memmap)
        opened_file = True

    fits_hdu = _get_hdu(filename, hdu=hdu, memmap=memmap)

    nside = fits_hdu.header.get("NSIDE")
    if nside is None:
        log.info("No NSIDE in the header file : will use length of array")
        pix = fits_hdu.data.field(0)
        nside = pixelfunc.npix2nside(pix.size)
    else:
        nside = int(nside)
    log.info("NSIDE = %d", nside)

    if not pixelfunc.isnsideok(nside):
        raise ValueError("Wrong nside parameter.")
    ordering = fits_hdu.header.get("ORDERING", "UNDEF").strip()
    if ordering == "UNDEF":
        ordering = nest and "NESTED" or "RING"
        log.info("No ORDERING keyword in header file : assume %s", ordering)
    log.info("ORDERING = %s in fits file", ordering)

    sz = pixelfunc.nside2npix(nside)
    ret = []

    # partial sky: check OBJECT, then INDXSCHM
    obj = str(fits_hdu.header.get("OBJECT", "UNDEF")).strip()
    if obj != "UNDEF":
        if obj == "PARTIAL":
            partial = True
        elif obj == "FULLSKY":
            partial = False

    schm = fits_hdu.header.get("INDXSCHM", "UNDEF").strip()
    if schm != "UNDEF":
        if schm == "EXPLICIT":
            if obj == "FULLSKY":
                raise ValueError("Incompatible INDXSCHM keyword")
            partial = True
        elif schm == "IMPLICIT":
            if obj == "PARTIAL":
                raise ValueError("Incompatible INDXSCHM keyword")
            partial = False

    if schm == "UNDEF":
        schm = partial and "EXPLICIT" or "IMPLICIT"
        log.info("No INDXSCHM keyword in header file: assume %s", schm)
    log.info("INDXSCHM = %s", schm)

    if field is None:
        field = range(len(fits_hdu.data.columns) - 1 * partial)
    if not (hasattr(field, "__len__") or isinstance(field, str)):
        field = (field,)

    if partial:
        # increment field counters
        field = tuple(f if isinstance(f, str) else f + 1 for f in field)
        try:
            pix = fits_hdu.data.field(0).astype(int, copy=False).ravel()
        except pf.VerifyError as e:
            log.warning(e)
            log.warning("Trying to fix a badly formatted header")
            fits_hdu.verify("fix")
            pix = fits_hdu.data.field(0).astype(int, copy=False).ravel()

    try:
        assert len(dtype) == len(
            field
        ), "The number of dtypes are not equal to the number of fields"
    except TypeError:
        dtype = [dtype] * len(field)

    for ff, curr_dtype in zip(field, dtype):
        try:
            if curr_dtype is None:
                m = fits_hdu.data.field(ff).ravel()
            else:
                m = fits_hdu.data.field(ff).astype(curr_dtype, copy=False).ravel()
        except pf.VerifyError as e:
            log.warning(e)
            log.warning("Trying to fix a badly formatted header")
            m = fits_hdu.verify("fix")
            if curr_dtype is None:
                m = fits_hdu.data.field(ff).ravel()
            else:
                m = fits_hdu.data.field(ff).astype(curr_dtype, copy=False).ravel()

        if partial:
            mnew = UNSEEN * np.ones(sz, dtype=m.dtype)
            mnew[pix] = m
            m = mnew

        if not pixelfunc.isnpixok(m.size) or (sz > 0 and sz != m.size):
            log.warning("nside={0:d}, sz={1:d}, m.size={2:d}".format(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]
                log.info("Ordering converted to NEST")
            elif (not nest) and ordering == "NESTED":
                idx = pixelfunc.ring2nest(nside, np.arange(m.size, dtype=np.int32))
                m = m[idx]
                log.info("Ordering converted to RING")
        try:
            m[pixelfunc.mask_bad(m)] = UNSEEN
        except OverflowError:
            pass
        ret.append(m)

    if h:
        header = []
        for (key, value) in fits_hdu.header.items():
            header.append((key, value))

    if opened_file:
        filename.close()

    if len(ret) == 1:
        if h:
            return ret[0], header
        else:
            return ret[0]
    else:
        if all(dt == dtype[0] for dt in dtype):
            ret = np.array(ret)
        if h:
            return ret, header
        else:
            return ret
def read_map ( filename , field = 0 , dtype = None , nest = False , partial = False , hdu = 1 , h = False , verbose = True , memmap = False , ) : opened_file = False if isinstance ( filename , allowed_paths ) : filename = pf . open ( filename , memmap = memmap ) opened_file = True fits_hdu = _get_hdu ( filename , hdu = hdu , memmap = memmap ) nside = fits_hdu . header . get ( "NSIDE" ) if nside is None : log . info ( "No NSIDE in the header file : will use length of array" ) pix = fits_hdu . data . field ( 0 ) nside = pixelfunc . npix2nside ( pix . size ) else : nside = int ( nside ) log . info ( "NSIDE = %d" , nside ) if not pixelfunc . isnsideok ( nside ) : raise ValueError ( "Wrong nside parameter." ) ordering = fits_hdu . header . get ( "ORDERING" , "UNDEF" ) . strip ( ) if ordering == "UNDEF" : ordering = nest and "NESTED" or "RING" log . info ( "No ORDERING keyword in header file : assume %s" , ordering ) log . info ( "ORDERING = %s in fits file" , ordering ) sz = pixelfunc . nside2npix ( nside ) ret = [ ] # partial sky: check OBJECT, then INDXSCHM obj = str ( fits_hdu . header . get ( "OBJECT" , "UNDEF" ) ) . strip ( ) if obj != "UNDEF" : if obj == "PARTIAL" : partial = True elif obj == "FULLSKY" : partial = False schm = fits_hdu . header . get ( "INDXSCHM" , "UNDEF" ) . strip ( ) if schm != "UNDEF" : if schm == "EXPLICIT" : if obj == "FULLSKY" : raise ValueError ( "Incompatible INDXSCHM keyword" ) partial = True elif schm == "IMPLICIT" : if obj == "PARTIAL" : raise ValueError ( "Incompatible INDXSCHM keyword" ) partial = False if schm == "UNDEF" : schm = partial and "EXPLICIT" or "IMPLICIT" log . info ( "No INDXSCHM keyword in header file: assume %s" , schm ) log . info ( "INDXSCHM = %s" , schm ) if field is None : field = range ( len ( fits_hdu . data . columns ) - 1 * partial ) if not ( hasattr ( field , "__len__" ) or isinstance ( field , str ) ) : field = ( field , ) if partial : # increment field counters field = tuple ( f if isinstance ( f , str ) else f + 1 for f in field ) try : pix = fits_hdu . data . field ( 0 ) . astype ( int , copy = False ) . ravel ( ) except pf . VerifyError as e : log . warning ( e ) log . warning ( "Trying to fix a badly formatted header" ) fits_hdu . verify ( "fix" ) pix = fits_hdu . data . field ( 0 ) . astype ( int , copy = False ) . ravel ( ) try : assert len ( dtype ) == len ( field ) , "The number of dtypes are not equal to the number of fields" except TypeError : dtype = [ dtype ] * len ( field ) for ff , curr_dtype in zip ( field , dtype ) : try : if curr_dtype is None : m = fits_hdu . data . field ( ff ) . ravel ( ) else : m = fits_hdu . data . field ( ff ) . astype ( curr_dtype , copy = False ) . ravel ( ) except pf . VerifyError as e : log . warning ( e ) log . warning ( "Trying to fix a badly formatted header" ) m = fits_hdu . verify ( "fix" ) if curr_dtype is None : m = fits_hdu . data . field ( ff ) . ravel ( ) else : m = fits_hdu . data . field ( ff ) . astype ( curr_dtype , copy = False ) . ravel ( ) if partial : mnew = UNSEEN * np . ones ( sz , dtype = m . dtype ) mnew [ pix ] = m m = mnew if not pixelfunc . isnpixok ( m . size ) or ( sz > 0 and sz != m . size ) : log . warning ( "nside={0:d}, sz={1:d}, m.size={2:d}" . format ( 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 ] log . info ( "Ordering converted to NEST" ) elif ( not nest ) and ordering == "NESTED" : idx = pixelfunc . ring2nest ( nside , np . arange ( m . size , dtype = np . int32 ) ) m = m [ idx ] log . info ( "Ordering converted to RING" ) try : m [ pixelfunc . mask_bad ( m ) ] = UNSEEN except OverflowError : pass ret . append ( m ) if h : header = [ ] for ( key , value ) in fits_hdu . header . items ( ) : header . append ( ( key , value ) ) if opened_file : filename . close ( ) if len ( ret ) == 1 : if h : return ret [ 0 ] , header else : return ret [ 0 ] else : if all ( dt == dtype [ 0 ] for dt in dtype ) : ret = np . array ( ret ) if h : return ret , header else : return ret