# `ASDF.Array`
[🔗](https://github.com/baoulo/asdf-elixir/blob/v0.1.0/lib/asdf/array.ex#L1)

A view of an ndarray.

`source` is `:inline`, `{:block, index}`, or `{:external, uri}`. External
URIs are not opened. Block bytes are read by `to_list/2` or `stream/2`,
not by `ASDF.tree/1`.

Uncompressed contiguous data is `pread` from the computed range. There is
no mmap. A sub-binary of that `pread` shares the bytes that were read, and
nothing else from the file. zlib blocks are inflated once, capped at the
header's `data_size`, which was already rejected if it exceeded the limit.

`float32` and `float16` stay bit containers unless `to_list/2` is called
with `widen: true`.

# `t`

```elixir
@type t() :: %ASDF.Array{
  block: ASDF.Block.t() | nil,
  byteorder: :big | :little | :not_applicable,
  datatype: term(),
  inline: term(),
  io: ASDF.Source.t(),
  offset: non_neg_integer(),
  shape: [non_neg_integer()],
  source: :inline | {:block, non_neg_integer()} | {:external, String.t()},
  strides: [integer()] | nil
}
```

A view of one ndarray. The payload is not copied into the struct.

  * `:datatype` — a parsed descriptor from `parse_datatype/1`, or `:inferred` for a YAML list
  * `:shape` — one non-negative integer per axis
  * `:byteorder` — `:big`, `:little`, or `:not_applicable` for inline YAML values
  * `:source` — `:inline`, `{:block, index}`, or `{:external, uri}`
  * `:offset` — element offset into the payload
  * `:strides` — element strides, or `nil` when the layout is contiguous C order
  * `:inline` — YAML values when `:source` is `:inline`, otherwise `nil`
  * `:block` — the `%ASDF.Block{}` for a block source, otherwise `nil`
  * `:io` — the `%ASDF.Source{}` for a block source

# `parse_datatype`

```elixir
@spec parse_datatype(term()) :: {:ok, term()} | {:error, ASDF.Error.t()}
```

Parses `descriptor` from an ndarray mapping.

`descriptor` is a core scalar name (`"int8"` through `"int64"`,
`"uint8"` through `"uint64"`, `"float16"`, `"float32"`, `"float64"`,
`"complex64"`, `"complex128"`, `"bool8"`), a pair `["ascii", n]` or
`["ucs4", n]` with `n > 0`, or a list of `%{"name" => name, "datatype" => dtype}`
field maps.

Returns `{:ok, descriptor}`. Integers are `{:int, bits, :signed | :unsigned}`,
floats are `{:float, bits}`, complex values are `{:complex, bits}`, bool is
`:bool`, strings are `{:ascii, n}` or `{:ucs4, n}`, and a field list is
`{:struct, [{name, descriptor}]}`. Anything else is
`{:error, %ASDF.Error{reason: :invalid_ndarray}}`. Does not raise.

## Examples

    iex> ASDF.Array.parse_datatype("int32")
    {:ok, {:int, 32, :signed}}

    iex> ASDF.Array.parse_datatype(["ascii", 4])
    {:ok, {:ascii, 4}}

    iex> ASDF.Array.parse_datatype([%{"name" => "x", "datatype" => "int8"}])
    {:ok, {:struct, [{"x", {:int, 8, :signed}}]}}

    iex> {:error, error} = ASDF.Array.parse_datatype("nope")
    ...> error.reason
    :invalid_ndarray

# `slice`

```elixir
@spec slice(t(), [non_neg_integer() | Range.t()]) ::
  {:ok, term()} | {:error, ASDF.Error.t()}
```

Returns a rectangular slice of `array`.

`array` is an `%ASDF.Array{}`. `ranges` has one entry per axis that is
sliced. An integer keeps that index. A `first..last//1` range is inclusive,
matching `Enum.slice/2`. A step other than 1 is
`{:error, %ASDF.Error{reason: :unsupported_slice}}`. An index outside the
axis is `reason: :invalid_ndarray`. An external URI is
`reason: :external_disabled`. Returns `{:ok, value}`. Does not raise.

## Examples

    iex> array = %ASDF.Array{datatype: :inferred, shape: [2, 2], source: :inline, inline: [[1, 2], [3, 4]]}
    ...> ASDF.Array.slice(array, [1])
    {:ok, [3, 4]}

    iex> array = %ASDF.Array{datatype: :inferred, shape: [2, 2], source: :inline, inline: [[1, 2], [3, 4]]}
    ...> ASDF.Array.slice(array, [0..0, 1])
    {:ok, [2]}

# `stream`

```elixir
@spec stream(t()) :: Enumerable.t() | {:error, ASDF.Error.t()}
```

Lazily yields axis-0 slices of `array`.

`array` is an `%ASDF.Array{}` with at least one axis. Returns a stream.
`Enum.to_list/1` yields one item per row. A one-axis array yields scalars.
Uncompressed contiguous rows are each a separate `pread`. A zlib block is
inflated once when the stream starts. The stream opens its own descriptor
and closes it on halt.

An external URI or a scalar shape returns
`{:error, %ASDF.Error{}}` with `reason: :external_disabled` or
`reason: :invalid_ndarray`, not a stream. A bad bool byte raises
`ASDF.Error` while the stream is consumed.

## Examples

    iex> array = %ASDF.Array{datatype: :inferred, shape: [2, 2], source: :inline, inline: [[1, 2], [3, 4]]}
    ...> Enum.to_list(ASDF.Array.stream(array))
    [[1, 2], [3, 4]]

    iex> array = %ASDF.Array{datatype: :inferred, shape: [], source: :inline, inline: 1}
    ...> {:error, error} = ASDF.Array.stream(array)
    ...> error.reason
    :invalid_ndarray

# `to_list`

```elixir
@spec to_list(t(), keyword()) :: {:ok, term()} | {:error, ASDF.Error.t()}
```

Decodes `array` into nested lists.

`array` is an `%ASDF.Array{}`. `opts` accepts `:widen`, default `false`.
`widen: true` turns `%ASDF.Float16{}` and `%ASDF.Float32{}` into Elixir
floats. Infinity and NaN in that widening raise `ASDF.Error`.

Returns `{:ok, value}`. A scalar array returns the scalar. An external URI
is `{:error, %ASDF.Error{reason: :external_disabled}}`. A bool byte other
than 0 or 1 is `reason: :invalid_ndarray`. `ucs4` has no decoder and is
the same reason. Does not raise for those errors.

## Examples

    iex> array = %ASDF.Array{datatype: :inferred, shape: [2], source: :inline, inline: [1, 2]}
    ...> ASDF.Array.to_list(array)
    {:ok, [1, 2]}

    iex> array = %ASDF.Array{datatype: :inferred, shape: [2], source: :inline, inline: [1, 2]}
    ...> ASDF.Array.to_list(array, widen: true)
    {:ok, [1, 2]}

    iex> array = %ASDF.Array{datatype: {:int, 8, :signed}, shape: [1], source: {:external, "a.bin"}}
    ...> {:error, error} = ASDF.Array.to_list(array)
    ...> error.reason
    :external_disabled

---

*Consult [api-reference.md](api-reference.md) for complete listing*
