Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close FITS files after loading #330

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion reproject/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ def test_parse_input_data(tmpdir):

data = np.arange(200).reshape((10, 20))

hdu = fits.ImageHDU(data)
hdu = fits.ImageHDU(data, header)

# We want to test that the WCS is being parsed and output correctly in each
# of these cases. WCS doesn't seem to implement __eq__, so we convert the
# output WCS to a Header and compare that. Here we convert the original
# Header to a WCS and back to ensure an apples-to-apples comparision.
ref_coord_system = WCS(header).to_header()

# As HDU
array, coordinate_system = parse_input_data(hdu)
np.testing.assert_allclose(array, data)
assert coordinate_system.to_header() == ref_coord_system

# As filename
filename = tmpdir.join("test.fits").strpath
Expand All @@ -33,15 +40,18 @@ def test_parse_input_data(tmpdir):

array, coordinate_system = parse_input_data(filename, hdu_in=1)
np.testing.assert_allclose(array, data)
assert coordinate_system.to_header() == ref_coord_system

# As array, header
array, coordinate_system = parse_input_data((data, header))
np.testing.assert_allclose(array, data)
assert coordinate_system.to_header() == ref_coord_system

# As array, WCS
wcs = WCS(hdu.header)
array, coordinate_system = parse_input_data((data, wcs))
np.testing.assert_allclose(array, data)
assert coordinate_system is wcs

ndd = NDData(data, wcs=wcs)
array, coordinate_system = parse_input_data(ndd)
Expand Down
4 changes: 2 additions & 2 deletions reproject/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def parse_input_data(input_data, hdu_in=None):
"""

if isinstance(input_data, str):
# NOTE: File handler is not closed here.
return parse_input_data(fits.open(input_data), hdu_in=hdu_in)
with fits.open(input_data) as hdul:
return parse_input_data(hdul, hdu_in=hdu_in)
elif isinstance(input_data, HDUList):
if hdu_in is None:
if len(input_data) > 1:
Expand Down