DataModel

class jwst.datamodels.DataModel(init=None, schema=None, memmap=False, pass_invalid_values=None, strict_validation=None, ignore_missing_extensions=True, **kwargs)[source]

Bases: jwst.datamodels.properties.ObjectNode, jwst.datamodels.ndmodel.NDModel

Base class of all of the data models.

Parameters
  • init (str, tuple, HDUList, ndarray, dict, None) –

    • None : Create a default data model with no shape.

    • tuple : Shape of the data array. Initialize with empty data array with shape specified by the.

    • file path: Initialize from the given file (FITS or ASDF)

    • readable file object: Initialize from the given file object

    • HDUList : Initialize from the given HDUList.

    • A numpy array: Used to initialize the data array

    • dict: The object model tree for the data model

  • schema (dict, str (optional)) – Tree of objects representing a JSON schema, or string naming a schema. The schema to use to understand the elements on the model. If not provided, the schema associated with this class will be used.

  • memmap (bool) – Turn memmap of FITS file on or off. (default: False). Ignored for ASDF files.

  • pass_invalid_values (bool or None) – If True, values that do not validate the schema will be added to the metadata. If False, they will be set to None. If None, value will be taken from the environmental PASS_INVALID_VALUES. Otherwise the default value is False.

  • strict_validation (bool or None) – If True, schema validation errors will generate an exception. If False, they will generate a warning. If None, value will be taken from the environmental STRICT_VALIDATION. Otherwise, the default value is False.

  • ignore_missing_extensions (bool) – When False, raise warnings when a file is read that contains metadata about extensions that are not available. Defaults to True.

  • kwargs (dict) – Additional arguments passed to lower level functions.

  • available built-in formats are (The) –

  • ==== ===== ============= (=========) – Format Read Write Auto-identify

  • ==== ===== =============

  • Yes Yes Yes (datamodel) –

  • ==== ===== =============

Attributes Summary

history

Get the history as a list of entries

override_handle

override_handle identifies in-memory models where a filepath would normally be used.

schema

schema_url

shape

Methods Summary

add_schema_entry(position, new_schema)

Extend the model’s schema by placing the given new_schema at the given dot-separated position in the tree.

clone(target, source[, deepcopy, memo])

close()

copy([memo])

Returns a deep copy of this model.

extend_schema(new_schema)

Extend the model’s schema using the given schema, by combining it in an “allOf” array.

find_fits_keyword(keyword[, return_result])

Utility function to find a reference to a FITS keyword in this model’s schema.

from_asdf(init[, schema])

Load a data model from an ASDF file.

from_fits(init[, schema])

Load a model from a FITS file.

get_fileext()

get_fits_wcs([hdu_name, hdu_ver, key])

Get a astropy.wcs.WCS object created from the FITS WCS information in the model.

get_item_as_json_value(key)

Equivalent to __getitem__, except returns the value as a JSON basic type, rather than an arbitrary Python type.

get_primary_array_name()

Returns the name “primary” array for this model, which controls the size of other arrays that are implicitly created.

get_section(name)

info()

Return datatype and dimension for each array or table

items()

Iterates over all of the schema items in a flat way.

iteritems()

Iterates over all of the schema items in a flat way.

iterkeys()

Iterates over all of the schema keys in a flat way.

itervalues()

Iterates over all of the schema values in a flat way.

keys()

Iterates over all of the schema keys in a flat way.

my_attribute(attr)

Test if attribute is part of the NDData interface

on_save([path])

This is a hook that is called just before saving the file.

open_asdf([init, ignore_version_mismatch, …])

Open an asdf object from a filename or create a new asdf object

read([init, schema, memmap, …])

param init

save(path[, dir_path])

Save to either a FITS or ASDF file, depending on the path.

search_schema(substring)

Utility function to search the metadata schema for a particular phrase.

set_fits_wcs(wcs[, hdu_name])

Sets the FITS WCS information on the model using the given astropy.wcs.WCS object.

to_asdf(init, *args, **kwargs)

Write a data model to an ASDF file.

to_fits(init, *args, **kwargs)

Write a data model to a FITS file.

to_flat_dict([include_arrays])

Returns a dictionary of all of the schema items as a flat dictionary.

update(d[, only, extra_fits])

Updates this model with the metadata elements from another model.

validate()

Re-validate the model instance againsst its schema

validate_required_fields()

Walk the schema and make sure all required fields are in the model

values()

Iterates over all of the schema values in a flat way.

write(path, *args, **kwargs)

Attributes Documentation

history

Get the history as a list of entries

override_handle

override_handle identifies in-memory models where a filepath would normally be used.

schema
schema_url = 'http://stsci.edu/schemas/jwst_datamodel/core.schema'
shape

Methods Documentation

add_schema_entry(position, new_schema)[source]

Extend the model’s schema by placing the given new_schema at the given dot-separated position in the tree.

Parameters
  • position (str) – Dot separated string indicating the position, e.g. meta.instrument.name.

  • new_schema (dict) – Schema tree.

static clone(target, source, deepcopy=False, memo=None)[source]
close()[source]
copy(memo=None)[source]

Returns a deep copy of this model.

extend_schema(new_schema)[source]

Extend the model’s schema using the given schema, by combining it in an “allOf” array.

Parameters

new_schema (dict) – Schema tree.

find_fits_keyword(keyword, return_result=True)[source]

Utility function to find a reference to a FITS keyword in this model’s schema. This is intended for interactive use, and not for use within library code.

Parameters

keyword (str) – A FITS keyword name.

Returns

locations – If return_result is True, a list of the locations in the schema where this FITS keyword is used. Each element is a dot-separated path.

Return type

list of str

Example

>>> model = DataModel()
>>> model.find_fits_keyword('DATE-OBS')
['meta.observation.date']
classmethod from_asdf(init, schema=None, **kwargs)[source]

Load a data model from an ASDF file.

Parameters
  • init (str, file object, AsdfFile) –

    • str : file path: initialize from the given file

    • readable file object: Initialize from the given file object

    • AsdfFile : Initialize from the given`~asdf.AsdfFile`.

  • schema – Same as for __init__

  • kwargs (dict) – Aadditional arguments passed to lower level functions

Returns

model – A data model.

Return type

DataModel instance

classmethod from_fits(init, schema=None, **kwargs)[source]

Load a model from a FITS file.

Parameters
  • init (file path, file object, astropy.io.fits.HDUList) –

    • file path: Initialize from the given file

    • readable file object: Initialize from the given file object

    • astropy.io.fits.HDUList: Initialize from the given HDUList.

  • schema (dict, str) – Same as for __init__

  • kwargs (dict) – Aadditional arguments passed to lower level functions.

Returns

model – A data model.

Return type

DataModel

get_fileext()[source]
get_fits_wcs(hdu_name='SCI', hdu_ver=1, key=' ')[source]

Get a astropy.wcs.WCS object created from the FITS WCS information in the model.

Note that modifying the returned WCS object will not modify the data in this model. To update the model, use set_fits_wcs.

Parameters
  • hdu_name (str, optional) – The name of the HDU to get the WCS from. This must use named HDU’s, not numerical order HDUs. To get the primary HDU, pass 'PRIMARY'.

  • key (str, optional) – The name of a particular WCS transform to use. This may be either ' ' or 'A'-'Z' and corresponds to the "a" part of the CTYPEia cards. key may only be provided if header is also provided.

  • hdu_ver (int, optional) – The extension version. Used when there is more than one extension with the same name. The default value, 1, is the first.

Returns

wcs – The type will depend on what libraries are installed on this system.

Return type

astropy.wcs.WCS or pywcs.WCS object

get_item_as_json_value(key)[source]

Equivalent to __getitem__, except returns the value as a JSON basic type, rather than an arbitrary Python type.

get_primary_array_name()[source]

Returns the name “primary” array for this model, which controls the size of other arrays that are implicitly created. This is intended to be overridden in the subclasses if the primary array’s name is not “data”.

get_section(name)[source]
info()[source]

Return datatype and dimension for each array or table

items()

Iterates over all of the schema items in a flat way.

Each element is a pair (key, value). Each key is a dot-separated name. For example, the schema element meta.observation.date will end up in the result as:

("meta.observation.date": "2012-04-22T03:22:05.432")
iteritems()[source]

Iterates over all of the schema items in a flat way.

Each element is a pair (key, value). Each key is a dot-separated name. For example, the schema element meta.observation.date will end up in the result as:

("meta.observation.date": "2012-04-22T03:22:05.432")
iterkeys()[source]

Iterates over all of the schema keys in a flat way.

Each result of the iterator is a key. Each key is a dot-separated name. For example, the schema element meta.observation.date will end up in the result as the string "meta.observation.date".

itervalues()[source]

Iterates over all of the schema values in a flat way.

keys()

Iterates over all of the schema keys in a flat way.

Each result of the iterator is a key. Each key is a dot-separated name. For example, the schema element meta.observation.date will end up in the result as the string "meta.observation.date".

my_attribute(attr)[source]

Test if attribute is part of the NDData interface

on_save(path=None)[source]

This is a hook that is called just before saving the file. It can be used, for example, to update values in the metadata that are based on the content of the data.

Override it in the subclass to make it do something, but don’t forget to “chain up” to the base class, since it does things there, too.

Parameters

path (str) – The path to the file that we’re about to save to.

static open_asdf(init=None, ignore_version_mismatch=True, ignore_unrecognized_tag=False, **kwargs)[source]

Open an asdf object from a filename or create a new asdf object

read(init=None, schema=None, memmap=False, pass_invalid_values=None, strict_validation=None, ignore_missing_extensions=True, **kwargs)
Parameters
  • init (str, tuple, HDUList, ndarray, dict, None) –

    • None : Create a default data model with no shape.

    • tuple : Shape of the data array. Initialize with empty data array with shape specified by the.

    • file path: Initialize from the given file (FITS or ASDF)

    • readable file object: Initialize from the given file object

    • HDUList : Initialize from the given HDUList.

    • A numpy array: Used to initialize the data array

    • dict: The object model tree for the data model

  • schema (dict, str (optional)) – Tree of objects representing a JSON schema, or string naming a schema. The schema to use to understand the elements on the model. If not provided, the schema associated with this class will be used.

  • memmap (bool) – Turn memmap of FITS file on or off. (default: False). Ignored for ASDF files.

  • pass_invalid_values (bool or None) – If True, values that do not validate the schema will be added to the metadata. If False, they will be set to None. If None, value will be taken from the environmental PASS_INVALID_VALUES. Otherwise the default value is False.

  • strict_validation (bool or None) – If True, schema validation errors will generate an exception. If False, they will generate a warning. If None, value will be taken from the environmental STRICT_VALIDATION. Otherwise, the default value is False.

  • ignore_missing_extensions (bool) – When False, raise warnings when a file is read that contains metadata about extensions that are not available. Defaults to True.

  • kwargs (dict) – Additional arguments passed to lower level functions.

  • available built-in formats are (The) –

  • ==== ===== ============= (=========) – Format Read Write Auto-identify

  • ==== ===== =============

  • Yes Yes Yes (datamodel) –

  • ==== ===== =============

save(path, dir_path=None, *args, **kwargs)[source]

Save to either a FITS or ASDF file, depending on the path.

Parameters
  • path (string or func) – File path to save to. If function, it takes one argument with is model.meta.filename and returns the full path string.

  • dir_path (string) – Directory to save to. If not None, this will override any directory information in the path

Returns

output_path – The file path the model was saved in.

Return type

str

search_schema(substring)[source]

Utility function to search the metadata schema for a particular phrase.

This is intended for interactive use, and not for use within library code.

The searching is case insensitive.

Parameters

substring (str) – The substring to search for.

Returns

locations

Return type

list of tuples

set_fits_wcs(wcs, hdu_name='SCI')[source]

Sets the FITS WCS information on the model using the given astropy.wcs.WCS object.

Note that the “key” of the WCS is stored in the WCS object itself, so it can not be set as a parameter to this method.

Parameters
  • wcs (astropy.wcs.WCS or pywcs.WCS object) – The object containing FITS WCS information

  • hdu_name (str, optional) – The name of the HDU to set the WCS from. This must use named HDU’s, not numerical order HDUs. To set the primary HDU, pass 'PRIMARY'.

to_asdf(init, *args, **kwargs)[source]

Write a data model to an ASDF file.

Parameters
  • init (file path or file object) –

  • args (tuple, list) – Additional positional arguments passed to write_to.

  • kwargs (dict) – Any additional keyword arguments are passed along to write_to.

to_fits(init, *args, **kwargs)[source]

Write a data model to a FITS file.

Parameters
  • init (file path or file object) –

  • kwargs (args,) – Any additional arguments are passed along to astropy.io.fits.writeto.

to_flat_dict(include_arrays=True)[source]

Returns a dictionary of all of the schema items as a flat dictionary.

Each dictionary key is a dot-separated name. For example, the schema element meta.observation.date will end up in the dictionary as:

{ "meta.observation.date": "2012-04-22T03:22:05.432" }
update(d, only=None, extra_fits=False)[source]

Updates this model with the metadata elements from another model.

Note: The update method skips a WCS object, if present.

Parameters
  • d (DataModel or dictionary-like object) – The model to copy the metadata elements from. Can also be a dictionary or dictionary of dictionaries or lists.

  • only (str, None) – Update only the named hdu, e.g. only='PRIMARY'. Can either be a string or list of hdu names. Default is to update all the hdus.

  • extra_fits (boolean) – Update from extra_fits. Default is False.

validate()[source]

Re-validate the model instance againsst its schema

validate_required_fields()[source]

Walk the schema and make sure all required fields are in the model

values()

Iterates over all of the schema values in a flat way.

write(path, *args, **kwargs)[source]