예제 #1
0
def read_targets_in_box(hpdirname, radecbox=[0., 360., -90., 90.],
                        columns=None):
    """Read in targets in an RA/Dec box.

    Parameters
    ----------
    hpdirname : :class:`str`
        Full path to either a directory containing targets that
        have been partitioned by HEALPixel (i.e. as made by
        `select_targets` with the `bundle_files` option). Or the
        name of a single file of targets.
    radecbox : :class:`list`, defaults to the entire sky
        4-entry list of coordinates [ramin, ramax, decmin, decmax]
        forming the edges of a box in RA/Dec (degrees).
    columns : :class:`list`, optional
        Only read in these target columns.

    Returns
    -------
    :class:`~numpy.ndarray`
        An array of targets in the passed RA/Dec box.
    """
    # ADM we'll need RA/Dec for final cuts, so ensure they're read.
    addedcols = []
    columnscopy = None
    if columns is not None:
        # ADM make a copy of columns, as it's a kwarg we'll modify.
        columnscopy = columns.copy()
        for radec in ["RA", "DEC"]:
            if radec not in columnscopy:
                columnscopy.append(radec)
                addedcols.append(radec)

    # ADM if a directory was passed, do fancy HEALPixel parsing...
    if os.path.isdir(hpdirname):
        # ADM approximate nside for area of passed box.
        nside = pixarea2nside(box_area(radecbox))

        # ADM HEALPixels that touch the box for that nside.
        pixlist = hp_in_box(nside, radecbox)

        # ADM read in targets in these HEALPixels.
        targets = read_targets_in_hp(hpdirname, nside, pixlist,
                                     columns=columnscopy)
    # ADM ...otherwise just read in the targets.
    else:
        targets = fitsio.read(hpdirname, columns=columnscopy)

    # ADM restrict only to targets in the requested RA/Dec box...
    ii = is_in_box(targets, radecbox)
    # ADM ...and remove RA/Dec columns if we added them.
    targets = rfn.drop_fields(targets[ii], addedcols)

    return targets
예제 #2
0
def gaia_in_file(infile, maglim=18, mindec=-30., mingalb=10.):
    """Retrieve the Gaia objects from a HEALPixel-split Gaia file.

    Parameters
    ----------
    infile : :class:`str`
        File name of a single Gaia "healpix" file.
    maglim : :class:`float`, optional, defaults to 18
        Magnitude limit for GFAs in Gaia G-band.
    mindec : :class:`float`, optional, defaults to -30
        Minimum declination (o) to include for output Gaia objects.
    mingalb : :class:`float`, optional, defaults to 10
        Closest latitude to Galactic plane for output Gaia objects
        (e.g. send 10 to limit to areas beyond -10o <= b < 10o)"

    Returns
    -------
    :class:`~numpy.ndarray`
        Gaia objects in the passed Gaia file brighter than `maglim`,
        formatted according to `desitarget.gfa.gfadatamodel`.

    Notes
    -----
       - A "Gaia healpix file" here is as made by, e.g.
         :func:`~desitarget.gaiamatch.gaia_fits_to_healpix()`
    """
    # ADM read in the Gaia file and limit to the passed magnitude.
    objs = read_gaia_file(infile)
    ii = objs['GAIA_PHOT_G_MEAN_MAG'] < maglim
    objs = objs[ii]

    # ADM rename GAIA_RA/DEC to RA/DEC, as that's what's used for GFAs.
    for radec in ["RA", "DEC"]:
        objs.dtype.names = [
            radec if col == "GAIA_" + radec else col
            for col in objs.dtype.names
        ]

    # ADM initiate the GFA data model.
    gfas = np.zeros(len(objs), dtype=gfadatamodel.dtype)
    # ADM make sure all columns initially have "ridiculous" numbers
    gfas[...] = -99.
    for col in gfas.dtype.names:
        if isinstance(gfas[col][0].item(), (bytes, str)):
            gfas[col] = 'U'
        if isinstance(gfas[col][0].item(), int):
            gfas[col] = -1
    # ADM some default special cases. Default to REF_EPOCH of Gaia DR2,
    # ADM make RA/Dec very precise for Gaia measurements.
    gfas["REF_EPOCH"] = 2015.5
    gfas["RA_IVAR"], gfas["DEC_IVAR"] = 1e16, 1e16

    # ADM populate the common columns in the Gaia/GFA data models.
    cols = set(gfas.dtype.names).intersection(set(objs.dtype.names))
    for col in cols:
        gfas[col] = objs[col]

    # ADM update the Gaia morphological type.
    gfas["TYPE"] = gaia_morph(gfas)

    # ADM populate the BRICKID columns.
    gfas["BRICKID"] = bricks.brickid(gfas["RA"], gfas["DEC"])

    # ADM limit by Dec first to speed transform to Galactic coordinates.
    decgood = is_in_box(gfas, [0., 360., mindec, 90.])
    gfas = gfas[decgood]
    # ADM now limit to requesed Galactic latitude range.
    bbad = is_in_gal_box(gfas, [0., 360., -mingalb, mingalb])
    gfas = gfas[~bbad]

    return gfas
예제 #3
0
def gaia_in_file(infile,
                 maglim=18,
                 mindec=-30.,
                 mingalb=10.,
                 nside=None,
                 pixlist=None,
                 addobjid=False):
    """Retrieve the Gaia objects from a HEALPixel-split Gaia file.

    Parameters
    ----------
    infile : :class:`str`
        File name of a single Gaia "healpix" file.
    maglim : :class:`float`, optional, defaults to 18
        Magnitude limit for GFAs in Gaia G-band.
    mindec : :class:`float`, optional, defaults to -30
        Minimum declination (o) to include for output Gaia objects.
    mingalb : :class:`float`, optional, defaults to 10
        Closest latitude to Galactic plane for output Gaia objects
        (e.g. send 10 to limit to areas beyond -10o <= b < 10o)"
    nside : :class:`int`, optional, defaults to `None`
        (NESTED) HEALPix `nside` to use with `pixlist`.
    pixlist : :class:`list` or `int`, optional, defaults to `None`
        Only return sources in a set of (NESTED) HEALpixels at the
        supplied `nside`.
    addobjid : :class:`bool`, optional, defaults to ``False``
        If ``True``, include, in the output, a column "GAIA_OBJID"
        that is the integer number of each row read from file.

    Returns
    -------
    :class:`~numpy.ndarray`
        Gaia objects in the passed Gaia file brighter than `maglim`,
        formatted according to `desitarget.gfa.gfadatamodel`.

    Notes
    -----
       - A "Gaia healpix file" here is as made by, e.g.
         :func:`~desitarget.gaiamatch.gaia_fits_to_healpix()`
    """
    # ADM read in the Gaia file and limit to the passed magnitude.
    objs = read_gaia_file(infile, addobjid=addobjid)
    ii = objs['GAIA_PHOT_G_MEAN_MAG'] < maglim
    objs = objs[ii]

    # ADM rename GAIA_RA/DEC to RA/DEC, as that's what's used for GFAs.
    for radec in ["RA", "DEC"]:
        objs.dtype.names = [
            radec if col == "GAIA_" + radec else col
            for col in objs.dtype.names
        ]

    # ADM initiate the GFA data model.
    dt = gfadatamodel.dtype.descr
    if addobjid:
        for tup in ('GAIA_BRICKID', '>i4'), ('GAIA_OBJID', '>i4'):
            dt.append(tup)

    gfas = np.zeros(len(objs), dtype=dt)
    # ADM make sure all columns initially have "ridiculous" numbers
    gfas[...] = -99.
    for col in gfas.dtype.names:
        if isinstance(gfas[col][0].item(), (bytes, str)):
            gfas[col] = 'U'
        if isinstance(gfas[col][0].item(), int):
            gfas[col] = -1
    # ADM some default special cases. Default to REF_EPOCH of Gaia DR2,
    # ADM make RA/Dec very precise for Gaia measurements.
    gfas["REF_EPOCH"] = 2015.5
    gfas["RA_IVAR"], gfas["DEC_IVAR"] = 1e16, 1e16

    # ADM populate the common columns in the Gaia/GFA data models.
    cols = set(gfas.dtype.names).intersection(set(objs.dtype.names))
    for col in cols:
        gfas[col] = objs[col]

    # ADM update the Gaia morphological type.
    gfas["TYPE"] = gaia_morph(gfas)

    # ADM populate the BRICKID columns.
    gfas["BRICKID"] = bricks.brickid(gfas["RA"], gfas["DEC"])

    # ADM limit by HEALPixel first as that's the fastest.
    if pixlist is not None:
        inhp = is_in_hp(gfas, nside, pixlist)
        gfas = gfas[inhp]
    # ADM limit by Dec first to speed transform to Galactic coordinates.
    decgood = is_in_box(gfas, [0., 360., mindec, 90.])
    gfas = gfas[decgood]
    # ADM now limit to requesed Galactic latitude range.
    if mingalb > 1e-9:
        bbad = is_in_gal_box(gfas, [0., 360., -mingalb, mingalb])
        gfas = gfas[~bbad]

    return gfas
예제 #4
0
    skies.remove('plver')

    skies = skies + ['plverf']
    ccd = ccd[cols + skies]

    exps = ccd[ccd['filter'] == band]

    result = np.zeros(len(randoms) * len(skies)).reshape(
        len(randoms), len(skies))
    count = np.zeros(len(randoms), dtype=np.int32)

    for i, x in enumerate(exps):
        try:
            ##  http://legacysurvey.org/ccdordering/
            if camera == b'decam':
                inccd = is_in_box(randoms,
                                  [x['ra3'], x['ra1'], x['dec3'], x['dec1']])

            elif camera == b'90prime':
                ##  BASS
                inccd = is_in_box(randoms,
                                  [x['ra2'], x['ra0'], x['dec2'], x['dec0']])

            elif camera == b'mosaic':
                ##  MzLS
                inccd = is_in_box(randoms,
                                  [x['ra0'], x['ra2'], x['dec0'], x['dec2']])

            else:
                raise ValueError('Invalid inut for camera.')

        except: