Skip to content

Commit

Permalink
Merge pull request #51 from LCOGT/fix/use-with-fits-open
Browse files Browse the repository at this point in the history
use with to close fits opens
  • Loading branch information
LTDakin authored Jan 24, 2025
2 parents 1db9f02 + 03d639a commit 5dd23d7
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions datalab/datalab_session/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ def get_hdu(path: str, extension: str = 'SCI') -> list[fits.HDUList]:
Returns a HDU for the fits in the given path
Warning: this function returns an opened file that must be closed after use
"""
hdu = fits.open(path)
try:
extension = hdu[extension]
except KeyError:
raise ClientAlertException(f"{extension} Header not found in fits file at {path.split('/')[-1]}")

return extension
with fits.open(path) as hdu:
try:
extension_copy = hdu[extension].copy()
except KeyError:
raise ClientAlertException(f"{extension} Header not found in fits file at {path.split('/')[-1]}")
return extension_copy

def get_fits_dimensions(fits_file, extension: str = 'SCI') -> tuple:
return fits.open(fits_file)[extension].shape
with fits.open(fits_file) as hdu:
hdu_shape = hdu[extension].shape
return hdu_shape

def create_fits(key: str, image_arr: np.ndarray, comment=None) -> str:
"""
Expand Down

0 comments on commit 5dd23d7

Please sign in to comment.