%YAML 1.1
---
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
id: "http://stsci.edu/schemas/asdf/core/ndarray-1.2.0"

title: >
  An *n*-dimensional array.

description: |
  There are two ways to store the data in an ndarray.

  - Inline in the tree: This is recommended only for small arrays.  In
    this case, the entire ``ndarray`` tag may be a nested list, in
    which case the type of the array is inferred from the content.
    (See the rules for type inference in the ``inline-data``
    definition below.)  The inline data may also be given in the
    ``data`` property, in which case it is possible to explicitly
    specify the ``datatype`` and other properties.

  - External to the tree: The data comes from a [block](ref:block)
    within the same ASDF file or an external ASDF file referenced by a
    URI.

examples:
  -
    - An inline array, with implicit data type
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          [[1, 0, 0],
           [0, 1, 0],
           [0, 0, 1]]

  -
    - An inline array, with an explicit data type
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          datatype: float64
          data:
            [[1, 0, 0],
             [0, 1, 0],
             [0, 0, 1]]

  -
    - An inline structured array, where the types of each column are
      automatically detected
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          [[M110, 110, 205, And],
           [ M31,  31, 224, And],
           [ M32,  32, 221, And],
           [M103, 103, 581, Cas]]

  -
    - An inline structured array, where the types of each column are
      explicitly specified
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          datatype: [['ascii', 4], uint16, uint16, ['ascii', 4]]
          data:
            [[M110, 110, 205, And],
             [ M31,  31, 224, And],
             [ M32,  32, 221, And],
             [M103, 103, 581, Cas]]

  -
    - A double-precision array, in contiguous memory in a block within
      the same file
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: 0
          shape: [1024, 1024]
          datatype: float64
          byteorder: little

  -
    - A view of a tile in that image
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: 0
          shape: [256, 256]
          datatype: float64
          byteorder: little
          strides: [8192, 8]
          offset: 2099200

  -
    - A structured datatype, with nested columns for a coordinate in
      (*ra*, *dec*), and a 3x3 convolution kernel
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: 0
          shape: [64]
          datatype:
            - name: coordinate
              datatype:
                - name: ra
                  datatype: float64
                - name: dec
                  datatype: float64
            - name: kernel
              datatype: float32
              shape: [3, 3]
          byteorder: little

  -
    - An array in Fortran order
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: 0
          shape: [1024, 1024]
          datatype: float64
          byteorder: little
          strides: [8192, 8]

  -
    - An array where values of -999 are treated as missing
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: 0
          shape: [256, 256]
          datatype: float64
          byteorder: little
          mask: -999

  -
    - An array where another array is used as a mask
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: 0
          shape: [256, 256]
          datatype: float64
          byteorder: little
          mask: !core/ndarray-1.2.0
            source: 1
            shape: [256, 256]
            datatype: bool8
            byteorder: little

  -
    - An array where the data is stored in the first block in
      another ASDF file.
    - asdf-standard-1.7.0
    - |
        !core/ndarray-1.2.0
          source: external.asdf
          shape: [256, 256]
          datatype: float64
          byteorder: little

definitions:
  inline-data:
    description: |
      Inline data is stored in YAML format directly in the tree, rather than
      referencing a binary block.  It is made out of nested lists.

      If the datatype of the array is not specified, it is inferred from
      the array contents.  Type inference is supported only for
      homogeneous arrays, not tables.

      - If any of the elements in the array are YAML strings, the
        `datatype` of the entire array is `ucs4`, with the width of
        the largest string in the column, otherwise...

      - If any of the elements in the array are complex numbers, the
        `datatype` of the entire column is `complex128`, otherwise...

      - If any of the types in the column are numbers with a decimal
        point, the `datatype` of the entire column is `float64`,
        otherwise..

      - If any of the types in the column are integers, the `datatype`
        of the entire column is `int64`, otherwise...

      - The `datatype` of the entire column is `bool8`.

      Masked values may be included in the array using `null`.  If an
      explicit mask array is also provided, it takes precedence.

    type: array
    items:
      anyOf:
        - type: number
        - type: string
        - type: "null"
        - $ref: "complex-1.0.0"
        - $ref: "#/definitions/inline-data"
        - type: boolean

anyOf:
  - $ref: "#/definitions/inline-data"
  - type: object
    properties:
      source:
        description: |
          The source of the data.

          - If an integer: If positive, the zero-based index of the
            block within the same file. If negative, the index from
            the last block within the same file.  For example, a
            source of `-1` corresponds to the last block in the same
            file.

          - If a string, a URI to an external ASDF file containing the
            block data.  Relative URIs and ``file:`` and ``http:``
            protocols must be supported.  Other protocols may be supported
            by specific library implementations.

          The ability to reference block data in an external ASDF file
          is intentionally limited to the first block in the external
          ASDF file, and is intended only to support the needs of
          [exploded](ref:exploded).  For the more general case of
          referencing data in an external ASDF file, use tree
          [references](ref:references).

        anyOf:
          - type: integer
          - type: string
            format: uri

      data:
        description: |
          The data for the array inline.

          If `datatype` and/or `shape` are also provided, they must
          match the data here and can be used as a consistency check.
          `strides`, `offset` and `byteorder` are meaningless when
          `data` is provided.
        $ref: "#/definitions/inline-data"

      shape:
        description: |
          The shape of the array.

          The first entry may be the string `*`, indicating that the
          length of the first index of the array will be automatically
          determined from the size of the block.  This is used for
          streaming support.
        type: array
        items:
          anyOf:
            - type: integer
              minimum: 0
            - enum: ['*']

      datatype:
        description: |
          The data format of the array elements.
        $ref: "datatype-1.0.0"

      byteorder:
        description: >
          The byte order (big- or little-endian) of the array data.
        type: string
        enum: [big, little]

      offset:
        description: >
          The offset, in bytes, within the data for this start of this
          view.
        type: integer
        minimum: 0
        default: 0

      strides:
        description: >
          The number of bytes to skip in each dimension.  If not provided,
          the array is assumed by be contiguous and in C order.  If
          provided, must be the same length as the shape property.
        type: array
        items:
          anyOf:
            - type: integer
              minimum: 1
            - type: integer
              maximum: -1

      mask:
        description: >
          Describes how missing values in the array are stored.  If a
          scalar number, that number is used to represent missing values.
          If an ndarray, the given array provides a mask, where non-zero
          values represent missing values in this array.  The mask array
          must be broadcastable to the dimensions of this array.
        anyOf:
          - type: number
          - $ref: "complex-1.0.0"
          - allOf:
            - $ref: "ndarray-1.2.0"
            - datatype: bool8

    dependencies:
      source: [shape, datatype, byteorder]

    oneOf:
      - required: [source]
      - required: [data]

    propertyOrder: [source, data, mask, datatype, byteorder, shape, offset, strides]
...
