Skip to content

Commit

Permalink
fix --indir usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed Sep 13, 2023
1 parent e1ae2a5 commit 5c88026
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions bin/desi_zcatalog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ context of this script.
Stephen Bailey
Lawrence Berkeley National Lab
Fall 2015
substantially updated Fal 2023
"""

from __future__ import absolute_import, division, print_function
Expand Down Expand Up @@ -143,7 +144,7 @@ def _wrap_read_redrock(optdict):

def read_redrock(rrfile, group=None, recoadd_fibermap=False, minimal=False, pertile=False, counter=None):
"""
Read a Redrock file
Read a Redrock file, combining REDSHIFTS, FIBERMAP, and TSNR2 HDUs
Args:
rrfile (str): full path to redrock filename
Expand Down Expand Up @@ -238,7 +239,7 @@ def read_redrock(rrfile, group=None, recoadd_fibermap=False, minimal=False, pert
if 'LASTNIGHT' not in data.colnames:
data.add_column(np.full(nrows, hdr['NIGHT'], dtype=np.int32),
index=icol, name='LASTNIGHT')
elif args.group == 'healpix':
elif group == 'healpix':
data.add_column(np.full(nrows, hdr['HPXPIXEL'], dtype=np.int32),
index=icol, name='HEALPIX')

Expand All @@ -265,19 +266,6 @@ def read_redrock(rrfile, group=None, recoadd_fibermap=False, minimal=False, pert

#--------------------------------------------------------------------------

"""
# stack specprod healpix, optionally filtering by SURVEY and PROGRAM
desi_zcatalog --group healpix --survey main --program dark -o zpix-main-dark.fits
# stack specprod tiles, optionally filtering by SURVEY and PROGRAM
desi_zcatalog --group cumulative --survey main --program dark -o ztile-main-dark.fits
# load whatever it can find in a directory
desi_zcatalog --indir testprod/tiles/cumulative/ -o zcat.fits
desi_zcatalog --indir testprod/tiles/cumulative/ --tiles tiles.txt -o zcat-subset.fits
"""

def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-i", "--indir", type=str,
Expand Down Expand Up @@ -327,20 +315,24 @@ def main():
log.critical('Unable to import desidatamodel, required to add units (try "module load desidatamodel" first)')
sys.exit(1)

if args.group == 'healpix':
if args.indir:
indir = args.indir
redrockfiles = sorted(io.iterfiles(f'{indir}', prefix='redrock', suffix='.fits'))
pertile = (args.group != 'healpix') # assume tile-based input unless explicitely healpix
elif args.group == 'healpix':
pertile = False
survey = args.survey if args.survey is not None else "*"
program = args.program if args.program is not None else "*"
hpixdir = args.indir if args.indir is not None else os.path.join(io.specprod_root(), 'healpix')
indir = os.path.join(io.specprod_root(), 'healpix')

#- specprod/healpix/SURVEY/PROGRAM/HPIXGROUP/HPIX/redrock*.fits
globstr = os.path.join(hpixdir, survey, program, '*', '*', 'redrock*.fits')
globstr = os.path.join(indir, survey, program, '*', '*', 'redrock*.fits')
log.info(f'Looking for healpix redrock files in {globstr}')
redrockfiles = sorted(glob.glob(globstr))
else:
pertile = True
tilefile = args.tiles if args.tiles is not None else io.findfile('tiles')
indir = args.indir if args.indir is not None else os.path.join(io.specprod_root(), 'tiles', args.group)
indir = os.path.join(io.specprod_root(), 'tiles', args.group)

log.info(f'Loading tiles from {tilefile}')
tiles = Table.read(tilefile)
Expand All @@ -366,29 +358,32 @@ def main():
if len(tmp) > 0:
redrockfiles.extend(tmp)
else:
log.error(f'no redrock files found in {args.indir}/{tileid}')
log.error(f'no redrock files found in {indir}/{tileid}')


nfiles = len(redrockfiles)
if nfiles == 0:
msg = f'No redrock files found in {args.indir}'
msg = f'No redrock files found in {indir}'
log.critical(msg)
raise ValueError(msg)
log.info(f'Reading {nfiles} redrock files')

#- build list of args to support multiprocessing parallelism
read_args = list()
for ifile, rrfile in enumerate(redrockfiles):
read_args.append(dict(rrfile=rrfile, group=args.group, pertile=pertile,
recoadd_fibermap=args.recoadd_fibermap, minimal=args.minimal,
counter=(ifile+1, nfiles)))

#- Read individual Redrock files
if args.nproc>1:
from multiprocessing import Pool
with Pool(args.nproc) as pool:
results = pool.map(_wrap_read_redrock, read_args)
else:
results = [_wrap_read_redrock(a) for a in read_args]

#- Stack catalogs
zcatdata = list()
exp_fibermaps = list()
for data, expfibermap in results:
Expand All @@ -415,6 +410,7 @@ def main():
else:
log.info('Missing TSNR2_LRG or ZWARN; not adding ZCAT_PRIMARY/_NSPEC')

#- Used for fuji, should not be needed for later prods
if args.patch_missing_ivar_w12:
from desimodel.footprint import radec2pix
missing = (zcat['FLUX_IVAR_W1'] < 0) | (zcat['FLUX_IVAR_W2'] < 0)
Expand Down

0 comments on commit 5c88026

Please sign in to comment.