pyimcom.compress.compressutils

Compression tools for PyIMCOM outputs.

Classes

CompressedOutput

Main class for compresseing a FITS file.

Functions

_parser

File name parser; only needed for file names with regular expressions.

ReadFile

Stand-alone function to read a compressed FITS file.

Classes

CompressedOutput

Class for compressing pyimcom output files.

Functions

_parser(fname)

Re-formats a file name containing a regular expression.

ReadFile(fname[, layers])

Wrapper to read a compressed file.

Module Contents

class CompressedOutput(fname, format=None, layers=None, extraargs={})[source]

Class for compressing pyimcom output files.

Parameters:
  • fname (str) – File name for uncompressed file.

  • format (str or None, optional) – Compression format.

  • layers (list, optional) – Which layers to de-compress (if given; otherwise de-compresses everything). Use only for reading (don’t write an instance initialized with layers to a file).

  • extraargs (dict, optional) – Extra arguments for astropy.io.fits.

gzip[source]

Is gzipped?

Type:

bool

ftype

File type. Currently the only option is ‘fits’.

Type:

str

cprstype

Compression type (for a full file, not currently used).

Type:

str

origfile[source]

Original file name (when opened)

Type:

str

hdul

HDU List of information (initially a deep copy of the input file).

Type:

astropy.io.fits.HDUList

cfg

Configuration file used to generate this image.

Type:

pyimcom.config.Config

__init__()[source]

Constructor.

compress_2d_image()[source]

Wrapper for 2d image compression (staticmethod).

get_compression_dict()[source]

Extract compression scheme for a PyIMCOM layer as a dictionary.

decompress_2d_image()[source]

Wrapper for 2d image decompression (staticmethod).

compress_layer()[source]

Compresses a layer.

decompress()[source]

Decompresses the whole file.

recompress()[source]

Recompress previously compressed layers.

to_file()[source]

Saves to a file.

close()[source]

Close associated file.

Notes

At some point, we may add file formats other than FITS, but not yet.

This object is big since it stores a deep copy of the whole file. Therefore, it is not a good idea to open lots of compressed files at once.

origfile[source]
gzip = False[source]
decompress_layers = None[source]
static compress_2d_image(im, scheme, pars)[source]

Wrapper to compress a 2D image.

Parameters:
  • im (np.array) – 2D image to be compressed.

  • scheme (str) – Name of the compression scheme.

  • pars (dict) – Parameters to be passed to the compression algorithm.

Returns:

  • imout (np.array) – The compressed 2D image, same shape as im.

  • ovflow (astropy.io.fits.BinTableHDU) – A table of values that overflowed the quantization range for the compression scheme. Returns None if not used.

classmethod get_compression_dict(hdulist, ilayer)[source]

Extract compression scheme for a PyIMCOM layer as a dictionary.

Parameters:
  • hdulist (astropy.io.fits.HDUList) – The FITS file as an HDUList.

  • ilayer (int) – The layer number to extract.

Returns:

The compression scheme parameter dictionary. Note that all values are returned as strings, the calling routine must typecast them as appropriate.

If the layer was not compressed, returns an empty list, {}.

Return type:

dict

static decompress_2d_image(im, scheme, pars, overflow=None)[source]

Wrapper to decompress a 2D image; overflow table used for some formats.

Parameters:
  • im (np.array) – The compressed image.

  • scheme (str) – Name of the compression scheme.

  • pars (dict) – Parameters to be passed to the compression algorithm.

  • overflow (astropy.io.fits.BinTableHDU or None, optional) – (If used.) A table of values that overflowed the quantization range for the compression scheme. Returns None if not used.

Returns:

imout – The de-compressed 2D image, same shape as im.

Return type:

np.array

compress_layer(layerid, scheme=None, pars={})[source]

Compresses the given layerid with the indicated scheme.

The pars is a dictionary of parameters that go with that scheme. It must be in a format that supports a FITS header. If scheme is None, then the algorithm will re-compress the layer in the same way was done previously (if it was compressed before), otherwise it will do the NULL compression.

Parameters:
  • layerid (int) – Number of the layer to be compressed.

  • scheme (str or None, optional) – Name of the compression scheme. Defaults to None (no compression).

  • pars (dict, optional) – Parameters to be passed to the compression algorithm.

Return type:

None

decompress()[source]

Decompresses all the layers that were compressed by compress_layer.

recompress()[source]

Recompresses all the layers that were previously compressed by compress_layer.

to_file(fname, overwrite=False)[source]

Saves to a file.

Parameters:
  • fname (str) – File name.

  • overwrite (bool, optional) – Whether to overwrite the file.

Return type:

None

close()[source]

Closes the associated file.

__enter__()[source]

Entry method.

__exit__(exc_type, exc_val, exc_tb)[source]

Has context manager automatically close.

_parser(fname)[source]

Re-formats a file name containing a regular expression.

Regular expressions are separated with the ^ character and contain a row and column index, followed by the suffix. For example:

>>> _parser('hello_world/Q_02_31.fits') # no ^, regular file name
'hello_world/Q_02_31.fits'
>>> _parser('hello_world/Row{1:2d}/Q_{0:02d}_{1:02d}^_02_31.fits') # FITS file
'hello_world/Row31/Q_02_31.fits'
>>> _parser('hello_world/Row{1:2d}/Q_{0:02d}_{1:02d}^_02_12.fits.gz') # gzipped; suffix is copied over
'hello_world/Row12/Q_02_12.fits.gz'

This is useful if the files are not all in the same directory.

Parameters:

fname (str or str-like) – Regular file name (not including ^) or regular expression.

Returns:

The formatted file name.

Return type:

str

ReadFile(fname, layers=None)[source]

Wrapper to read a compressed file.

This should read a file just like astropy.io.fits.open(fname, mode=’readonly’), but works even if the file is compressed using compressutils.

Parameters:
  • fname (str or str-like) – File name to read.

  • layers (list, optional) – Which layers to de-compress (if given; otherwise de-compresses everything). Use only for reading (don’t write an instance initialized with layers to a file).

Notes

This can also be used with the Python context manager, e.g.:

with ReadFile('my.fits.gz') as f:
  ...

File names with sepcific types of regular expressions are allowed, and unpacked by the _parser function. Regular expressions are separated with the ^ character and contain a row and column index, followed by the suffix. For example:

>>> _parser('hello_world/Q_02_31.fits') # no ^, regular file name
'hello_world/Q_02_31.fits'
>>> _parser('hello_world/Row{1:2d}/Q_{0:02d}_{1:02d}^_02_31.fits') # FITS file
'hello_world/Row31/Q_02_31.fits'
>>> _parser('hello_world/Row{1:2d}/Q_{0:02d}_{1:02d}^_02_12.fits.gz') # gzipped; suffix is copied over
'hello_world/Row12/Q_02_12.fits.gz'