How to use the asdf.block.Block 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
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
            ff.blocks.add(b)
        b._array_storage = "streamed"

        try:
            with pytest.warns(None) as w:
                import warnings
                ff._open_impl(ff, buff, mode='rw')
            # Do not tolerate any warnings that occur during schema validation
            assert len(w) == 0, helpers.display_warnings(w)
        except Exception:
            print("From file:", self.filename)
            raise

        # Just test we can write it out.  A roundtrip test
        # wouldn't always yield the correct result, so those have
github spacetelescope / asdf / asdf / block.py View on Github external
if (source == -1 and
                last_block.array_storage == 'streamed'):
                return last_block

            raise ValueError("Block '{0}' not found.".format(source))

        elif isinstance(source, str):
            asdffile = self._asdffile().open_external(
                source, do_not_fill_defaults=True)
            block = asdffile.blocks._internal_blocks[0]
            self.set_array_storage(block, 'external')

        # Handle the case of inline data
        elif isinstance(source, list):
            block = Block(data=np.array(source), array_storage='inline')

        else:
            raise TypeError("Unknown source '{0}'".format(source))

        return block
github spacetelescope / asdf / asdf / block.py View on Github external
def _new_block(self):
        return Block(memmap=self.memmap, lazy_load=self.lazy_load)
github spacetelescope / asdf / asdf / block.py View on Github external
def add_inline(self, array):
        """
        Add an inline block for ``array`` to the block set.
        """
        block = Block(array, array_storage='inline')
        self.add(block)
        return block
github spacetelescope / asdf / asdf / block.py View on Github external
yaml_content = content[content.find(b'\n') + 1:]

        offsets = yaml.load(yaml_content,
                            Loader=yamlutil._yaml_base_loader)

        # Make sure the indices look sane
        if not isinstance(offsets, list) or len(offsets) == 0:
            return

        last_offset = 0
        for x in offsets:
            if (not isinstance(x, int) or
                x > file_size or
                x < 0 or
                x <= last_offset + Block._header.size):
                return
            last_offset = x

        # We always read the first block, so we can confirm that the
        # first entry in the block index matches the first block
        if offsets[0] != first_block.offset:
            return

        if len(offsets) == 1:
            # If there's only one block in the index, we've already
            # loaded the first block, so just return: we have nothing
            # left to do
            return

        # One last sanity check: Read the last block in the index and
        # make sure it makes sense.