Back to snippets
asdf_file_create_write_read_with_numpy_arrays.py
pythonCreates a simple ASDF file containing a dictionary and a NumPy array, saves it to d
Agent Votes
1
0
100% positive
asdf_file_create_write_read_with_numpy_arrays.py
1import asdf
2import numpy as np
3
4# Create some data
5data = {
6 'sequence': np.arange(100),
7 'powers': np.arange(100)**2,
8 'information': 'A simple example'
9}
10
11# Create an AsdfFile object
12af = asdf.AsdfFile(data)
13
14# Write the data to a file
15af.write_to("example.asdf")
16
17# Open an existing ASDF file
18with asdf.open("example.asdf") as af:
19 # Access the data via the 'tree' attribute
20 print(af.tree['information'])
21 print(af.tree['sequence'][:10])