Skip to content

Commit

Permalink
require cat version, and make units opt out in zcatalog and wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
akremin committed Nov 26, 2024
1 parent cdb998b commit 55be5b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
9 changes: 5 additions & 4 deletions py/desispec/scripts/zcatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def parse(options=None):
help="Use target files to patch missing FLUX_IVAR_W1/W2 values")
parser.add_argument('--recoadd-fibermap', action='store_true',
help="Re-coadd FIBERMAP from spectra files")
parser.add_argument('--add-units', action='store_true',
help="Add units to output catalog from desidatamodel "
parser.add_argument('--do-not-add-units', action='store_true',
help="Don't add units to output catalog from desidatamodel "
"column descriptions")
parser.add_argument('--nproc', type=int, default=1,
help="Number of multiprocessing processes to use")
Expand All @@ -343,7 +343,8 @@ def main(args=None):
args.outfile = io.findfile('zcatalog')

#- If adding units, check dependencies before doing a lot of work
if args.add_units:
add_units = not args.do_not_add_units
if add_units:
try:
import desidatamodel
except ImportError:
Expand Down Expand Up @@ -572,7 +573,7 @@ def main(args=None):
header['PROGRAM'] = args.program

#- Add units if requested
if args.add_units:
if add_units:
datamodeldir = str(importlib.resources.files('desidatamodel'))
unitsfile = os.path.join(datamodeldir, 'data', 'column_descriptions.csv')
log.info(f'Adding units from {unitsfile}')
Expand Down
36 changes: 16 additions & 20 deletions py/desispec/scripts/zcatalog_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
from desispec.io.meta import findfile
from desispec.io import specprod_root
from desispec.scripts import zcatalog as zcatalog_script
from desispec.zcatalog import create_summary_catalog_wrapper
from desispec.zcatalog import create_summary_catalog


def parse(options=None):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-g", "--group", type=str,
parser.add_argument("-g", "--group", type=str, required=True,
help="Add columns specific to this spectral grouping "
"e.g. pernight, perexp, cumulative, healpix")

parser.add_argument("--cat-version",type=str, default=None,
parser.add_argument('-V', "--cat-version",type=str, required=True,
help="The version number of the output catalogs")

parser.add_argument("-i", "--indir", type=str, default=None,
help="input directory")
parser.add_argument("-o", "--outdir", type=str, default=None,
Expand All @@ -48,15 +49,21 @@ def parse(options=None):
help="Use target files to patch missing FLUX_IVAR_W1/W2 values")
parser.add_argument('--recoadd-fibermap', action='store_true',
help="Re-coadd FIBERMAP from spectra files")
parser.add_argument('--add-units', action='store_true',
help="Add units to output catalog from desidatamodel "
parser.add_argument('--do-not-add-units', action='store_true',
help="Set if you do not want to add units to output catalog from desidatamodel "
"column descriptions")
parser.add_argument('-v', '--verbose', action='store_true',
help="Set log level to DEBUG.")
args = parser.parse_args(options)

return args

def _create_summary_catalog_wrapper(kwargs):
"""
Trivial wrapper around create_summary_catalog that takes a dict
and passes the key-value pairs to create_summary_catalog as keyword arguments
"""
return create_summary_catalog(**kwargs)

def main(args=None):
if not isinstance(args, argparse.Namespace):
Expand All @@ -67,13 +74,8 @@ def main(args=None):
else:
log=get_logger()

## Ensure we know where to save the files
if args.outdir is None and args.cat_version is None:
log.critical(f"Either --outdir or --cat-version must be specifiied. Exiting")
return 1

## If adding units, check dependencies before doing a lot of work
if args.add_units:
if not args.do_not_add_units:
try:
import desidatamodel
except ImportError:
Expand All @@ -99,12 +101,6 @@ def main(args=None):
groupname=args.group,
survey='dummy', faprogram='dummy'))

## Define catalog version (currently not used except with findfile,
## but since we are only using findfile for the filename and not the
## directory tree, the version isn't actually relevant
if args.cat_version is None:
args.cat_version = os.path.basename(args.outdir)

logdir = os.path.join(args.outdir, 'logs')
if not os.path.exists(logdir):
log.info(f"Output log directory {logdir} does not exist, creating now.")
Expand Down Expand Up @@ -135,7 +131,7 @@ def main(args=None):
("--minimal", args.minimal),
('--patch-missing-ivar-w12', args.patch_missing_ivar_w12),
('--recoadd-fibermap', args.recoadd_fibermap),
('--add-units', args.add_units)]:
('--do-not-add-units', args.do_not_add_units)]:
if argval:
cmd += f" {argument}"

Expand Down Expand Up @@ -211,7 +207,7 @@ def main(args=None):
## Redirect logging to seperate file and only run of all files output in the last
## step exist
with stdouterr_redirected(outlog):
result, success = runcmd(create_summary_catalog_wrapper, args=kwargs,
result, success = runcmd(_create_summary_catalog_wrapper, args=kwargs,
inputs=survey_program_outfiles, outputs=[outfile])
if not success:
error_count += 1
Expand Down
7 changes: 0 additions & 7 deletions py/desispec/zcatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,6 @@ def _get_survey_program_from_filename(filename):
program = arr[2]
return survey, program

def create_summary_catalog_wrapper(kwargs):
"""
Trivial wrapper around create_summary_catalog that takes a dict
and passes the key-value pairs to create_summary_catalog as keyword arguments
"""
return create_summary_catalog(**kwargs)

def create_summary_catalog(specgroup, indir=None, specprod=None,
all_columns = True, columns_list = None,
output_filename=None):
Expand Down

0 comments on commit 55be5b5

Please sign in to comment.