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

detect gzipped-FITS files by magic-bytes #6693

Merged
merged 10 commits into from
Jan 7, 2023
18 changes: 10 additions & 8 deletions sunpy/io/file_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,16 @@ def detect_filetype(filepath):
cdf_magic_number = fp.read(4).hex()

# FITS
# Check the extensions to see if it is a gzipped FITS file
filepath_rest_ext1, ext1 = os.path.splitext(filepath)
_, ext2 = os.path.splitext(filepath_rest_ext1)

gzip_extensions = [".gz"]
fits_extensions = [".fts", ".fit", ".fits"]
if (ext1 in gzip_extensions and ext2 in fits_extensions):
return 'fits'
# Checks for gzip signature.
# If found, decompresses first few bytes and checks for FITS
if cdf_magic_number[:4] == '1f8b':
exitflynn marked this conversation as resolved.
Show resolved Hide resolved
import gzip
exitflynn marked this conversation as resolved.
Show resolved Hide resolved
with gzip.open(filepath, 'rb') as fp:
first80 = fp.read(80)
match = re.match(br"[A-Z0-9_]{0,8} *=", first80)
if match is not None:
return 'fits'

exitflynn marked this conversation as resolved.
Show resolved Hide resolved

# Check for "KEY_WORD =" at beginning of file
match = re.match(br"[A-Z0-9_]{0,8} *=", first80)
Expand Down