Packages

Read and write Advanced Scientific Data Format (ASDF) 1.5.0 files from Elixir.

Current section

Files

Jump to
asdf docs guide.md
Raw

docs/guide.md

# Guide

This page is the shape of an ASDF file and the calls that read and write one. Function arguments and return values are in the module docs, starting at `ASDF`.

## The file

A 1.0.z file is, in order:

1. `#ASDF 1.0.z` and optional `#` comments. At most one `#ASDF_STANDARD` comment. The writer emits `#ASDF 1.0.0` and `#ASDF_STANDARD 1.5.0`.
2. One YAML 1.1 document, closed by a line that is `...`.
3. Zero or more binary blocks. Each block has a header and a payload. A block index, when present, lists the header offsets.

`ASDF.open/2` checks the header, finds the tree, and scans block headers. The payload stays unread until `ASDF.Array.to_list/2`, `ASDF.Array.slice/2`, or `ASDF.stream/2`.

## Opening

```elixir
{:ok, handle} = ASDF.open(path, mode: :strict, limits: [max_tree_bytes: 1_000_000])
```

`mode` is `:strict` or `:permissive`. Strict mode returns `{:error, %ASDF.Error{}}` for a duplicate YAML key and for a block index that does not match the scan. Permissive mode keeps the last duplicate key and the forward scan, and records an `%ASDF.Warning{}` on `ASDF.info/1`.

`limits` is an `%ASDF.Limits{}` or a keyword list of its fields. Unknown keys and non-positive integers are errors. The defaults cap the tree, the header, aliases, and block sizes. A block that claims more than `max_block_bytes` is `reason: :decompression_bomb` before inflate.

`:validate` runs the vendored core schemas during open. `:allow_external` is stored for the caller. Open still does not fetch external URIs.

`ASDF.info/1`, `ASDF.tree/1`, `ASDF.metadata/1`, and `ASDF.blocks/1` read that stored result. `metadata/1` is `tree/1`. `blocks/1` returns `%ASDF.Block{}` headers, not payloads.

## The tree

`ASDF.YAML` parses one `%YAML 1.1` document. A missing directive is YAML 1.2 and is `reason: :unsupported_yaml_version`. A second document is `reason: :multiple_documents`.

Mapping keys are booleans, integers, or strings. A float key is `reason: :invalid_key`. Merge keys (`<<`) stay as keys and are not expanded.

Tags stay `%ASDF.Tagged{tag, value}` unless a handler is registered. The tag is the expanded URI as a binary. File text is not turned into an atom. `.inf` and `.nan` are `reason: :unsupported_float` because this runtime has no non-finite floats.

`ASDF.read/2` walks from the root. A string step fetches a mapping key. An integer step fetches a sequence element. The path `["items", 1]` is the second element of `items`. A missing step is `reason: :bad_path`. The walk stops at an `%ASDF.Array{}`. Use `ASDF.Array.slice/2` to index into the array.

## Arrays

`core/ndarray-1.0.0` and `core/ndarray-1.1.0` become `%ASDF.Array{}`.

`source` is `:inline`, `{:block, index}`, or `{:external, uri}`. Inline data is already in the tree. A block source records the header and the byte order. An external URI is not opened. `to_list/2` and `stream/2` return `reason: :external_disabled`.

`ASDF.Array.to_list/2` returns nested lists, one list per axis. `slice/2` takes one entry per axis: an integer keeps that index, and `first..last//1` is inclusive, as in `Enum.slice/2`. `ASDF.stream/2` yields one axis-0 chunk at a time and closes its own descriptor when the stream stops. The handle stays open.

Integers are exact. `float64` is an Elixir float. `float32` is `%ASDF.Float32{bits}` and `float16` is `%ASDF.Float16{bits}` unless `widen: true`. `complex64` and `complex128` are `%ASDF.Complex{real, imag}`. `bool8` must be the byte 0 or 1. `ucs4` has no decoder.

`ASDF.Float16.to_float/1` widens a finite value, including subnormals and signed zero. Infinity and NaN raise `ASDF.Error`.

## Checks and handlers

`ASDF.validate/1` checks known core tags against the schemas vendored from asdf-standard 1.5.0, commit `daff9d5`, manifest `core-1.5.0`. Schemas are not fetched. An unknown tag is `reason: :unresolved_tag` in strict mode and a warning in permissive mode. The value stays `%ASDF.Tagged{}`.

`ndarray` and `integer` schemas refer to themselves. `ASDF.Schema.resolve/1` reports `reason: :cyclic_ref` for those ids. Validation records that as a warning and does not treat the cycle as a successful check.

`ASDF.Registry` is empty by default. A handler is a module that implements `ASDF.Registry.Handler`:

```elixir
defmodule NoteHandler do
  @behaviour ASDF.Registry.Handler

  @impl true
  def decode(%{"text" => text}) when is_binary(text), do: {:ok, text}

  def decode(_value) do
    ASDF.Error.wrap(reason: :invalid_tag, message: "note text is missing")
  end
end

ASDF.Registry.register("tag:example:note-1.0.0", NoteHandler)
```

`decode/1` receives the tag's value after nested tags have been walked. `{:ok, term}` replaces the node. `{:error, %ASDF.Error{}}` fails the read. The tag string is not turned into a module name. Remove a handler with `ASDF.Registry.unregister/1`.

## Writing

`ASDF.write/3` writes one file.

- Core tags under `tag:stsci.edu:asdf/` use the `!` handle. Any other tag is written as `!<uri>`.
- Mapping keys are sorted.
- An array with a concrete datatype is a contiguous C-order block. `byteorder` is the array's own order. `float16` uses `ndarray-1.1.0`. Other arrays use `ndarray-1.0.0`.
- `compression: :zlib` compresses blocks. `:none` is the default.
- Words such as `yes`, `on`, `null`, and `010` are quoted, so they stay strings. YAML 1.1 would otherwise read them as booleans, null, or octal numbers.
- An external ndarray URI is written only when `allow_external: true` and `external_root` is set. The path must be relative, must not contain `..`, and must stay under that root. The file is not opened. Anything else is `reason: :external_disabled` or `reason: :external_path`.

A path that cannot be written is `reason: :io`. A term the writer does not encode is `reason: :unsupported_write`. Streamed blocks are not written. `bzp2` is not offered.

## Errors

Library functions return `{:ok, value}` or `{:error, %ASDF.Error{}}`. `reason` is a stable atom. `message` is the text raised by the bang form. `ASDF.Error` does not include file bytes.

`ASDF.open!/2`, `ASDF.tree!/1`, `ASDF.read!/2`, `ASDF.write!/3`, and the other bang functions raise that struct. `ASDF.Header.parse!/2` and `ASDF.Limits.new!/1` do the same.

A closed handle is `reason: :closed`. A file that does not start with `#ASDF` is `reason: :not_asdf`. A checksum mismatch is `reason: :checksum_mismatch` when the payload is read, not when the header is scanned.

## This version

[Compatibility](compatibility.md) is the list of implemented, partial, and unsupported behavior. [Specification coverage](spec-coverage.md) maps those rows to the 1.5.0 text. Standard 1.6.0, bzip2, streamed writes, remote URIs, and unit, time, FITS, and WCS schemas are outside this version.