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

ENH(catalog): path property for FITS catalogues #69

Merged
merged 1 commit into from
Dec 1, 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
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
Loading