Skip to content
ASDF Projects Tutorials Community
Back to top
View this page

# AsdfFile

class asdf.AsdfFile(tree=None, uri=None, extensions=None, version=None, ignore_unrecognized_tag=False, memmap=False, lazy_load=True, custom_schema=None)

  Bases: `object`

  The main class that represents an ASDF file object.

  Parameters:
    tree dict or AsdfFile, optional

      The main tree data in the ASDF file. Must conform to the ASDF schema.

    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 `asdf.open`.

    extensions object, optional

      Additional extensions to use when reading and writing the file. May be an `asdf.extension.Extension` or a `list` of extensions.

    version str, optional

      The ASDF core schemas version. If not provided, defaults to the configured default version. See `asdf.config.AsdfConfig.default_version`.

    ignore_unrecognized_tag bool, optional

      When `True`, do not raise warnings for unrecognized tags. Set to `False` by default.

    memmap bool, optional

      When `True`, when reading files, attempt to memmap underlying data arrays when possible. Defaults to `False`.

    lazy_load bool, optional

      When `True` and the underlying file handle is seekable, data arrays will only be loaded lazily: i.e. when they are accessed for the first time. In this case the underlying file must stay open during the lifetime of the tree. Setting to False causes all data arrays to be loaded up front, which means that they can be accessed even after the underlying file is closed.

    custom_schema str, optional

      Path to a custom schema file that will be used for a secondary validation pass. This can be used to ensure that particular ASDF files follow custom conventions beyond those enforced by the specification.

  Attributes Summary

  `comments` | Get the comments after the header, before the tree.
  `extension_manager` | Get the ExtensionManager for this AsdfFile.
  `extensions` | Get the list of user extensions that are enabled for use with this AsdfFile.
  `file_format_version` |
  `tree` | Get/set the tree of data in the ASDF file.
  `uri` | Get the URI associated with the `AsdfFile`.
  `version` | Get this AsdfFile's ASDF core schemas version.
  `version_string` | Get this AsdfFile's ASDF core schemas version as a string.

  Methods Summary

  `add_history_entry`(description[, software]) | Add an entry to the history list.
  `close`() | Close the file handles associated with the `asdf.AsdfFile`.
  `copy`() |
  `fill_defaults`() | Fill in any values that are missing in the tree using default values from the schema.
  `find_references`() | Finds all external "JSON References" in the tree and converts them to `reference.Reference` objects.
  `get_array_compression`(arr) | Get the compression type for the given array data.
  `get_array_compression_kwargs`(arr) |
  `get_array_save_base`(arr) | Returns the `save_base` option for `arr`.
  `get_array_storage`(arr) | Get the block type for the given array data.
  `get_history_entries`() | Get a list of history entries from the file object.
  `info`([max_rows, max_cols, show_values]) | Print a rendering of this file's tree to stdout.
  `keys`() |
  `make_reference`([path]) | Make a new reference to a part of this file's tree, that can be assigned as a reference to another tree.
  `open_external`(uri, **kwargs) | Open an external ASDF file, from the given (possibly relative) URI.
  `remove_defaults`() | Remove any values in the tree that are the same as the default values in the schema
  `resolve_references`() | Finds all external "JSON References" in the tree, loads the external content, and places it directly in the tree.
  `resolve_uri`(uri) | Resolve a (possibly relative) URI against the URI of this ASDF file.
  `schema_info`([key, path, preserve_list]) | Get a nested dictionary of the schema information for a given key, relative to the path.
  `search`([key, type_, value, filter_]) | Search this file's tree.
  `set_array_compression`(arr, compression, ...) | Set the compression to use for the given array data.
  `set_array_save_base`(arr, save_base) | Set the `save_base` option for `arr`.
  `set_array_storage`(arr, array_storage) | Set the block type to use for the given array data.
  `update`([all_array_storage, ...]) | Update the file on disk in place.
  `validate`() | Validate the current state of the tree against the ASDF schema.
  `write_to`(fd[, all_array_storage, ...]) | Write the ASDF file to the given file-like object.

  Attributes Documentation

  comments

    Get the comments after the header, before the tree.

  extension_manager

    Get the ExtensionManager for this AsdfFile.

    Returns:
      asdf.extension.ExtensionManager
  extensions

    Get the list of user extensions that are enabled for use with this AsdfFile.

    Returns:
      list of asdf.extension.ExtensionProxy
  file_format_version
  tree

    Get/set the tree of data in the ASDF file.

    When set, the tree will be validated against the ASDF schema.

  uri

    Get the URI associated with the `AsdfFile`.

    In many cases, it is automatically determined from the file handle used to read or write the file.

  version

    Get this AsdfFile’s ASDF core schemas version.

    Returns:
      asdf.versioning.AsdfVersion
  version_string

    Get this AsdfFile’s ASDF core schemas version as a string.

    Returns:
      str

  Methods Documentation

  add_history_entry(description, software=None)

    Add an entry to the history list.

    Parameters:
      description str

        A description of the change.

      software dict or list of dict

        A description of the software used. It should not include asdf itself, as that is automatically notated in the `asdf_library` entry.

        Each dict must have the following keys:

        - `name`: The name of the software

        - `author`: The author or institution that produced the software

        - `homepage`: A URI to the homepage of the software

        - `version`: The version of the software

  close()

    Close the file handles associated with the `asdf.AsdfFile`.

  copy()
  fill_defaults()

    Fill in any values that are missing in the tree using default values from the schema.

  find_references()

    Finds all external “JSON References” in the tree and converts them to `reference.Reference` objects.

  get_array_compression(arr)

    Get the compression type for the given array data.

    Parameters:
      arr numpy.ndarray
    Returns:
      compression str or None
  get_array_compression_kwargs(arr)
  get_array_save_base(arr)

    Returns the `save_base` option for `arr`. When `arr` is written to a file, if `save_base` is `True` the base array for `arr` will be saved.

    Parameters:
      arr numpy.ndarray
    Returns:
      save_base bool or None
  get_array_storage(arr)

    Get the block type for the given array data.

    Parameters:
      arr numpy.ndarray
  get_history_entries()

    Get a list of history entries from the file object.

    Returns:
      entries list

        A list of history entries.

  info(max_rows=24, max_cols=120, show_values=True)

    Print a rendering of this file’s tree to stdout.

    Parameters:
      max_rows int, tuple, or None, optional

        Maximum number of lines to print. Nodes that cannot be displayed will be elided with a message. If int, constrain total number of displayed lines. If tuple, constrain lines per node at the depth corresponding to the tuple index. If None, display all lines.

      max_cols int or None, optional

        Maximum length of line to print. Nodes that cannot be fully displayed will be truncated with a message. If int, constrain length of displayed lines. If None, line length is unconstrained.

      show_values bool, optional

        Set to False to disable display of primitive values in the rendered tree.

  keys()
  make_reference(path=None)

    Make a new reference to a part of this file’s tree, that can be assigned as a reference to another tree.

    Parameters:
      path list of str and int, optional

        The parts of the path pointing to an item in this tree. If omitted, points to the root of the tree.

    Returns:
      reference

        A reference object.

    Examples

    For the given AsdfFile `ff`, add an external reference to the data in an external file:

        >>> import asdf
        >>> flat = asdf.open("http://stsci.edu/reference_files/flat.asdf")
        >>> ff.tree['flat_field'] = flat.make_reference(['data'])

  open_external(uri, **kwargs)

    Open an external ASDF file, from the given (possibly relative) URI. There is a cache (internal to this ASDF file) that ensures each external ASDF file is loaded only once.

    Parameters:
      uri str

        An absolute or relative URI to resolve against the URI of this ASDF file.

    Returns:
      asdffile AsdfFile

        The external ASDF file.

  remove_defaults()

    Remove any values in the tree that are the same as the default values in the schema

  resolve_references()

    Finds all external “JSON References” in the tree, loads the external content, and places it directly in the tree. Saving a ASDF file after this operation means it will have no external references, and will be completely self-contained.

  resolve_uri(uri)

    Resolve a (possibly relative) URI against the URI of this ASDF file. May be overridden by base classes to change how URIs are resolved. This does not apply any `uri_mapping` that was passed to the constructor.

    Parameters:
      uri str

        An absolute or relative URI to resolve against the URI of this ASDF file.

    Returns:
      uri str

        The resolved URI.

  schema_info(key='description', path=None, preserve_list=True)

    Get a nested dictionary of the schema information for a given key, relative to the path.

    This method will only return unambiguous info. If a property is subject to multiple subschemas or contains ambiguous entries (multiple titles) no result will be returned for that property.

    Parameters:
      key str

        The key to look up. Default: “description”

      path str or asdf.search.AsdfSearchResult

        A dot-separated path to the parameter to find the key information on or an `asdf.search.AsdfSearchResult` object. Default = None (full dictionary).

      preserve_list bool

        If True, then lists are preserved. Otherwise, they are turned into dicts.

  search(key=NotSet, type_=NotSet, value=NotSet, filter_=None)

    Search this file’s tree.

    Parameters:
      key NotSet, str, or any other object

        Search query that selects nodes by dict key or list index. If NotSet, the node key is unconstrained. If str, the input is searched among keys/indexes as a regular expression pattern. If any other object, node’s key or index must equal the queried key.

      type_NotSet, str, or builtins.type

        Search query that selects nodes by type. If NotSet, the node type is unconstrained. If str, the input is searched among (fully qualified) node type names as a regular expression pattern. If builtins.type, the node must be an instance of the input.

      value NotSet, str, or any other object

        Search query that selects nodes by value. If NotSet, the node value is unconstrained. If str, the input is searched among values as a regular expression pattern. If any other object, node’s value must equal the queried value.

      filter_callable

        Callable that filters nodes by arbitrary criteria. The callable accepts one or two arguments:

        - the node

        - the node’s list index or dict key (optional)

        and returns True to retain the node, or False to remove it from the search results.

    Returns:
      asdf.search.AsdfSearchResult

        the result of the search

  set_array_compression(arr, compression, **compression_kwargs)

    Set the compression to use for the given array data.

    Parameters:
      arr numpy.ndarray

        The array to set. If multiple views of the array are in the tree, only the most recent compression setting will be used, since all views share a single block.

      compression str or None

        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.

  set_array_save_base(arr, save_base)

    Set the `save_base` option for `arr`. When `arr` is written to a file, if `save_base` is `True` the base array for `arr` will be saved.

    Note that similar to other array options this setting is linked to the base array if `arr` is a view.

    Parameters:
      arr numpy.ndarray
      save_base bool or None

        if `None` the `default_array_save_base` value from asdf config will be used

  set_array_storage(arr, array_storage)

    Set the block type to use for the given array data.

    Parameters:
      arr numpy.ndarray

        The array to set. If multiple views of the array are in the tree, only the most recent block type setting will be used, since all views share a single block.

      array_storage str

        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.

  update(all_array_storage=NotSet, all_array_compression=NotSet, compression_kwargs=NotSet, pad_blocks=False, include_block_index=True, version=None)

    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

      compression_kwargs dict, optional

        If provided, set this as the compression keyword arguments for all binary blocks in the file.

      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

        Update the ASDF core schemas version of this AsdfFile before writing.

  validate()

    Validate the current state of the tree against the ASDF schema.

  write_to(fd, all_array_storage=NotSet, all_array_compression=NotSet, compression_kwargs=NotSet, pad_blocks=False, include_block_index=True, version=None)

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

    `write_to` does not change the underlying file descriptor in the `asdf.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, it is the caller’s responsibility to close the object.

      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.

      compression_kwargs dict, optional

        If provided, set this as the compression keyword arguments for all binary blocks in the file.

      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

        Update the ASDF core schemas version of this AsdfFile before writing.