Example #1
0
    def _write_hpx_fits(pixlist):
        """from files that touch a pixel, write out objects in each pixel"""
        pixnum, files = pixlist
        # ADM only proceed if some files touch a pixel.
        if len(files) > 0:
            # ADM track if it's our first time through the files loop.
            first = True
            # ADM Read in files that touch a pixel.
            for file in files:
                filename = os.path.join(fitsdir, file)
                objs = fitsio.read(filename)
                # ADM only retain objects in the correct pixel.
                pix = radec2pix(nside, objs["RA"], objs["DEC"])
                if first:
                    done = objs[pix == pixnum]
                    first = False
                else:
                    done = np.hstack([done, objs[pix == pixnum]])
            # ADM construct the name of the output file.
            outfilename = io.hpx_filename(pixnum)
            outfile = os.path.join(hpxdir, outfilename)
            # ADM write out the file.
            hdr = fitsio.FITSHDR()
            hdr['HPXNSIDE'] = nside
            hdr['HPXNEST'] = True
            fitsio.write(outfile, done, extname='GAIAHPX', header=hdr)

        return
Example #2
0
def find_gaia_files_tiles(tiles=None, neighbors=True):
    """
    Parameters
    ----------
    tiles : :class:`~numpy.ndarray`
        Array of tiles, or ``None`` to use all DESI tiles from
        :func:`desimodel.io.load_tiles`.
    neighbors : :class:`bool`, optional, defaults to ``True``
        Also return all neighboring pixels that touch the files of interest
        in order to prevent edge effects (e.g. if a Gaia source is 1 arcsec
        away from a primary source and so in an adjacent pixel).

    Returns
    -------
    :class:`list`
        A list of all Gaia files that touch the passed tiles.

    Notes
    -----
        - The environment variables $GAIA_DIR and $DESIMODEL must be set.
    """
    # ADM check that the DESIMODEL environment variable is set.
    if os.environ.get('DESIMODEL') is None:
        msg = "DESIMODEL environment variable must be set!!!"
        log.critical(msg)
        raise ValueError(msg)

    # ADM the resolution at which the healpix files are stored.
    nside = _get_gaia_nside()

    # ADM check that the GAIA_DIR is set and retrieve it.
    gaiadir = get_gaia_dir()
    hpxdir = os.path.join(gaiadir, 'healpix')

    # ADM determine the pixels that touch the tiles.
    from desimodel.footprint import tiles2pix
    pixnum = tiles2pix(nside, tiles=tiles)

    # ADM if neighbors was sent, then retrieve all pixels that touch each
    # ADM pixel covered by the provided locations, to prevent edge effects...
    if neighbors:
        pixnum = add_hp_neighbors(nside, pixnum)

    # ADM reformat in the Gaia healpix format used by desitarget.
    gaiafiles = [os.path.join(hpxdir, io.hpx_filename(pn)) for pn in pixnum]

    return gaiafiles
Example #3
0
def find_tycho_files_hp(nside, pixlist, neighbors=True):
    """Find full paths to Tycho healpix files in a set of HEALPixels.

    Parameters
    ----------
    nside : :class:`int`
        (NESTED) HEALPixel nside.
    pixlist : :class:`list` or `int`
        A set of HEALPixels at `nside`.
    neighbors : :class:`bool`, optional, defaults to ``True``
        Also return files corresponding to all neighbors that touch the
        pixels in `pixlist` to prevent edge effects (e.g. a Tycho source
        is 1 arcsec outside of `pixlist` and so in an adjacent pixel).

    Returns
    -------
    :class:`list`
        A list of all Tycho files that need to be read in to account for
        objects in the passed list of pixels.

    Notes
    -----
        - The environment variable $TYCHO_DIR must be set.
    """
    # ADM the resolution at which the healpix files are stored.
    filenside = get_tycho_nside()

    # ADM check that the TYCHO_DIR is set and retrieve it.
    tychodir = get_tycho_dir()
    hpxdir = os.path.join(tychodir, 'healpix')

    # ADM work with pixlist as an array.
    pixlist = np.atleast_1d(pixlist)

    # ADM determine the pixels that touch the passed pixlist.
    pixnum = nside2nside(nside, filenside, pixlist)

    # ADM if neighbors was sent, then retrieve all pixels that touch each
    # ADM pixel covered by the provided locations, to prevent edge effects...
    if neighbors:
        pixnum = add_hp_neighbors(filenside, pixnum)

    # ADM reformat in the healpix format used by desitarget.
    tychofiles = [os.path.join(hpxdir, io.hpx_filename(pn)) for pn in pixnum]

    return tychofiles
Example #4
0
def find_gaia_files_box(gaiabounds, neighbors=True):
    """Find full paths to Gaia healpix files in an RA/Dec box.

    Parameters
    ----------
    gaiabounds : :class:`list`
        A region of the sky bounded by RA/Dec. Pass as a 4-entry list to
        represent an area bounded by [RAmin, RAmax, DECmin, DECmax]
    neighbors : :class:`bool`, optional, defaults to ``True``
        Also return files corresponding to all neighboring pixels that touch
        the files that touch the box in order to prevent edge effects (e.g. if a Gaia
        source might be 1 arcsec outside of the box and so in an adjacent pixel)

    Returns
    -------
    :class:`list`
        A list of all Gaia files that need to be read in to account for objects
        in the passed box.

    Notes
    -----
        - Uses the `healpy` routines that rely on `fact`, so the usual
          warnings about returning different pixel sets at different values
          of `fact` apply. See:
          https://healpy.readthedocs.io/en/latest/generated/healpy.query_polygon.html
        - The environment variable $GAIA_DIR must be set.
    """
    # ADM the resolution at which the healpix files are stored.
    nside = _get_gaia_nside()

    # ADM check that the GAIA_DIR is set and retrieve it.
    gaiadir = get_gaia_dir()
    hpxdir = os.path.join(gaiadir, 'healpix')

    # ADM determine the pixels that touch the box.
    pixnum = hp_in_box(nside, gaiabounds, inclusive=True, fact=4)

    # ADM if neighbors was sent, then retrieve all pixels that touch each
    # ADM pixel covered by the provided locations, to prevent edge effects...
    if neighbors:
        pixnum = add_hp_neighbors(nside, pixnum)

    # ADM reformat in the Gaia healpix format used by desitarget.
    gaiafiles = [os.path.join(hpxdir, io.hpx_filename(pn)) for pn in pixnum]

    return gaiafiles
Example #5
0
def find_gaia_files_beyond_gal_b(mingalb, neighbors=True):
    """Find full paths to Gaia healpix files beyond a Galactic b.

    Parameters
    ----------
    mingalb : :class:`float`
        Closest latitude to Galactic plane to return HEALPixels
        (e.g. send 10 to limit to pixels beyond -10o <= b < 10o).
    neighbors : :class:`bool`, optional, defaults to ``True``
        Also return files corresponding to neighboring pixels that touch
        in order to prevent edge effects (e.g. if a Gaia source might be
        1 arcsec beyond mingalb and so in an adjacent pixel).

    Returns
    -------
    :class:`list`
        All Gaia files that need to be read in to account for objects
        further from the Galactic plane than `mingalb`.

    Notes
    -----
        - The environment variable $GAIA_DIR must be set.
        - :func:`desitarget.geomask.hp_beyond_gal_b()` is already quite
          inclusive, so you may retrieve some extra files along the
          `mingalb` boundary.
    """
    # ADM the resolution at which the healpix files are stored.
    nside = _get_gaia_nside()

    # ADM check that the GAIA_DIR is set and retrieve it.
    gaiadir = get_gaia_dir()
    hpxdir = os.path.join(gaiadir, 'healpix')

    # ADM determine the pixels beyond mingalb.
    pixnum = hp_beyond_gal_b(nside, mingalb, neighbors=True)

    # ADM if neighbors was sent, retrieve all pixels that touch each
    # ADM retrieved, to prevent edge effects...
    if neighbors:
        pixnum = add_hp_neighbors(nside, pixnum)

    # ADM reformat in the Gaia healpix format used by desitarget.
    gaiafiles = [os.path.join(hpxdir, io.hpx_filename(pn)) for pn in pixnum]

    return gaiafiles
Example #6
0
def tycho_fits_to_healpix():
    """Convert files in $TYCHO_DIR/fits to files in $TYCHO_DIR/healpix.

    Returns
    -------
    Nothing
        But the archived Tycho FITS files in $TYCHO_DIR/fits are
        rearranged by HEALPixel in the directory $TYCHO_DIR/healpix.
        The HEALPixel sense is nested with nside=get_tycho_nside(), and
        each file in $TYCHO_DIR/healpix is called healpix-xxxxx.fits,
        where xxxxx corresponds to the HEALPixel number.

    Notes
    -----
        - The environment variable $TYCHO_DIR must be set.
    """
    # ADM the resolution at which the Tycho HEALPix files are stored.
    nside = get_tycho_nside()
    npix = hp.nside2npix(nside)

    # ADM check that the TYCHO_DIR is set.
    tychodir = get_tycho_dir()

    # ADM construct the directories for reading/writing files.
    fitsdir = os.path.join(tychodir, "fits")
    tychofn = os.path.join(fitsdir, "tycho2.kd.fits")
    hpxdir = os.path.join(tychodir, "healpix")

    # ADM make sure the output directory is empty.
    if os.path.exists(hpxdir):
        if len(os.listdir(hpxdir)) > 0:
            msg = "{} must be empty to make Tycho HEALPix files!".format(hpxdir)
            log.critical(msg)
            raise ValueError(msg)
    # ADM make the output directory, if needed.
    else:
        log.info("Making Tycho directory for storing HEALPix files")
        os.makedirs(hpxdir)

    # ADM read in the Tycho file and assing Tycho objects to HEALPixels.
    objs, allhdr = fitsio.read(tychofn, header=True, upper=True)
    pix = radec2pix(nside, objs["RA"], objs["DEC"])

    # ADM loop through the pixels and write out the files.
    for pixnum in range(npix):
        # ADM construct the name of the output file.
        outfilename = io.hpx_filename(pixnum)
        outfile = os.path.join(hpxdir, outfilename)
        # ADM update the header with new information.
        hdr = dict(allhdr).copy()
        hdr["HPXNSIDE"] = nside
        hdr["HPXNEST"] = True
        hdr["HPXDATE"] = datetime.utcnow().isoformat(timespec='seconds')

        # ADM determine which objects are in this pixel and write out.
        done = objs[pix == pixnum]

        fitsio.write(outfile, done, extname="TYCHOHPX", header=hdr)

    log.info('Wrote Tycho HEALPix files...t={:.1f}s'.format(time()-start))

    return