Skip to content

Commit

Permalink
Add destructor to FITS and RGB openers
Browse files Browse the repository at this point in the history
- ensure files are closed when opener is dereferenced
  • Loading branch information
ejeschke committed Feb 2, 2024
1 parent 8712fc6 commit 6582c0d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 34 deletions.
11 changes: 1 addition & 10 deletions ginga/BaseImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,6 @@ def get_data(self):
def _get_data(self):
return self._data

def _get_fast_data(self):
"""
Return an array similar to but possibly smaller than self._data,
for fast calculation of the intensity distribution.
NOTE: this is used by the Ginga plugin for Glue
"""
return self._data

def copy_data(self):
data = self._get_data()
return data.copy()
Expand Down Expand Up @@ -251,7 +242,7 @@ def has_valid_wcs(self):
self.wcs.has_valid_wcs())

def _set_minmax(self):
data = self._get_fast_data()
data = self._get_data()
try:
self.maxval = np.nanmax(data)
self.minval = np.nanmin(data)
Expand Down
29 changes: 5 additions & 24 deletions ginga/util/io/io_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""
import re
import numpy as np
import warnings

from ginga.AstroImage import AstroImage, AstroHeader
from ginga.table.AstroTable import AstroTable
Expand Down Expand Up @@ -86,12 +85,6 @@ def use(fitspkg, raise_err=True):

class BaseFitsFileHandler(io_base.BaseIOHandler):

# TODO: remove in a future version
@classmethod
def register_type(cls, name, klass):
raise Exception("This method has been deprecated;"
"you don't need to call it anymore.")

def __init__(self, logger):
super(BaseFitsFileHandler, self).__init__(logger)

Expand All @@ -101,6 +94,9 @@ def __init__(self, logger):
self.hdu_db = {}
self.extver_db = {}

def __del__(self):
self.close()

def get_factory(self):
hdlr = self.__class__(self.logger)
return hdlr
Expand Down Expand Up @@ -442,7 +438,8 @@ def close(self):
self.info = None
fits_f = self.fits_f
self.fits_f = None
fits_f.close()
if fits_f is not None:
fits_f.close()

def find_first_good_hdu(self):

Expand Down Expand Up @@ -547,16 +544,6 @@ def write_fits(self, path, data, header, **kwargs):
def save_as_file(self, filepath, data, header, **kwargs):
self.write_fits(filepath, data, header, **kwargs)

def fromHDU(self, hdu, ahdr):
warnings.warn("fromHDU will be deprecated in the next release--"
"use copy_header instead",
DeprecationWarning)
return self.copy_header(hdu, ahdr)


# TO BE DEPRECATED
PyFitsFileHandler = AstropyFitsFileHandler


class FitsioFileHandler(BaseFitsFileHandler):
"""For loading FITS (Flexible Image Transport System) data files.
Expand Down Expand Up @@ -836,12 +823,6 @@ def write_fits(self, path, data, header):
def save_as_file(self, filepath, data, header, **kwargs):
self.write_fits(filepath, data, header, **kwargs)

def fromHDU(self, hdu, ahdr):
warnings.warn("fromHDU will be deprecated in the next release--"
"use copy_header instead",
DeprecationWarning)
return self.copy_header(hdu, ahdr)


if not fits_configured:
if have_astropy:
Expand Down
3 changes: 3 additions & 0 deletions ginga/util/io/io_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return False

def __del__(self):
self.close()

def load_idx_cont(self, idx_spec, loader_cont_fn, **kwargs):

# TODO: raise an error if idx_spec doesn't match a single image
Expand Down

0 comments on commit 6582c0d

Please sign in to comment.