GeminiDRSoftware / DRAGONS

Data Reduction for Astronomy from Gemini Observatory North and South
Other
27 stars 16 forks source link

missing ad.close() or equivalent #424

Open phirstgemini opened 1 year ago

phirstgemini commented 1 year ago

There doesn't appear to be any way to cause AstroData to close a file that was opened with astrodata.open(). It seems that in many cases the file ends up getting closed when the ad object goes out of scope or has zero refcount or something, but it also seems like we shouldn't be relying on this.

jehturner commented 1 year ago

From @saimn:

Writing is not an issue, the code uses HDUList.write : https://github.com/GeminiDRSoftware/DRAGONS/blob/bc51afbfc006c86d315905a989273d9461378e53/astrodata/fits.py#L628 Reading is more complicated. the file is kept opened and IIRC it's mostly because the hdu is wrapped with FitsLazyLoadable to delay the scaling: https://github.com/GeminiDRSoftware/DRAGONS/blob/bc51afbfc006c86d315905a989273d9461378e53/astrodata/fits.py#L486 And on top of that, when using memmap, each .data array also needs a file handle to load the data when needed.

So I guess adding a .close() method would be a good idea, but currently the ad object doesn't keep a reference to the HDUList to close it. I'm not completely sure what would happen when accessing ad.data after the fie is closed though, it may depend on whether ad.data had been used or not (astropy will closes the memmap handles if there are not references to hdu.data, and since there is FitsLazyLoadable in between, I'm not sure. Also accessing a closed memmap object will segfault, so needs to be checked).