2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library
|
|
|
|
# $Id$
|
|
|
|
#
|
2022-02-15 03:22:46 +03:00
|
|
|
# FITS file handling
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# Copyright (c) 1998-2003 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2024-03-26 13:29:25 +03:00
|
|
|
import gzip
|
2022-02-15 03:22:46 +03:00
|
|
|
import math
|
2016-09-24 12:10:46 +03:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
from . import Image, ImageFile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2024-01-13 08:26:15 +03:00
|
|
|
def _accept(prefix: bytes) -> bool:
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[:6] == b"SIMPLE"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
class FitsImageFile(ImageFile.ImageFile):
|
2010-07-31 06:52:47 +04:00
|
|
|
format = "FITS"
|
|
|
|
format_description = "FITS"
|
|
|
|
|
2024-01-13 08:26:15 +03:00
|
|
|
def _open(self) -> None:
|
|
|
|
assert self.fp is not None
|
|
|
|
|
|
|
|
headers: dict[bytes, bytes] = {}
|
2024-03-26 13:29:25 +03:00
|
|
|
header_in_progress = False
|
|
|
|
decoder_name = ""
|
2021-04-14 15:48:27 +03:00
|
|
|
while True:
|
|
|
|
header = self.fp.read(80)
|
|
|
|
if not header:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "Truncated FITS file"
|
|
|
|
raise OSError(msg)
|
2021-04-14 15:48:27 +03:00
|
|
|
keyword = header[:8].strip()
|
2024-03-26 13:29:25 +03:00
|
|
|
if keyword in (b"SIMPLE", b"XTENSION"):
|
|
|
|
header_in_progress = True
|
|
|
|
elif headers and not header_in_progress:
|
|
|
|
# This is now a data unit
|
2021-04-14 15:48:27 +03:00
|
|
|
break
|
2024-03-26 13:29:25 +03:00
|
|
|
elif keyword == b"END":
|
|
|
|
# Seek to the end of the header unit
|
|
|
|
self.fp.seek(math.ceil(self.fp.tell() / 2880) * 2880)
|
|
|
|
if not decoder_name:
|
|
|
|
decoder_name, offset, args = self._parse_headers(headers)
|
|
|
|
|
|
|
|
header_in_progress = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
if decoder_name:
|
|
|
|
# Keep going to read past the headers
|
|
|
|
continue
|
|
|
|
|
2023-02-25 08:44:07 +03:00
|
|
|
value = header[8:].split(b"/")[0].strip()
|
2021-04-14 15:48:27 +03:00
|
|
|
if value.startswith(b"="):
|
|
|
|
value = value[1:].strip()
|
|
|
|
if not headers and (not _accept(keyword) or value != b"T"):
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "Not a FITS file"
|
|
|
|
raise SyntaxError(msg)
|
2021-04-14 15:48:27 +03:00
|
|
|
headers[keyword] = value
|
|
|
|
|
2024-03-26 13:29:25 +03:00
|
|
|
if not decoder_name:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "No image data"
|
|
|
|
raise ValueError(msg)
|
2024-03-26 13:29:25 +03:00
|
|
|
|
|
|
|
offset += self.fp.tell() - 80
|
|
|
|
self.tile = [(decoder_name, (0, 0) + self.size, offset, args)]
|
|
|
|
|
|
|
|
def _get_size(
|
|
|
|
self, headers: dict[bytes, bytes], prefix: bytes
|
|
|
|
) -> tuple[int, int] | None:
|
|
|
|
naxis = int(headers[prefix + b"NAXIS"])
|
|
|
|
if naxis == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if naxis == 1:
|
|
|
|
return 1, int(headers[prefix + b"NAXIS1"])
|
2021-04-14 15:48:27 +03:00
|
|
|
else:
|
2024-03-26 13:29:25 +03:00
|
|
|
return int(headers[prefix + b"NAXIS1"]), int(headers[prefix + b"NAXIS2"])
|
|
|
|
|
|
|
|
def _parse_headers(
|
|
|
|
self, headers: dict[bytes, bytes]
|
|
|
|
) -> tuple[str, int, tuple[str | int, ...]]:
|
|
|
|
prefix = b""
|
|
|
|
decoder_name = "raw"
|
|
|
|
offset = 0
|
|
|
|
if (
|
|
|
|
headers.get(b"XTENSION") == b"'BINTABLE'"
|
|
|
|
and headers.get(b"ZIMAGE") == b"T"
|
|
|
|
and headers[b"ZCMPTYPE"] == b"'GZIP_1 '"
|
|
|
|
):
|
|
|
|
no_prefix_size = self._get_size(headers, prefix) or (0, 0)
|
|
|
|
number_of_bits = int(headers[b"BITPIX"])
|
|
|
|
offset = no_prefix_size[0] * no_prefix_size[1] * (number_of_bits // 8)
|
2021-04-14 15:48:27 +03:00
|
|
|
|
2024-03-26 13:29:25 +03:00
|
|
|
prefix = b"Z"
|
|
|
|
decoder_name = "fits_gzip"
|
|
|
|
|
|
|
|
size = self._get_size(headers, prefix)
|
|
|
|
if not size:
|
|
|
|
return "", 0, ()
|
|
|
|
|
|
|
|
self._size = size
|
|
|
|
|
|
|
|
number_of_bits = int(headers[prefix + b"BITPIX"])
|
2021-04-14 15:48:27 +03:00
|
|
|
if number_of_bits == 8:
|
2023-07-29 02:28:18 +03:00
|
|
|
self._mode = "L"
|
2021-04-14 15:48:27 +03:00
|
|
|
elif number_of_bits == 16:
|
2024-03-26 13:29:25 +03:00
|
|
|
self._mode = "I;16"
|
2021-04-14 15:48:27 +03:00
|
|
|
elif number_of_bits == 32:
|
2023-07-29 02:28:18 +03:00
|
|
|
self._mode = "I"
|
2021-04-14 15:48:27 +03:00
|
|
|
elif number_of_bits in (-32, -64):
|
2023-07-29 02:28:18 +03:00
|
|
|
self._mode = "F"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2024-06-18 15:44:17 +03:00
|
|
|
args: tuple[str | int, ...]
|
|
|
|
if decoder_name == "raw":
|
|
|
|
args = (self.mode, 0, -1)
|
|
|
|
else:
|
|
|
|
args = (number_of_bits,)
|
2024-03-26 13:29:25 +03:00
|
|
|
return decoder_name, offset, args
|
|
|
|
|
|
|
|
|
|
|
|
class FitsGzipDecoder(ImageFile.PyDecoder):
|
|
|
|
_pulls_fd = True
|
|
|
|
|
2024-06-11 16:26:00 +03:00
|
|
|
def decode(self, buffer: bytes) -> tuple[int, int]:
|
2024-03-26 13:29:25 +03:00
|
|
|
assert self.fd is not None
|
|
|
|
value = gzip.decompress(self.fd.read())
|
|
|
|
|
|
|
|
rows = []
|
|
|
|
offset = 0
|
|
|
|
number_of_bits = min(self.args[0] // 8, 4)
|
|
|
|
for y in range(self.state.ysize):
|
|
|
|
row = bytearray()
|
|
|
|
for x in range(self.state.xsize):
|
|
|
|
row += value[offset + (4 - number_of_bits) : offset + 4]
|
|
|
|
offset += 4
|
|
|
|
rows.append(row)
|
|
|
|
self.set_as_raw(bytes([pixel for row in rows[::-1] for pixel in row]))
|
|
|
|
return -1, 0
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Registry
|
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
Image.register_open(FitsImageFile.format, FitsImageFile, _accept)
|
2024-03-26 13:29:25 +03:00
|
|
|
Image.register_decoder("fits_gzip", FitsGzipDecoder)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2022-02-15 03:22:46 +03:00
|
|
|
Image.register_extensions(FitsImageFile.format, [".fit", ".fits"])
|