forked from fsspec/kerchunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdf.py
1156 lines (1037 loc) · 39.9 KB
/
hdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import io
import logging
from typing import Union, BinaryIO
import fsspec.core
from fsspec.implementations.reference import LazyReferenceMapper
import numpy as np
import zarr
import numcodecs
from .codecs import FillStringsCodec
from .utils import _encode_for_JSON
try:
import h5py
except ModuleNotFoundError: # pragma: no cover
raise ImportError(
"h5py is required for kerchunking HDF5/NetCDF4 files. Please install with "
"`pip/conda install h5py`. See https://docs.h5py.org/en/latest/build.html "
"for more details."
)
try:
from zarr.meta import encode_fill_value
except ModuleNotFoundError:
# https://github.com/zarr-developers/zarr-python/issues/2021
from zarr.v2.meta import encode_fill_value
lggr = logging.getLogger("h5-to-zarr")
_HIDDEN_ATTRS = { # from h5netcdf.attrs
"REFERENCE_LIST",
"CLASS",
"DIMENSION_LIST",
"NAME",
"_Netcdf4Dimid",
"_Netcdf4Coordinates",
"_nc3_strict",
"_NCProperties",
}
class SingleHdf5ToZarr:
"""Translate the content of one HDF5 file into Zarr metadata.
HDF5 groups become Zarr groups. HDF5 datasets become Zarr arrays. Zarr array
chunks remain in the HDF5 file.
Parameters
----------
h5f : file-like or str
Input HDF5 file. Can be a binary Python file-like object (duck-typed, adhering
to BinaryIO is optional), in which case must also provide url. If a str,
file will be opened using fsspec and storage_options.
url : string
URI of the HDF5 file, if passing a file-like object or h5py File/Group
spec : int
The version of output to produce (see README of this repo)
inline_threshold : int
Include chunks smaller than this value directly in the output. Zero or negative
to disable
storage_options: dict
passed to fsspec if h5f is a str
error: "warn" (default) | "pdb" | "ignore" | "raise"
vlen_encode: ["embed", "null", "leave", "encode"]
What to do with VLEN string variables or columns of tabular variables
leave: pass through the 16byte garbage IDs unaffected, but requires no codec
null: set all the strings to None or empty; required that this library is available
at read time
embed: include all the values in the output JSON (should not be used for large tables)
encode: save the ID-to-value mapping in a codec, to produce the real values at read
time; requires this library to be available. Can be efficient storage where there
are few unique values.
out: dict-like or None
This allows you to supply an fsspec.implementations.reference.LazyReferenceMapper
to write out parquet as the references get filled, or some other dictionary-like class
to customise how references get stored
"""
def __init__(
self,
h5f: "BinaryIO | str | h5py.File | h5py.Group",
url: str = None,
spec=1,
inline_threshold=500,
storage_options=None,
error="warn",
vlen_encode="embed",
out=None,
):
# Open HDF5 file in read mode...
lggr.debug(f"HDF5 file: {h5f}")
if isinstance(h5f, str):
fs, path = fsspec.core.url_to_fs(h5f, **(storage_options or {}))
self.input_file = fs.open(path, "rb")
url = h5f
self._h5f = h5py.File(self.input_file, mode="r")
elif isinstance(h5f, io.IOBase):
self.input_file = h5f
self._h5f = h5py.File(self.input_file, mode="r")
elif isinstance(h5f, (h5py.File, h5py.Group)):
# assume h5py object (File or group/dataset)
self._h5f = h5f
fs, path = fsspec.core.url_to_fs(url, **(storage_options or {}))
self.input_file = fs.open(path, "rb")
else:
raise ValueError("type of input `h5f` not recognised")
self.spec = spec
self.inline = inline_threshold
if vlen_encode not in ["embed", "null", "leave", "encode"]:
raise NotImplementedError
self.vlen = vlen_encode
self.store = out or {}
self._zroot = zarr.group(store=self.store, overwrite=True)
self._uri = url
self.error = error
lggr.debug(f"HDF5 file URI: {self._uri}")
def translate(self, preserve_linked_dsets=False):
"""Translate content of one HDF5 file into Zarr storage format.
This method is the main entry point to execute the workflow, and
returns a "reference" structure to be used with zarr/kerchunk
No data is copied out of the HDF5 file.
Parameters
----------
preserve_linked_dsets : bool (optional, default False)
If True, translate HDF5 soft and hard links for each `h5py.Dataset`
into the reference structure. Requires h5py version 3.11.0 or later.
Will not translate external links or links to `h5py.Group` objects.
Returns
-------
dict
Dictionary containing reference structure.
"""
lggr.debug("Translation begins")
self._transfer_attrs(self._h5f, self._zroot)
self._h5f.visititems(self._translator)
if preserve_linked_dsets:
if not has_visititems_links():
raise RuntimeError(
"'preserve_linked_dsets' kwarg requires h5py 3.11.0 or later "
f"is installed, found {h5py.__version__}"
)
self._h5f.visititems_links(self._translator)
if self.spec < 1:
return self.store
elif isinstance(self.store, LazyReferenceMapper):
self.store.flush()
return self.store
else:
store = _encode_for_JSON(self.store)
return {"version": 1, "refs": store}
def _unref(self, ref):
name = h5py.h5r.get_name(ref, self._h5f.id)
return self._h5f[name]
def _transfer_attrs(
self,
h5obj: Union[h5py.Dataset, h5py.Group],
zobj: Union[zarr.Array, zarr.Group],
):
"""Transfer attributes from an HDF5 object to its equivalent Zarr object.
Parameters
----------
h5obj : h5py.Group or h5py.Dataset
An HDF5 group or dataset.
zobj : zarr.hierarchy.Group or zarr.core.Array
An equivalent Zarr group or array to the HDF5 group or dataset with
attributes.
"""
for n, v in h5obj.attrs.items():
if n in _HIDDEN_ATTRS:
continue
# Fix some attribute values to avoid JSON encoding exceptions...
if isinstance(v, bytes):
v = v.decode("utf-8") or " "
elif isinstance(v, (np.ndarray, np.number, np.bool_)):
if v.dtype.kind == "S":
v = v.astype(str)
if n == "_FillValue":
continue # strip it out!
elif v.size == 1:
v = v.flatten()[0]
if isinstance(v, (np.ndarray, np.number, np.bool_)):
v = v.tolist()
else:
v = v.tolist()
elif isinstance(v, h5py._hl.base.Empty):
v = ""
if v == "DIMENSION_SCALE":
continue
try:
zobj.attrs[n] = v
except TypeError:
lggr.debug(
f"TypeError transferring attr, skipping:\n {n}@{h5obj.name} = {v} ({type(v)})"
)
def _decode_filters(self, h5obj: Union[h5py.Dataset, h5py.Group]):
if h5obj.scaleoffset:
raise RuntimeError(
f"{h5obj.name} uses HDF5 scaleoffset filter - not supported by kerchunk"
)
if h5obj.compression in ("szip", "lzf"):
raise RuntimeError(
f"{h5obj.name} uses szip or lzf compression - not supported by kerchunk"
)
filters = []
if h5obj.shuffle and h5obj.dtype.kind != "O":
# cannot use shuffle if we materialised objects
filters.append(numcodecs.Shuffle(elementsize=h5obj.dtype.itemsize))
for filter_id, properties in h5obj._filters.items():
if str(filter_id) == "32001":
blosc_compressors = (
"blosclz",
"lz4",
"lz4hc",
"snappy",
"zlib",
"zstd",
)
(
_1,
_2,
bytes_per_num,
total_bytes,
clevel,
shuffle,
compressor,
) = properties
pars = dict(
blocksize=total_bytes,
clevel=clevel,
shuffle=shuffle,
cname=blosc_compressors[compressor],
)
filters.append(numcodecs.Blosc(**pars))
elif str(filter_id) == "32015":
filters.append(numcodecs.Zstd(level=properties[0]))
elif str(filter_id) == "gzip":
filters.append(numcodecs.Zlib(level=properties))
elif str(filter_id) == "32004":
raise RuntimeError(
f"{h5obj.name} uses lz4 compression - not supported by kerchunk"
)
elif str(filter_id) == "32008":
raise RuntimeError(
f"{h5obj.name} uses bitshuffle compression - not supported by kerchunk"
)
elif str(filter_id) == "shuffle":
# already handled before this loop
pass
else:
raise RuntimeError(
f"{h5obj.name} uses filter id {filter_id} with properties {properties},"
f" not supported by kerchunk."
)
return filters
def _translator(
self,
name: str,
h5obj: Union[
h5py.Dataset, h5py.Group, h5py.SoftLink, h5py.HardLink, h5py.ExternalLink
],
):
"""Produce Zarr metadata for all groups and datasets in the HDF5 file."""
try: # method must not raise exception
kwargs = {}
if isinstance(h5obj, (h5py.SoftLink, h5py.HardLink)):
h5obj = self._h5f[name]
if isinstance(h5obj, h5py.Group):
# continues iteration of visititems_links
lggr.debug(
f"Skipping translation of HDF5 linked group: '{h5obj.name}'"
)
return None
if isinstance(h5obj, h5py.Dataset):
lggr.debug(f"HDF5 dataset: {h5obj.name}")
lggr.debug(f"HDF5 compression: {h5obj.compression}")
if h5obj.id.get_create_plist().get_layout() == h5py.h5d.COMPACT:
# Only do if h5obj.nbytes < self.inline??
kwargs["data"] = h5obj[:]
filters = []
else:
filters = self._decode_filters(h5obj)
dt = None
# Get storage info of this HDF5 dataset...
cinfo = self._storage_info(h5obj)
if "data" in kwargs:
fill = None
else:
# encodings
if h5obj.dtype.kind in "US":
fill = h5obj.fillvalue or " " # cannot be None
elif h5obj.dtype.kind == "O":
if self.vlen == "embed":
if np.isscalar(h5obj):
out = str(h5obj)
elif h5obj.ndim == 0:
out = np.array(h5obj).tolist().decode()
else:
out = h5obj[:]
out2 = out.ravel()
for i, val in enumerate(out2):
if isinstance(val, bytes):
out2[i] = val.decode()
elif isinstance(val, str):
out2[i] = val
elif isinstance(val, h5py.h5r.Reference):
# TODO: recursively recreate references
out2[i] = None
else:
out2[i] = [
v.decode() if isinstance(v, bytes) else v
for v in val
]
kwargs["data"] = out
kwargs["object_codec"] = numcodecs.JSON()
fill = None
elif self.vlen == "null":
dt = "O"
kwargs["object_codec"] = FillStringsCodec(dtype="S16")
fill = " "
elif self.vlen == "leave":
dt = "S16"
fill = " "
elif self.vlen == "encode":
assert len(cinfo) == 1
v = list(cinfo.values())[0]
data = _read_block(self.input_file, v["offset"], v["size"])
indexes = np.frombuffer(data, dtype="S16")
labels = h5obj[:]
mapping = {
index.decode(): label.decode()
for index, label in zip(indexes, labels)
}
kwargs["object_codec"] = FillStringsCodec(
dtype="S16", id_map=mapping
)
fill = " "
else:
raise NotImplementedError
elif _is_netcdf_datetime(h5obj) or _is_netcdf_variable(h5obj):
fill = None
else:
fill = h5obj.fillvalue
if h5obj.dtype.kind == "V":
fill = None
if self.vlen == "encode":
assert len(cinfo) == 1
v = list(cinfo.values())[0]
dt = [
(
v,
(
"S16"
if h5obj.dtype[v].kind == "O"
else str(h5obj.dtype[v])
),
)
for v in h5obj.dtype.names
]
data = _read_block(self.input_file, v["offset"], v["size"])
labels = h5obj[:]
arr = np.frombuffer(data, dtype=dt)
mapping = {}
for field in labels.dtype.names:
if labels[field].dtype == "O":
mapping.update(
{
index.decode(): label.decode()
for index, label in zip(
arr[field], labels[field]
)
}
)
kwargs["object_codec"] = FillStringsCodec(
dtype=str(dt), id_map=mapping
)
dt = [
(
v,
(
"O"
if h5obj.dtype[v].kind == "O"
else str(h5obj.dtype[v])
),
)
for v in h5obj.dtype.names
]
elif self.vlen == "null":
dt = [
(
v,
(
"S16"
if h5obj.dtype[v].kind == "O"
else str(h5obj.dtype[v])
),
)
for v in h5obj.dtype.names
]
kwargs["object_codec"] = FillStringsCodec(dtype=str(dt))
dt = [
(
v,
(
"O"
if h5obj.dtype[v].kind == "O"
else str(h5obj.dtype[v])
),
)
for v in h5obj.dtype.names
]
elif self.vlen == "leave":
dt = [
(
v,
(
"S16"
if h5obj.dtype[v].kind == "O"
else h5obj.dtype[v]
),
)
for v in h5obj.dtype.names
]
elif self.vlen == "embed":
# embed fails due to https://github.com/zarr-developers/numcodecs/issues/333
data = h5obj[:].tolist()
data2 = []
for d in data:
data2.append(
[
(
_.decode(errors="ignore")
if isinstance(_, bytes)
else _
)
for _ in d
]
)
dt = "O"
kwargs["data"] = data2
kwargs["object_codec"] = numcodecs.JSON()
fill = None
else:
raise NotImplementedError
if h5py.h5ds.is_scale(h5obj.id) and not cinfo:
return
if h5obj.attrs.get("_FillValue") is not None:
fill = encode_fill_value(
h5obj.attrs.get("_FillValue"), dt or h5obj.dtype
)
# Create a Zarr array equivalent to this HDF5 dataset...
za = self._zroot.require_dataset(
h5obj.name,
shape=h5obj.shape,
dtype=dt or h5obj.dtype,
chunks=h5obj.chunks or False,
fill_value=fill,
compression=None,
filters=filters,
overwrite=True,
**kwargs,
)
lggr.debug(f"Created Zarr array: {za}")
self._transfer_attrs(h5obj, za)
adims = self._get_array_dims(h5obj)
za.attrs["_ARRAY_DIMENSIONS"] = adims
lggr.debug(f"_ARRAY_DIMENSIONS = {adims}")
if "data" in kwargs:
return # embedded bytes, no chunks to copy
# Store chunk location metadata...
if cinfo:
for k, v in cinfo.items():
if h5obj.fletcher32:
logging.info("Discarding fletcher32 checksum")
v["size"] -= 4
if (
self.inline
and isinstance(v, dict)
and v["size"] < self.inline
):
self.input_file.seek(v["offset"])
data = self.input_file.read(v["size"])
try:
# easiest way to test if data is ascii
data.decode("ascii")
except UnicodeDecodeError:
data = b"base64:" + base64.b64encode(data)
self.store[za._chunk_key(k)] = data
else:
self.store[za._chunk_key(k)] = [
self._uri,
v["offset"],
v["size"],
]
elif isinstance(h5obj, h5py.Group):
lggr.debug(f"HDF5 group: {h5obj.name}")
zgrp = self._zroot.require_group(h5obj.name)
self._transfer_attrs(h5obj, zgrp)
except Exception as e:
import traceback
msg = "\n".join(
[
"The following excepion was caught and quashed while traversing HDF5",
str(e),
traceback.format_exc(limit=5),
]
)
if self.error == "ignore":
return
elif self.error == "pdb":
print(msg)
import pdb
pdb.post_mortem()
elif self.error == "raise":
raise
else:
# "warn" or anything else, the default
import warnings
warnings.warn(msg)
del e # garbage collect
def _get_array_dims(self, dset):
"""Get a list of dimension scale names attached to input HDF5 dataset.
This is required by the xarray package to work with Zarr arrays. Only
one dimension scale per dataset dimension is allowed. If dataset is
dimension scale, it will be considered as the dimension to itself.
Parameters
----------
dset : h5py.Dataset
HDF5 dataset.
Returns
-------
list
List with HDF5 path names of dimension scales attached to input
dataset.
"""
dims = list()
rank = len(dset.shape)
if rank:
for n in range(rank):
num_scales = len(dset.dims[n])
if num_scales == 1:
dims.append(dset.dims[n][0].name[1:])
elif h5py.h5ds.is_scale(dset.id):
dims.append(dset.name[1:])
elif num_scales > 1:
raise RuntimeError(
f"{dset.name}: {len(dset.dims[n])} "
f"dimension scales attached to dimension #{n}"
)
elif num_scales == 0:
# Some HDF5 files do not have dimension scales.
# If this is the case, `num_scales` will be 0.
# In this case, we mimic netCDF4 and assign phony dimension names.
# See https://github.com/fsspec/kerchunk/issues/41
dims.append(f"phony_dim_{n}")
return dims
def _storage_info(self, dset: h5py.Dataset) -> dict:
"""Get storage information of an HDF5 dataset in the HDF5 file.
Storage information consists of file offset and size (length) for every
chunk of the HDF5 dataset.
Parameters
----------
dset : h5py.Dataset
HDF5 dataset for which to collect storage information.
Returns
-------
dict
HDF5 dataset storage information. Dict keys are chunk array offsets
as tuples. Dict values are pairs with chunk file offset and size
integers.
"""
# Empty (null) dataset...
if dset.shape is None:
return dict()
dsid = dset.id
if dset.chunks is None:
# Contiguous dataset...
if dsid.get_offset() is None:
# No data ever written...
return dict()
else:
key = (0,) * (len(dset.shape) or 1)
return {
key: {"offset": dsid.get_offset(), "size": dsid.get_storage_size()}
}
else:
# Chunked dataset...
num_chunks = dsid.get_num_chunks()
if num_chunks == 0:
# No data ever written...
return dict()
# Go over all the dataset chunks...
stinfo = dict()
chunk_size = dset.chunks
def get_key(blob):
return tuple([a // b for a, b in zip(blob.chunk_offset, chunk_size)])
def store_chunk_info(blob):
stinfo[get_key(blob)] = {"offset": blob.byte_offset, "size": blob.size}
has_chunk_iter = callable(getattr(dsid, "chunk_iter", None))
if has_chunk_iter:
dsid.chunk_iter(store_chunk_info)
else:
for index in range(num_chunks):
store_chunk_info(dsid.get_chunk_info(index))
return stinfo
def _simple_type(x):
if isinstance(x, bytes):
return x.decode()
if isinstance(x, np.number):
if x.dtype.kind == "i":
return int(x)
return float(x)
return x
def _read_block(open_file, offset, size):
place = open_file.tell()
open_file.seek(offset)
data = open_file.read(size)
open_file.seek(place)
return data
def _is_netcdf_datetime(dataset: h5py.Dataset):
units = dataset.attrs.get("units")
if isinstance(units, bytes):
units = units.decode("utf-8")
# This is the same heuristic used by xarray
# https://github.com/pydata/xarray/blob/f8bae5974ee2c3f67346298da12621af4cae8cf8/xarray/coding/times.py#L670
return units and "since" in units
def _is_netcdf_variable(dataset: h5py.Dataset):
return any("_Netcdf4" in _ for _ in dataset.attrs)
def has_visititems_links():
return hasattr(h5py.Group, "visititems_links")
decoders = {}
def reg(name):
def f(func):
decoders[name] = func
return func
return f
class HDF4ToZarr:
def __init__(
self,
path,
storage_options=None,
inline_threshold=100,
out=None,
remote_protocol=None,
remote_options=None,
):
self.path = path
self.st = storage_options
self.thresh = inline_threshold
self.out = out or {}
self.remote_protocol = remote_protocol
self.remote_options = remote_options
def read_int(self, n):
return int.from_bytes(self.f.read(n), "big")
def read_ddh(self):
return {"ndd": self.read_int(2), "next": self.read_int(4)}
def read_dd(self):
loc = self.f.tell()
i = int.from_bytes(self.f.read(2), "big")
if i & 0x4000:
extended = True
i = i - 0x4000
else:
extended = False
tag = tags.get(i, i)
no_data = tag not in {"NULL"}
ref = (tag, int.from_bytes(self.f.read(2), "big"))
info = {
"offset": int.from_bytes(self.f.read(4), "big") * no_data,
"length": int.from_bytes(self.f.read(4), "big") * no_data,
"extended": extended,
"loc": loc,
}
return ref, info
def decode(self, tag, info):
self.f.seek(info["offset"])
ident = lambda _, __: info
return decoders.get(tag, ident)(self, info)
def translate(self):
import zarr
self.f = fsspec.open(self.path, **(self.st or {})).open()
fs = fsspec.filesystem(
"reference",
fo=self.out,
remote_protocol=self.remote_protocol,
remote_options=self.remote_options,
)
g = zarr.open_group("reference://", storage_options=dict(fs=fs))
# magic header
assert self.f.read(4) == b"\x0e\x03\x13\x01"
# all the data descriptors in a linked list
self.tags = {}
while True:
ddh = self.read_ddh()
for _ in range(ddh["ndd"]):
ident, info = self.read_dd()
self.tags[ident] = info
if ddh["next"] == 0:
# "finished" sentry
break
# or continue
self.f.seek(ddh["next"])
# basic decode
for tag, ref in self.tags:
self._dec(tag, ref)
# global attributes
attrs = {}
for (tag, ref), info in self.tags.items():
if tag == "VH" and info["names"][0].upper() == "VALUES":
dtype = dtypes[info["types"][0]]
inf2 = self.tags[("VS", ref)]
self.f.seek(inf2["offset"])
data = self.f.read(inf2["length"])
# NASA conventions
if info["name"].startswith(("CoreMetadata.", "ArchiveMetadata.")):
obj = None
for line in data.decode().split("\n"):
if "OBJECT" in line:
obj = line.split()[-1]
if "VALUE" in line:
attrs[obj] = line.split()[-1].lstrip('"').rstrip('"')
g.attrs.update(attrs)
# there should be only one root, and it's probably the last VG
# so maybe this loop isn't needed
roots = set()
children = set()
child = {}
for (tag, ref), info in self.tags.items():
if tag == "VG":
here = child.setdefault((tag, ref), set())
for t, r in zip(info["tag"], info["refs"]):
if t == "VG":
children.add((t, r))
roots.discard((t, r))
here.add((t, r))
if tag not in children:
roots.add((tag, ref))
for t, r in roots:
self.tags[(t, r)] = self._descend_vg(t, r)
return self.tags, roots
def _descend_vg(self, tag, ref):
info = self.tags[(tag, ref)]
out = {}
for t, r in zip(info["tag"], info["refs"]):
inf2 = self.tags[(t, r)]
if t == "VG":
tmp = self._descend_vg(t, r)
if list(tmp)[0] == inf2["name"]:
tmp = tmp[inf2["name"]]
out[inf2["name"]] = tmp
elif t == "VH":
if len(inf2["names"]) == 1 and inf2["names"][0].lower() == "values":
dtype = dtypes[inf2["types"][0]]
inf2 = self.tags[("VS", r)]
self.f.seek(inf2["offset"])
data = self.f.read(inf2["length"])
if dtype == "str":
out[info["name"]] = (
data.decode().lstrip('"').rstrip('"')
) # decode() ?
else:
out[info["name"]] = np.frombuffer(data, dtype)[0]
elif t == "NT":
out["dtype"] = inf2["typ"]
elif t == "SD":
out["refs"] = inf2["data"][:-1]
out["chunks"] = [_["chunk_length"] for _ in inf2["data"][-1]]
elif t == "SDD":
out["dims"] = inf2["dims"]
else:
# NDGs contain same info as NT, SD and SDD
pass
return out
def _dec(self, tag, ref):
info = self.tags[(tag, ref)]
if not set(info) - {"length", "offset", "extended", "loc"}:
self.f.seek(info["offset"])
if info["extended"]:
info["data"] = self._dec_extended()
else:
info.update(self.decode(tag, info))
return info
def _dec_extended(self):
ext_type = spec[self.read_int(2)]
if ext_type == "CHUNKED":
return self._dec_chunked()
elif ext_type == "LINKED":
return self._dec_linked_header()
elif ext_type == "COMP":
return self._dec_comp()
def _dec_linked_header(self):
# get the bytes of a linked set - these will always be inlined
length = self.read_int(4)
blk_len = self.read_int(4)
num_blk = self.read_int(4)
next_ref = self.read_int(2)
out = []
while next_ref:
next_ref, data = self._dec_linked_block(self.tags[("LINKED", next_ref)])
out.extend([d for d in data if d])
bits = []
for ref in out:
info = self.tags[("LINKED", ref)]
self.f.seek(info["offset"])
bits.append(self.f.read(info["length"]))
return b"".join(bits)
def _dec_linked_block(self, block):
self.f.seek(block["offset"])
next_ref = self.read_int(2)
refs = [self.read_int(2) for _ in range((block["length"] // 2) - 1)]
return next_ref, refs
def _dec_chunked(self):
# we want to turn the chunks table into references
# tag_head_len = self.read_int(4)
# version = self.f.read(1)[0]
# flag = self.read_int(4)
# elem_tot_len = self.read_int(4)
# chunk_size = self.read_int(4)
# nt_size = self.read_int(4)
self.f.seek(21, 1)
chk_tbl_tag = tags[self.read_int(2)] # should be VH
chk_tbl_ref = self.read_int(2)
sp_tag = tags[self.read_int(2)]
sp_ref = self.read_int(2)
ndims = self.read_int(4)
dims = [ # we don't use these, could
{
"flag": self.read_int(4),
"dim_length": self.read_int(4),
"chunk_length": self.read_int(4),
}
for _ in range(ndims)
]
fill_value = self.f.read(
self.read_int(4)
) # to be interpreted as a number later; but chunk table probs has no fill
# self.f.seek(12*ndims + 4, 1)
header = self._dec(chk_tbl_tag, chk_tbl_ref)
data = self._dec("VS", chk_tbl_ref)["data"] # corresponding table
# header gives the field pattern for the rows of data, one per chunk
# maybe faster to use struct and iter than numpy, since we iterate anyway
dt = [(f"ind{i}", ">u4") for i in range(ndims)] + [
("tag", ">u2"),
("ref", ">u2"),
]
rows = np.frombuffer(data, dtype=dt, count=header["nvert"])
# rows["tag"] should always be 61 -> CHUNK
refs = []
for *ind, tag, ref in rows:
# maybe ind needs reversing since everything is FORTRAN
chunk_tag = self.tags[("CHUNK", ref)]
if chunk_tag["extended"]:
self.f.seek(chunk_tag["offset"])
# these are always COMP?
ctype, offset, length = self._dec_extended()
refs.append([".".join(str(_) for _ in ind), offset, length, ctype])
else:
refs.append(
[
".".join(str(_) for _ in ind),
chunk_tag["offset"],
chunk_tag["length"],
]
)
refs.append(dims)
return refs
def _dec_comp(self):
# version = self.read_int(2) # always 0
# len_uncomp = self.read_int(4)
self.f.seek(6, 1)
data_ref = self.read_int(2)
# model = self.read_int(2) # always 0
ctype = "DEFLATE" # comp[self.read_int(2)]
tag = self.tags[("COMPRESSED", data_ref)]
return ctype, tag["offset"], tag["length"]
@reg("NDG")
def _dec_ndg(self, info):
# links together these things as a Data Group
return {
"tags": [
(tags[self.read_int(2)], self.read_int(2))
for _ in range(0, info["length"], 4)
]
}
@reg("SDD")
def _dec_sdd(self, info):
rank = self.read_int(2)
dims = [self.read_int(4) for _ in range(rank)]
data_tag = (tags[self.read_int(2)], self.read_int(2))
scale_tags = [(tags[self.read_int(2)], self.read_int(2)) for _ in range(rank)]
return _pl(locals())
@reg("VERSION")
def _dec_version(self, info):
return {
"major": self.read_int(4),
"minor": self.read_int(4),
"release": self.read_int(4),
"string:": _null_str(self.f.read(info["length"] - 10).decode()),
}
@reg("VH")
def _dec_vh(self, info):
# virtual group ("table") header
interface = self.read_int(2)
nvert = self.read_int(4)
ivsize = self.read_int(2)
nfields = self.read_int(2)
types = [self.read_int(2) for _ in range(nfields)]
isize = [self.read_int(2) for _ in range(nfields)]
offsets = [self.read_int(2) for _ in range(nfields)]