def get_multiple_reference_paths(input_file, reference_file_types): """Aligns JWST pipeline requirements with CRDS library top level interfaces. get_multiple_reference_paths() layers these additional tasks onto crds.getreferences(): 1. It converts an input file into a flat dictionary of JWST data model dotted parameters. 2. It verifies than any true filepath (not N/A) returned is openable. Returns { filetype : filepath or "N/A", ... } """ from .. import datamodels gc.collect() if not reference_file_types: # [] interpreted as *all types*. return {} if six.PY2: model_types = (str, unicode, datamodels.DataModel) else: model_types = (str, datamodels.DataModel) if isinstance(input_file, model_types): with datamodels.open(input_file) as dm: data_dict = dm.to_flat_dict(include_arrays=False) else: data_dict = _flatten_dict(input_file) gc.collect() try: bestrefs = crds.getreferences(data_dict, reftypes=reference_file_types, observatory="jwst") except crds.CrdsBadRulesError as exc: raise crds.CrdsBadRulesError(str(exc)) except crds.CrdsBadReferenceError as exc: raise crds.CrdsBadReferenceError(str(exc)) refpaths = { filetype: filepath if "N/A" not in filepath.upper() else "N/A" for (filetype, filepath) in bestrefs.items() } return refpaths
def get_multiple_reference_paths(input_file, reference_file_types): """Aligns JWST pipeline requirements with CRDS library top level interfaces. get_multiple_reference_paths() layers these additional tasks onto crds.getreferences(): It converts an input file into a flat dictionary of JWST data model dotted parameters for defining CRDS best references. Returns { filetype : filepath or "N/A", ... } """ from .. import datamodels gc.collect() if not reference_file_types: # [] interpreted as *all types*. return {} if isinstance(input_file, (six.string_types, datamodels.DataModel)): with datamodels.open(input_file) as dm: data_dict = dm.to_flat_dict(include_arrays=False) else: # XXX not sure what this does... seems unneeded. data_dict = _flatten_dict(input_file) gc.collect() try: if crds_cache_locking is not None: with crds_cache_locking.get_cache_lock(): bestrefs = crds.getreferences(data_dict, reftypes=reference_file_types, observatory="jwst") else: bestrefs = crds.getreferences(data_dict, reftypes=reference_file_types, observatory="jwst") except crds.CrdsBadRulesError as exc: raise crds.CrdsBadRulesError(str(exc)) except crds.CrdsBadReferenceError as exc: raise crds.CrdsBadReferenceError(str(exc)) refpaths = { filetype: filepath if "N/A" not in filepath.upper() else "N/A" for (filetype, filepath) in bestrefs.items() } return refpaths
def _get_refpaths(data_dict, reference_file_types): """Tailor the CRDS core library getreferences() call to the JWST CAL code by adding locking and truncating expected exceptions. Also simplify 'NOT FOUND n/a' to 'N/A'. Re-interpret empty reference_file_types as "no types" instead of core library default of "all types." """ if not reference_file_types: # [] interpreted as *all types*. return {} try: # catch exceptions to truncate expected tracebacks with crds_cache_locking.get_cache_lock(): bestrefs = crds.getreferences(data_dict, reftypes=reference_file_types, observatory="jwst") except crds.CrdsBadRulesError as exc: raise crds.CrdsBadRulesError(str(exc)) except crds.CrdsBadReferenceError as exc: raise crds.CrdsBadReferenceError(str(exc)) except crds.CrdsLookupError as exc: raise crds.CrdsLookupError(str(exc)) refpaths = { filetype: filepath if "N/A" not in filepath.upper() else "N/A" for (filetype, filepath) in bestrefs.items() } return refpaths