DataModel

class jwst.datamodels.DataModel(init=None, schema=None, extensions=None, pass_invalid_values=False, strict_validation=False)[source]

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

Base class of all of the data models.

Parameters:
  • init (shape tuple, file path, file object, astropy.io.fits.HDUList, numpy array, None) –
    • None: A default data model with no shape
    • shape tuple: Initialize with empty data of the given shape
    • file path: Initialize from the given file (FITS or ASDF)
    • readable file object: Initialize from the given file object
    • astropy.io.fits.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 (tree of objects representing a JSON schema, or string naming a schema, optional) – The schema to use to understand the elements on the model. If not provided, the schema associated with this class will be used.
  • extensions (classes extending the standard set of extensions, optional.) – If an extension is defined, the prefix used should be ‘url’.
  • pass_invalid_values (If true, values that do not validate the schema) – will be added to the metadata. If false, they will be set to None
  • strict_validation (if true, an schema validation errors will generate) – an excption. If false, they will generate a warning.
  • 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
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 a ASDF file.
from_fits(init[, schema]) Load a model from a FITS file.
get_envar(name, value)
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_resolver(asdf_file)
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.
read([init, schema, extensions, …])
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 DataModel to an ASDF file.
to_fits(init, *args, **kwargs) Write a DataModel 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]) Updates this model with the metadata elements from another model.
validate() Re-validate the model instance againsst its schema
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

schema
schema_url = 'core.schema.yaml'
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) –
  • new_schema (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 (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.find_fits_keyword('DATE-OBS')
['observation.date']
classmethod from_asdf(init, schema=None)[source]

Load a data model from a ASDF file.

Parameters:
  • init (file path, file object, asdf.AsdfFile object) –
    • file path: Initialize from the given file
    • readable file object: Initialize from the given file object
    • asdf.AsdfFile: Initialize from the given AsdfFile.
  • schema – Same as for __init__
Returns:

model

Return type:

DataModel instance

classmethod from_fits(init, schema=None)[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 – Same as for __init__
Returns:

model

Return type:

DataModel instance

get_envar(name, value)[source]
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_resolver(asdf_file)[source]
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.
read(init=None, schema=None, extensions=None, pass_invalid_values=False, strict_validation=False)
Parameters:
  • init (shape tuple, file path, file object, astropy.io.fits.HDUList, numpy array, None) –
    • None: A default data model with no shape
    • shape tuple: Initialize with empty data of the given shape
    • file path: Initialize from the given file (FITS or ASDF)
    • readable file object: Initialize from the given file object
    • astropy.io.fits.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 (tree of objects representing a JSON schema, or string naming a schema, optional) – The schema to use to understand the elements on the model. If not provided, the schema associated with this class will be used.
  • extensions (classes extending the standard set of extensions, optional.) – If an extension is defined, the prefix used should be ‘url’.
  • pass_invalid_values (If true, values that do not validate the schema) – will be added to the metadata. If false, they will be set to None
  • strict_validation (if true, an schema validation errors will generate) – an excption. If false, they will generate a warning.
  • 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 DataModel to an ASDF file.

Parameters:
  • init (file path or file object) –
  • kwargs (args,) – Any additional arguments are passed along to asdf.AsdfFile.write_to.
to_fits(init, *args, **kwargs)[source]

Write a DataModel 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='')[source]

Updates this model with the metadata elements from another model.

Parameters:
  • d (model or dictionary-like object) – The model to copy the metadata elements from. Can also be a dictionary or dictionary of dictionaries or lists.
  • only (only update the named hdu from extra_fits, e.g.) – only=’PRIMARY’. Can either be a list of hdu names or a single string. If left blank, update all the hdus.
validate()[source]

Re-validate the model instance againsst its schema

values()

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

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