|
1 | 1 | libasdf
|
2 | 2 | #######
|
3 | 3 |
|
4 |
| -C library for reading (and eventually writing) `ASDF |
5 |
| -<https://www.asdf-format.org/en/latest/>`__ files |
| 4 | +.. _begin-badges: |
| 5 | + |
| 6 | +.. image:: https://github.com/asdf-format/libasdf/workflows/Build/badge.svg |
| 7 | + :target: https://github.com/asdf-format/libasdf/actions |
| 8 | + :alt: CI Status |
| 9 | + |
| 10 | +.. _end-badges: |
| 11 | + |
| 12 | +A C library for reading (and eventually writing) `ASDF |
| 13 | +<https://www.asdf-format.org/en/latest/>`__ files. |
6 | 14 |
|
7 | 15 |
|
8 | 16 | Introduction
|
9 | 17 | ============
|
10 | 18 |
|
11 |
| -Let's have something to say here first. |
| 19 | +libasdf is largely a wrapper around `libfyaml <https://pantoniou.github.io/libfyaml/>`__ |
| 20 | +but with an understanding of the structure of ASDF files, with the capability to read and |
| 21 | +extract binary block data, as well as typed getters for metadata in the ASDF tree. |
| 22 | + |
| 23 | +It also features an extension mechanism (still nascent) for reading ASDF schemas, including |
| 24 | +the core schemas such as ``core/ndarray-<x.y.z>`` into C-native datastructures. |
| 25 | + |
| 26 | +Getting Started |
| 27 | +--------------- |
| 28 | + |
| 29 | +To open an ASDF file with libasdf the simplest way is to use the ``asdf_open`` function. |
| 30 | +This returns an ``asdf_file_t *`` which is your main interface to the ASDF file. |
| 31 | +When done with the file make sure to call ``asdf_close`` to free resources: |
| 32 | + |
| 33 | +.. code:: c |
| 34 | + :name: test-open-close-file |
| 35 | +
|
| 36 | + #include <stdio.h> |
| 37 | + #include <asdf.h> |
| 38 | + |
| 39 | + int main(int argc, char **argv) { |
| 40 | + if (argc < 2) { |
| 41 | + fprintf(stderr, "Usage: %s filename\n", argv[0]); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + const char *filename = argv[1]; |
| 45 | + asdf_file_t *file = asdf_open(filename, "r"); |
| 46 | +
|
| 47 | + if (file == NULL) { |
| 48 | + fprintf(stderr, "error opening the ASDF file\n"); |
| 49 | + return 1; |
| 50 | + } |
| 51 | +
|
| 52 | + asdf_close(file); |
| 53 | + return 0; |
| 54 | + } |
| 55 | +
|
| 56 | +The following more complete example demonstrates how to read different metadata out of |
| 57 | +the ASDF tree, as well as extract block data. Inline comments provide further explanation: |
| 58 | + |
| 59 | +.. code:: c |
| 60 | + :name: test-read-metadata-ndarray |
| 61 | +
|
| 62 | + #include <stdio.h> |
| 63 | + #include <stdlib.h> |
| 64 | + #include <asdf.h> |
| 65 | + |
| 66 | + int main(int argc, char **argv) { |
| 67 | + if (argc < 2) { |
| 68 | + fprintf(stderr, "Usage: %s filename\n", argv[0]); |
| 69 | + return 1; |
| 70 | + } |
| 71 | + const char *filename = argv[1]; |
| 72 | +
|
| 73 | + // The mode string "r" is required and is the only currently-supported mode |
| 74 | + asdf_file_t *file = asdf_open(filename, "r"); |
| 75 | + |
| 76 | + if (file == NULL) { |
| 77 | + fprintf(stderr, "error opening the ASDF file\n"); |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + // The simplest way to read metadata from the file is with the |
| 82 | + // `asdf_get_<type>*` family of functions |
| 83 | + // They all return a value by pointer argument and return an |
| 84 | + // `asdf_value_error_t` |
| 85 | + // For example you can read a string from the metadata like: |
| 86 | + |
| 87 | + const char *software = NULL; |
| 88 | + // Returns a 0-terminated string into *software. |
| 89 | + asdf_value_err_t err = asdf_get_string0(file, "asdf_library/author", &software); |
| 90 | + |
| 91 | + if (err == ASDF_VALUE_OK) { |
| 92 | + printf("software: %s\n", software); |
| 93 | + } |
| 94 | + |
| 95 | + // Other errors could be e.g. ASDF_VALUE_ERR_NOT_FOUND if the key doesn't |
| 96 | + // exist, or ASDF_VALUE_ERR_TYPE_MISMATCH if it's not a string. |
| 97 | + |
| 98 | + // There are also extensions registered for some (not all yet) of the |
| 99 | + // core schemas. Objects defined by extension schemas (identified by |
| 100 | + // their YAML tags) also have corresponding asdf_get_<type> functions: |
| 101 | + asdf_meta_t *meta = NULL; |
| 102 | + |
| 103 | + // This reads the top-level core/asdf-1.0.0 schema |
| 104 | + err = asdf_get_meta(file, "/", &meta); |
| 105 | + if (err == ASDF_VALUE_OK) { |
| 106 | + if (meta->history.entries[0]) { |
| 107 | + // This is a NULL-terminated array of asdf_history_entry_t* |
| 108 | + printf("first history entry: %s\n", meta->history.entries[0]->description); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Functions like `asdf_get_meta` that return into a double-pointer to a |
| 113 | + // struct allocate memory for that structure automatically. |
| 114 | + // The all have a corresponding `asdf_<type>_destroy` function. |
| 115 | + // The plan is to track these on the file object (issue #34) to make |
| 116 | + // memory management easier and cleaner, but for now you have to free |
| 117 | + // them manually when you're done with them. This is good practice in any |
| 118 | + // case. |
| 119 | + asdf_meta_destroy(meta); |
| 120 | + |
| 121 | + // ndarrays work no differently; this reads an ndarray named "cube". |
| 122 | + asdf_ndarray_t *ndarray = NULL; |
| 123 | + err = asdf_get_ndarray(file, "cube", &ndarray); |
| 124 | + if (err != ASDF_VALUE_OK) { |
| 125 | + fprintf(stderr, "error reading ndarray metadata: %d\n", err); |
| 126 | + return 1; |
| 127 | + } |
| 128 | + |
| 129 | + printf("number of data dimensions: %d\n", ndarray->ndim); |
| 130 | + |
| 131 | + // Get just a raw pointer to the ndarray data block (if uncompressed). |
| 132 | + // Optionally returns the size in bytes as well |
| 133 | + size_t size = 0; |
| 134 | + void *data = asdf_ndarray_data_raw(ndarray, &size); |
| 135 | +
|
| 136 | + if (data == NULL) { |
| 137 | + fprintf(stderr, "error reading ndarray data\n"); |
| 138 | + return 1; |
| 139 | + } |
| 140 | + |
| 141 | + // Slightly more useful is the asdf_ndarray_read_tile_ functions. |
| 142 | + // They can copy the data, including converting endianness into a tile |
| 143 | + // buffer. If an existing buffer is not passed it will allocate one of |
| 144 | + // the correct size to hold the data. The user is responsible for |
| 145 | + // freeing the buffer. |
| 146 | + |
| 147 | + // Read a 10x10x10 cube |
| 148 | + const uint64_t origin[3] = {0, 0, 0}; |
| 149 | + const uint64_t shape[3] = {10, 10, 10}; |
| 150 | + void *tile = NULL; |
| 151 | + asdf_ndarray_err_t array_err = asdf_ndarray_read_tile_ndim( |
| 152 | + ndarray, |
| 153 | + origin, |
| 154 | + shape, |
| 155 | + &tile |
| 156 | + ); |
| 157 | + |
| 158 | + if (array_err != ASDF_NDARRAY_OK) { |
| 159 | + fprintf(stderr, "error reading ndarray: %d\n", array_err); |
| 160 | + return 1; |
| 161 | + } |
| 162 | + |
| 163 | + free(tile); |
| 164 | + asdf_ndarray_destroy(ndarray); |
| 165 | + asdf_close(file); |
| 166 | + return 0; |
| 167 | + } |
12 | 168 |
|
13 | 169 |
|
14 | 170 | Development
|
|
0 commit comments