ModelContainer

class jwst.datamodels.ModelContainer(init=None, persist=True, **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.
  • persist (boolean. If True, do not close model after opening it) –

Examples

>>> container = datamodels.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 = datamodels.ModelContainer()
>>> m = datamodels.open('myfile.fits')
>>> c.append(m)

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)
copy() Returns a deep copy of the models in this model container.
extend(models)
from_asn(filepath, **kwargs) Load fits files from a JWST association file.
get_recursively(field) Returns a list of values of the specified field from meta.
insert(index, model)
pop([index])
save(path_not_used[, path]) 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.

Data from different detectors of the same exposure will have the same group id, which allows grouping by exposure. The following metadata is used for grouping:

meta.observation.program_number meta.observation.observation_number meta.observation.visit_number meta.observation.visit_group meta.observation.sequence_id meta.observation.activity_id meta.observation.exposure_number meta.instrument.name meta.instrument.channel

schema_url = 'container.schema.yaml'

Methods Documentation

append(model)[source]
copy()[source]

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

extend(models)[source]
from_asn(filepath, **kwargs)[source]

Load fits files from a JWST association file.

Parameters:filepath (str) – The path to an association file.
get_recursively(field)[source]

Returns a list of values of the specified field from meta.

insert(index, model)[source]
pop(index=-1)[source]
save(path_not_used, path=None, *args, **kwargs)[source]

Write out models in container to FITS or ASDF.

Parameters:
  • path_not_used (string) – This first argument is ignored in this implementation of the save method. It is used by pipeline steps to save individual files, but that is not applicable here. Instead, we use the path arg below and read the filename output from meta.filename in each file in the container.
  • path (string) – 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.