Skip to content

Commit

Permalink
ENH(catalog): path property for FITS catalogues (#69)
Browse files Browse the repository at this point in the history
Adds a `.path` property to FITS catalogues.

Closes: #68
  • Loading branch information
ntessore authored Dec 1, 2023
1 parent 60d9b01 commit fde7018
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
15 changes: 10 additions & 5 deletions heracles/catalog/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,37 @@ def rowfilter(array, expr):
class FitsCatalog(CatalogBase):
"""flexible reader for catalogues from FITS files"""

def __init__(self, filename, *, columns=None, ext=None):
def __init__(self, path, *, columns=None, ext=None):
"""create a new FITS catalogue reader
Neither opens the FITS file nor reads the catalogue immediately.
"""
super().__init__()
self._filename = filename
self._path = path
self._columns = columns
self._ext = ext

def __copy__(self):
"""return a copy of this catalog"""
other = super().__copy__()
other._filename = self._filename
other._path = self._path
other._columns = self._columns
other._ext = self._ext
return other

def __repr__(self):
"""string representation of FitsCatalog"""
s = self._filename
s = self._path
if self._ext is not None:
s = s + f"[{self._ext!r}]"
return s

@property
def path(self):
"""path of the FITS file"""
return self._path

def hdu(self):
"""HDU for catalogue data"""

Expand All @@ -77,7 +82,7 @@ def hdu(self):
if hdu is None:
# need to open the fits file explicitly, not via context manager
# we will not close it, to keep the HDU alive
fits = fitsio.FITS(self._filename)
fits = fitsio.FITS(self._path)

# but ensure fits gets closed in case of error
try:
Expand Down
16 changes: 8 additions & 8 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,13 @@ def test_fits_catalog(rng, tmp_path):
ra = rng.uniform(-180, 180, size=size)
dec = rng.uniform(-90, 90, size=size)

filename = str(tmp_path / "catalog.fits")
path = tmp_path / "catalog.fits"

with fitsio.FITS(filename, "rw") as fits:
with fitsio.FITS(path, "rw") as fits:
fits.write(None)
fits.write_table([ra, dec], names=["RA", "DEC"], extname="MYEXT")

catalog = FitsCatalog(filename)
catalog = FitsCatalog(path)

assert isinstance(catalog, Catalog)

Expand Down Expand Up @@ -441,7 +441,7 @@ def test_fits_catalog(rng, tmp_path):

assert isinstance(copied, FitsCatalog)
assert copied is not catalog
assert copied._filename == catalog._filename
assert copied._path == catalog._path
assert copied._columns == catalog._columns
assert copied._ext == catalog._ext

Expand All @@ -457,13 +457,13 @@ def test_fits_catalog_caching(rng, tmp_path):
ra = rng.uniform(-180, 180, size=size)
dec = rng.uniform(-90, 90, size=size)

filename = str(tmp_path / "cached.fits")
path = tmp_path / "cached.fits"

with fitsio.FITS(filename, "rw") as fits:
with fitsio.FITS(path, "rw") as fits:
fits.write(None)
fits.write_table([ra, dec], names=["RA", "DEC"], extname="MYEXT")

catalog = FitsCatalog(filename)
catalog = FitsCatalog(path)

hdu = catalog.hdu()
assert catalog.hdu() is hdu
Expand All @@ -472,7 +472,7 @@ def test_fits_catalog_caching(rng, tmp_path):

_fits = hdu._FITS

assert _fits.filename() == filename
assert _fits.filename() == str(path)

del hdu
gc.collect()
Expand Down

0 comments on commit fde7018

Please sign in to comment.