AsdfInFits

class asdf.fits_embed.AsdfInFits(hdulist=None, tree=None, **kwargs)[source]

Bases: asdf.AsdfFile

Embed ASDF tree content in a FITS file.

The YAML rendering of the tree is stored in a special FITS extension with the EXTNAME of ASDF. Arrays in the ASDF tree may refer to binary data in other FITS extensions by setting source to a string with the prefix fits: followed by an EXTNAME, EXTVER pair, e.g. fits:SCI,0.

Examples

Create a FITS file with ASDF structure, based on an existing FITS file:

from astropy.io import fits

hdulist = fits.HDUList()
hdulist.append(fits.ImageHDU(np.arange(512, dtype=np.float), name='SCI'))
hdulist.append(fits.ImageHDU(np.arange(512, dtype=np.float), name='DQ'))

tree = {
    'model': {
        'sci': {
            'data': hdulist['SCI'].data,
            'wcs': 'WCS info'
        },
        'dq': {
            'data': hdulist['DQ'].data,
            'wcs': 'WCS info'
        }
    }
}

ff = fits_embed.AsdfInFits(hdulist, tree)
ff.write_to('test.fits')  # doctest: +SKIP

Methods Summary

close() Close the file handles associated with the AsdfFile.
open(fd[, uri, validate_checksums, …]) Creates a new AsdfInFits object based on given input data
update([all_array_storage, …]) Update the file on disk in place.
write_to(filename[, all_array_storage, …]) Write the ASDF file to the given file-like object.

Methods Documentation

close()[source]

Close the file handles associated with the AsdfFile.

classmethod open(fd, uri=None, validate_checksums=False, extensions=None, ignore_version_mismatch=True, ignore_unrecognized_tag=False, strict_extension_check=False, ignore_missing_extensions=False)[source]

Creates a new AsdfInFits object based on given input data

Parameters:

fd : FITS HDUList instance, URI string, or file-like object

May be an already opened instance of a FITS HDUList instance, string file or http URI, or a Python file-like object.

uri : str, optional

The URI for this ASDF file. Used to resolve relative references against. If not provided, will be automatically determined from the associated file object, if possible and if created from AsdfFile.open.

validate_checksums : bool, optional

If True, validate the blocks against their checksums. Requires reading the entire file, so disabled by default.

extensions : list of AsdfExtension, optional

A list of extensions to the ASDF to support when reading and writing ASDF files. See asdftypes.AsdfExtension for more information.

ignore_version_mismatch : bool, optional

When True, do not raise warnings for mismatched schema versions.

strict_extension_check : bool, optional

When True, if the given ASDF file contains metadata about the extensions used to create it, and if those extensions are not installed, opening the file will fail. When False, opening a file under such conditions will cause only a warning. Defaults to False.

ignore_missing_extensions : bool, optional

When True, do not raise warnings when a file is read that contains metadata about extensions that are not available. Defaults to False.

update(all_array_storage=None, all_array_compression=None, auto_inline=None, pad_blocks=False)[source]

Update the file on disk in place.

Parameters:

all_array_storage : string, optional

If provided, override the array storage type of all blocks in the file immediately before writing. Must be one of:

  • internal: The default. The array data will be stored in a binary block in the same ASDF file.
  • external: Store the data in a binary block in a separate ASDF file.
  • inline: Store the data as YAML inline in the tree.

all_array_compression : string, optional

If provided, set the compression type on all binary blocks in the file. Must be one of:

  • '' or None: No compression.
  • zlib: Use zlib compression.
  • bzp2: Use bzip2 compression.
  • lz4: Use lz4 compression.
  • input: Use the same compression as in the file read. If there is no prior file, acts as None

auto_inline : int, optional

When the number of elements in an array is less than this threshold, store the array as inline YAML, rather than a binary block. This only works on arrays that do not share data with other arrays. Default is 0.

pad_blocks : float or bool, optional

Add extra space between blocks to allow for updating of the file. If False (default), add no padding (always return 0). If True, add a default amount of padding of 10% If a float, it is a factor to multiple content_size by to get the new total size.

include_block_index : bool, optional

If False, don’t include a block index at the end of the file. (Default: True) A block index is never written if the file has a streamed block.

version : str, optional

The ASDF version to write out. If not provided, it will write out in the latest version supported by asdf.

write_to(filename, all_array_storage=None, all_array_compression=None, auto_inline=None, pad_blocks=False, use_image_hdu=False, *args, **kwargs)[source]

Write the ASDF file to the given file-like object.

write_to does not change the underlying file descriptor in the AsdfFile object, but merely copies the content to a new file.

Parameters:

fd : string or file-like object

May be a string path to a file, or a Python file-like object. If a string path, the file is automatically closed after writing. If not a string path,

all_array_storage : string, optional

If provided, override the array storage type of all blocks in the file immediately before writing. Must be one of:

  • internal: The default. The array data will be stored in a binary block in the same ASDF file.
  • external: Store the data in a binary block in a separate ASDF file.
  • inline: Store the data as YAML inline in the tree.

all_array_compression : string, optional

If provided, set the compression type on all binary blocks in the file. Must be one of:

  • '' or None: No compression.
  • zlib: Use zlib compression.
  • bzp2: Use bzip2 compression.
  • lz4: Use lz4 compression.
  • input: Use the same compression as in the file read. If there is no prior file, acts as None.

auto_inline : int, optional

When the number of elements in an array is less than this threshold, store the array as inline YAML, rather than a binary block. This only works on arrays that do not share data with other arrays. Default is 0.

pad_blocks : float or bool, optional

Add extra space between blocks to allow for updating of the file. If False (default), add no padding (always return 0). If True, add a default amount of padding of 10% If a float, it is a factor to multiple content_size by to get the new total size.

include_block_index : bool, optional

If False, don’t include a block index at the end of the file. (Default: True) A block index is never written if the file has a streamed block.

version : str, optional

The ASDF version to write out. If not provided, it will write out in the latest version supported by asdf.