# ASDF (Advanced Scientific Data Format)

[![Hex.pm](https://img.shields.io/hexpm/v/asdf.svg)](https://hex.pm/packages/asdf)
[![Docs](https://img.shields.io/badge/docs-hexdocs-blue.svg)](https://hexdocs.pm/asdf)
[![CI](https://github.com/baoulo/asdf-elixir/actions/workflows/ci.yml/badge.svg)](https://github.com/baoulo/asdf-elixir/actions/workflows/ci.yml)
[![License](https://img.shields.io/github/license/baoulo/asdf-elixir)](https://github.com/baoulo/asdf-elixir/blob/main/LICENSE)

![Oak tree whose squirrels, labeled ASDF, carry Elixir bottles and scrolls](docs/asdf.jpeg)

The [Advanced Scientific Data Format](https://www.asdf-format.org/en/latest/overview.html) (ASDF, pronounced “Az-diff”) is an interchange format for scientific data. One file holds hierarchical metadata as YAML and array values in binary blocks. The metadata is text a person can read and edit. The arrays can be compressed, and a reader can map them rather than copy them. Schemas check that the content matches a declared model. The file format and the objects inside it are versioned, so an older file stays readable. The format is built on YAML 1.1, JSON Schema, and semantic versioning.

FITS has carried astronomical images and tables for decades, with metadata stored as a flat list of keywords. Newer observations need richer models: coordinates, units, provenance, and expressions that do not fit a keyword card. A text tree such as VOTable can hold that structure and cannot hold large binary arrays. HDF5 holds the arrays and is aimed at large-scale computation, and its metadata is a weak interchange format. ASDF is the hybrid: a structured, extensible header for exchange, and raw binary that loads quickly.

The file begins with the token `#ASDF`. This project is an implementation of that format. It is not the [asdf](https://asdf-vm.com/) version manager.

## When to use it

ASDF is for handing a data product from an instrument or a pipeline to a scientist, or from one scientist to another.

- The metadata is a tree: nested structures, units, coordinates, models, and history, in a form a person can open in an editor.
- The numbers are large enough to belong in binary blocks, optionally compressed, beside that tree.
- A schema should accept or reject the file, and a later revision of the schema should still open older files.
- New kinds of metadata will appear. The file layout stays fixed while tagged types and extension schemas carry the new content.

The same file is not tied to one language. The standard is the contract. Python is the reference implementation; other languages implement the same specification.

## When not to use it

ASDF is an interchange and delivery format. The specification does not try to be the file a distributed or high-performance job computes on. A layout aimed at parallel I/O can conflict with a file designed to be exchanged and archived. ASDF still carries the product into those environments. The calculation then uses whatever store the job wants.

## Who uses it

The [Space Telescope Science Institute](https://www.stsci.edu/) develops the standard.

- The James Webb Space Telescope calibration pipeline works from an ASDF-shaped model. World-coordinate information that does not fit FITS is stored as ASDF. Archive products are still distributed as FITS, and those FITS files carry the most accurate world-coordinate description in an ASDF extension. The pipeline can also read and write whole ASDF files.
- The Nancy Grace Roman Space Telescope uses ASDF as the format of its data products. Its calibration pipeline is built for those files.
- The Daniel K. Inouye Solar Telescope uses ASDF for the metadata of products that point at thousands of files, and for the world-coordinate description of the referenced data.
- The Vera C. Rubin Observatory uses ASDF to interchange world-coordinate information.

Astronomy is where the format started. The layout itself is ordinary scientific and engineering data: a described tree plus arrays. People outside astronomy use it for that.

The [overview](https://www.asdf-format.org/en/latest/overview.html) is the short account from the standard's authors.

## Quick Start

### Install

```elixir
def deps do
  [
    {:asdf, "~> 0.1.0"}
  ]
end
```

Requires Elixir `~> 1.17`.

### Write and open

`ASDF.write!/2` writes one file. A bare map is wrapped as `tag:stsci.edu:asdf/core/asdf-1.1.0`. Keys must be strings. `ASDF.open!/1` then reads the header, the YAML tree, and the block headers. It does not decode array payloads.

```elixir
:ok = ASDF.write!("observation.asdf", %{"count" => 2, "items" => [10, 20]})

handle = ASDF.open!("observation.asdf")
info = ASDF.info!(handle)
tree = ASDF.tree!(handle)

info.format_version
tree.value["count"]
ASDF.read!(handle, ["items", 0])
ASDF.close!(handle)
```

`info.format_version` is `{1, 0, 0}`. `info.standard_version` is `{1, 5, 0}` for a file this library writes, and `nil` when an input file has no `#ASDF_STANDARD` comment. `info.block_count` is the number of block headers. `tree.value["count"]` is `2`. `ASDF.read!/2` walks from the root: a string is a mapping key, an integer indexes a sequence, and the call above returns `10`.

`{:binary, bytes}` skips the filesystem: `ASDF.write(:binary, map)` returns `{:ok, bytes}`, and `ASDF.open!({:binary, bytes})` reads them.

### Write an array

An `%ASDF.Array{}` with a concrete datatype becomes a block. `ASDF.open!/1` leaves the payload unread. `ASDF.Array.to_list/2` decodes it.

```elixir
array = %ASDF.Array{
  datatype: {:int, 8, :signed},
  shape: [3],
  byteorder: :little,
  source: :inline,
  inline: [1, 2, 3]
}

{:ok, bytes} = ASDF.write(:binary, %{"data" => array}, compression: :none)
handle = ASDF.open!({:binary, bytes})
{:ok, view} = ASDF.read(handle, ["data"])
{:ok, [1, 2, 3]} = ASDF.Array.to_list(view)
ASDF.close!(handle)
```

Pass `compression: :zlib` to compress blocks. The default is `:none`.

`float32` and `float16` stay bit containers. Pass `widen: true` to turn finite values into Elixir floats. Infinity and NaN raise `ASDF.Error`.

### Close

`ASDF.close/1` stops the handle. A later call returns `{:error, %ASDF.Error{reason: :closed}}`. Bang functions raise that error. A second close returns `:ok`.

## Roadmap

0.1.0 is the current release: read and write ASDF standard 1.5.0, contiguous blocks, and zlib. The gap inventory is [Gaps with Python asdf](docs/python-asdf-gaps.md). Each later version is one delivery. Checksums, the strict block index, bool8 values of only 0 or 1, quoting of YAML 1.1 words, and the size limits stay in every release.

### 0.2.0 — Ordinary files round-trip with asdf 6.0.0

A file from `ASDF.write/3` and an uncompressed default file from asdf 6.0.0 open in the other library. The written file uses one manifest: the standard comment, the ndarray tags including float16, and the integer tags agree, and `history` and `asdf_library` are written when that schema requires them. asdf 6.0.0 reads back a map plus C-contiguous int, float64, float32, and float16 arrays. The Python file carries core schemas 1.6.0, history, a checksum, and a block index; the numeric block decodes and history stays `%ASDF.Tagged{}`. YAML `.inf` and `.nan`, and non-finite float16 and float32 payloads, open as an explicit term. Finite float16 and float32 stay bit containers, and `widen: true` still raises on a non-finite value. The oracle runs against asdf 6.0.0.

### 0.3.0 — bzp2 and lz4 blocks

Done when a `bzp2` block round-trips, and an `lz4` block round-trips when the codec is available. Any other compression label remains an error.

### 0.4.0 — String and structured columns

Done when `ucs4` and a structured dtype round-trip.

### 0.5.0 — Strided arrays

Done when a negative stride round-trips and `ASDF.Array.slice/2` accepts an array that is not C order.

### 0.6.0 — Streamed blocks

Done when a streamed shape `*` round-trips: one uncompressed last block, and no block index.

### 0.7.0 — External blocks

Done when an external block is written as a sibling file under `external_root` and opened on read. A path with `..`, a leading `/`, or a `://` URI remains rejected.

### 0.8.0 — Complex binary samples

Done when complex64 and complex128 arrays round-trip as binary blocks.

### 0.9.0 — Cyclic trees

Done when a cyclic mapping round-trips through anchors and aliases.

### 0.10.0 — Integer and boolean keys

Done when integer and boolean mapping keys round-trip.

### 0.11.0 — Validated writes

Done when `ASDF.write/3` validates the tree before it returns, including the ndarray schemas that 0.1.0 skips.

### Outside this series

Diff, edit, explode, implode, and search. Units, time, coordinates, FITS, transforms, and GWCS. asdf 6.0.0 keeps those in asdf-astropy, gwcs, and `asdftool`.

## Read next

- [Guide](docs/guide.md) covers the tree, arrays, validation, handlers, and errors.
- [Compatibility](docs/compatibility.md) lists what this version implements.
- [Gaps with Python asdf](docs/python-asdf-gaps.md) is the comparison behind the roadmap.
