| 1 |
# This file connects ASDF to the astropy.table.Table class |
| 2 |
import contextlib |
| 3 |
|
| 4 |
import asdf |
| 5 |
from astropy.io import registry as io_registry |
| 6 |
from astropy.table import Table |
| 7 |
|
| 8 |
|
| 9 |
def read_table(filename, data_key=None, find_table=None, **kwargs): |
| 10 |
""" |
| 11 |
Read a `~astropy.table.Table` object from an ASDF file |
| 12 |
|
| 13 |
By default, this function will look for a Table object with the key of |
| 14 |
``data`` in the top-level ASDF tree. The parameters ``data_key`` and |
| 15 |
``find_key`` can be used to override the default behavior. |
| 16 |
|
| 17 |
This function is registered as the Table reader for ASDF files with the |
| 18 |
unified I/O interface. |
| 19 |
|
| 20 |
Parameters |
| 21 |
---------- |
| 22 |
filename : str or :class:`py.path:local` |
| 23 |
Name of the file to be read |
| 24 |
data_key : str |
| 25 |
Optional top-level key to use for finding the Table in the tree. If not |
| 26 |
provided, uses ``data`` by default. Use of this parameter is not |
| 27 |
compatible with ``find_table``. |
| 28 |
find_table : function |
| 29 |
Optional function to be used for locating the Table in the tree. The |
| 30 |
function takes a single parameter, which is a dictionary representing |
| 31 |
the top of the ASDF tree. The function must return a |
| 32 |
`~astropy.table.Table` instance. |
| 33 |
|
| 34 |
Returns |
| 35 |
------- |
| 36 |
table : `~astropy.table.Table` |
| 37 |
`~astropy.table.Table` instance |
| 38 |
""" |
| 39 |
if data_key and find_table: |
| 40 |
msg = "Options 'data_key' and 'find_table' are not compatible" |
| 41 |
raise ValueError(msg) |
| 42 |
|
| 43 |
with asdf.open(filename, **kwargs) as af: |
| 44 |
if find_table: |
| 45 |
return find_table(af.tree) |
| 46 |
|
| 47 |
return af[data_key or "data"] |
| 48 |
|
| 49 |
|
| 50 |
def write_table(table, filename, data_key=None, make_tree=None, **kwargs): |
| 51 |
""" |
| 52 |
Write a `~astropy.table.Table` object to an ASDF file. |
| 53 |
|
| 54 |
By default, this function will write a Table object in the top-level ASDF |
| 55 |
tree using the key of ``data``. The parameters ``data_key`` and |
| 56 |
``make_tree`` can be used to override the default behavior. |
| 57 |
|
| 58 |
This function is registered as the Table writer for ASDF files with the |
| 59 |
unified I/O interface. |
| 60 |
|
| 61 |
Parameters |
| 62 |
---------- |
| 63 |
table : `~astropy.table.Table` |
| 64 |
`~astropy.table.Table` instance to be written |
| 65 |
filename : str or :class:`py.path:local` |
| 66 |
Name of the new ASDF file to be created |
| 67 |
data_key : str |
| 68 |
Optional top-level key in the ASDF tree to use when writing the Table. |
| 69 |
If not provided, uses ``data`` by default. Use of this parameter is not |
| 70 |
compatible with ``make_tree``. |
| 71 |
make_tree : function |
| 72 |
Optional function to be used for creating the ASDF tree. The function |
| 73 |
takes a single parameter, which is the `~astropy.table.Table` instance |
| 74 |
to be written. The function must return a `dict` representing the ASDF |
| 75 |
tree to be created. |
| 76 |
""" |
| 77 |
if data_key and make_tree: |
| 78 |
msg = "Options 'data_key' and 'make_tree' are not compatible" |
| 79 |
raise ValueError(msg) |
| 80 |
|
| 81 |
tree = make_tree(table) if make_tree else {data_key or "data": table} |
| 82 |
|
| 83 |
with asdf.AsdfFile(tree) as af: |
| 84 |
af.write_to(filename, **kwargs) |
| 85 |
|
| 86 |
|
| 87 |
def asdf_identify(origin, filepath, fileobj, *args, **kwargs): |
| 88 |
return filepath is not None and filepath.endswith(".asdf") |
| 89 |
|
| 90 |
|
| 91 |
# This means we're using an older version of astropy |
| 92 |
# and it has already claimed the 'asdf' identifier. |
| 93 |
with contextlib.suppress(io_registry.IORegistryError): |
| 94 |
io_registry.register_reader("asdf", Table, read_table) |
| 95 |
io_registry.register_writer("asdf", Table, write_table) |
| 96 |
io_registry.register_identifier("asdf", Table, asdf_identify) |