Skip to content

Commit

Permalink
Merge pull request #330 from svank/close-fits-files
Browse files Browse the repository at this point in the history
Close fits files after loading
  • Loading branch information
astrofrog authored Jan 9, 2023
2 parents d39de23 + 6fbceb0 commit 775171d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

0 comments on commit 775171d

Please sign in to comment.