ASDF Python Library

5.2.0 · active · verified Thu Apr 16

Python implementation of the ASDF Standard, a language-agnostic serialization format for scientific data. It handles serialization of Python objects (including NumPy arrays, Astropy objects, and custom types) into ASDF files, which are YAML-based with support for binary data. The library is actively developed with several releases per year, and the current version is 5.2.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple ASDF data structure, write it to a file, and then read the data back. It uses a dictionary for the tree structure and includes a NumPy array, a common use case for ASDF.

import asdf
import numpy as np
import os

# Create some data
tree = {
    'scientific_name': 'M42',
    'coordinates': {
        'ra': 83.822083,
        'dec': -5.391111
    },
    'data': np.array([[1, 2], [3, 4]], dtype=np.int64)
}

# Create an ASDF file object
ff = asdf.AsdfFile(tree)

# Define file path
file_path = "example.asdf"

# Write the file
ff.write_to(file_path)

print(f"ASDF file written to {file_path}")

# Read the file back
with asdf.open(file_path) as af:
    print(f"Read data:\n{af.tree['data']}")
    print(f"Read coordinates: {af.tree['coordinates']}")

# Clean up the created file
os.remove(file_path)

view raw JSON →