Repository navigation
Issue with certain compressed FITS files with astropy.io.fits #541
Description
Activity
When I open the file I get the warning:
File may have been truncated: actual file length (51388416) is smaller than the expected size (63106560)Do you get this too? The other HDUs decompress fine, and the error you're getting strongly suggests that the header is reporting a larger size for the file than it actually contains data-wise. So it's strange that imcopy doesn't complain about it.
I think the file may be corrupt - I will double check tomorrow, and will close the issue if so - sorry!
It's still strange that CFITSIO had no problem with it. It's possible that it's just being more permissive with truncated images. I think, if that's the case, PyFITS should try to be too, and could be fairly easily. If nothing else a more useful error message should be raised.
And yeah, after going over the header and manipulating the file a bit I'm also convinced that it's truncated. There are several thousand bytes missing from the heap that should be there.
@iguananaut - weird, I went back to re-download this specific file, and it has a different checksum and works, so it's possible that the original file got truncated during download. There is still the question of how cfitsio can manage to read in this file anyway. But I wouldn't worry about it too much. Certainly not 0.2-worthy, so I'm tagging it as 'Future'.
I guess so. I would like to see what went on here. You say that CFITSIO could read the file, but did you get it to actually extract the data from that last HDU? And if so did you display it to confirm that it was being decompressed successfully?
If it's using tile compression it's conceivable that CFITSIO could still compress up to a certain number of tiles before hitting EOF, but you should still get a truncated image. If you are still getting the full image out of CFITSIO even with the truncated file, I say wizards did it.
I'm going to go ahead an accelerate this a bit because now, with some recent changes in PyFITS, when trying to decompress a truncated image like this I'm actually getting a segfault +_+ Obviously it would have something to do with trying to access memory past the end of the mmap, but I'm not sure why it's not bombing out on creating the mmap to begin with like the exception you were getting previously.
It's possible that the exception is just happening while in the C extension now but isn't being handled. I'll have to take a look...
Fixed the segfault issue (as I suspected it was just an unhandled exception). But I do want to see if I can get some nicer default behavior for truncated arrays. Like imcopy, funpack will also gladly decompress the truncated data (without even outputting a warning, troublingly) and fills the missing bits with zeros. But I think that I will put back to the backburner again after all. The fact that PyFITS doesn't do this isn't a bug, but it would be a nice to have feature.
Meant to reopen this as a placeholder for the issue of adding better support for truncated data
- added a commit that references this issue
on Oct 30, 2013 - added a commit that references this issue
on Nov 9, 2013 I'm getting these warnings when building astropy master on my Mac (with a recent clang compiler):
cextern/cfitsio/imcompress.c:7253:58: warning: comparison of constant -2147483646 with expression of type 'unsigned char' is always false [-Wtautological-constant-out-of-range-compare] if (dither_method == SUBTRACTIVE_DITHER_2 && input[ii] == ZERO_VALUE) ~~~~~~~~~ ^ ~~~~~~~~~~https://gist.github.com/cdeil/9228460
Related or worth reporting to the CFITSIO maintainer?
Well I don't think that would be related, but that does look a bit odd.
Does that warning also occur on line 7331? It should...
@embray Yes, that warning also appears on line 7331:
https://gist.github.com/cdeil/9228460I'll write an email to the CFITSIO maintainer then, because to me this compiler warning looks like it might cause real errors (which I have not observed).
Indeed, it looks like a bug to me. I haven't seen too many files in the wild using
SUBTRACTIVE_DITHER_2, but I think I have seen some?! Further more it's not likely they would be using fewer than 32-bit integers for quantization. That said, as written, the code makes no sense.- added a commit that references this issue
on Jul 2, 2025
I am using astropy.io.fits to read in many FITS files from the UKIDSS GPS survey, which uses RICE compression to reduce their file size. They warn on their website that a recent version of cftisio is required:
Here is an example file that is causing an issue (these represent 5% of all files I've been using):
http://mpia.de/~robitaille/astropy/w20070425_02461_sf_st.fit
The error occurs if I try and access the 4th HDU:
However, if I download and compile the latest cfitsio, then do
make imcopyand useimcopyto decompress the file, it works fine. Therefore, I think there must be some bug/issue in astropy.io.fits - I'm not sure if it's to do with pyfits or the bundling of cfitsio in astropy, so I just thought I'd report it here.