How to use the asdf.AsdfFile function in asdf

To help you get started, we’ve selected a few asdf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spacetelescope / asdf / pytest_asdf / plugin.py View on Github external
def runtest(self):

        name, version = parse_schema_filename(self.filename)
        if should_skip(name, version):
            return

        standard_version = self._find_standard_version(name, version)

        # Make sure that the examples in the schema files (and thus the
        # ASDF standard document) are valid.
        buff = helpers.yaml_to_asdf(
            'example: ' + self.example.strip(), standard_version=standard_version)
        ff = AsdfFile(
            uri=util.filepath_to_url(os.path.abspath(self.filename)),
            extensions=TestExtension())

        # Fake an external file
        ff2 = AsdfFile({'data': np.empty((1024*1024*8), dtype=np.uint8)})

        ff._external_asdf_by_uri[
            util.filepath_to_url(
                os.path.abspath(
                    os.path.join(
                        os.path.dirname(self.filename), 'external.asdf')))] = ff2

        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
github spacetelescope / asdf / pytest_asdf / plugin.py View on Github external
import yaml
import pytest

import numpy as np

import asdf
from asdf import AsdfFile
from asdf import block
from asdf import schema
from asdf import extension
from asdf import treeutil
from asdf import util
from asdf import versioning
from asdf.tests import helpers, CustomTestType

_ctx = AsdfFile()
_resolver = _ctx.resolver


class LabelMapperTestType(CustomTestType):
    version = '1.0.0'
    name = 'transform/label_mapper'


class RegionsSelectorTestType(CustomTestType):
    version = '1.0.0'
    name = 'transform/regions_selector'


class TestExtension(extension.BuiltinExtension):
    """This class defines an extension that represents tags whose
    implementations current reside in other repositories (such as GWCS) but
github ccmc / ccmc-software / kameleon-plus / trunk / kameleon-plus-working / src / ccmc / pyreaders / read_asdf.py View on Github external
def write_asdf_test(filename, explode_variables = None, inline_variables = None, **data):
	"""Writes an asdf file to be tested"""
	# print '\nwriting to', filename
	ff = asdf.AsdfFile(data)
	if explode_variables is None:
		pass
	else:
		for v in explode_variables:
			ff.set_array_storage(v, 'external')

	if inline_variables is None:
		pass
	else:
		for v in inline_variables:
			ff.set_array_storage(v, 'inline')
	ff.write_to(filename)
github spacetelescope / asdf / asdf / commands / tags.py View on Github external
def list_tags(display_classes=False, iostream=sys.stdout):
    """Function to list tags"""
    af = AsdfFile()
    type_by_tag = af._extensions._type_index._type_by_tag
    tags = sorted(type_by_tag.keys())

    for tag in tags:
        string = str(tag)
        if display_classes:
            string += ":  " + _qualified_name(type_by_tag[tag])
        iostream.write(string + '\n')
github astropy / astropy / astropy / io / misc / asdf / connect.py View on Github external
"""
    try:
        import asdf
    except ImportError:
        raise Exception(
            "The asdf module is required to read and write ASDF files")

    if data_key and make_tree:
        raise ValueError("Options 'data_key' and 'make_tree' are not compatible")

    if make_tree:
        tree = make_tree(table)
    else:
        tree = { data_key or 'data' : table }

    with asdf.AsdfFile(tree) as af:
        af.write_to(filename, **kwargs)
github spacetelescope / asdf / asdf / commands / defragment.py View on Github external
Parameters
    ----------
    input : str or file-like object
        The input file.

    output : str of file-like object
        The output file.

    resolve_references : bool, optional
        If `True` resolve all external references before saving.

    compress : str, optional
        Compression to use.
    """
    with asdf.open(input) as ff:
        ff2 = AsdfFile(ff)
        if resolve_references:
            ff2.resolve_references()
        ff2.write_to(
            output,
            all_array_storage='internal',
            all_array_compression=compress)
github spacetelescope / asdf / asdf / commands / extract.py View on Github external
def extract_file(input_file, output_file):
    """Function for performing extraction from ASDF-in-FITS to pure ASDF."""

    try:
        with asdf.open(input_file) as ih:
            if not isinstance(ih, AsdfInFits):
                msg = "Given input file '{}' is not ASDF-in-FITS"
                raise RuntimeError(msg.format(input_file))

            with asdf.AsdfFile(ih.tree) as oh:
                oh.write_to(output_file)

    except (IOError, ValueError) as error:
        raise RuntimeError(str(error))
github spacetelescope / asdf / asdf / tagged.py View on Github external
"""
    Tag an object by wrapping it in a ``Tagged`` instance.
    """
    if isinstance(instance, Tagged):
        instance._tag = tag
    elif isinstance(instance, dict):
        instance = TaggedDict(instance, tag)
    elif isinstance(instance, list):
        instance = TaggedList(instance, tag)
    elif isinstance(instance, str):
        instance = TaggedString(instance)
        instance._tag = tag
    else:
        from . import AsdfFile, yamlutil
        if ctx is None:
            ctx = AsdfFile()
        try:
            instance = yamlutil.custom_tree_to_tagged_tree(instance, ctx)
        except TypeError:
            raise TypeError("Don't know how to tag a {0}".format(type(instance)))
        instance._tag = tag
    return instance
github spacetelescope / asdf / asdf / commands / exploded.py View on Github external
Parameters
    ----------
    input : str or file-like object
        The input file.

    output : str of file-like object
        The output file.

    resolve_references : bool, optional
        If `True` resolve all external references before saving.
    """
    if output is None:
        base, ext = os.path.splitext(input)
        output = base + '_all' + '.asdf'
    with asdf.open(input) as ff:
        ff2 = AsdfFile(ff)
        if resolve_references:
            ff2.resolve_references()
        ff2.write_to(output, all_array_storage='internal')