|
| 1 | +zarr - Persistence |
| 2 | +================== |
| 3 | + |
| 4 | +This document describes the file organisation and formats used to save zarr |
| 5 | +arrays on disk. |
| 6 | + |
| 7 | +All data and metadata associated with a zarr array is stored within a |
| 8 | +directory on the file system. Within this directory there are a number |
| 9 | +of files and sub-directories storing different components of the data |
| 10 | +and metadata. Here I'll refer to a directory containing a zarr array |
| 11 | +as a root directory. |
| 12 | + |
| 13 | +Configuration metadata |
| 14 | +---------------------- |
| 15 | + |
| 16 | +Within a root directory, a file called "__zmeta__" contains essential |
| 17 | +configuration metadata about the array. This comprises the shape of the |
| 18 | +array, chunk shape, data type (dtype), compression library, |
| 19 | +compression level, shuffle filter and default fill value for |
| 20 | +uninitialised portions of the array. The format of this file is JSON. |
| 21 | + |
| 22 | +Mandatory fields and allowed values are as follows: |
| 23 | + |
| 24 | +* ``shape`` - list of integers - the size of each dimension of the array |
| 25 | +* ``chunks`` - list of integers - the size of each dimension of a chunk, i.e., the chunk shape |
| 26 | +* ``dtype`` - string or list of lists - a description of the data type, following Numpy convention |
| 27 | +* ``fill_value`` - scalar value - value to use for uninitialised portions of the array |
| 28 | +* ``cname`` - string - name of the compression library used |
| 29 | +* ``clevel`` - integer - compression level |
| 30 | +* ``shuffle`` - integer - shuffle filter (0 = no shuffle, 1 = byte shuffle, 2 = bit shuffle) |
| 31 | + |
| 32 | +For example:: |
| 33 | + |
| 34 | + >>> import zarr |
| 35 | + >>> z = zarr.open('example.zarr', mode='w', shape=(1000000, 1000), |
| 36 | + ... chunks=(10000, 100), dtype='i4', fill_value=42, |
| 37 | + ... cname='lz4', clevel=3, shuffle=1) |
| 38 | + >>> print(open('example.zarr/__zmeta__').read()) |
| 39 | + { |
| 40 | + "chunks": [ |
| 41 | + 10000, |
| 42 | + 100 |
| 43 | + ], |
| 44 | + "clevel": 3, |
| 45 | + "cname": "lz4", |
| 46 | + "dtype": "<i4", |
| 47 | + "fill_value": 42, |
| 48 | + "shape": [ |
| 49 | + 1000000, |
| 50 | + 1000 |
| 51 | + ], |
| 52 | + "shuffle": 1 |
| 53 | + } |
| 54 | + |
| 55 | +User metadata (attributes) |
| 56 | +-------------------------- |
| 57 | + |
| 58 | +Within a root directory, a file called "__zattr__" contains user |
| 59 | +metadata associated with the array, i.e., user attributes. The format |
| 60 | +of this file is JSON. |
| 61 | + |
| 62 | +For example:: |
| 63 | + |
| 64 | + >>> import zarr |
| 65 | + >>> z = zarr.open('example.zarr', mode='w', shape=(1000000, 1000), |
| 66 | + ... chunks=(10000, 100), dtype='i4', fill_value=42, |
| 67 | + ... cname='lz4', clevel=3, shuffle=1) |
| 68 | + >>> z.attrs['foo'] = 42 |
| 69 | + >>> z.attrs['bar'] = 4.2 |
| 70 | + >>> z.attrs['baz'] = 'quux' |
| 71 | + >>> print(open('example.zarr/__zattr__').read()) |
| 72 | + |
| 73 | +TODO add results above |
| 74 | + |
| 75 | +Array data |
| 76 | +---------- |
| 77 | + |
| 78 | +Within a root directory, a sub-directory called "__zdata__" contains |
| 79 | +the array data. The array data is divided into chunks, each of which |
| 80 | +is compressed using the [blosc meta-compression library](TODO). Each |
| 81 | +chunk is stored in a separate file. |
| 82 | + |
| 83 | +The chunk files are named according to the chunk indices. E.g., for a |
| 84 | +2-dimensional array with shape (100, 100) and chunk shape (10, 10) |
| 85 | +there will be 100 chunks in total. The file "0.0.blosc" stores data |
| 86 | +for the chunk with indices (0, 0) within chunk rows and columns |
| 87 | +respectively, i.e., the first chunk, containing data for the segment |
| 88 | +of the array that would be obtained by the slice ``z[0:10, 0:10]``; |
| 89 | +the file "4.2.blosc" stores the chunk in the fifth row third column, |
| 90 | +containing data for the slize ``z[40:50, 20:30]``; etc. |
| 91 | + |
| 92 | +Each chunk file is a binary file following the blosc version 1 format, |
| 93 | +comprising a 16 byte header followed by the compressed data. The |
| 94 | +header is organised as follows:: |
| 95 | + |
| 96 | + |-0-|-1-|-2-|-3-|-4-|-5-|-6-|-7-|-8-|-9-|-A-|-B-|-C-|-D-|-E-|-F-| |
| 97 | + ^ ^ ^ ^ | nbytes | blocksize | cbytes | |
| 98 | + | | | | |
| 99 | + | | | +--typesize |
| 100 | + | | +------flags |
| 101 | + | +----------blosclz version |
| 102 | + +--------------blosc version |
| 103 | + |
| 104 | +For more details on the header, see the [C-Blosc header |
| 105 | +description](https://github.com/Blosc/c-blosc/blob/master/README_HEADER.rst). |
| 106 | + |
| 107 | +If a file does not exist on the file system for any given chunk in an |
| 108 | +array, that indicates the chunk has not been initialised, and the |
| 109 | +chunk should be interpreted as completely filled with whatever value |
| 110 | +has been configured as the fill value for the array. I.e., chunk files |
| 111 | +are not required to exist. |
| 112 | + |
| 113 | +For example:: |
| 114 | + |
| 115 | + >>> import zarr |
| 116 | + >>> z = zarr.open('example.zarr', mode='w', shape=(1000000, 1000), |
| 117 | + ... chunks=(10000, 100), dtype='i4', fill_value=42, |
| 118 | + ... cname='lz4', clevel=3, shuffle=1) |
| 119 | + >>> import os |
| 120 | + >>> os.listdir('example.zarr/__zdata__') |
| 121 | + [] |
| 122 | + >>> z[:] = 0 |
| 123 | + >>> sorted(os.listdir('example.zarr/__zdata__'))[:5] |
| 124 | + ['0.0.blosc', '0.1.blosc', '0.2.blosc', '0.3.blosc', '0.4.blosc'] |
0 commit comments