hdf5-as-virtual-zarr 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (255) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +39 -0
  3. package/dist/hdf-store.d.ts +9 -0
  4. package/dist/hdf-store.js +18 -0
  5. package/dist/index.d.ts +3 -0
  6. package/dist/index.js +3 -0
  7. package/dist/jsfive/btree.d.ts +52 -0
  8. package/dist/jsfive/btree.js +354 -0
  9. package/dist/jsfive/core.d.ts +35 -0
  10. package/dist/jsfive/core.js +196 -0
  11. package/dist/jsfive/dataobjects.d.ts +64 -0
  12. package/dist/jsfive/dataobjects.js +1001 -0
  13. package/dist/jsfive/datatype-msg.d.ts +15 -0
  14. package/dist/jsfive/datatype-msg.js +110 -0
  15. package/dist/jsfive/high-level.d.ts +42 -0
  16. package/dist/jsfive/high-level.js +150 -0
  17. package/dist/jsfive/misc-low-level.d.ts +67 -0
  18. package/dist/jsfive/misc-low-level.js +506 -0
  19. package/dist/ref-spec-to-consolidated-metadata.d.ts +12 -0
  20. package/dist/ref-spec-to-consolidated-metadata.js +22 -0
  21. package/dist/ref-store.d.ts +10 -0
  22. package/dist/ref-store.js +79 -0
  23. package/dist/single-hdf5-to-zarr.d.ts +76 -0
  24. package/dist/single-hdf5-to-zarr.js +745 -0
  25. package/dist/types.d.ts +34 -0
  26. package/dist/types.js +12 -0
  27. package/package.json +53 -0
  28. package/src/hdf-store.ts +25 -0
  29. package/src/index.ts +3 -0
  30. package/src/jsfive/README.md +1 -0
  31. package/src/jsfive/btree.ts +474 -0
  32. package/src/jsfive/core.ts +228 -0
  33. package/src/jsfive/dataobjects.ts +1262 -0
  34. package/src/jsfive/datatype-msg.ts +111 -0
  35. package/src/jsfive/high-level.ts +182 -0
  36. package/src/jsfive/misc-low-level.ts +641 -0
  37. package/src/ref-spec-to-consolidated-metadata.ts +38 -0
  38. package/src/ref-store.ts +115 -0
  39. package/src/single-hdf5-to-zarr.ts +868 -0
  40. package/src/types.ts +49 -0
  41. package/test/fixtures/dense.adata.zarr/.zattrs +4 -0
  42. package/test/fixtures/dense.adata.zarr/.zgroup +3 -0
  43. package/test/fixtures/dense.adata.zarr/.zmetadata +285 -0
  44. package/test/fixtures/dense.adata.zarr/X/.zarray +22 -0
  45. package/test/fixtures/dense.adata.zarr/X/.zattrs +4 -0
  46. package/test/fixtures/dense.adata.zarr/X/0.0 +0 -0
  47. package/test/fixtures/dense.adata.zarr/layers/.zattrs +4 -0
  48. package/test/fixtures/dense.adata.zarr/layers/.zgroup +3 -0
  49. package/test/fixtures/dense.adata.zarr/obs/.zattrs +9 -0
  50. package/test/fixtures/dense.adata.zarr/obs/.zgroup +3 -0
  51. package/test/fixtures/dense.adata.zarr/obs/_index/.zarray +24 -0
  52. package/test/fixtures/dense.adata.zarr/obs/_index/.zattrs +4 -0
  53. package/test/fixtures/dense.adata.zarr/obs/_index/0 +0 -0
  54. package/test/fixtures/dense.adata.zarr/obs/categorical/.zattrs +5 -0
  55. package/test/fixtures/dense.adata.zarr/obs/categorical/.zgroup +3 -0
  56. package/test/fixtures/dense.adata.zarr/obs/categorical/categories/.zarray +24 -0
  57. package/test/fixtures/dense.adata.zarr/obs/categorical/categories/.zattrs +4 -0
  58. package/test/fixtures/dense.adata.zarr/obs/categorical/categories/0 +0 -0
  59. package/test/fixtures/dense.adata.zarr/obs/categorical/codes/.zarray +20 -0
  60. package/test/fixtures/dense.adata.zarr/obs/categorical/codes/.zattrs +4 -0
  61. package/test/fixtures/dense.adata.zarr/obs/categorical/codes/0 +0 -0
  62. package/test/fixtures/dense.adata.zarr/obs/string/.zarray +24 -0
  63. package/test/fixtures/dense.adata.zarr/obs/string/.zattrs +4 -0
  64. package/test/fixtures/dense.adata.zarr/obs/string/0 +0 -0
  65. package/test/fixtures/dense.adata.zarr/obsm/.zattrs +4 -0
  66. package/test/fixtures/dense.adata.zarr/obsm/.zgroup +3 -0
  67. package/test/fixtures/dense.adata.zarr/obsm/X_umap/.zarray +22 -0
  68. package/test/fixtures/dense.adata.zarr/obsm/X_umap/.zattrs +4 -0
  69. package/test/fixtures/dense.adata.zarr/obsm/X_umap/0.0 +0 -0
  70. package/test/fixtures/dense.adata.zarr/obsp/.zattrs +4 -0
  71. package/test/fixtures/dense.adata.zarr/obsp/.zgroup +3 -0
  72. package/test/fixtures/dense.adata.zarr/raw/.zarray +10 -0
  73. package/test/fixtures/dense.adata.zarr/raw/.zattrs +4 -0
  74. package/test/fixtures/dense.adata.zarr/raw/0 +0 -0
  75. package/test/fixtures/dense.adata.zarr/uns/.zattrs +4 -0
  76. package/test/fixtures/dense.adata.zarr/uns/.zgroup +3 -0
  77. package/test/fixtures/dense.adata.zarr/var/.zattrs +6 -0
  78. package/test/fixtures/dense.adata.zarr/var/.zgroup +3 -0
  79. package/test/fixtures/dense.adata.zarr/var/_index/.zarray +24 -0
  80. package/test/fixtures/dense.adata.zarr/var/_index/.zattrs +4 -0
  81. package/test/fixtures/dense.adata.zarr/var/_index/0 +0 -0
  82. package/test/fixtures/dense.adata.zarr/varm/.zattrs +4 -0
  83. package/test/fixtures/dense.adata.zarr/varm/.zgroup +3 -0
  84. package/test/fixtures/dense.adata.zarr/varp/.zattrs +4 -0
  85. package/test/fixtures/dense.adata.zarr/varp/.zgroup +3 -0
  86. package/test/fixtures/dense.adata.zarr.json +182 -0
  87. package/test/fixtures/dense.adata.zmetadata.json +1 -0
  88. package/test/fixtures/dense.h5ad +0 -0
  89. package/test/fixtures/dense.h5ad.refspec.json +1 -0
  90. package/test/fixtures/dense.v3.adata.zarr/X/c/0/0 +0 -0
  91. package/test/fixtures/dense.v3.adata.zarr/X/zarr.json +45 -0
  92. package/test/fixtures/dense.v3.adata.zarr/layers/zarr.json +8 -0
  93. package/test/fixtures/dense.v3.adata.zarr/obs/_index/c/0 +0 -0
  94. package/test/fixtures/dense.v3.adata.zarr/obs/_index/zarr.json +41 -0
  95. package/test/fixtures/dense.v3.adata.zarr/obs/categorical/categories/c/0 +0 -0
  96. package/test/fixtures/dense.v3.adata.zarr/obs/categorical/categories/zarr.json +41 -0
  97. package/test/fixtures/dense.v3.adata.zarr/obs/categorical/codes/c/0 +0 -0
  98. package/test/fixtures/dense.v3.adata.zarr/obs/categorical/codes/zarr.json +40 -0
  99. package/test/fixtures/dense.v3.adata.zarr/obs/categorical/zarr.json +9 -0
  100. package/test/fixtures/dense.v3.adata.zarr/obs/string/c/0 +0 -0
  101. package/test/fixtures/dense.v3.adata.zarr/obs/string/zarr.json +41 -0
  102. package/test/fixtures/dense.v3.adata.zarr/obs/zarr.json +13 -0
  103. package/test/fixtures/dense.v3.adata.zarr/obsm/X_umap/c/0/0 +0 -0
  104. package/test/fixtures/dense.v3.adata.zarr/obsm/X_umap/zarr.json +45 -0
  105. package/test/fixtures/dense.v3.adata.zarr/obsm/zarr.json +8 -0
  106. package/test/fixtures/dense.v3.adata.zarr/obsp/zarr.json +8 -0
  107. package/test/fixtures/dense.v3.adata.zarr/raw/zarr.json +36 -0
  108. package/test/fixtures/dense.v3.adata.zarr/uns/zarr.json +8 -0
  109. package/test/fixtures/dense.v3.adata.zarr/var/_index/c/0 +0 -0
  110. package/test/fixtures/dense.v3.adata.zarr/var/_index/zarr.json +41 -0
  111. package/test/fixtures/dense.v3.adata.zarr/var/zarr.json +10 -0
  112. package/test/fixtures/dense.v3.adata.zarr/varm/zarr.json +8 -0
  113. package/test/fixtures/dense.v3.adata.zarr/varp/zarr.json +8 -0
  114. package/test/fixtures/dense.v3.adata.zarr/zarr.json +469 -0
  115. package/test/fixtures/dense.v3.adata.zarr.json +102 -0
  116. package/test/fixtures/generate_fixtures.py +157 -0
  117. package/test/fixtures/generate_fixtures_v3.py +126 -0
  118. package/test/fixtures/minimal.adata.zarr/.zattrs +4 -0
  119. package/test/fixtures/minimal.adata.zarr/.zgroup +3 -0
  120. package/test/fixtures/minimal.adata.zarr/.zmetadata +168 -0
  121. package/test/fixtures/minimal.adata.zarr/X/.zarray +22 -0
  122. package/test/fixtures/minimal.adata.zarr/X/.zattrs +4 -0
  123. package/test/fixtures/minimal.adata.zarr/X/0.0 +0 -0
  124. package/test/fixtures/minimal.adata.zarr/layers/.zattrs +4 -0
  125. package/test/fixtures/minimal.adata.zarr/layers/.zgroup +3 -0
  126. package/test/fixtures/minimal.adata.zarr/obs/.zattrs +6 -0
  127. package/test/fixtures/minimal.adata.zarr/obs/.zgroup +3 -0
  128. package/test/fixtures/minimal.adata.zarr/obs/_index/.zarray +24 -0
  129. package/test/fixtures/minimal.adata.zarr/obs/_index/.zattrs +4 -0
  130. package/test/fixtures/minimal.adata.zarr/obs/_index/0 +0 -0
  131. package/test/fixtures/minimal.adata.zarr/obsm/.zattrs +4 -0
  132. package/test/fixtures/minimal.adata.zarr/obsm/.zgroup +3 -0
  133. package/test/fixtures/minimal.adata.zarr/obsp/.zattrs +4 -0
  134. package/test/fixtures/minimal.adata.zarr/obsp/.zgroup +3 -0
  135. package/test/fixtures/minimal.adata.zarr/raw/.zarray +10 -0
  136. package/test/fixtures/minimal.adata.zarr/raw/.zattrs +4 -0
  137. package/test/fixtures/minimal.adata.zarr/raw/0 +0 -0
  138. package/test/fixtures/minimal.adata.zarr/uns/.zattrs +4 -0
  139. package/test/fixtures/minimal.adata.zarr/uns/.zgroup +3 -0
  140. package/test/fixtures/minimal.adata.zarr/var/.zattrs +6 -0
  141. package/test/fixtures/minimal.adata.zarr/var/.zgroup +3 -0
  142. package/test/fixtures/minimal.adata.zarr/var/_index/.zarray +24 -0
  143. package/test/fixtures/minimal.adata.zarr/var/_index/.zattrs +4 -0
  144. package/test/fixtures/minimal.adata.zarr/var/_index/0 +0 -0
  145. package/test/fixtures/minimal.adata.zarr/varm/.zattrs +4 -0
  146. package/test/fixtures/minimal.adata.zarr/varm/.zgroup +3 -0
  147. package/test/fixtures/minimal.adata.zarr/varp/.zattrs +4 -0
  148. package/test/fixtures/minimal.adata.zarr/varp/.zgroup +3 -0
  149. package/test/fixtures/minimal.adata.zarr.json +126 -0
  150. package/test/fixtures/minimal.adata.zmetadata.json +1 -0
  151. package/test/fixtures/minimal.h5ad +0 -0
  152. package/test/fixtures/minimal.h5ad.refspec.json +1 -0
  153. package/test/fixtures/minimal.v3.adata.zarr/X/c/0/0 +0 -0
  154. package/test/fixtures/minimal.v3.adata.zarr/X/zarr.json +45 -0
  155. package/test/fixtures/minimal.v3.adata.zarr/layers/zarr.json +8 -0
  156. package/test/fixtures/minimal.v3.adata.zarr/obs/_index/c/0 +0 -0
  157. package/test/fixtures/minimal.v3.adata.zarr/obs/_index/zarr.json +41 -0
  158. package/test/fixtures/minimal.v3.adata.zarr/obs/zarr.json +10 -0
  159. package/test/fixtures/minimal.v3.adata.zarr/obsm/zarr.json +8 -0
  160. package/test/fixtures/minimal.v3.adata.zarr/obsp/zarr.json +8 -0
  161. package/test/fixtures/minimal.v3.adata.zarr/raw/zarr.json +36 -0
  162. package/test/fixtures/minimal.v3.adata.zarr/uns/zarr.json +8 -0
  163. package/test/fixtures/minimal.v3.adata.zarr/var/_index/c/0 +0 -0
  164. package/test/fixtures/minimal.v3.adata.zarr/var/_index/zarr.json +41 -0
  165. package/test/fixtures/minimal.v3.adata.zarr/var/zarr.json +10 -0
  166. package/test/fixtures/minimal.v3.adata.zarr/varm/zarr.json +8 -0
  167. package/test/fixtures/minimal.v3.adata.zarr/varp/zarr.json +8 -0
  168. package/test/fixtures/minimal.v3.adata.zarr/zarr.json +285 -0
  169. package/test/fixtures/minimal.v3.adata.zarr.json +66 -0
  170. package/test/fixtures/mouse_liver.h5ad +0 -0
  171. package/test/fixtures/mouse_liver.h5ad.refspec.json +1 -0
  172. package/test/fixtures/mouse_liver.py +44 -0
  173. package/test/fixtures/sparse.adata.zarr/.zattrs +4 -0
  174. package/test/fixtures/sparse.adata.zarr/.zgroup +3 -0
  175. package/test/fixtures/sparse.adata.zarr/.zmetadata +304 -0
  176. package/test/fixtures/sparse.adata.zarr/X/.zattrs +8 -0
  177. package/test/fixtures/sparse.adata.zarr/X/.zgroup +3 -0
  178. package/test/fixtures/sparse.adata.zarr/X/data/.zarray +20 -0
  179. package/test/fixtures/sparse.adata.zarr/X/data/0 +0 -0
  180. package/test/fixtures/sparse.adata.zarr/X/indices/.zarray +20 -0
  181. package/test/fixtures/sparse.adata.zarr/X/indices/0 +0 -0
  182. package/test/fixtures/sparse.adata.zarr/X/indptr/.zarray +20 -0
  183. package/test/fixtures/sparse.adata.zarr/X/indptr/0 +0 -0
  184. package/test/fixtures/sparse.adata.zarr/layers/.zattrs +4 -0
  185. package/test/fixtures/sparse.adata.zarr/layers/.zgroup +3 -0
  186. package/test/fixtures/sparse.adata.zarr/obs/.zattrs +9 -0
  187. package/test/fixtures/sparse.adata.zarr/obs/.zgroup +3 -0
  188. package/test/fixtures/sparse.adata.zarr/obs/_index/.zarray +24 -0
  189. package/test/fixtures/sparse.adata.zarr/obs/_index/.zattrs +4 -0
  190. package/test/fixtures/sparse.adata.zarr/obs/_index/0 +0 -0
  191. package/test/fixtures/sparse.adata.zarr/obs/categorical/.zattrs +5 -0
  192. package/test/fixtures/sparse.adata.zarr/obs/categorical/.zgroup +3 -0
  193. package/test/fixtures/sparse.adata.zarr/obs/categorical/categories/.zarray +24 -0
  194. package/test/fixtures/sparse.adata.zarr/obs/categorical/categories/.zattrs +4 -0
  195. package/test/fixtures/sparse.adata.zarr/obs/categorical/categories/0 +0 -0
  196. package/test/fixtures/sparse.adata.zarr/obs/categorical/codes/.zarray +20 -0
  197. package/test/fixtures/sparse.adata.zarr/obs/categorical/codes/.zattrs +4 -0
  198. package/test/fixtures/sparse.adata.zarr/obs/categorical/codes/0 +0 -0
  199. package/test/fixtures/sparse.adata.zarr/obs/string/.zarray +24 -0
  200. package/test/fixtures/sparse.adata.zarr/obs/string/.zattrs +4 -0
  201. package/test/fixtures/sparse.adata.zarr/obs/string/0 +0 -0
  202. package/test/fixtures/sparse.adata.zarr/obsm/.zattrs +4 -0
  203. package/test/fixtures/sparse.adata.zarr/obsm/.zgroup +3 -0
  204. package/test/fixtures/sparse.adata.zarr/obsp/.zattrs +4 -0
  205. package/test/fixtures/sparse.adata.zarr/obsp/.zgroup +3 -0
  206. package/test/fixtures/sparse.adata.zarr/raw/.zarray +10 -0
  207. package/test/fixtures/sparse.adata.zarr/raw/.zattrs +4 -0
  208. package/test/fixtures/sparse.adata.zarr/raw/0 +0 -0
  209. package/test/fixtures/sparse.adata.zarr/uns/.zattrs +4 -0
  210. package/test/fixtures/sparse.adata.zarr/uns/.zgroup +3 -0
  211. package/test/fixtures/sparse.adata.zarr/var/.zattrs +6 -0
  212. package/test/fixtures/sparse.adata.zarr/var/.zgroup +3 -0
  213. package/test/fixtures/sparse.adata.zarr/var/_index/.zarray +24 -0
  214. package/test/fixtures/sparse.adata.zarr/var/_index/.zattrs +4 -0
  215. package/test/fixtures/sparse.adata.zarr/var/_index/0 +0 -0
  216. package/test/fixtures/sparse.adata.zarr/varm/.zattrs +4 -0
  217. package/test/fixtures/sparse.adata.zarr/varm/.zgroup +3 -0
  218. package/test/fixtures/sparse.adata.zarr/varp/.zattrs +4 -0
  219. package/test/fixtures/sparse.adata.zarr/varp/.zgroup +3 -0
  220. package/test/fixtures/sparse.adata.zarr.json +190 -0
  221. package/test/fixtures/sparse.adata.zmetadata.json +1 -0
  222. package/test/fixtures/sparse.h5ad +0 -0
  223. package/test/fixtures/sparse.h5ad.refspec.json +1 -0
  224. package/test/fixtures/sparse.v3.adata.zarr/X/data/c/0 +0 -0
  225. package/test/fixtures/sparse.v3.adata.zarr/X/data/zarr.json +40 -0
  226. package/test/fixtures/sparse.v3.adata.zarr/X/indices/c/0 +0 -0
  227. package/test/fixtures/sparse.v3.adata.zarr/X/indices/zarr.json +40 -0
  228. package/test/fixtures/sparse.v3.adata.zarr/X/indptr/c/0 +0 -0
  229. package/test/fixtures/sparse.v3.adata.zarr/X/indptr/zarr.json +40 -0
  230. package/test/fixtures/sparse.v3.adata.zarr/X/zarr.json +12 -0
  231. package/test/fixtures/sparse.v3.adata.zarr/layers/zarr.json +8 -0
  232. package/test/fixtures/sparse.v3.adata.zarr/obs/_index/c/0 +0 -0
  233. package/test/fixtures/sparse.v3.adata.zarr/obs/_index/zarr.json +41 -0
  234. package/test/fixtures/sparse.v3.adata.zarr/obs/categorical/categories/c/0 +0 -0
  235. package/test/fixtures/sparse.v3.adata.zarr/obs/categorical/categories/zarr.json +41 -0
  236. package/test/fixtures/sparse.v3.adata.zarr/obs/categorical/codes/c/0 +0 -0
  237. package/test/fixtures/sparse.v3.adata.zarr/obs/categorical/codes/zarr.json +40 -0
  238. package/test/fixtures/sparse.v3.adata.zarr/obs/categorical/zarr.json +9 -0
  239. package/test/fixtures/sparse.v3.adata.zarr/obs/string/c/0 +0 -0
  240. package/test/fixtures/sparse.v3.adata.zarr/obs/string/zarr.json +41 -0
  241. package/test/fixtures/sparse.v3.adata.zarr/obs/zarr.json +13 -0
  242. package/test/fixtures/sparse.v3.adata.zarr/obsm/zarr.json +8 -0
  243. package/test/fixtures/sparse.v3.adata.zarr/obsp/zarr.json +8 -0
  244. package/test/fixtures/sparse.v3.adata.zarr/raw/zarr.json +36 -0
  245. package/test/fixtures/sparse.v3.adata.zarr/uns/zarr.json +8 -0
  246. package/test/fixtures/sparse.v3.adata.zarr/var/_index/c/0 +0 -0
  247. package/test/fixtures/sparse.v3.adata.zarr/var/_index/zarr.json +41 -0
  248. package/test/fixtures/sparse.v3.adata.zarr/var/zarr.json +10 -0
  249. package/test/fixtures/sparse.v3.adata.zarr/varm/zarr.json +8 -0
  250. package/test/fixtures/sparse.v3.adata.zarr/varp/zarr.json +8 -0
  251. package/test/fixtures/sparse.v3.adata.zarr/zarr.json +516 -0
  252. package/test/fixtures/sparse.v3.adata.zarr.json +114 -0
  253. package/test/hdf-store.test.ts +268 -0
  254. package/test/index.test.ts +762 -0
  255. package/test/partial-reads.test.ts +150 -0
@@ -0,0 +1,745 @@
1
+ import { File as Hdf5File, Dataset as Hdf5Dataset, Group as Hdf5Group } from "./jsfive/high-level.js";
2
+ import { BTreeV1RawDataChunks } from "./jsfive/btree.js";
3
+ import { struct } from "./jsfive/core.js";
4
+ import { UNDEFINED_ADDRESS } from "./jsfive/misc-low-level.js";
5
+ import { fetchRange } from "./types.js";
6
+ /**
7
+ * Hidden HDF5 attributes that should not be transferred to Zarr.
8
+ * From h5netcdf.attrs.
9
+ */
10
+ const HIDDEN_ATTRS = new Set([
11
+ "REFERENCE_LIST",
12
+ "CLASS",
13
+ "DIMENSION_LIST",
14
+ "NAME",
15
+ "_Netcdf4Dimid",
16
+ "_Netcdf4Coordinates",
17
+ "_nc3_strict",
18
+ "_NCProperties",
19
+ ]);
20
+ /**
21
+ * Convert jsfive dtype string to Zarr-compatible dtype string.
22
+ * jsfive produces numpy-style strings like "<f4", "<i4", etc.
23
+ * For single-byte types, normalize to use "|" prefix.
24
+ */
25
+ function jsfiveDtypeToZarr(dtype) {
26
+ if (Array.isArray(dtype)) {
27
+ const dtypeClass = dtype[0];
28
+ if (dtypeClass === "VLEN_STRING")
29
+ return "|O";
30
+ if (dtypeClass === "VLEN_SEQUENCE")
31
+ return "|O";
32
+ if (dtypeClass === "REFERENCE")
33
+ return "|O";
34
+ return "|O";
35
+ }
36
+ // Fixed-length string: "S123" -> "|S123"
37
+ if (/^S\d+/.test(dtype)) {
38
+ return "|" + dtype;
39
+ }
40
+ // Parse the dtype parts
41
+ const match = dtype.match(/^([<>|])([iufS])(\d+)$/);
42
+ if (!match)
43
+ return dtype;
44
+ const [, endian, typeChar, sizeStr] = match;
45
+ const size = parseInt(sizeStr, 10);
46
+ // Single-byte integers use "|" prefix
47
+ if ((typeChar === "i" || typeChar === "u") && size === 1) {
48
+ return `|${typeChar}${size}`;
49
+ }
50
+ return dtype;
51
+ }
52
+ /**
53
+ * Convert jsfive filter pipeline to Zarr compressor/filters metadata.
54
+ */
55
+ function decodeFilterPipeline(filterPipeline) {
56
+ const zarrFilters = [];
57
+ let compressor = null;
58
+ if (!filterPipeline) {
59
+ return { compressor, zarrFilters: null };
60
+ }
61
+ for (const filterInfo of filterPipeline) {
62
+ const filterId = filterInfo.get("filter_id");
63
+ const clientData = filterInfo.get("client_data");
64
+ if (filterId === 1) {
65
+ // H5Z_FILTER_DEFLATE (gzip)
66
+ compressor = {
67
+ id: "zlib",
68
+ level: clientData?.[0] ?? 4,
69
+ };
70
+ }
71
+ else if (filterId === 2) {
72
+ // H5Z_FILTER_SHUFFLE
73
+ zarrFilters.push({
74
+ id: "shuffle",
75
+ elementsize: clientData?.[0] ?? 4,
76
+ });
77
+ }
78
+ else if (filterId === 32001) {
79
+ // Blosc
80
+ const blosccnames = [
81
+ "blosclz",
82
+ "lz4",
83
+ "lz4hc",
84
+ "snappy",
85
+ "zlib",
86
+ "zstd",
87
+ ];
88
+ const cd = clientData || [];
89
+ compressor = {
90
+ id: "blosc",
91
+ cname: blosccnames[cd[4]] || "lz4",
92
+ clevel: cd[3] || 5,
93
+ shuffle: cd[5] || 0,
94
+ blocksize: cd[2] || 0,
95
+ };
96
+ }
97
+ else if (filterId === 32015) {
98
+ // Zstd
99
+ compressor = {
100
+ id: "zstd",
101
+ level: clientData?.[0] ?? 3,
102
+ };
103
+ }
104
+ else if (filterId === 3) {
105
+ // H5Z_FILTER_FLETCHER32 - checksum, not a compressor
106
+ }
107
+ else if (filterId === 4) {
108
+ // H5Z_FILTER_SZIP
109
+ }
110
+ }
111
+ return {
112
+ compressor,
113
+ zarrFilters: zarrFilters.length > 0 ? zarrFilters : null,
114
+ };
115
+ }
116
+ /**
117
+ * Encode a fill value for JSON serialization.
118
+ */
119
+ function encodeFillValue(fillValue, dtype) {
120
+ if (fillValue === null || fillValue === undefined) {
121
+ return null;
122
+ }
123
+ if (typeof fillValue === "number") {
124
+ if (Number.isNaN(fillValue))
125
+ return "NaN";
126
+ if (fillValue === Infinity)
127
+ return "Infinity";
128
+ if (fillValue === -Infinity)
129
+ return "-Infinity";
130
+ if (dtype.match(/[iu]\d/)) {
131
+ return Math.round(fillValue);
132
+ }
133
+ return fillValue;
134
+ }
135
+ if (typeof fillValue === "bigint") {
136
+ return Number(fillValue);
137
+ }
138
+ if (typeof fillValue === "string") {
139
+ return fillValue;
140
+ }
141
+ return 0;
142
+ }
143
+ /**
144
+ * Inline binary data, matching kerchunk's encoding:
145
+ * - If all bytes are < 128 (ASCII-safe), store as a raw character string
146
+ * - Otherwise, encode as "base64:" + base64
147
+ */
148
+ function inlineBytes(data) {
149
+ let asciiSafe = true;
150
+ for (let i = 0; i < data.length; i++) {
151
+ if (data[i] >= 128) {
152
+ asciiSafe = false;
153
+ break;
154
+ }
155
+ }
156
+ if (asciiSafe) {
157
+ let result = "";
158
+ for (let i = 0; i < data.length; i++) {
159
+ result += String.fromCharCode(data[i]);
160
+ }
161
+ return result;
162
+ }
163
+ if (typeof Buffer !== "undefined") {
164
+ return "base64:" + Buffer.from(data).toString("base64");
165
+ }
166
+ let binary = "";
167
+ for (let i = 0; i < data.length; i++) {
168
+ binary += String.fromCharCode(data[i]);
169
+ }
170
+ return "base64:" + btoa(binary);
171
+ }
172
+ /**
173
+ * Encode an array of strings in the json2 codec format used by kerchunk.
174
+ */
175
+ function encodeJson2(strings, shape) {
176
+ const parts = [...strings, "|O", shape];
177
+ return JSON.stringify(parts);
178
+ }
179
+ /**
180
+ * The json2 filter metadata used by kerchunk for vlen string data.
181
+ */
182
+ const JSON2_FILTER = {
183
+ allow_nan: true,
184
+ check_circular: true,
185
+ encoding: "utf-8",
186
+ ensure_ascii: true,
187
+ id: "json2",
188
+ indent: null,
189
+ separators: [",", ":"],
190
+ skipkeys: false,
191
+ sort_keys: true,
192
+ strict: true,
193
+ };
194
+ /**
195
+ * JSON.stringify with keys sorted alphabetically at all levels.
196
+ */
197
+ function jsonStringifySorted(obj) {
198
+ return JSON.stringify(obj, (_key, value) => {
199
+ if (value && typeof value === "object" && !Array.isArray(value)) {
200
+ const sorted = {};
201
+ for (const k of Object.keys(value).sort()) {
202
+ sorted[k] = value[k];
203
+ }
204
+ return sorted;
205
+ }
206
+ return value;
207
+ });
208
+ }
209
+ /**
210
+ * Serialize a ZarrArrayMeta to JSON, matching kerchunk's output.
211
+ */
212
+ function serializeZarrayMeta(meta) {
213
+ let json = jsonStringifySorted(meta);
214
+ if (/[<>]f\d/.test(meta.dtype) &&
215
+ typeof meta.fill_value === "number" &&
216
+ Number.isFinite(meta.fill_value) &&
217
+ meta.fill_value === Math.round(meta.fill_value)) {
218
+ const intStr = String(Math.round(meta.fill_value));
219
+ json = json.replace(`"fill_value":${intStr}`, `"fill_value":${intStr}.0`);
220
+ }
221
+ return json;
222
+ }
223
+ /**
224
+ * Get storage info from a vendored DataObjects instance.
225
+ * Reads layout class, contiguous offset/size, or chunk address/dims/shape.
226
+ */
227
+ function getStorageInfo(dataobjects) {
228
+ const DATA_STORAGE_MSG_TYPE = 0x0008;
229
+ const msg = dataobjects.find_msg_type(DATA_STORAGE_MSG_TYPE)[0];
230
+ if (!msg)
231
+ return { layoutClass: -1 };
232
+ const relOffset = msg.get("offset_to_message") - dataobjects.bufStart;
233
+ const props = dataobjects._get_data_message_properties(relOffset);
234
+ const { version, dims, layout_class: layoutClass, property_offset } = props;
235
+ if (layoutClass === 1) {
236
+ // Contiguous storage
237
+ const localBuf = dataobjects.buf;
238
+ const dataOffset = struct.unpack_from("<Q", localBuf, property_offset)[0];
239
+ if (dataOffset === UNDEFINED_ADDRESS || dataOffset >= Number.MAX_SAFE_INTEGER * 0.99) {
240
+ return { layoutClass: 1 };
241
+ }
242
+ let size;
243
+ if (version === 3 || version === 4) {
244
+ size = struct.unpack_from("<Q", localBuf, property_offset + 8)[0];
245
+ }
246
+ else {
247
+ const shape = dataobjects.shape;
248
+ const dtype = dataobjects.dtype;
249
+ let itemSize = 0;
250
+ if (typeof dtype === "string") {
251
+ const m = dtype.match(/\d+$/);
252
+ itemSize = m ? parseInt(m[0], 10) : 1;
253
+ }
254
+ else {
255
+ itemSize = 8;
256
+ }
257
+ size = shape.reduce((a, b) => a * b, 1) * itemSize;
258
+ }
259
+ return { layoutClass: 1, contiguousOffset: dataOffset, contiguousSize: size };
260
+ }
261
+ else if (layoutClass === 2) {
262
+ // Use the vendored DataObjects' chunk params
263
+ dataobjects._get_chunk_params();
264
+ const chunkAddress = dataobjects._chunk_address;
265
+ const chunkDims = dataobjects._chunk_dims;
266
+ const chunkShape = dataobjects._chunks;
267
+ if (chunkAddress == null || chunkAddress === UNDEFINED_ADDRESS) {
268
+ return { layoutClass: 2 };
269
+ }
270
+ return {
271
+ layoutClass: 2,
272
+ chunkAddress: chunkAddress,
273
+ chunkDims: chunkDims,
274
+ chunkShape: chunkShape,
275
+ };
276
+ }
277
+ return { layoutClass };
278
+ }
279
+ /**
280
+ * Get chunk locations from a B-tree for chunked datasets.
281
+ * Now async – creates the B-tree via Source.fetch().
282
+ */
283
+ async function getChunkLocations(source, btreeAddress, chunkDims, dataShape, chunkShape) {
284
+ const btree = await BTreeV1RawDataChunks.create(source, btreeAddress, chunkDims);
285
+ const results = [];
286
+ const leafNodes = btree.all_nodes.get(0);
287
+ if (!leafNodes)
288
+ return results;
289
+ for (const node of leafNodes) {
290
+ const nodeKeys = node.keys;
291
+ const nodeAddresses = node.addresses;
292
+ const nkeys = nodeKeys.length;
293
+ for (let ik = 0; ik < nkeys; ik++) {
294
+ const nodeKey = nodeKeys[ik];
295
+ const addr = nodeAddresses[ik];
296
+ const chunkOffset = nodeKey.chunk_offset;
297
+ const chunkSize = nodeKey.chunk_size;
298
+ const chunkIndex = chunkOffset.slice(0, -1).map((co, d) => Math.floor(co / chunkShape[d]));
299
+ while (chunkIndex.length < dataShape.length) {
300
+ chunkIndex.push(0);
301
+ }
302
+ while (chunkIndex.length > dataShape.length) {
303
+ chunkIndex.pop();
304
+ }
305
+ results.push({
306
+ chunkIndex,
307
+ offset: addr,
308
+ size: chunkSize,
309
+ });
310
+ }
311
+ }
312
+ return results;
313
+ }
314
+ const ATTRIBUTE_MSG_TYPE = 0x000C;
315
+ const DATATYPE_ENUMERATED = 8;
316
+ /**
317
+ * Get the raw HDF5 datatype class for each attribute in a DataObjects instance.
318
+ * Returns a Map from attribute name to datatype class number.
319
+ * Used to detect enum (boolean) attributes.
320
+ */
321
+ function getAttrDatatypeClasses(dataobjects) {
322
+ const result = new Map();
323
+ const attrMsgs = dataobjects.find_msg_type(ATTRIBUTE_MSG_TYPE);
324
+ const fh = dataobjects.buf;
325
+ const bufStart = dataobjects.bufStart;
326
+ for (const msg of attrMsgs) {
327
+ let offset = msg.get("offset_to_message") - bufStart;
328
+ const version = struct.unpack_from("<B", fh, offset)[0];
329
+ let nameSize;
330
+ let paddingMultiple;
331
+ if (version === 1) {
332
+ // ATTR_MSG_HEADER_V1: version(B) + reserved(B) + name_size(H) + datatype_size(H) + dataspace_size(H) = 8 bytes
333
+ nameSize = struct.unpack_from("<H", fh, offset + 2)[0];
334
+ paddingMultiple = 8;
335
+ offset += 8; // skip header
336
+ }
337
+ else if (version === 3) {
338
+ // ATTR_MSG_HEADER_V3: version(B) + flags(B) + name_size(H) + datatype_size(H) + dataspace_size(H) + encoding(B) = 9 bytes
339
+ nameSize = struct.unpack_from("<H", fh, offset + 2)[0];
340
+ paddingMultiple = 1;
341
+ offset += 9; // skip header
342
+ }
343
+ else {
344
+ continue;
345
+ }
346
+ // Read attribute name
347
+ const nameBytes = struct.unpack_from("<" + nameSize.toFixed() + "s", fh, offset)[0];
348
+ const name = nameBytes.replace(/\x00$/, "");
349
+ // Skip past padded name
350
+ const paddedNameSize = paddingMultiple > 1
351
+ ? Math.ceil(nameSize / paddingMultiple) * paddingMultiple
352
+ : nameSize;
353
+ offset += paddedNameSize;
354
+ // Read raw datatype class from first byte of datatype message
355
+ const classAndVersion = struct.unpack_from("<B", fh, offset)[0];
356
+ const datatypeClass = classAndVersion & 0x0f;
357
+ result.set(name, datatypeClass);
358
+ }
359
+ return result;
360
+ }
361
+ /**
362
+ * Translate the content of one HDF5 file into Zarr metadata
363
+ * following the Zarr References Specification v1.
364
+ *
365
+ * Ported from kerchunk's SingleHdf5ToZarr Python class.
366
+ * Uses vendored async HDF5 parser with Source-based partial reads.
367
+ */
368
+ export class SingleHdf5ToZarr {
369
+ source;
370
+ h5File;
371
+ url;
372
+ inlineThreshold;
373
+ refs;
374
+ /**
375
+ * @param source - An AsyncReadable instance for reading the HDF5 file.
376
+ * @param options - Configuration options.
377
+ */
378
+ constructor(source, options = {}) {
379
+ this.source = source;
380
+ this.url = options.url ?? null;
381
+ this.inlineThreshold = options.inlineThreshold ?? 300;
382
+ this.refs = {};
383
+ }
384
+ /**
385
+ * Translate the HDF5 file content into a Zarr reference spec.
386
+ */
387
+ async translate() {
388
+ this.h5File = await Hdf5File.create(this.source);
389
+ this.refs = {};
390
+ // Root group
391
+ this.refs[".zgroup"] = jsonStringifySorted({ zarr_format: 2 });
392
+ await this.transferAttrs(this.h5File, "");
393
+ // Walk all items recursively
394
+ await this.walkGroup(this.h5File, "");
395
+ return {
396
+ version: 1,
397
+ refs: this.refs,
398
+ };
399
+ }
400
+ /**
401
+ * Recursively walk an HDF5 group, processing all children.
402
+ */
403
+ async walkGroup(group, parentPath) {
404
+ const keys = group.keys;
405
+ for (const key of keys) {
406
+ const childPath = parentPath ? `${parentPath}/${key}` : key;
407
+ let child;
408
+ try {
409
+ child = await group.get(key);
410
+ }
411
+ catch {
412
+ continue;
413
+ }
414
+ if (!child)
415
+ continue;
416
+ if (child instanceof Hdf5Dataset) {
417
+ await this.processDataset(childPath, child);
418
+ }
419
+ else if (child instanceof Hdf5Group) {
420
+ await this.processGroup(childPath, child);
421
+ await this.walkGroup(child, childPath);
422
+ }
423
+ }
424
+ }
425
+ /**
426
+ * Process an HDF5 group into Zarr group metadata.
427
+ */
428
+ async processGroup(path, group) {
429
+ this.refs[`${path}/.zgroup`] = jsonStringifySorted({
430
+ zarr_format: 2,
431
+ });
432
+ await this.transferAttrs(group, path);
433
+ }
434
+ /**
435
+ * Process an HDF5 dataset into Zarr array metadata and chunk references.
436
+ */
437
+ async processDataset(path, dataset) {
438
+ const dataobjects = dataset._dataobjects;
439
+ const shape = dataobjects.shape;
440
+ if (!shape || shape.length === 0) {
441
+ // Scalar dataset
442
+ await this.processScalarDataset(path, dataset, dataobjects);
443
+ return;
444
+ }
445
+ const rawDtype = dataobjects.dtype;
446
+ const isVlen = Array.isArray(rawDtype) &&
447
+ (rawDtype[0] === "VLEN_STRING" || rawDtype[0] === "VLEN_SEQUENCE");
448
+ const isFixedString = typeof rawDtype === "string" && /^S\d+/.test(rawDtype);
449
+ const dtype = jsfiveDtypeToZarr(rawDtype);
450
+ const chunks = dataobjects.chunks || [...shape];
451
+ const filterPipeline = dataobjects.filter_pipeline;
452
+ const { compressor, zarrFilters } = decodeFilterPipeline(filterPipeline);
453
+ // Determine fill value
454
+ let fillValue = null;
455
+ if (dtype.startsWith("|S")) {
456
+ fillValue = "";
457
+ }
458
+ else if (dtype === "|O") {
459
+ fillValue = null;
460
+ }
461
+ else if (/[<>|]f\d/.test(dtype)) {
462
+ fillValue = 0.0;
463
+ }
464
+ else {
465
+ fillValue = 0;
466
+ }
467
+ fillValue = encodeFillValue(fillValue, dtype);
468
+ const zarrMeta = {
469
+ zarr_format: 2,
470
+ shape: [...shape],
471
+ chunks: [...chunks],
472
+ dtype,
473
+ compressor,
474
+ fill_value: fillValue,
475
+ order: "C",
476
+ filters: zarrFilters,
477
+ };
478
+ this.refs[`${path}/.zarray`] = serializeZarrayMeta(zarrMeta);
479
+ // Transfer attributes
480
+ await this.transferAttrs(dataset, path);
481
+ // Add _ARRAY_DIMENSIONS
482
+ const dims = this.getArrayDims(dataset, path, shape);
483
+ const existingAttrsKey = `${path}/.zattrs`;
484
+ const existingAttrs = this.refs[existingAttrsKey]
485
+ ? JSON.parse(this.refs[existingAttrsKey])
486
+ : {};
487
+ existingAttrs["_ARRAY_DIMENSIONS"] = dims;
488
+ this.refs[existingAttrsKey] = jsonStringifySorted(existingAttrs);
489
+ // Process data: vlen strings get inlined, others get file references
490
+ if (isVlen) {
491
+ await this.inlineVlenData(path, dataset, shape, chunks);
492
+ }
493
+ else if (isFixedString) {
494
+ await this.inlineFixedStringData(path, dataset, shape, chunks, rawDtype);
495
+ }
496
+ else {
497
+ await this.processChunkReferences(path, dataobjects, shape, chunks, dtype);
498
+ }
499
+ }
500
+ /**
501
+ * Process a scalar dataset (shape=[]).
502
+ */
503
+ async processScalarDataset(path, dataset, dataobjects) {
504
+ const rawDtype = dataobjects.dtype;
505
+ const isVlen = Array.isArray(rawDtype) &&
506
+ (rawDtype[0] === "VLEN_STRING" || rawDtype[0] === "VLEN_SEQUENCE");
507
+ const dtype = jsfiveDtypeToZarr(rawDtype);
508
+ let fillValue = null;
509
+ if (dtype === "|O")
510
+ fillValue = null;
511
+ else if (/[<>|]f\d/.test(dtype))
512
+ fillValue = 0.0;
513
+ else
514
+ fillValue = 0;
515
+ fillValue = encodeFillValue(fillValue, dtype);
516
+ const zarrMeta = {
517
+ zarr_format: 2,
518
+ shape: [],
519
+ chunks: [],
520
+ dtype,
521
+ compressor: null,
522
+ fill_value: fillValue,
523
+ order: "C",
524
+ filters: null,
525
+ };
526
+ if (isVlen) {
527
+ zarrMeta.dtype = "|O";
528
+ zarrMeta.fill_value = null;
529
+ zarrMeta.filters = [JSON2_FILTER];
530
+ }
531
+ this.refs[`${path}/.zarray`] = serializeZarrayMeta(zarrMeta);
532
+ await this.transferAttrs(dataset, path);
533
+ // Add _ARRAY_DIMENSIONS
534
+ const existingAttrsKey = `${path}/.zattrs`;
535
+ const existingAttrs = this.refs[existingAttrsKey]
536
+ ? JSON.parse(this.refs[existingAttrsKey])
537
+ : {};
538
+ existingAttrs["_ARRAY_DIMENSIONS"] = [];
539
+ this.refs[existingAttrsKey] = jsonStringifySorted(existingAttrs);
540
+ // Inline data
541
+ if (isVlen) {
542
+ try {
543
+ const data = await dataobjects.get_data();
544
+ const strings = Array.isArray(data) ? data.map(String) : [String(data)];
545
+ this.refs[`${path}/0`] = encodeJson2(strings, []);
546
+ }
547
+ catch {
548
+ // Cannot read scalar vlen data
549
+ }
550
+ }
551
+ else {
552
+ try {
553
+ const data = await dataobjects.get_data();
554
+ if (data !== null && data !== undefined) {
555
+ const strings = Array.isArray(data) ? data.map(String) : [String(data)];
556
+ this.refs[`${path}/0`] = inlineBytes(new Uint8Array(new Float64Array(strings.map(Number)).buffer));
557
+ }
558
+ }
559
+ catch {
560
+ // Cannot read scalar data
561
+ }
562
+ }
563
+ }
564
+ /**
565
+ * Inline vlen string data using json2 codec encoding.
566
+ */
567
+ async inlineVlenData(path, dataset, shape, chunks) {
568
+ try {
569
+ const data = await dataset._dataobjects.get_data();
570
+ if (data === null || data === undefined)
571
+ return;
572
+ const strings = Array.isArray(data) ? data.map(String) : [String(data)];
573
+ const key = `${path}/${chunks.map(() => "0").join(".")}`;
574
+ this.refs[key] = encodeJson2(strings, shape);
575
+ // Update .zarray for vlen string codec
576
+ const zarrMeta = JSON.parse(this.refs[`${path}/.zarray`]);
577
+ zarrMeta.dtype = "|O";
578
+ zarrMeta.fill_value = null;
579
+ zarrMeta.filters = [JSON2_FILTER];
580
+ zarrMeta.compressor = null;
581
+ this.refs[`${path}/.zarray`] = serializeZarrayMeta(zarrMeta);
582
+ }
583
+ catch {
584
+ // Cannot read vlen data
585
+ }
586
+ }
587
+ /**
588
+ * Inline fixed-length string data.
589
+ */
590
+ async inlineFixedStringData(path, dataset, shape, chunks, rawDtype) {
591
+ try {
592
+ const data = await dataset._dataobjects.get_data();
593
+ if (data === null || data === undefined)
594
+ return;
595
+ const strings = Array.isArray(data) ? data.map(String) : [String(data)];
596
+ const key = `${path}/${chunks.map(() => "0").join(".")}`;
597
+ // For fixed-length strings, inline the raw bytes
598
+ const sizeMatch = rawDtype.match(/S(\d+)/);
599
+ const strSize = sizeMatch ? parseInt(sizeMatch[1], 10) : 1;
600
+ const totalSize = strings.length * strSize;
601
+ const buf = new Uint8Array(totalSize);
602
+ const encoder = new TextEncoder();
603
+ for (let i = 0; i < strings.length; i++) {
604
+ const encoded = encoder.encode(strings[i]);
605
+ buf.set(encoded.subarray(0, strSize), i * strSize);
606
+ }
607
+ this.refs[key] = inlineBytes(buf);
608
+ }
609
+ catch {
610
+ // Cannot read fixed string data
611
+ }
612
+ }
613
+ /**
614
+ * Process chunk references for non-string datasets.
615
+ * Uses file byte offset references for chunked and contiguous data.
616
+ */
617
+ async processChunkReferences(path, dataobjects, shape, chunks, dtype) {
618
+ const storageInfo = getStorageInfo(dataobjects);
619
+ if (storageInfo.layoutClass === 1 && storageInfo.contiguousOffset !== undefined) {
620
+ // Contiguous storage - single chunk reference
621
+ const key = `${path}/${shape.map(() => "0").join(".")}`;
622
+ const offset = storageInfo.contiguousOffset;
623
+ const size = storageInfo.contiguousSize;
624
+ if (size <= this.inlineThreshold) {
625
+ const ab = await fetchRange(this.source, offset, size);
626
+ this.refs[key] = inlineBytes(new Uint8Array(ab));
627
+ }
628
+ else {
629
+ this.refs[key] = [this.url, offset, size];
630
+ }
631
+ }
632
+ else if (storageInfo.layoutClass === 2 && storageInfo.chunkAddress !== undefined) {
633
+ // Chunked storage - get chunk locations from B-tree
634
+ const filterPipeline = dataobjects.filter_pipeline;
635
+ const hasFilters = filterPipeline && filterPipeline.length > 0;
636
+ // Item size from dtype
637
+ let itemSize = 1;
638
+ const sizeMatch = dtype.match(/\d+$/);
639
+ if (sizeMatch)
640
+ itemSize = parseInt(sizeMatch[0], 10);
641
+ const chunkElements = chunks.reduce((a, b) => a * b, 1);
642
+ const uncompressedChunkBytes = chunkElements * itemSize;
643
+ // Check for fletcher32 (filter_id=3) to adjust sizes
644
+ let hasF32 = false;
645
+ if (filterPipeline) {
646
+ for (const f of filterPipeline) {
647
+ if (f.get("filter_id") === 3) {
648
+ hasF32 = true;
649
+ break;
650
+ }
651
+ }
652
+ }
653
+ const chunkLocations = await getChunkLocations(this.source, storageInfo.chunkAddress, storageInfo.chunkDims, shape, storageInfo.chunkShape);
654
+ for (const loc of chunkLocations) {
655
+ const key = `${path}/${loc.chunkIndex.join(".")}`;
656
+ let size = hasFilters ? loc.size : uncompressedChunkBytes;
657
+ if (hasF32) {
658
+ size -= 4; // Strip fletcher32 checksum
659
+ }
660
+ if (size <= this.inlineThreshold) {
661
+ const ab = await fetchRange(this.source, loc.offset, size);
662
+ this.refs[key] = inlineBytes(new Uint8Array(ab));
663
+ }
664
+ else {
665
+ this.refs[key] = [this.url, loc.offset, size];
666
+ }
667
+ }
668
+ }
669
+ else if (storageInfo.layoutClass === 1) {
670
+ // Contiguous but no data written - empty dataset, no refs needed
671
+ }
672
+ }
673
+ /**
674
+ * Get dimension names for a dataset.
675
+ */
676
+ getArrayDims(dataset, path, shape) {
677
+ if (!shape || shape.length === 0)
678
+ return [];
679
+ const rank = shape.length;
680
+ const dims = [];
681
+ for (let i = 0; i < rank; i++) {
682
+ dims.push(`phony_dim_${i}`);
683
+ }
684
+ return dims;
685
+ }
686
+ /**
687
+ * Transfer attributes from an HDF5 object to the Zarr .zattrs.
688
+ */
689
+ async transferAttrs(h5obj, path) {
690
+ const attrs = {};
691
+ let rawAttrs;
692
+ try {
693
+ rawAttrs = await h5obj.get_attrs();
694
+ if (!rawAttrs || typeof rawAttrs !== "object")
695
+ return;
696
+ }
697
+ catch {
698
+ return;
699
+ }
700
+ // Detect which attributes are HDF5 enum type (for boolean conversion)
701
+ let enumAttrs = new Map();
702
+ try {
703
+ const dataobjects = h5obj._dataobjects;
704
+ if (dataobjects) {
705
+ enumAttrs = getAttrDatatypeClasses(dataobjects);
706
+ }
707
+ }
708
+ catch {
709
+ // ignore
710
+ }
711
+ for (const [name, value] of Object.entries(rawAttrs)) {
712
+ if (HIDDEN_ATTRS.has(name))
713
+ continue;
714
+ if (name === "_FillValue")
715
+ continue;
716
+ let v = value;
717
+ // Handle bytes/strings
718
+ if (v === null || v === undefined) {
719
+ v = "";
720
+ }
721
+ if (v === "DIMENSION_SCALE")
722
+ continue;
723
+ // Convert HDF5 enum (boolean) attributes: 0 → false, 1 → true
724
+ if (enumAttrs.get(name) === DATATYPE_ENUMERATED) {
725
+ if (v === 0)
726
+ v = false;
727
+ else if (v === 1)
728
+ v = true;
729
+ }
730
+ // Convert typed arrays to plain arrays
731
+ if (ArrayBuffer.isView(v) && !(v instanceof DataView)) {
732
+ v = Array.from(v);
733
+ }
734
+ // Convert single-element arrays to scalars
735
+ if (Array.isArray(v) && v.length === 1) {
736
+ v = v[0];
737
+ }
738
+ attrs[name] = v;
739
+ }
740
+ if (Object.keys(attrs).length > 0) {
741
+ const key = path ? `${path}/.zattrs` : ".zattrs";
742
+ this.refs[key] = jsonStringifySorted(attrs);
743
+ }
744
+ }
745
+ }