def _build_schema_ignore_list(schema): """ Create a list of metadata that should be ignored when blending. Parameters ---------- schema : JSON schema fragment The schema in which to search. Returns ------- results : list List with schema attributes that needs to be ignored """ def build_rules_list(subschema, path, combiner, ctx, recurse): # Only interpret elements of the meta component of the model if len(path) > 1 and path[0] == 'meta' and 'items' not in path: attr = '.'.join(path) if subschema.get('properties'): return # Ignore ObjectNodes kwtype = subschema.get('type') if kwtype == 'array': results.append(attr) else: return results = [] dm_schema.walk_schema(schema, build_rules_list, results) return results
def build_fits_dict(schema): """ Utility function to create a dict that maps FITS keywords to their metadata attribute in a input schema. Parameters ---------- schema : JSON schema fragment The schema in which to search. Returns ------- results : dict Dictionary with FITS keywords as keys and schema metadata attributes as values """ def build_fits_dict(subschema, path, combiner, ctx, recurse): if len(path) and path[0] == 'extra_fits': return True kw = subschema.get('fits_keyword') if kw is not None: results[kw] = '.'.join(path) results = {} walk_schema(schema, build_fits_dict, results) return results
def _build_schema_rules_dict(schema): """ Create a dict that extracts blend rules from an input schema. Parameters ---------- schema : JSON schema fragment The schema in which to search. Returns ------- results : OrderedDict Dictionary with schema attributes as keys and blend rules as values """ def build_rules_dict(subschema, path, combiner, ctx, recurse): # Only interpret elements of the meta component of the model if len(path) > 1 and path[0] == 'meta' and 'items' not in path: for combiner in ['anyOf', 'oneOf']: if combiner in path: path = path[:path.index(combiner)] break attr = '.'.join(path) if subschema.get('properties'): return # Ignore ObjectNodes # Get blending info kwrule = subschema.get('blend_rule') kwtab = subschema.get('blend_table') kwname = subschema.get('fits_keyword', attr) # If rules had already been set, only modify # the rules if there are explicit settings. rule_spec = None result = results.get(attr, []) if kwrule: rule_spec = {attr: {'rule': kwrule}} elif not results.get(attr): rule_spec = {attr: {'rule': 'first'}} if rule_spec: result.append(rule_spec) # Add keyword to table if specified. if kwtab: result.append({attr: kwname}) # Add the results back. if len(result): results[attr] = result else: return results = OrderedDict() dm_schema.walk_schema(schema, build_rules_dict, results) return results
def fill_extensions(im, defaults): """ Set image extensions to their default values """ def fill_hdu(subschema, path, combiner, shape, recurse): if 'fits_hdu' not in subschema: return dtype = subschema.get('datatype') if dtype is None: return ndim = subschema.get('ndim') if ndim and ndim != len(shape): return dtype = ndarray.asdf_datatype_to_numpy_dtype(dtype) keyword = '.'.join(path) default = subschema.get('default', 0.0) im[keyword] = np.full(shape, default, dtype=dtype) shape = get_shape(im, defaults) mschema.walk_schema(im._schema, fill_hdu, shape)