Skip to content

Commit dd49934

Browse files
committed
io.fits: allow limiting the number of read extensions
In the convenience functions only read as many extensions as necessary to satisfy the users request. This can greatly increase performance when one is only interested in an early subset of extensions. It adds the `ext` keyword to the fitsopen function so it will also be accepted by the top level fits.open function, but this is an implementational detail and not documented. Closes astropygh-4589
1 parent 6aa9a29 commit dd49934

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

‎astropy/io/fits/convenience.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def getdata(filename, *args, **kwargs):
188188
data = hdu.data
189189
if data is None and extidx == 0:
190190
try:
191+
hdulist, extidx = _getext(filename, mode, ext=1, *args, **kwargs)
191192
hdu = hdulist[1]
192193
data = hdu.data
193194
except IndexError:
@@ -753,7 +754,7 @@ def _getext(filename, mode, *args, **kwargs):
753754
elif extver and extname is None:
754755
raise TypeError('extver alone cannot specify an extension.')
755756

756-
hdulist = fitsopen(filename, mode=mode, **kwargs)
757+
hdulist = fitsopen(filename, mode=mode, ext=ext, **kwargs)
757758

758759
return hdulist, ext
759760

‎astropy/io/fits/hdu/hdulist.py‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,7 @@ def index_of(self, key):
521521
nfound = 0
522522
found = None
523523
for idx, hdu in enumerate(self):
524-
name = hdu.name
525-
if isinstance(name, string_types):
526-
name = name.strip().upper()
527-
# 'PRIMARY' should always work as a reference to the first HDU
528-
if ((name == _key or (_key == 'PRIMARY' and idx == 0)) and
529-
(_ver is None or _ver == hdu.ver)):
524+
if self._ext_equal((_key, _ver), hdu, idx):
530525
found = idx
531526
nfound += 1
532527

@@ -786,6 +781,22 @@ def filename(self):
786781
return self._file.name
787782
return None
788783

784+
@classmethod
785+
def _ext_equal(self, ext, hdu, idx):
786+
if _is_int(ext):
787+
return ext == idx
788+
# it has to be a tuple now
789+
extname, extver = ext
790+
if extname is None:
791+
return False
792+
extname = extname.upper()
793+
hduname = hdu.name
794+
if isinstance(hduname, string_types):
795+
hduname = hduname.strip().upper()
796+
# 'PRIMARY' should always work as a reference to the first HDU
797+
return ((hduname == extname or (extname == 'PRIMARY' and idx == 0)) and
798+
(extver is None or extver == hdu.ver))
799+
789800
@classmethod
790801
def _readfrom(cls, fileobj=None, data=None, mode=None,
791802
memmap=None, save_backup=False, cache=True, **kwargs):
@@ -820,6 +831,9 @@ def _readfrom(cls, fileobj=None, data=None, mode=None,
820831

821832
saved_compression_enabled = compressed.COMPRESSION_ENABLED
822833

834+
# (extname, extver) tuple
835+
ext = kwargs.pop("ext", None)
836+
idx = 0
823837
try:
824838
if ('disable_image_compression' in kwargs and
825839
kwargs['disable_image_compression']):
@@ -864,6 +878,10 @@ def _readfrom(cls, fileobj=None, data=None, mode=None,
864878
del exc
865879
break
866880

881+
if ext is not None and HDUList._ext_equal(ext, hdu, idx):
882+
break
883+
idx += 1
884+
867885
# If we're trying to read only and no header units were found,
868886
# raise and exception
869887
if mode in ('readonly', 'denywrite') and len(hdulist) == 0:

0 commit comments

Comments
 (0)