(
    filename,
    m,
    nest=False,
    dtype=None,
    fits_IDL=True,
    coord=None,
    partial=False,
    column_names=None,
    column_units=None,
    extra_header=(),
    overwrite=False,
)
Writes a healpix map into a healpix FITS file.

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

    Parameters
    ----------
    filename : str
      the fits file name
    m : array or sequence of 3 arrays
      the map to write. Possibly a sequence of 3 maps of same size.
      They will be considered as I, Q, U maps.
      Supports masked maps, see the `ma` function.
    nest : bool, optional
      If True, ordering scheme is assumed to be NESTED, otherwise, RING. Default: RING.
      The map ordering is not modified by this function, the input map array
      should already be in the desired ordering (run `ud_grade` beforehand).
    fits_IDL : bool, optional
      If True, reshapes columns in rows of 1024, otherwise all the data will
      go in one column. Default: True
    coord : str
      The coordinate system, typically 'E' for Ecliptic, 'G' for Galactic or 'C' for
      Celestial (equatorial)
    partial : bool, optional
      If True, fits file is written as a partial-sky file with explicit indexing.
      Otherwise, implicit indexing is used.  Default: False.
    column_names : str or list
      Column name or list of column names, if None here the default column names based on
      the number of columns:
      1 : "TEMPERATURE",
      2 : ["Q_POLARISATION", "U_POLARISATION"],
      3 : ["TEMPERATURE", "Q_POLARISATION", "U_POLARISATION"],
      6 : ["II", "IQ", "IU", "QQ", "QU", "UU"]
      COLUMN_1, COLUMN_2... otherwise (FITS is 1-based)
    column_units : str or list
      Units for each column, or same units for all columns.
    extra_header : list
      Extra records to add to FITS header.
    dtype: np.dtype or list of np.dtypes, optional
      The datatype in which the columns will be stored. Will be converted
      internally from the numpy datatype to the fits convention. If a list,
      the length must correspond to the number of map arrays.
      Default: use the data type of the input array(s)
      .. note::
      this changed in 1.15.0, previous versions saved in float32
      by default
    overwrite : bool, optional
      If True, existing file is silently overwritten. Otherwise trying to write
      an existing file raises an OSError (IOError for Python 2).
Writes a healpix map into a healpix FITS file.
Writes a healpix map into a healpix FITS file .
def write_map(
    filename,
    m,
    nest=False,
    dtype=None,
    fits_IDL=True,
    coord=None,
    partial=False,
    column_names=None,
    column_units=None,
    extra_header=(),
    overwrite=False,
):
    """Writes a healpix map into a healpix FITS file.

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

    Parameters
    ----------
    filename : str
      the fits file name
    m : array or sequence of 3 arrays
      the map to write. Possibly a sequence of 3 maps of same size.
      They will be considered as I, Q, U maps.
      Supports masked maps, see the `ma` function.
    nest : bool, optional
      If True, ordering scheme is assumed to be NESTED, otherwise, RING. Default: RING.
      The map ordering is not modified by this function, the input map array
      should already be in the desired ordering (run `ud_grade` beforehand).
    fits_IDL : bool, optional
      If True, reshapes columns in rows of 1024, otherwise all the data will
      go in one column. Default: True
    coord : str
      The coordinate system, typically 'E' for Ecliptic, 'G' for Galactic or 'C' for
      Celestial (equatorial)
    partial : bool, optional
      If True, fits file is written as a partial-sky file with explicit indexing.
      Otherwise, implicit indexing is used.  Default: False.
    column_names : str or list
      Column name or list of column names, if None here the default column names based on
      the number of columns:
      1 : "TEMPERATURE",
      2 : ["Q_POLARISATION", "U_POLARISATION"],
      3 : ["TEMPERATURE", "Q_POLARISATION", "U_POLARISATION"],
      6 : ["II", "IQ", "IU", "QQ", "QU", "UU"]
      COLUMN_1, COLUMN_2... otherwise (FITS is 1-based)
    column_units : str or list
      Units for each column, or same units for all columns.
    extra_header : list
      Extra records to add to FITS header.
    dtype: np.dtype or list of np.dtypes, optional
      The datatype in which the columns will be stored. Will be converted
      internally from the numpy datatype to the fits convention. If a list,
      the length must correspond to the number of map arrays.
      Default: use the data type of the input array(s)
      .. note::
      this changed in 1.15.0, previous versions saved in float32
      by default
    overwrite : bool, optional
      If True, existing file is silently overwritten. Otherwise trying to write
      an existing file raises an OSError (IOError for Python 2).
    """
    if not hasattr(m, "__len__"):
        raise TypeError("The map must be a sequence")

    m = pixelfunc.ma_to_array(m)
    if pixelfunc.maptype(m) == 0:  # a single map is converted to a list
        m = [m]

    # check the dtype and convert it
    if dtype is None:
        dtype = [x.dtype for x in m]
        log.warning("setting the output map dtype to %s" % str(dtype))
    try:
        fitsformat = []
        for curr_dtype in dtype:
            fitsformat.append(getformat(curr_dtype))
    except TypeError:
        # dtype is not iterable
        fitsformat = [getformat(dtype)] * len(m)

    if column_names is None:
        column_names = standard_column_names.get(
            len(m), ["COLUMN_%d" % n for n in range(1, len(m) + 1)]
        )
    else:
        assert len(column_names) == len(m), "Length column_names != number of maps"

    if column_units is None or isinstance(column_units, str):
        column_units = [column_units] * len(m)

    # maps must have same length
    assert len(set(map(len, m))) == 1, "Maps must have same length"
    nside = pixelfunc.npix2nside(len(m[0]))

    if nside < 0:
        raise ValueError("Invalid healpix map : wrong number of pixel")

    cols = []
    if partial:
        fits_IDL = False
        mask = pixelfunc.mask_good(m[0])
        pix = np.where(mask)[0]
        if len(pix) == 0:
            raise ValueError("Invalid healpix map : empty partial map")
        m = [mm[mask] for mm in m]
        ff = getformat(np.min_scalar_type(-pix.max()))
        if ff is None:
            ff = "I"
        cols.append(pf.Column(name="PIXEL", format=ff, array=pix, unit=None))

    for cn, cu, mm, curr_fitsformat in zip(column_names, column_units, m, fitsformat):
        if len(mm) > 1024 and fits_IDL:
            # I need an ndarray, for reshape:
            mm2 = np.asarray(mm)
            cols.append(
                pf.Column(
                    name=cn,
                    format="1024%s" % curr_fitsformat,
                    array=mm2.reshape(mm2.size // 1024, 1024),
                    unit=cu,
                )
            )
        else:
            cols.append(
                pf.Column(name=cn, format="%s" % curr_fitsformat, array=mm, unit=cu)
            )

    tbhdu = pf.BinTableHDU.from_columns(cols)
    # add needed keywords
    tbhdu.header["PIXTYPE"] = ("HEALPIX", "HEALPIX pixelisation")
    if nest:
        ordering = "NESTED"
    else:
        ordering = "RING"
    tbhdu.header["ORDERING"] = (
        ordering,
        "Pixel ordering scheme, either RING or NESTED",
    )
    if coord:
        tbhdu.header["COORDSYS"] = (
            coord,
            "Ecliptic, Galactic or Celestial (equatorial)",
        )
    tbhdu.header["EXTNAME"] = ("xtension", "name of this binary table extension")
    tbhdu.header["NSIDE"] = (nside, "Resolution parameter of HEALPIX")
    if not partial:
        tbhdu.header["FIRSTPIX"] = (0, "First pixel # (0 based)")
        tbhdu.header["LASTPIX"] = (
            pixelfunc.nside2npix(nside) - 1,
            "Last pixel # (0 based)",
        )
    tbhdu.header["INDXSCHM"] = (
        "EXPLICIT" if partial else "IMPLICIT",
        "Indexing: IMPLICIT or EXPLICIT",
    )
    tbhdu.header["OBJECT"] = (
        "PARTIAL" if partial else "FULLSKY",
        "Sky coverage, either FULLSKY or PARTIAL",
    )

    # FIXME: In modern versions of Pyfits, header.update() understands a
    # header as an argument, and headers can be concatenated with the `+'
    # operator.
    for args in extra_header:
        tbhdu.header[args[0]] = args[1:]

    # Add str to convert pathlib.Path into str
    # Due to https://github.com/astropy/astropy/issues/10594
    tbhdu.writeto(str(filename), overwrite=overwrite)
def write_map ( filename , m , nest = False , dtype = None , fits_IDL = True , coord = None , partial = False , column_names = None , column_units = None , extra_header = ( ) , overwrite = False , ) : if not hasattr ( m , "__len__" ) : raise TypeError ( "The map must be a sequence" ) m = pixelfunc . ma_to_array ( m ) if pixelfunc . maptype ( m ) == 0 : # a single map is converted to a list m = [ m ] # check the dtype and convert it if dtype is None : dtype = [ x . dtype for x in m ] log . warning ( "setting the output map dtype to %s" % str ( dtype ) ) try : fitsformat = [ ] for curr_dtype in dtype : fitsformat . append ( getformat ( curr_dtype ) ) except TypeError : # dtype is not iterable fitsformat = [ getformat ( dtype ) ] * len ( m ) if column_names is None : column_names = standard_column_names . get ( len ( m ) , [ "COLUMN_%d" % n for n in range ( 1 , len ( m ) + 1 ) ] ) else : assert len ( column_names ) == len ( m ) , "Length column_names != number of maps" if column_units is None or isinstance ( column_units , str ) : column_units = [ column_units ] * len ( m ) # maps must have same length assert len ( set ( map ( len , m ) ) ) == 1 , "Maps must have same length" nside = pixelfunc . npix2nside ( len ( m [ 0 ] ) ) if nside < 0 : raise ValueError ( "Invalid healpix map : wrong number of pixel" ) cols = [ ] if partial : fits_IDL = False mask = pixelfunc . mask_good ( m [ 0 ] ) pix = np . where ( mask ) [ 0 ] if len ( pix ) == 0 : raise ValueError ( "Invalid healpix map : empty partial map" ) m = [ mm [ mask ] for mm in m ] ff = getformat ( np . min_scalar_type ( - pix . max ( ) ) ) if ff is None : ff = "I" cols . append ( pf . Column ( name = "PIXEL" , format = ff , array = pix , unit = None ) ) for cn , cu , mm , curr_fitsformat in zip ( column_names , column_units , m , fitsformat ) : if len ( mm ) > 1024 and fits_IDL : # I need an ndarray, for reshape: mm2 = np . asarray ( mm ) cols . append ( pf . Column ( name = cn , format = "1024%s" % curr_fitsformat , array = mm2 . reshape ( mm2 . size // 1024 , 1024 ) , unit = cu , ) ) else : cols . append ( pf . Column ( name = cn , format = "%s" % curr_fitsformat , array = mm , unit = cu ) ) tbhdu = pf . BinTableHDU . from_columns ( cols ) # add needed keywords tbhdu . header [ "PIXTYPE" ] = ( "HEALPIX" , "HEALPIX pixelisation" ) if nest : ordering = "NESTED" else : ordering = "RING" tbhdu . header [ "ORDERING" ] = ( ordering , "Pixel ordering scheme, either RING or NESTED" , ) if coord : tbhdu . header [ "COORDSYS" ] = ( coord , "Ecliptic, Galactic or Celestial (equatorial)" , ) tbhdu . header [ "EXTNAME" ] = ( "xtension" , "name of this binary table extension" ) tbhdu . header [ "NSIDE" ] = ( nside , "Resolution parameter of HEALPIX" ) if not partial : tbhdu . header [ "FIRSTPIX" ] = ( 0 , "First pixel # (0 based)" ) tbhdu . header [ "LASTPIX" ] = ( pixelfunc . nside2npix ( nside ) - 1 , "Last pixel # (0 based)" , ) tbhdu . header [ "INDXSCHM" ] = ( "EXPLICIT" if partial else "IMPLICIT" , "Indexing: IMPLICIT or EXPLICIT" , ) tbhdu . header [ "OBJECT" ] = ( "PARTIAL" if partial else "FULLSKY" , "Sky coverage, either FULLSKY or PARTIAL" , ) # FIXME: In modern versions of Pyfits, header.update() understands a # header as an argument, and headers can be concatenated with the `+' # operator. for args in extra_header : tbhdu . header [ args [ 0 ] ] = args [ 1 : ] # Add str to convert pathlib.Path into str # Due to https://github.com/astropy/astropy/issues/10594 tbhdu . writeto ( str ( filename ) , overwrite = overwrite )