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

calling len() adds a HDU to an empty FITS #348

Open
ntessore opened this issue Apr 14, 2022 · 3 comments
Open

calling len() adds a HDU to an empty FITS #348

ntessore opened this issue Apr 14, 2022 · 3 comments

Comments

@ntessore
Copy link
Contributor

Calling len() on an empty (i.e. newly created) FITS adds a HDU:

>>> fits = fitsio.FITS('new.fits', 'rw')
>>> len(fits)
1
>>> 0 in fits
True
>>> fits[-1]

  file: test.fits
  extension: 0
  type: IMAGE_HDU
  image info:

The len() call indeed creates the HDU:

>>> fits = fitsio.FITS('another.fits', 'rw')
>>> 0 in fits
False
>>> fits[-1]
ValueError: Requested hdu '-1' not present

This makes the intuitive (well, to me) check len(fits) == 0 for a newly created FITS unusable.

@esheldon
Copy link
Owner

There is always at least one extension in a FITS file, so I think this is the right behavior. Maybe we could add a method that does something like len(fits) == 1 and not fits[0].has_data() to indicate no data other than header entries are present

@ntessore
Copy link
Contributor Author

There is always at least one extension in a FITS file

A FITS file, yes, but the second code example shows that this is not true for a FITS object. Creating a FITS object and not doing anything to it does initially result in something that has no HDUs.

I suppose it goes back to the question of whether or not a FITS object corresponds a valid FITS file on disk (#323).

But in any case, I would argue that calling len() should never modify an object, or start writing things to disk.

@esheldon
Copy link
Owner

I think the bug here is that fits[-1] doesn't work.

The reason len creates the hdu list is because FITS files can have thousands of extensions, so this is done lazily. It may be there is a better way, but do note that it is not possible to determine the number of extensions without running through the whole file, which can be expensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants