spacetelescope/stdatamodels

close hdulist instead of keeping a reference in `DataModel`

Opened this issue · 0 comments

When opening a DataModel stored in a FITS file:

if file_type == "fits":
hdulist = fits.open(init, memmap=memmap)
self._file_references.append(_FileReference(hdulist))
hdulist = self._migrate_hdulist(hdulist)
asdffile = fits_support.from_fits(
hdulist, self._schema, self._ctx, **kwargs
)

DataModel will create a FileReference for the hdulist and keep it open. This appears to be unnecessary (adding a hdulist.close() on line doesn't break any jwst unit tests). This makes sense because the call to from_fits has to deal with the various quirks of FITS and results in loading all contents from the FITS file.

If we instead just close the hdulist a FileReference is no longer needed.

If we take this one step further and use lazy_load=False with asdf we could entirely remove FileReference and DataModel.close which would simplify the DataModel and ModelContainer api (by no longer needing to close or keep track of closing files).