mirror of
https://github.com/meteoinfo/MeteoInfo.git
synced 2025-12-08 20:36:05 +00:00
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
"""
|
|
Binary serialization
|
|
"""
|
|
|
|
__all__ = []
|
|
|
|
|
|
EXPECTED_KEYS = {'descr', 'fortran_order', 'shape'}
|
|
MAGIC_PREFIX = b'\x93NUMPY'
|
|
MAGIC_LEN = len(MAGIC_PREFIX) + 2
|
|
ARRAY_ALIGN = 64 # plausible values are powers of 2 between 16 and 4096
|
|
BUFFER_SIZE = 2**18 # size of buffer for reading npz files in bytes
|
|
|
|
# difference between version 1.0 and 2.0 is a 4 byte (I) header length
|
|
# instead of 2 bytes (H) allowing storage of large structured arrays
|
|
_header_size_info = {
|
|
(1, 0): ('<H', 'latin1'),
|
|
(2, 0): ('<I', 'latin1'),
|
|
(3, 0): ('<I', 'utf8'),
|
|
}
|
|
|
|
|
|
def _check_version(version):
|
|
if version not in [(1, 0), (2, 0), (3, 0), None]:
|
|
msg = "we only support format version (1,0), (2,0), and (3,0), not %s"
|
|
raise ValueError(msg % (version,))
|
|
|
|
def magic(major, minor):
|
|
""" Return the magic string for the given file format version.
|
|
|
|
Parameters
|
|
----------
|
|
major : int in [0, 255]
|
|
minor : int in [0, 255]
|
|
|
|
Returns
|
|
-------
|
|
magic : str
|
|
|
|
Raises
|
|
------
|
|
ValueError if the version cannot be formatted.
|
|
"""
|
|
if major < 0 or major > 255:
|
|
raise ValueError("major version must be 0 <= major < 256")
|
|
if minor < 0 or minor > 255:
|
|
raise ValueError("minor version must be 0 <= minor < 256")
|
|
return MAGIC_PREFIX + bytes([major, minor]) |