About datamodels¶
The purpose of the data model is to abstract away the peculiarities of the underlying file format. The same data model may be used for data created from scratch in memory, or loaded from ASDF files or some future file format.
The detailed datamodel structure and specifics are contained in the
documentation included with the roman_datamodels
package found here.
Each model instance is created to contain a variety of attributes and data that
are needed for analysis or to propagate information about the file and the
contents of the file. For example, the ImageModel
class has the following
arrays associated with it:
data
: The science data
dq
: The data quality array
err
: The error array
Along with data arrays the datamodel also contains information about the observation that can include the observation program, exposure information, pointing information and processing steps.
Working with models¶
Reading a data model¶
If you have an existing data file it is straightforward to access the file using python.
>>> from roman_datamodels import datamodels as rdm
>>> fn = 'r0019106003005004023_0034_wfi01_cal.asdf'
>>> image_model = rdm.open(fn)
>>> type(image_model)
<class 'roman_datamodels.datamodels._datamodels.ImageModel'>
Where the output of the type command tells you that you have imported an ImageModel from roman_datamodels,
Creating a data model from scratch¶
To create a new ImageModel
, you can just
>>> from roman_datamodels import datamodels as rdm
>>> new_model = rdm.ImageModel.create_fake_data()
>>> type(new_model)
<class 'roman_datamodels.datamodels._datamodels.ImageModel'>
Warning
The values in the file generated by create_fake_data are intended to be clearly incorrect and should be replaced if the file is intended to be used for anything besides a demonstration.
Saving a data model to a file¶
Simply call the save
method on the model instance:
>>> image_model.save("myimage.asdf")
PosixPath('myimage.asdf')
Note
This save
always clobbers the output file. For now the only format
supported is ASDF.
Creating a data model from a file¶
The roman_datamodels.open
function is a convenient way to create a
model from a file on disk. It may be passed any of the following:
a path to an ASDF file
a readable file-like object
The file will be opened, and based on the nature of the data in the
file, the correct data model class will be returned. For example, if
the file contains 2-dimensional data, an ImageModel
instance will be
returned. You will generally want to instantiate a model using a
with
statement so that the file will be closed automatically when
exiting the with
block.
>>> with rdm.open("myimage.asdf") as im:
... assert isinstance(im, rdm.ImageModel)
If you know the type of data stored in the file, or you want to ensure that what is being loaded is of a particular type, use the constructor of the desired concrete class. For example, if you want to ensure that the file being opened contains 2-dimensional image data
>>> with rdm.ImageModel("myimage.asdf") as im:
... pass # raises exception if myimage.asdf is not an image file
This will raise an exception if the file contains data of the wrong type.
Copying a model¶
To create a new model based on another model, simply use its copy
method. This will perform a deep-copy: that is, no changes to the
original model will propagate to the new model
>>> new_model = image_model.copy()
Looking at the contents of a model¶
You can examine the contents of your model from within python using
>>> image_model.info()
root (AsdfObject)
└─roman (WfiImage) # Level 2 (L2) Calibrated Roman Wide Field Instrument (WFI) Rate Image.
├─meta (dict)
│ ├─calibration_software_name (CalibrationSoftwareName): RomanCAL # Calibration Software Name
│ ├─calibration_software_version (CalibrationSoftwareVersion): 9.9.0 # Calibration Software Version Number
│ ├─filename (Filename): r0019106003005004023_0034_wfi01_cal.asdf # File Name
│ ├─file_date (FileDate): 2020-01-01T00:00:00.000
│ ├─model_type (ModelType): ImageModel # Data Model Type
...
or you can print specifics
>>> image_model.search("detector")
root (AsdfObject)
└─roman (WfiImage) # Level 2 (L2) Calibrated Roman Wide Field Instrument (WFI) Rate Image.
└─meta (dict)
└─instrument (WfiMode) # Wide Field Instrument (WFI) Configuration Information
└─detector (str): WFI01