stdatamodels.asdf_in_fits contains functions to help migrate code that uses the AsdfInFits format (ASDF data stored in a FITS file) from ASDF (which is dropping support for AsdfInFits) to stdatamodels.

Opening AsdfInFits files

AsdfInFits files can be opened with This function aims to replace previous calls to or and can similarly be used in a with statement.

with'some_file.fits') as af:
    # access the contents of `af` as a normal `asdf.AsdfFile`

It is recommended that a with statement is used to ensure the resulting asdf.AsdfFile and are closed properly.

If a with statement cannot be used, can be used as a regular function (be sure to close the file when done).

af ='some_file.fits')
# access the contents of `af` as a normal `asdf.AsdfFile`

Writing AsdfInFits files

stdatamodels.asdf_in_fits.write() can be used to write ASDF data within a FITS file.

tree = {'sci': [1, 2, 3]}   # data to be stored in ASDF format
stdatamodels.asdf_in_fits.write('some_file.fits', tree)

This functions is meant to replace calls to asdf.AsdfInFits.write_to(). Please see the stdatamodels.asdf_in_fits.write() documentation for all available arguments.

stdatamodels.asdf_in_fits.write() supports references to array data within the This can be accomplished by first creating a and populating it with data

from import fits

hdulist = fits.HDUList()
hdulist.append(fits.ImageHDU(np.arange(512, dtype=float), name='SCI'))
hdulist.append(fits.ImageHDU(np.arange(512, dtype=float), name='DQ'))

Then constructing a tree with references to the same data.

tree = {
    'model': {
        'sci': {
            'data': hdulist['SCI'].data,
        'dq': {
            'data': hdulist['DQ'].data,

Finally providing the tree and hdulist to stdatamodels.asdf_in_fits.write().

stdatamodels.asdf_in_fits.write('some_file.fits', tree, hdulist)

When read back with the data for sci and dq will be read from the HDUList instead of from the ASDF data embeded in the HDUList.