Example #1
0
def add_brick_data(T, north):
    B = fits_table('survey-bricks.fits.gz')
    print('Looking up brick bounds')
    ibrick = dict([(n, i) for i, n in enumerate(B.brickname)])
    bi = np.array([ibrick[n] for n in T.brickname])
    T.brickid = B.brickid[bi]
    T.ra1 = B.ra1[bi]
    T.ra2 = B.ra2[bi]
    T.dec1 = B.dec1[bi]
    T.dec2 = B.dec2[bi]
    assert (np.all(T.ra2 > T.ra1))
    T.area = ((T.ra2 - T.ra1) * (T.dec2 - T.dec1) *
              np.cos(np.deg2rad((T.dec1 + T.dec2) / 2.)))

    print('Resolving north/south split')
    from astrometry.util.starutil_numpy import radectolb
    ll, bb = radectolb(T.ra, T.dec)
    from desitarget.io import desitarget_resolve_dec
    decsplit = desitarget_resolve_dec()
    if north:
        T.survey_primary = (bb > 0) * (T.dec >= decsplit)
    else:
        T.survey_primary = np.logical_not((bb > 0) * (T.dec >= decsplit))

    print('Looking up in_desi')
    from desimodel.io import load_tiles
    from desimodel.footprint import is_point_in_desi
    desitiles = load_tiles()
    T.in_desi = is_point_in_desi(desitiles, T.ra, T.dec)
Example #2
0
def _get_healpixels_in_footprint(nside=64):
    """Obtain a list of HEALPix pixels in the DESI footprint.

    Parameters
    ----------
    nside : int
        HEALPix nside parameter (in form nside=2**k, k=[1,2,3,...]).

    Returns
    -------
    healpixels : ndarray
        List of HEALPix pixels within the DESI footprint.
    """
    from desimodel import footprint
    from desimodel.io import load_tiles

    # Load DESI tiles.
    tile_tab = load_tiles()

    npix = hp.nside2npix(nside)
    pix_ids = np.arange(npix)
    ra, dec = hp.pix2ang(nside, pix_ids, lonlat=True)

    # Get a list of pixel IDs inside the DESI footprint.
    in_desi = footprint.is_point_in_desi(tile_tab, ra, dec)
    healpixels = pix_ids[in_desi]

    return healpixels
Example #3
0
def get_tile_radec(tileid):
    """Return (ra, dec) in degrees for the requested tileid.

    Raises ValueError if tileid is not in list of known tiles
    """
    tiles = io.load_tiles()
    if tileid in tiles['TILEID']:
        i = np.where(tiles['TILEID'] == tileid)[0][0]
        return tiles[i]['RA'], tiles[i]['DEC']
    else:
        raise ValueError('Unknown tileid {}'.format(tileid))
Example #4
0
def main_surveyqa(options=None):
    parser = argparse.ArgumentParser(usage='{prog} [options]')

    parser.add_argument('-i',
                        '--infile',
                        type=str,
                        required=True,
                        help='file containing data to feed into surveyqa')
    parser.add_argument(
        '-o',
        '--outdir',
        type=str,
        required=True,
        help=
        'directory threshold json/html files should be written to (will be written to outdir/surveyqa, outdir should be same location as other nightwatch files)'
    )
    parser.add_argument('-t',
                        '--tilefile',
                        type=str,
                        help='file containing data on tiles')
    parser.add_argument(
        '-r',
        '--rawdir',
        type=str,
        help='directory containing raw data files (without YYYMMDD/EXPID/)')

    if options is None:
        options = sys.argv[2:]
    args = parser.parse_args(options)

    if args.tilefile is None:
        tiles = load_tiles()
    else:
        tiles = Table.read(args.tilefile, hdu=1)

    if args.rawdir is None:
        args.rawdir = desispec.io.meta.rawdata_root()

    name_dict = {
        "EXPID": "EXPID",
        "MJD": "MJD",
        "AIRMASS": "AIRMASS",
        "TRANSP": "TRANSPARENCY",
        "NIGHT": "NIGHT",
        "MOONSEP": "MOON_SEP_DEG",
        "RA": "SKYRA",
        "DEC": "SKYDEC",
        "SKY": "SKY_MAG_AB",
        "SEEING": "FWHM_ASEC"
    }

    run.write_summaryqa(args.infile, name_dict, tiles, args.rawdir,
                        args.outdir)
Example #5
0
def get_tile_radec(tileid):
    """Return (ra, dec) in degrees for the requested tileid.

    If tileid is not in DESI, return (0.0, 0.0)
    TODO: should it raise and exception instead?
    """
    tiles = io.load_tiles()
    if tileid in tiles['TILEID']:
        i = np.where(tiles['TILEID'] == tileid)[0][0]
        return tiles[i]['RA'], tiles[i]['DEC']
    else:
        return (0.0, 0.0)
Example #6
0
def generate_rnd(factor=3, out_path= None, method='random_choice'):
    """
    Routine to generate a random catalog in 3D following
    certain N(z) distribution
    
    Args:
    ----
    rad: z-values of the data catalog
    factor: Size of the generated catalog (before masking)
    out_path: Name of output file where randoms will be saved (default: None)
    method: Method to generate the random catalog (default: 'random_choice')
    """
    #Creating random that follows N(z)
    tiles = load_tiles()
    nz_file = astropy.table.Table.read('example_data/Nz_qso_2_highZ.txt',format='ascii')
    zvec = np.linspace(np.min(nz_file['col1']),np.max(nz_file['col1']),5000)
    spl_z = interp1d(nz_file['col1'],nz_file['col2'])
    dndz = spl_z(zvec)
    ntot = 4*180**2/np.pi*np.sum(nz_file['col2'])*(nz_file['col1'][1]-nz_file['col1'][0])
    ntot = int(factor*ntot)  
    if method=='rnd_choice':
        z_rnd = np.random.choice(zvec[zvec>1.8],p=dndz[zvec>1.8]/np.sum(dndz[zvec>1.8]),size=ntot)+2*(z[1]-z[0])*np.random.normal(size=ntot)
    if method=='cdf':
        cdf = np.cumsum(dndz[zvec>=1.8])/np.sum(dndz[zvec>=1.8])
        icdf = interp1d(cdf,zvec[zvec>=1.8],fill_value='extrapolate',bounds_error=False)
        z_rnd = icdf(np.random.random(size=ntot))
    ra_rnd = 360.*np.random.random(size=len(z_rnd))
    cth_rnd = -1+2.*np.random.random(size=len(z_rnd))
    dec_rnd = np.arcsin(cth_rnd)*180/np.pi
    good = is_point_in_desi(tiles,ra_rnd,dec_rnd)
    ra_rnd = ra_rnd[good]
    dec_rnd = dec_rnd[good]
    z_rnd = z_rnd[good]
    if out_path is not None:
        tab_out = astropy.table.Table([ra_rnd,dec_rnd,z_rnd],names=('RA','DEC','Z'))
        tab_out.write(out_path,overwrite=True)
    return None
Example #7
0
import numpy               as     np
import astropy.io.fits     as     fits
import astropy.units       as     u

from astropy.table         import Table, vstack
from desimodel.footprint   import is_point_in_desi, tiles2pix
from desimodel.io          import load_tiles 
from desitarget.targetmask import desi_mask


gcs            = Table(fits.open('/global/cscratch1/sd/mjwilson/BGS/SV-ASSIGN/masks/NGC-star-clusters.fits')[1].data)

tiles          = np.array([list(x) for x in load_tiles(onlydesi=True)])
tiles          = Table([tiles[:, 0].astype(np.int), tiles[:, 1].astype(np.float32), tiles[:,2].astype(np.float32)], names=['TILEID', 'RA', 'DEC'])

isin, index    = is_point_in_desi(tiles, gcs['ra'], gcs['dec'], return_tile_index=True)

gcs['TILEID']       = -99
gcs['TILEID'][isin] = tiles['TILEID'][index[isin]]

gcs            = gcs[gcs['name'] == 'NGC5904']
gcs['RA']      = gcs['ra']
gcs['DEC']     = gcs['dec']
gcs            = gcs['RA', 'DEC', 'name', 'TILEID']

tiles          = tiles[tiles['TILEID'] == gcs['TILEID']]

gcs.pprint()
tiles.pprint()

pix            = tiles2pix(nside=2, tiles=tiles)
Example #8
0
def make_QSO_filter(footprint, N_side=16, pixel_list=None):

    #See if we can use desimodel. This is preferable as it will be the most
    #up-do-date footprint.
    try:
        from desimodel.footprint import tiles2pix, is_point_in_desi
        from desimodel.io import load_tiles
        desimodel_installed = True
    except ModuleNotFoundError:
        print(
            'WARN: desimodel is not installed; footprint pixel data will be read from file.'
        )
        desimodel_installed = False

    #If we have desimodel and want to replicate the footprint precisely, use
    #function "is_point_in_desi".
    if desimodel_installed and footprint == 'desi':
        tiles = load_tiles()

        def QSO_filter(RA, DEC):
            return is_point_in_desi(tiles, RA, DEC)

    #If not, but we still want to filter...
    elif footprint in ['desi', 'desi_pixel', 'desi_pixel_plus']:

        #If desimodel is installed, then we use "tiles2pix" to determine which
        #pixels to include.
        if desimodel_installed:
            from desimodel.footprint import tiles2pix
            if footprint == 'desi_pixel':
                valid_pixels = tiles2pix(N_side)
            elif footprint == 'desi_pixel_plus':
                valid_pixels = tiles2pix(N_side)
                valid_pixels = add_pixel_neighbours(valid_pixels)

        #Otherwise, we load pixel lists from file. Note: using desimodel is
        #preferable to loading from file as the footprint could change, and
        #desimodel will be more up to date than the lists in this case.
        else:
            if footprint == 'desi':
                print(
                    'WARN: desimodel is not installed; footprint pixel data will be read from file.'
                )
                valid_pixels = np.loadtxt(
                    'input_files/pixel_footprints/DESI_pixels.txt', dtype=int)
            elif footprint == 'desi_pixel':
                valid_pixels = np.loadtxt(
                    'input_files/pixel_footprints/DESI_pixels.txt', dtype=int)
            elif footprint == 'desi_pixel_plus':
                valid_pixels = np.loadtxt(
                    'input_files/pixel_footprints/DESI_pixels_plus.txt',
                    dtype=int)

        #With a list of valid pixels, we now can make a filter.

        def QSO_filter(RA, DEC):
            theta = (np.pi / 180.0) * (90.0 - DEC)
            phi = (np.pi / 180.0) * RA
            pix = hp.ang2pix(N_side, theta, phi, nest=True)
            w = np.in1d(pix, valid_pixels)
            return w

    #Else if we don't want to filter at all, set the filter to "None".
    elif footprint == 'full_sky':

        def QSO_filter(RA, DEC):
            return np.ones(RA.shape).astype('bool')

    else:
        print('Footprint not recognised; no filter applied.')

        def QSO_filter(RA, DEC):
            return np.ones(RA.shape).astype('bool')

    return QSO_filter