def test_brickname(self): self.assertEqual(io.brickname_from_filename('tractor-3301m002.fits'), '3301m002') self.assertEqual(io.brickname_from_filename('tractor-3301p002.fits'), '3301p002') self.assertEqual( io.brickname_from_filename('/a/b/tractor-3301p002.fits'), '3301p002')
def get_brick_info(drdirs, counts=False, allbricks=False): """Retrieve brick names and coordinates from Legacy Surveys directories. Parameters ---------- drdirs : :class:`list` or `str` A list of strings, each of which corresponds to a directory pointing to a Data Release from the Legacy Surveys. Can be of length one. e.g. ['/global/project/projectdirs/cosmo/data/legacysurvey/dr7']. or '/global/project/projectdirs/cosmo/data/legacysurvey/dr7' Can be None if `allbricks` is passed. counts : :class:`bool`, optional, defaults to ``False`` If ``True`` also return a count of the number of times each brick appears ([RAcen, DECcen, RAmin, RAmax, DECmin, DECmax, CNT]). allbricks : :class:`bool`, optional, defaults to ``False`` If ``True`` ignore `drdirs` and simply return a dictionary of ALL of the bricks. Returns ------- :class:`dict` UNIQUE bricks covered by the Data Release(s). Keys are brick names and values are a list of the brick center and the brick corners ([RAcen, DECcen, RAmin, RAmax, DECmin, DECmax]). Notes ----- - Tries a few different ways in case the survey bricks files have not yet been created. """ # ADM convert a single input string to a list. if isinstance(drdirs, str): drdirs = [ drdirs, ] # ADM initialize the bricks class, retrieve the brick information look-up # ADM table and turn it into a fast look-up dictionary. from desiutil import brick bricktable = brick.Bricks(bricksize=0.25).to_table() brickdict = {} for b in bricktable: brickdict[b["BRICKNAME"]] = [ b["RA"], b["DEC"], b["RA1"], b["RA2"], b["DEC1"], b["DEC2"] ] # ADM if requested, return the dictionary of ALL bricks. if allbricks: return brickdict bricknames = [] for dd in drdirs: # ADM in the simplest case, read in the survey bricks file, which lists # ADM the bricks of interest for this DR. sbfile = glob(dd + '/*bricks-dr*') if len(sbfile) > 0: brickinfo = fitsio.read(sbfile[0]) # ADM fitsio reads things in as bytes, so convert to unicode. bricknames.append(brickinfo['brickname'].astype('U')) else: # ADM hack for test bricks where we didn't generate the bricks file. fns = glob(os.path.join(dd, 'tractor', '*', '*fits')) bricknames.append([brickname_from_filename(fn) for fn in fns]) # ADM don't count bricks twice, but record number of duplicate bricks. bricknames, cnts = np.unique(np.concatenate(bricknames), return_counts=True) # ADM only return the subset of the dictionary with bricks in the DR. if counts: return {bn: brickdict[bn] + [cnt] for bn, cnt in zip(bricknames, cnts)} return {bn: brickdict[bn] for bn in bricknames}
def select_randoms(drdir, density=100000, numproc=32, nside=4, pixlist=None, bundlebricks=None, brickspersec=2.5, dustdir=None): """NOBS, GALDEPTH, PSFDEPTH (per-band) for random points in a DR of the Legacy Surveys Parameters ---------- drdir : :class:`str` The root directory pointing to a Data Release from the Legacy Surveys e.g. /global/project/projectdirs/cosmo/data/legacysurvey/dr7. density : :class:`int`, optional, defaults to 100,000 The number of random points to return per sq. deg. As a typical brick is ~0.25 x 0.25 sq. deg. about (0.0625*density) points will be returned numproc : :class:`int`, optional, defaults to 32 The number of processes over which to parallelize nside : :class:`int`, optional, defaults to nside=4 (214.86 sq. deg.) The (NESTED) HEALPixel nside to be used with the `pixlist` and `bundlebricks` input. pixlist : :class:`list` or `int`, optional, defaults to None Bricks will only be processed if the CENTER of the brick lies within the bounds of pixels that are in this list of integers, at the supplied HEALPixel `nside`. Uses the HEALPix NESTED scheme. Useful for parallelizing. If pixlist is None then all bricks in the passed `survey` will be processed. bundlebricks : :class:`int`, defaults to None If not None, then instead of selecting the skies, print, to screen, the slurm script that will approximately balance the brick distribution at `bundlebricks` bricks per node. So, for instance, if bundlebricks is 14000 (which as of the latest git push works well to fit on the interactive nodes on Cori and run in about an hour), then commands would be returned with the correct pixlist values to pass to the code to pack at about 14000 bricks per node across all of the bricks in `survey`. brickspersec : :class:`float`, optional, defaults to 2.5 The rough number of bricks processed per second by the code (parallelized across a chosen number of nodes). Used in conjunction with `bundlebricks` for the code to estimate time to completion when parallelizing across pixels. dustdir : :class:`str`, optional, defaults to $DUST_DIR+'maps' The root directory pointing to SFD dust maps. If not sent the code will try to use $DUST_DIR+'maps') before failing. Returns ------- :class:`~numpy.ndarray` a numpy structured array with the following columns: RA: Right Ascension of a random point DEC: Declination of a random point BRICKNAME: Passed brick name NOBS_G: Number of observations at this location in the g-band NOBS_R: Number of observations at this location in the r-band NOBS_Z: Number of observations at this location in the z-band PSFDEPTH_G: PSF depth at this location in the g-band PSFDEPTH_R: PSF depth at this location in the r-band PSFDEPTH_Z: PSF depth at this location in the z-band GALDEPTH_G: Galaxy depth at this location in the g-band GALDEPTH_R: Galaxy depth at this location in the r-band GALDEPTH_Z: Galaxy depth at this location in the z-band MASKBITS: Extra mask bits info as stored in the header of e.g., dr7dir + 'coadd/111/1116p210/legacysurvey-1116p210-maskbits.fits.gz' EBV: E(B-V) at this location from the SFD dust maps """ # ADM read in the survey bricks file, which lists the bricks of interest for this DR. # ADM if this is pre-or-post-DR8 we need to find the correct directory or directories. drdirs = _pre_or_post_dr8(drdir) bricknames = [] brickinfo = [] for dd in drdirs: sbfile = glob(dd + '/*bricks-dr*') if len(sbfile) > 0: sbfile = sbfile[0] hdu = fits.open(sbfile) brickinfo.append(hdu[1].data) bricknames.append(hdu[1].data['BRICKNAME']) else: # ADM this is a hack for test bricks where we didn't always generate the # ADM bricks file. It's probably safe to remove it at some point. from desitarget.io import brickname_from_filename fns = glob(os.path.join(dd, 'tractor', '*', '*fits')) bricknames.append([brickname_from_filename(fn) for fn in fns]) brickinfo.append([]) if pixlist is not None or bundlebricks is not None: msg = 'DR-specific bricks file not found' msg += 'and pixlist or bundlebricks passed!!!' log.critical(msg) raise ValueError(msg) bricknames = np.concatenate(bricknames) brickinfo = np.concatenate(brickinfo) # ADM if the pixlist or bundlebricks option was sent, we'll need the HEALPixel # ADM information for each brick. if pixlist is not None or bundlebricks is not None: theta, phi = np.radians(90 - brickinfo["dec"]), np.radians( brickinfo["ra"]) pixnum = hp.ang2pix(nside, theta, phi, nest=True) # ADM if the bundlebricks option was sent, call the packing code. if bundlebricks is not None: bundle_bricks(pixnum, bundlebricks, nside, brickspersec=brickspersec, prefix='randoms', surveydir=drdir) return # ADM restrict to only bricks in a set of HEALPixels, if requested. if pixlist is not None: # ADM if an integer was passed, turn it into a list. if isinstance(pixlist, int): pixlist = [pixlist] wbricks = np.where([pix in pixlist for pix in pixnum])[0] bricknames = bricknames[wbricks] if len(wbricks) == 0: log.warning('ZERO bricks in passed pixel list!!!') log.info( "Processing bricks in (nside={}, pixel numbers={}) HEALPixels". format(nside, pixlist)) nbricks = len(bricknames) log.info( 'Processing {} bricks from DR at {} at density {:.1e} per sq. deg...t = {:.1f}s' .format(nbricks, drdir, density, time() - start)) # ADM a little more information if we're slurming across nodes. if os.getenv('SLURMD_NODENAME') is not None: log.info('Running on Node {}'.format(os.getenv('SLURMD_NODENAME'))) # ADM initialize the bricks class, and retrieve the brick information look-up table # ADM so it can be used in a common fashion. from desiutil import brick bricktable = brick.Bricks(bricksize=0.25).to_table() # ADM the critical function to run on every brick. def _get_quantities(brickname): '''wrapper on nobs_positions_in_a_brick_from_edges() given a brick name''' # ADM retrieve the edges for the brick that we're working on wbrick = np.where(bricktable["BRICKNAME"] == brickname)[0] ramin, ramax, decmin, decmax = np.array(bricktable[wbrick]["RA1", "RA2", "DEC1", "DEC2"])[0] # ADM populate the brick with random points, and retrieve the quantities # ADM of interest at those points. return get_quantities_in_a_brick(ramin, ramax, decmin, decmax, brickname, drdir, density=density, dustdir=dustdir) # ADM this is just to count bricks in _update_status nbrick = np.zeros((), dtype='i8') t0 = time() def _update_status(result): ''' wrapper function for the critical reduction operation, that occurs on the main parallel process ''' if nbrick % 50 == 0 and nbrick > 0: rate = nbrick / (time() - t0) log.info('{}/{} bricks; {:.1f} bricks/sec'.format( nbrick, nbricks, rate)) # ADM if we're going to exceed 4 hours, warn the user if nbricks / rate > 4 * 3600.: log.error( "May take > 4 hours to run. Try running with bundlebricks instead." ) nbrick[...] += 1 # this is an in-place modification return result # - Parallel process input files if numproc > 1: pool = sharedmem.MapReduce(np=numproc) with pool: qinfo = pool.map(_get_quantities, bricknames, reduce=_update_status) else: qinfo = list() for brickname in bricknames: qinfo.append(_update_status(_get_quantities(brickname))) # ADM concatenate the randoms into a single long list and resolve whether # ADM they are officially in the north or the south. qinfo = np.concatenate(qinfo) qinfo = resolve(qinfo) # ADM one last shuffle to randomize across brick boundaries. np.random.seed(616) np.random.shuffle(qinfo) return qinfo
def test_brickname(self): self.assertEqual(io.brickname_from_filename('tractor-3301m002.fits'), '3301m002') self.assertEqual(io.brickname_from_filename('tractor-3301p002.fits'), '3301p002') self.assertEqual(io.brickname_from_filename('/a/b/tractor-3301p002.fits'), '3301p002')