question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to open regular file when it has an extension indicating it's compressed

See original GitHub issue

Description

If a FITS file has an extension that indicates it’s a compressed file, but it’s not actually compressed, Astropy can’t open the file. This is because in line 441 of https://github.com/astropy/astropy/blob/main/astropy/io/fits/file.py, Astropy determines file type based on the extension, so if the file has .gz at the end of it, it’ll get uncompressed regardless of whether it’s actually compressed or not.

Expected behavior

I would expect AstroPy to be able to understand that the file isn’t actually compressed, and be able to open the file.

Actual behavior

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/edbraun/opt/anaconda3/lib/python3.8/site-packages/astropy/io/fits/hdu/hdulist.py", line 173, in fitsopen
    return HDUList.fromfile(name, mode, memmap, save_backup, cache,
  File "/Users/edbraun/opt/anaconda3/lib/python3.8/site-packages/astropy/io/fits/hdu/hdulist.py", line 408, in fromfile
    return cls._readfrom(fileobj=fileobj, mode=mode, memmap=memmap,
  File "/Users/edbraun/opt/anaconda3/lib/python3.8/site-packages/astropy/io/fits/hdu/hdulist.py", line 1113, in _readfrom
    raise OSError('Empty or corrupt FITS file')
OSError: Empty or corrupt FITS file

Steps to Reproduce

  1. Take a regular FITS file (example.fits) and rename it to add a .gz extension (example.fits.gz). DO NOT actually compress the file.
  2. Try to open the file in AstroPy, e.g., with the following script:
from astropy.io import fits
hdu = fits.open('example.fits.gz')

System Details

macOS-10.15.7-x86_64-i386-64bit Python 3.8.5 (default, Sep 4 2020, 02:22:02) [Clang 10.0.0 ] Numpy 1.19.4 astropy 4.3.dev1522+gbb4c1973f Scipy 1.5.2 Matplotlib 3.3.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
embraycommented, May 17, 2021

Oh, I think I remember why the extension checks are there. They are used for writing: If you write a new FITS file (or update an existing one) that has common compression extensions it will write it compressed.

So any fix for this needs to account for the mode argument and ensure that on write mode it will open the file with the correct compression wrapper. But for reading/updating an existing file it could rely on the magic number alone.

1reaction
pllimcommented, May 7, 2021

OK, thanks for the clarifications!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Open a File With No Extension - Online Tech Tips
You should first confirm if your file actually has no extension, or you just don't have the program needed to open a particular...
Read more >
"Cannot play back the file. The format is not supported. (Error ...
Discusses the cause and resolution of error 80040265 in Windows Media Player. This error is caused by an unsupported file format or codec...
Read more >
About compressed files in Unix - IU KB - Indiana University
Compressed files take up less disk space than normal files, but you cannot read them in the usual way; you must first expand,...
Read more >
Common Windows file extensions | Technical Support Services
To show file extensions: 1. In the File Explorer, click the "View" tab and select the check box next to "File name extensions."...
Read more >
Compress and uncompress file archives in Terminal on Mac
In Terminal, you can use the GNU tar command to compress and uncompress files and folders. The usual file extension for a compressed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found