[AstroPy] passing hdus as arguments

Slavin, Jonathan jslavin at cfa.harvard.edu
Fri Jan 6 14:42:42 EST 2023


Hi Madison,

Thanks for your response. I was aware that the HDUList returned by
fits.open() can contain more than one HDU. Your discussion of the
considerations was very helpful. We're just looking to read in the data
from a file on disk at this point, so it seems like we should be okay.
Though I'm still a bit undecided whether it's better to pass in the
HDULists or the file paths.

Regards,
Jon

On Fri, Jan 6, 2023 at 12:00 PM <astropy-request at python.org> wrote:

> Message: 1
> Date: Thu, 5 Jan 2023 18:35:21 +0100
> From: "E. Madison Bray" <erik.m.bray at gmail.com>
> To: Astronomical Python mailing list <astropy at python.org>
> Subject: Re: [AstroPy] passing hdus as arguments
> Message-ID:
>         <CAOTD34ZFe9hOCeuWbORvqads7W61Zwuz0qumuV0nGeKSOD4_=
> g at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hi Jon,
>
> First a quick clarification about nomenclature : what fits.open() returns
> is not an HDU (Header Data Unit, of which there can be one or more in a
> single FITS file. Rather, something that for historical reasons is called
> HDUList, but is just an object representing the whole FITS file. As you
> surmised it contains a wrapper around a file handle. Keeping that open
> doesn't use any significant memory, but reading the actual data arrays in
> the individual HDUs will of course map their data into "real" memory.
>
> As for the ramifications it depends on what your function is doing and how
> it's intended to be used. In particular if it's just reading the data (and
> not syncing updates back to the original file) I wouldn't overly worry
> about it.
>
> As long as there aren't any reference leaks, once there are no more
> references to the HDUList object it will eventually be garbage collected.
>
> Of course, if you're actually noticing some memory leaks that's worth
> bringing up. There have been some in the past IIRC.
>
>
> On Thu, Jan 5, 2023, 16:39 Slavin, Jonathan <jslavin at cfa.harvard.edu>
> wrote:
>
> > Hi,
> >
> > I have a question related to astropy.io.fits.
> > I'm wondering about the ramifications of defining a function to require
> > hdus as arguments. So the user would do:
> > hdu = fits.open(fitsfilename)
> > result = fcn(hdu,...)
> > What I wonder about is the ramifications if the hdu isn't closed. Of
> > course the call could use a context manager:
> > with fits.open(fitsfilename) as hdu:
> >     result = fcn(hdu,...)
> > which assures the hdu will be closed, though I don't think there's any
> way
> > to enforce calling it in that way. I suppose that the worst that is
> likely
> > to happen is that memory will be used up. I guess this is not really
> > different from any function using file handles as arguments. Any
> thoughts?
> >
> > Jon
> >
> > --
> > Jonathan D. Slavin (he/him)
> > Astrophysicist - High Energy Astrophysics Division
> > Center for Astrophysics | Harvard & Smithsonian
> > Office: (617) 496-7981 | Cell: (781) 363-0035
> > 60 Garden Street | MS 04 | Cambridge, MA 02138
> >
> >
> > _______________________________________________
> > AstroPy mailing list
> > AstroPy at python.org
> > https://mail.python.org/mailman/listinfo/astropy
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> https://mail.python.org/pipermail/astropy/attachments/20230105/72a3f2a7/attachment-0001.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
>
>
> ------------------------------
>
> End of AstroPy Digest, Vol 194, Issue 2
> ***************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/astropy/attachments/20230106/fe2c172c/attachment.html>


More information about the AstroPy mailing list