ModelContainer

class jwst.datamodels.ModelContainer(init=None, asn_exptypes=None, asn_n_members=None, **kwargs)[source]

Bases: jwst.datamodels.DataModel

A container for holding DataModels.

This functions like a list for holding DataModel objects. It can be iterated through like a list, DataModels within the container can be addressed by index, and the datamodels can be grouped into a list of lists for grouped looping, useful for NIRCam where grouping together all detectors of a given exposure is useful for some pipeline steps.

Parameters

init (file path, list of DataModels, or None) –

  • file path: initialize from an association table

  • list: a list of DataModels of any type

  • None: initializes an empty ModelContainer instance, to which DataModels can be added via the append() method.

  • asn_exptypes: list of exposure types from the asn file to read into the ModelContainer, if None read all the given files.

  • asn_n_members: Open only the first N qualifying members.

Examples

>>> container = ModelContainer('example_asn.json')
>>> for dm in container:
...     print(dm.meta.filename)

Say the association was a NIRCam dithered dataset. The models_grouped attribute is a list of lists, the first index giving the list of exposure groups, with the second giving the individual datamodels representing each detector in the exposure (2 or 8 in the case of NIRCam).

>>> total_exposure_time = 0.0
>>> for group in container.models_grouped:
...     total_exposure_time += group[0].meta.exposure.exposure_time
>>> c = ModelContainer()
>>> m = datamodels.open('myfile.fits')
>>> c.append(m)
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 keyword arguments passed to lower level functions. These arguments are generally file format-specific. Arguments of note are:

    • FITS

      skip_fits_update - bool or None

      True to skip updating the ASDF tree from the FITS headers, if possible. If None, value will be taken from the environmental SKIP_FITS_UPDATE. Otherwise, the default value is True.

  • available built-in formats are (The) –

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

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

  • Yes Yes Yes (datamodel) –

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

Attributes Summary

group_names

Return list of names for the DataModel groups by exposure.

models_grouped

Returns a list of a list of datamodels grouped by exposure.

schema_url

Methods Summary

append(model)

close()

Close all datamodels.

copy([memo])

Returns a deep copy of the models in this model container.

extend(model)

from_asn(asn_data[, asn_file_path])

Load fits files from a JWST association file.

insert(index, model)

pop([index])

read_asn(filepath)

Load fits files from a JWST association file.

save([path, dir_path, save_model_func])

Write out models in container to FITS or ASDF.

Attributes Documentation

group_names

Return list of names for the DataModel groups by exposure.

models_grouped

Returns a list of a list of datamodels grouped by exposure.

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

Methods Documentation

append(model)[source]
close()[source]

Close all datamodels.

copy(memo=None)[source]

Returns a deep copy of the models in this model container.

extend(model)[source]
from_asn(asn_data, asn_file_path=None)[source]

Load fits files from a JWST association file.

Parameters
  • asn_data (Association) – An association dictionary

  • asn_file_path (str) – Filepath of the association, if known.

insert(index, model)[source]
pop(index=- 1)[source]
read_asn(filepath)[source]

Load fits files from a JWST association file.

Parameters

filepath (str) – The path to an association file.

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

Write out models in container to FITS or ASDF.

Parameters
  • path (str or func or None) –

    • If None, the meta.filename is used for each model.

    • If a string, the string is used as a root and an index is appended.

    • If a function, the function takes the two arguments: the value of model.meta.filename and the idx index, returning constructed file name.

  • dir_path (str) – Directory to write out files. Defaults to current working dir. If directory does not exist, it creates it. Filenames are pulled from meta.filename of each datamodel in the container.

  • save_model_func (func or None) – Alternate function to save each model instead of the models save method. Takes one argument, the model, and keyword argument idx for an index.

Returns

output_paths – List of output file paths of where the models were saved.

Return type

[str[, ..]]