ModelContainer
- class jwst.datamodels.container.ModelContainer(init=None, asn_exptypes=None, asn_n_members=None, **kwargs)[source]
Bases:
SequenceA list-like 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:
- initfile path, list of DataModels, or None
If a file path, initialize from an association table. If a list, can be a list of DataModels of any type If None, initializes an empty
ModelContainerinstance, to which DataModels can be added via theappend()method.- asn_exptypesstr or None
List of exposure types from the asn file to read into the
ModelContainer. If None, read all the given files.- asn_n_membersint
Open only the first N qualifying members.
- **kwargsdict
Additional keyword arguments passed to
datamodel.open(), such asmemmap,guess,strict_validation, etc. Seeopen()for a full list of available keyword arguments.
Notes
When ASN table’s members contain attributes listed in
RECOGNIZED_MEMBER_FIELDS,ModelContainerwill read those attribute values and update the corresponding attributes in themetaof input models.Example of ASN table with additional model attributes to supply custom catalogs."products": [ { "name": "resampled_image", "members": [ { "expname": "input_image1_cal.fits", "exptype": "science", "tweakreg_catalog": "custom_catalog1.ecsv", "group_id": "custom_group_id_number_1" }, { "expname": "input_image2_cal.fits", "exptype": "science", "tweakreg_catalog": "custom_catalog2.ecsv", "group_id": 2 }, { "expname": "input_image3_cal.fits", "exptype": "science", "tweakreg_catalog": "custom_catalog3.ecsv", "group_id": Null } ] } ]
Warning
Input files will be updated in-place with new
metaattribute values when the ASN table’s members contain additional attributes.Warning
Custom
group_idaffects how models are grouped both fortweakregandskymatchsteps. If one wants to group models in one way for thetweakregstep and in a different way for theskymatchstep, one will need to run each step separately with their own ASN tables.Note
group_idcan be an integer, a string, or Null. Whengroup_idisNull, it is converted toNonein Python and a group ID will be assigned based on various exposure attributes - see themodels_groupedproperty for more details.Examples
container = ModelContainer('example_asn.json') for model in container: print(model.meta.filename)
Say the association was a NIRCam dithered dataset. The
models_groupedattribute 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)
Attributes Summary
Return the observatory name for CRDS queries.
List all the group names in the container.
Assign a grouping ID by exposure, if not already assigned.
Methods Summary
append(model)close()Close all datamodels.
copy([memo])Make a deep copy of the container.
extend(model)from_asn(asn_data)Load FITS files from a JWST association file.
Get CRDS parameters for this container.
ind_asn_type(asn_exptype)Determine the indices of models corresponding to
asn_exptype.insert(index, model)pop([index])read_asn(filepath)Load fits files from a JWST association file.
save([path, save_model_func])Write out models in container to FITS or ASDF.
Attributes Documentation
- crds_observatory
Return the observatory name for CRDS queries.
- Returns:
- str
The observatory name for CRDS queries.
- group_names
List all the group names in the container.
- Returns:
- list
A list of group names.
- models_grouped
Assign a grouping ID by exposure, if not already assigned.
If
model.meta.group_iddoes not exist or it isNone, then data from different detectors of the same exposure will be assigned the same group ID, which allows grouping by exposure in thetweakregandskymatchsteps. The following metadata is used when determining 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
If a model already has
model.meta.group_idset, that value will be used for grouping.- Returns:
- list
A list of lists of datamodels grouped by exposure.
Methods Documentation
- copy(memo=None)[source]
Make a deep copy of the container.
- Parameters:
- memodict
Keeps track of elements that have already been copied to avoid infinite recursion.
- Returns:
ModelContainerA deep copy of the container and all the models in it.
- from_asn(asn_data)[source]
Load FITS files from a JWST association file.
- Parameters:
- asn_data
Association An association dictionary
- asn_data
- get_crds_parameters()[source]
Get CRDS parameters for this container.
Notes
stpipe requires ModelContainer to have a crds_observatory attribute in order to pass through
step.run(), but it is never accessed.
- ind_asn_type(asn_exptype)[source]
Determine the indices of models corresponding to
asn_exptype.- Parameters:
- asn_exptypestr
Exposure type as defined in an association, e.g., “science”.
- Returns:
- indlist
Indices of models in the container matching
asn_exptype.
- static read_asn(filepath)[source]
Load fits files from a JWST association file.
- Parameters:
- filepathstr
The path to an association file.
- Returns:
- dict
An association dictionary
- save(path=None, save_model_func=None, **kwargs)[source]
Write out models in container to FITS or ASDF.
- Parameters:
- pathstr or None
Control how output files are written:
If None, the
meta.filenameis used for each model.If a string, the string is used as a root and an index is appended, along with the ‘.fits’ extension.
- save_model_funcfunc or None
Alternate function to save each model instead of the models
savemethod. Takes one argument, the model, and keyword argumentidxfor an index.- **kwargsdict
Additional parameters to be passed to the
savemethod of each model.
- Returns:
- output_paths[str[, …]]
List of output file paths of where the models were saved.