Compression

The tiles of a compressed image or binary table are compressed by an algorithm (Sect. 10 of the FITS standard), which ZCMPTYPE names. A compression type has everything that its algorithm needs as its fields and type parameters, so every encode and decode has two arguments: the compression type, and the data of the tile for encode, or its compressed buffer for decode. The method that is called is that of the type. The parameters of the header are fields (BLOCKSIZE of Rice, SCALE and SMOOTH of HCompress), the type of the values (BITPIX) is a type parameter (Rice{Int32}, Gzip{Float32}), and the number of pixels of the tile (ZTILE), which the compressed bytes of Rice and PLIO do not say, is a field npix.

julia> using FITSFiles: Rice, encode, decode

julia> data = Int32[10, 12, 11, 15, 15, 14, 100, 3];

julia> rice = Rice{Int32}(; blocksize = 32, npix = length(data));

julia> bytes = encode(rice, data);

julia> decode(rice, bytes)
8-element Vector{Int32}:
  10
  12
  11
  15
  15
  14
 100
   3

The compression types of the algorithms that are implemented are Rice (RICE_1), Gzip (GZIP_1, and GZIP_2 if the bytes are shuffled), PLIO (PLIO_1), HCompress (HCOMPRESS_1), and NoCompression (NOCOMPRESS). The bytes of Gzip and NoCompression are those of the values in the big-endian order of FITS, and a gzip stream is read by the gzip program. The compressed buffer of PLIO is a vector of 16-bit integers, and the others are bytes. The image of HCompress is a matrix of Int32 whose first axis is the fast one, with the dimensions and the scale in its bytes, and its failures are an HCompressError.

FITSFiles.CompressionType — Type
CompressionType

The abstract type of the compression algorithms, whose fields and type parameters are the parameters of the algorithm. encode(compression, data) compresses the data of a tile, and decode(compression, buffer) decompresses the buffer of a tile.

source
FITSFiles.Rice — Type
Rice{T}(; blocksize = 32, npix)

The Rice compression of the npix integers of the type T of a tile (ZCMPTYPE = 'RICE_1', Sect. 10.4.1 of the standard), where T is Int8, Int16, Int32, or Int64, the type of BYTEPIX of 1, 2, 4, or 8 bytes. The field blocksize is ZNAME = 'BLOCKSIZE', the number of pixels in a block, which is 32 by default, and npix is the number of pixels of the tile, which the compressed bytes do not say (ZTILE; the last tile of an image is smaller than the others). The tile is encoded as the differences of adjacent pixels in blocks, whose smaller values take fewer bits, so a smaller block is better for a tile whose noise varies, but it stores more of the bits that say how many.

encode(rice, data) is the bytes of the npix integers of data, and decode(rice, bytes) is the npix integers of the type T.

source
FITSFiles.Gzip — Type
Gzip{T}(; shuffle = false, level = 6)

The gzip compression of the bytes of the values of the type T of a tile (ZCMPTYPE = 'GZIP_1', or 'GZIP_2' if shuffle is true, Sect. 10.4.2 of the standard), which are in the big-endian order of FITS, where T is the type of BITPIX. Gzip is the DEFLATE algorithm (LZ77 and Huffman coding, RFC 1952), and has no parameters in the header. GZIP_2 first shuffles the bytes of the values, so that the bytes are in order of decreasing significance, all of the first bytes of the values, then all of the second bytes, and so on, which compresses the numbers whose most significant bytes are alike. The level of the compression, from 1 (the fastest) to 9 (the smallest), is not a parameter of the format, and the bytes of any level are decompressed the same.

encode(gzip, data) is the bytes of the numbers of data, converted to the type T, and decode(gzip, bytes) is the values of the type T that the bytes are of.

source
FITSFiles.NoCompression — Type
NoCompression{T}()

The tile of numbers of the type T that is not compressed (ZCMPTYPE = 'NOCOMPRESS', Table 36 of the standard), whose bytes are those of its values, which FITS stores in the big-endian order of the bytes, and has no parameters.

encode(nocompression, data) is the bytes of the numbers of data, converted to the type T, and decode(nocompression, bytes) is the values of the type T that the bytes are of.

source
FITSFiles.PLIO — Type
PLIO(; npix)

The IRAF pixel list compression of the npix integers of a tile (ZCMPTYPE = 'PLIO_1', Sect. 10.4.3 of the standard), which compresses the masks and the pixels whose values are mostly repeated, as the lists of the values and the numbers of repeats, and has no parameters, but the number of pixels of the tile, which the list does not say (ZTILE). The values are the integers from 0 to 2^24 (PLIOMAX), which are more than the 12 bits of the data of an instruction, so a value that is far from the one before is in two instructions.

encode(plio, data) is the list of 16-bit integers of the npix pixels of data, and decode(plio, list) is the npix pixels as Int32 integers.

source
FITSFiles.HCompress — Type
HCompress(; scale = 0, divisor = 0, smooth = false)

The H-compress compression of the 2-dimensional integer images of a tile (ZCMPTYPE = 'HCOMPRESS_1', Sect. 10.4.4 of the standard), whose fields are the parameter ZNAME = 'SCALE', which is the scale or the divisor, and ZNAME = 'SMOOTH', whether the decompressed image is smoothed, which removes the blocks of a lossy compression, and is false by default.

The compression is lossless if the scale and the divisor are 0, which is the default. The scale is a floating-point number that is a multiple of the RMS noise of the tile (the least of the 2nd, 3rd, and 5th order estimates of the background noise that cfitsio makes), so the coefficients of the transform of a tile are divided by the integer that is the scale times the noise of the tile, rounded. The divisor is the integer that divides the coefficients of every tile (divisor = 4 divides them by 4 whatever the tile is). The scale and the divisor are exclusive, and a divisor of 0 or 1 is lossless. The keyword ZVAL1 is that of cfitsio: the scale if it is positive, and minus the divisor if it is negative. The divisor of the tile is in the compressed bytes, so the divisor of the decoding is that of the bytes.

encode(hcompress, image) is the bytes of the image, a matrix of integers of 32 bits or less, and decode(hcompress, bytes) is the image, a matrix of Int32. A failure is an HCompressError.

source
FITSFiles.HCompressError — Type
HCompressError(msg)

The error of the H-compress compression of an image that cannot be encoded, and of the bytes that cannot be decoded.

source
FITSFiles.encode — Function
encode(compression::CompressionType, data)

The compressed buffer of the data of a tile: the bytes, or the 16-bit integers of PLIO. The compression type has everything that the algorithm needs, as its fields and type parameters, so the method has two arguments, and the method that is called is that of the type.

source
FITSFiles.decode — Function
decode(compression::CompressionType, buffer)

The data of a tile that the compressed buffer was compressed from by the compression algorithm, which is the inverse of encode. The compression type has everything that the algorithm needs to know and the buffer does not say (the number of pixels of a tile, and the type of the values), as its fields and type parameters, so the method has two arguments, and the method that is called is that of the type.

source

The encoders work in one pass, for speed: the bits are put in an array of the largest size that they can have (a little more than the bytes of the integers for Rice, 3 codes a pixel for PLIO, and the bits of the planes and the signs for HCompress), from which the bytes that are filled are copied to the array that is returned. NoCompression fills its array directly, and Gzip compresses with zlib (Zlib_jll) from a buffer of 2 kilobytes into an array of the bound of the size of its stream, which is copied to the exact size if it is much smaller. HCompress also makes the copy of the image that its transform changes, and the buffers of its quadtrees. The decoders allocate the array that they return (Gzip reads its size from the end of the stream, and inflates into it).

The bytes of Rice (825 tiles of 1 to 257 pixels, in blocks of 1 to 32, of 8-, 16- and 32-bit integers), the list of PLIO (196 tiles, of values as large as 30000) and the bytes of HCompress are those of fits_rcomp, pl_p2li and fits_hcompress of cfitsio for every tile that was compared, and for HCompress (575 images of 1 to 200 pixels per side, of 5 kinds of values, and 5 scales, and 648 more images of 3 types, 7 tile sizes, 4 noises, and 9 scales, which are multiples of the noise, divisors, and 0), and cfitsio decodes the images that are written here, smoothed or not. The values of PLIO are the integers from 0 to 2^24, as the standard says (320 more tiles of up to 40000 pixels, of values as large as 2^24, are the lists of pl_p2li too), which decode gives as Int32; a value that is outside the range is an error.

Tile-compressed images

A compressed image (ZIMAGE = T, Sect. 10.1 of the standard) is a binary table whose rows are the compressed tiles of an image. It is the HDU FITSFiles.ZImage: the header is the table header with the keywords of the image (ZBITPIX, ZNAXISn, ZTILEn) and of the compression (ZCMPTYPE, ZNAMEi and ZVALi, ZQUANTIZ, ZDITHER0, ZBLANK), and hdu.data is the image, which is decompressed when the data are read, and is the array of the type of ZBITPIX (its BSCALE, BZERO, and BLANK are those of an image, so a BLANK pixel is missing). size(hdu) is the size of the image, which is that of ZNAXISn, without reading the data. The tiles are the rows of the table in the order of the axes of the image, the first fastest, and a tile at an edge of the image is smaller. The values of a floating-point image that was quantized (Sect. 10.2) are the integers of its tiles, with ZSCALE and ZZERO of each tile, and the subtractive dither of ZQUANTIZ is subtracted from them. The files that cfitsio wrote are read: the values are exactly those of cfitsio, for the integer algorithms and for the floating-point images (the multiplication and the addition of the dequantization are fused, as cfitsio has them, which matters for the tiles whose zero is far from their values). The standard says the second dither marks a pixel of 0.0 by the integer -2147483647, and cfitsio by -2147483646, which is the integer that is written, and either is read. A file that is read is written as it was.

The undefined pixels of a lossy compression (H-compress with a scale) are not kept by it, so Sect. 10.2.2 of the standard has them in a mask. When a tile has a mask, which is the compressed bytes of its NULL_PIXEL_MASK row (empty for a tile that has no undefined pixel), its pixels are missing for an integer image (they are BLANK, which the image must have) and NaN for a floating-point one, whatever the values that the compression gave them. The algorithm of the masks is ZMASKCMP: PLIO_1, GZIP_1, GZIP_2, RICE_1 (of 8-bit integers), or NOCOMPRESS, whose integers are of any size. cfitsio does not read or write this column, so the masks that are written here are read by this package only (a file with masks has the values of the undefined pixels of its tiles that were changed to the mean of the others).

The keywords of the structure of the original image are kept in the header (Sect. 10.1.1): ZSIMPLE, ZEXTEND, and ZBLOCKED for a primary array, ZTENSION, ZPCOUNT, and ZGCOUNT for an IMAGE extension, and ZHECKSUM and ZDATASUM, with the values and the comments of SIMPLE, EXTEND, BLOCKED, XTENSION, PCOUNT, GCOUNT, CHECKSUM, and DATASUM. HDU(ZImage, hdu) compresses the image of a primary or an image HDU and moves its keywords, and HDU(hdu) of a compressed image is the HDU that was compressed (a primary array if the header has ZSIMPLE, and an image extension if it does not, or the type that is given as HDU(Primary, hdu) and HDU(Image, hdu)), whose array is the data of the compressed image and whose cards are those of the original, without the keywords of the table and of the compression, as the standard says to reconstruct it. The checksums are those of the original image, so they are the last cards of its header, and may not be those of the data that are changed.

julia> using FITSFiles: HDU, ZImage, Primary

julia> image = HDU(Primary, Int16[100i + j for i in 1:20, j in 1:6]);

julia> compressed = HDU(ZImage, image; tile = (10, 2));

julia> compressed.cards["ZSIMPLE"]
true

julia> HDU(compressed) isa HDU{Primary}
true

An HDU is made from an array with the compression type and the size of the tiles as keywords:

julia> using FITSFiles: HDU, ZImage, Rice

julia> image = Int16[100i + j for i in 1:20, j in 1:6];

julia> hdu = HDU(ZImage, image; compression = Rice{Int32}(; blocksize = 32, npix = 1), tile = (10, 2));

julia> open("image.fits", "w") do io
           write(io, HDU(Int16[], missing))
           write(io, hdu)
       end;

The keywords of the HDU that is made are:

  • compression = Rice{Int32}(; blocksize = 32, npix = 1): the compression type, whose parameters are those of the header (BLOCKSIZE and BYTEPIX of Rice{T}, GZIP_2 for Gzip(; shuffle = true)). The npix is set to that of each tile when it is written. A Rice integer has at least the bits of the type of the values of the image, and of the quantized integers (32 bits) of a floating-point image, and at most 32; use Gzip for 64-bit integers. NoCompression (NOCOMPRESS is the value of an HDU that remains uncompressed, Table 36, and cfitsio cannot read tiles of it) is not written. HCompress is of the tiles of two dimensions (the axes of the tile after the second have one pixel) of integers of 32 bits or less, or of the quantized values of a floating-point image: its scale and divisor are exclusive, and are ZVAL of SCALE as cfitsio has it: both are 0 for a lossless compression; a scale is a floating-point multiple of the RMS noise of each tile (the least of the 2nd, 3rd, and 5th order estimates of its background noise that cfitsio makes, FITSFiles.hnoise; the pixels that are BLANK are not counted), so the coefficients are divided by the integer that is the scale times the noise of the tile, rounded (scale = 1.5); and a divisor is the integer factor itself (divisor = 4 divides by 4 in every tile). The factor of each tile is in its bytes, and ZVAL1 is the scale, or minus the divisor, which is kept when the data of an HDU that is read are compressed again. smooth is ZVAL of SMOOTH. PLIO has no negative integers, so the unsigned 16-bit integers (UInt16, stored as Int16 with BZERO = 32768) are stored 32768 higher in its tiles, which cfitsio does too (the standard does not say it), and are lowered when the tiles are read: the tiles are those of cfitsio, and the files of each are read by the other. An undefined pixel (BLANK) of such an image is kept by this package, but cfitsio compares BLANK with the raised integers, so it does not see it.
  • tile: the size of the tiles, the first axis whole and one of the others if it is nothing; a shorter tuple has 1 for the other axes. The tiles of HCompress are of two dimensions of 4 pixels or more, and so is the last tile of each axis, as cfitsio has it (it refuses a smaller tile, and cannot read a tile of one pixel); the error of a tile that is too small says the size that cfitsio would use. The default tile of HCompress is cfitsio's: the whole first axis, and all of the rows if there are 30 or fewer, or 16 (14 to 30 if the last tile would have fewer than 4 rows), which is the same for 4 to 140 rows.
  • quantize = 4: the floating-point values are quantized with a scale of the noise of the tile divided by this number, which is FZQVALUE of the header. The noise is the least of the 2nd, 3rd, and 5th order estimates of the background noise of the tile that cfitsio makes for the floating-point values (FITSFiles.floatnoise; the rows of the tile are those of its first axis, and a NaN is not counted), and the zero is the least value, moved to a multiple of the scale, or, if the tile has a NaN or the dither is the second, one that puts the integers just above that of the undefined pixels, as cfitsio does it. A negative quantize is an absolute scale: the scale of every tile is its absolute value (quantize = -0.01 is a step of 0.01 whatever the noise, and then only the range of the tile is used), which is FZQVALUE as well. If it is nothing, or 0 (as in fits_set_quantize_level of cfitsio), the values are not quantized, which is only for Gzip, and they are compressed without loss. A tile that cannot be quantized (a single pixel, an infinite value, a noise of 0, or a range of more than 2^32 of the scale) is compressed without loss by gzip of level 1, as cfitsio does, in the column GZIP_COMPRESSED_DATA, with a ZSCALE and a ZZERO of 0. The integers, the scales, and the zeros of the tiles are exactly those of cfitsio, for 2700 more images of 5 absolute scales, and for 1440 images of Float32 and Float64 of 10 tile shapes, 8 kinds of values (with NaN, zeros, and a wide range), 3 quantization levels, and 3 dithers, but for one thing: cfitsio fuses a multiplication and an addition in the 5th order noise and in the zero of the undefined pixels (on the processors of Apple and of ARM that were tested, as the compilers do), and so does Julia here (fma), where the cfitsio of a processor that does not may differ in the last bits of ZSCALE and ZZERO. The same holds for HCompress, whose transform has 64-bit integers as that of cfitsio for 32-bit integers has, so the integers near those of the undefined pixels, and those near the limits of 32 bits, are not changed by the transform of a lossless compression (the files are those of cfitsio, tile by tile, for the integers of 32 bits of large values, and for quantized values with undefined pixels).
  • dither = 1: the dither of the quantization, 0 for none, 1 for the first subtractive dither (SUBTRACTIVE_DITHER_1), and 2 for the second, which keeps the pixels of 0.0 (not for HCompress, which a lossy compression would change, and which cfitsio does not allow either).
  • seed = 1: the seed of the dither, from 1 to 10000, which is ZDITHER0.
  • mask = "PLIO_1": the algorithm of the masks of the undefined pixels (missing or NaN) of a lossy HCompress (a scale or a divisor that is not 0), "PLIO_1" or "GZIP_1", which is ZMASKCMP; nothing writes no masks, and then the compression changes the undefined pixels. A tile that has no undefined pixels has an empty mask, and an image that has none has no column NULL_PIXEL_MASK and no ZMASKCMP. The pixels of a mask are replaced by the mean of the others in the tile that is compressed.

A NaN of a quantized image is ZBLANK = -2147483647, as cfitsio has it, and the undefined values of an integer image are missing with the BLANK of the header, as for an image. The keywords of the structure of the table (NAXIS1, NAXIS2, PCOUNT, TFORMn) are made when the HDU is written, from the tiles that are made then, and those of the HDU that is made are those of a table of no values. An HDU that was read from a file and whose data were not read is written by copying the bytes, and one whose data were read is compressed again, which is lossy for a quantized image. The files that are written are read by cfitsio, which gives the same pixels as this package does.

Tile-compressed tables

A compressed table (ZTABLE = T, Sect. 10.3 of the standard) is a binary table whose rows are tiles of rows of another table, and each column of a row has the bytes of the column of the rows of the tile compressed by the algorithm of ZCTYPn (RICE_1, GZIP_1, GZIP_2, or NOCOMPRESS) as a variable-length array of bytes (1QB). It is the HDU FITSFiles.ZTable: the header is that of the table that was compressed, except for the keywords of its structure, which are ZNAXIS1, ZNAXIS2, ZPCOUNT, ZTHEAP, and ZFORMn, and hdu.data is the table that was compressed, with the same types as the data of a Bintable (a named tuple of columns, or the records if the HDU is read with record = true). size(hdu) is the width in bytes and the number of rows of that table, which are ZNAXIS1 and ZNAXIS2, without reading the data. The tiles are made of ZTILELEN rows, the last of fewer.

The values of a column of a tile that has fixed-size cells are the bytes of its rows in order. GZIP_2 shuffles the bytes of each number, and RICE_1 compresses the 1, 2, or 4-byte integers. The cells of a variable-length column are compressed one by one, and those of a tile are described by the gzip-compressed array of the descriptors of the table that was compressed and those of the arrays that were compressed (Sect. 10.3.6). The files of cfitsio are read as the table that cfitsio gives back, and have two details that the standard does not say: the descriptors of the table that was compressed come first, and an array that is not smaller compressed is not compressed, which is known from its number of bytes.

julia> using FITSFiles: HDU, ZTable

julia> table = (id = Int32.(1:1000), flux = [sin(i / 10) for i in 1:1000], name = ["star$i" for i in 1:1000]);

julia> hdu = HDU(ZTable, table; compression = "GZIP_2", tilelen = 250);

julia> open("table.fits", "w") do io
           write(io, HDU(Int16[], missing))
           write(io, hdu)
       end;

The keywords of the HDU that is made are:

  • compression: the algorithm of the columns, a string of one of them for all, or the columns that a named tuple, a dictionary, or a vector of pairs names (by name or by number) with their algorithms; the others have that of the keywords FZALGn and FZALGOR of the header (Sect. 10.3.3), or the algorithm that cfitsio chooses: RICE_1 for the 32-bit integers, GZIP_2 for the other numbers, and GZIP_1 for the others. RICE_1 is for the integers of 1, 2, or 4 bytes, so the other numbers have GZIP_2, and the columns of one byte, of strings, of logicals, of bits, and of complex numbers have GZIP_1, since cfitsio does not read them as shuffled. cfitsio cannot read NOCOMPRESS, which is written as the standard says.
  • tilelen: the number of rows of a tile, which is ZTILELEN of the cards, or FZTILELN of the header, or all of the rows (up to 100 MB).

The keywords of the structure of the compressed table (NAXIS1, NAXIS2, PCOUNT, TFORMn) are made when the HDU is written, from the tiles that are made then, and those of the HDU that is made are those of a table of no values. An HDU that was read from a file and whose data were not read is written by copying the bytes, and one whose data were read is compressed again. The tables that are written are read by cfitsio (fits_uncompress_table), which gives the same table.

The checksums of the table that is compressed, CHECKSUM and DATASUM of its cards, are kept as ZHECKSUM and ZDATASUM with their values and comments (Sect. 10.3.4), as they are for an image. The table that was compressed is made again from a compressed table with HDU(hdu) (or HDU(Bintable, hdu)), which has its data and the cards of its header: ZNAXIS1, ZNAXIS2, ZPCOUNT, ZTHEAP, and ZFORMn are NAXIS1, NAXIS2, PCOUNT, THEAP, and TFORMn again, ZHECKSUM and ZDATASUM are the last cards, CHECKSUM and DATASUM, and the keywords of the compression (ZTABLE, ZTILELEN, ZCTYPn) are not there. The keywords of cfitsio that steer the compression (FZTILELN, FZALGOR, FZALGn) are kept, since they are keywords of the header that was compressed. The strings that are read have not their fill, so the width of a column of strings is that of the longest string, and a warning says the TFORMn and NAXIS1 that are set.

julia> plain = HDU(hdu);   # the HDU of a Bintable, which is written as any other

julia> open("plain.fits", "w") do io
           write(io, HDU(Int16[], missing))
           write(io, plain)
       end;

Threads

The tiles of a compressed image, and the columns of the tiles of a compressed table, are compressed and decompressed each on its own, on the threads of Julia (Threads.@threads), so start Julia with julia -t auto to use them. The files that are written are the same whatever the number of threads. A tile that cannot be decompressed (a corrupt file) raises its exception in a task of a thread, so with more than one thread it is a TaskFailedException (or a CompositeException) that has it.