ASDF.Writer (asdf v0.1.0)

Copy Markdown View Source

Writes one ASDF 1.0.0 file.

The tree is YAML 1.1 with %TAG ! tag:stsci.edu:asdf/. Core tags under tag:stsci.edu:asdf/ are written with the ! handle. Any other tag is written as !<uri> so the text survives a round trip.

Mapping keys are sorted. That keeps the file stable. Block payloads are contiguous and C-ordered. byteorder is the array's own order. A zlib block is written only when compression: :zlib is passed. Streamed blocks are not written.

An external ndarray URI is written only when allow_external: true and external_root is set. The path must be relative, must not contain a .. segment, and must stay under that root. The file is not opened.

Summary

Functions

write(sink, tree, opts \\ [])

@spec write(Path.t() | :binary, term(), keyword()) ::
  :ok | {:ok, binary()} | {:error, ASDF.Error.t()}

Writes tree to sink.

sink is a filesystem path or :binary. tree is a map, a list, a scalar, an %ASDF.Tagged{}, an %ASDF.Array{}, an %ASDF.Float32{}, or an %ASDF.Float16{}. A bare map is wrapped as tag:stsci.edu:asdf/core/asdf-1.1.0. opts:

  • :compression — :none (default) or :zlib
  • :allow_external — false (default). true allows an external URI
  • :external_root — required directory when :allow_external is true

A path returns :ok. :binary returns {:ok, bytes}. Mapping keys that are not strings are reason: :invalid_key. An external URI without both options is reason: :external_disabled. A path that escapes :external_root is reason: :external_path. A term the writer cannot encode is reason: :unsupported_write. A path that cannot be written is reason: :io. Does not raise.

Examples

iex> {:ok, bytes} = ASDF.Writer.write(:binary, %{"hello" => "world"})
...> String.contains?(bytes, "%YAML 1.1")
true

iex> {:error, error} = ASDF.Writer.write(:binary, %{count: 1})
...> error.reason
:invalid_key