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