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([memo]) 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, 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.

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

schema_url = 'container.schema.yaml'

Methods Documentation

append(model)[source]
copy(memo=None)[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=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[, ..]]