@@ -23,6 +23,26 @@ export const h5wasm = {
2323
2424The Emscripten filesystem is important for operations, and it can be accessed after the WASM is loaded as below.
2525
26+ ## Contents
27+
28+ - [ QuickStart] ( #quickstart )
29+ - [ Browser (no-build)] ( #browser-no-build )
30+ - [ Worker usage] ( #worker-usage )
31+ - [ Browser target (build system)] ( #browser-target-build-system )
32+ - [ Node.js] ( #nodejs )
33+ - [ User Guide] ( #user-guide )
34+ - [ Opening a file] ( #opening-a-file )
35+ - [ Reading] ( #reading )
36+ - [ Writing] ( #writing )
37+ - [ Editing] ( #editing )
38+ - [ SWMR (single writer multiple readers)] ( #swmr-single-writer-multiple-readers )
39+ - [ Links] ( #links )
40+ - [ Library version bounds (libver)] ( #library-version-bounds-libver )
41+ - [ Web Helpers] ( #web-helpers )
42+ - [ Persistent file store (web)] ( #persistent-file-store-web )
43+
44+ # QuickStart
45+
2646## Browser (no-build)
2747``` js
2848import h5wasm from " https://cdn.jsdelivr.net/npm/h5wasm@latest/dist/esm/hdf5_hl.js" ;
@@ -47,7 +67,7 @@ let f = new h5wasm.File("sans59510.nxs.ngv", "r");
4767// File {path: "/", file_id: 72057594037927936n, filename: "data.h5", mode: "r"}
4868```
4969
50- ### Worker usage
70+ ## Worker usage
5171Since ESM is not supported in all web worker contexts (e.g. Firefox), an additional ``` ./dist/iife/h5wasm.js ``` is provided in the package for ` h5wasm>=0.4.8 ` ; it can be loaded in a worker and used as in the example below (which uses the WORKERFS file system for random access on local files):
5272``` js
5373// worker.js
@@ -106,8 +126,33 @@ File {
106126*/
107127```
108128
109- ## Usage
129+ # User Guide
110130_ (all examples are written in ESM - for Typescript some type casting is probably required, as ` get ` returns either Group or Dataset)_
131+
132+ ## Opening a file
133+
134+ ``` js
135+ new h5wasm.File (filename, mode? , options? )
136+ ` ` `
137+
138+ | Argument | Type | Default | Description |
139+ |---|---|---|---|
140+ | ` filename` | ` string` | — | Path to the HDF5 file |
141+ | ` mode` | ` string` | ` " r" ` | Access mode (see table below) |
142+ | ` options .track_order ` | ` boolean` | ` false ` | Preserve insertion order of groups and attributes |
143+ | ` options .libver ` | ` string \| [string, string]` | — | Library version bound(s) for new objects (see [libver](#library-version-bounds-libver)) |
144+
145+ Available modes:
146+
147+ | Mode | Description |
148+ |---|---|
149+ | ` " r" ` | Read-only |
150+ | ` " a" ` | Read/write (file must exist) |
151+ | ` " w" ` | Create / truncate |
152+ | ` " x" ` | Create, fail if exists |
153+ | ` " Sw" ` | SWMR write |
154+ | ` " Sr" ` | SWMR read |
155+
111156### Reading
112157` ` ` js
113158let f = new h5wasm.File (" sans59510.nxs.ngv" , " r" );
@@ -189,47 +234,6 @@ data.to_array()
189234*/
190235` ` `
191236
192- ### SWMR (single writer multiple readers)
193-
194- SWMR requires a file created with at least ` libver: "v110" ` and a chunked, extensible dataset.
195-
196- ``` js
197- // First, create a SWMR-compatible file
198- const f = new h5wasm.File (" swmr.h5" , " w" , { libver: " v110" });
199- f .create_dataset ({
200- name: " data" ,
201- data: new Float32Array ([1 , 2 , 3 ]),
202- maxshape: [null ], // unlimited along first axis
203- chunks: [10 ]
204- });
205- f .flush ();
206- f .close ();
207-
208- // Open for SWMR write and SWMR read simultaneously
209- const f_write = new h5wasm.File (" swmr.h5" , " Sw" );
210- const f_read = new h5wasm.File (" swmr.h5" , " Sr" );
211-
212- const dset_write = f_write .get (" data" );
213- const dset_read = f_read .get (" data" );
214-
215- // Extend the dataset and write new values
216- dset_write .resize ([6 ]);
217- dset_write .write_slice ([[3 , 6 ]], new Float32Array ([4 , 5 , 6 ]));
218- f_write .flush ();
219-
220- // The read handle still sees the old shape until refreshed
221- dset_read .shape ;
222- // [3]
223- dset_read .refresh ();
224- dset_read .shape ;
225- // [6]
226- dset_read .value ;
227- // Float32Array(6) [1, 2, 3, 4, 5, 6]
228-
229- f_write .close ();
230- f_read .close ();
231- ```
232-
233237### Writing
234238` ` ` js
235239let new_file = new h5wasm.File (" myfile.h5" , " w" );
@@ -300,6 +304,56 @@ new_file.close()
300304
301305` ` `
302306
307+ ### Editing
308+ One can also open an existing file and write to it:
309+ ` ` ` js
310+ let f = new h5wasm.File (" myfile.h5" , " a" );
311+
312+ f .create_attribute (" new_attr" , " something wicked this way comes" );
313+ f .close ()
314+ ` ` `
315+
316+ ### SWMR (single writer multiple readers)
317+
318+ SWMR requires a file created with at least ` libver: " v110" ` and a chunked, extensible dataset.
319+
320+ ` ` ` js
321+ // First, create a SWMR-compatible file
322+ const f = new h5wasm.File (" swmr.h5" , " w" , { libver: " v110" });
323+ f .create_dataset ({
324+ name: " data" ,
325+ data: new Float32Array ([1 , 2 , 3 ]),
326+ maxshape: [null ], // unlimited along first axis
327+ chunks: [10 ]
328+ });
329+ f .flush ();
330+ f .close ();
331+
332+ // Open for SWMR write and SWMR read simultaneously
333+ const f_write = new h5wasm.File (" swmr.h5" , " Sw" );
334+ const f_read = new h5wasm.File (" swmr.h5" , " Sr" );
335+
336+ const dset_write = f_write .get (" data" );
337+ const dset_read = f_read .get (" data" );
338+
339+ // Extend the dataset and write new values
340+ dset_write .resize ([6 ]);
341+ dset_write .write_slice ([[3 , 6 ]], new Float32Array ([4 , 5 , 6 ]));
342+ f_write .flush ();
343+
344+ // The read handle still sees the old shape until refreshed
345+ dset_read .shape ;
346+ // [3]
347+ dset_read .refresh ();
348+ dset_read .shape ;
349+ // [6]
350+ dset_read .value ;
351+ // Float32Array(6) [1, 2, 3, 4, 5, 6]
352+
353+ f_write .close ();
354+ f_read .close ();
355+ ` ` `
356+
303357### Links
304358` ` ` js
305359let new_file = new h5wasm.File (" myfile.h5" , " w" );
@@ -309,12 +363,12 @@ new_file.get("entry").create_dataset({name: "auto", data: [3.1, 4.1, 0.0, -1.0]}
309363// create a soft link in root:
310364new_file .create_soft_link (" /entry/auto" , " my_soft_link" );
311365new_file .get (" my_soft_link" ).value ;
312- // Float64Array(4) [3.1, 4.1, 0, -1]
366+ // Float64Array(4) [3.1, 4.1, 0, -1]
313367
314368// create a hard link:
315369new_file .create_hard_link (" /entry/auto" , " my_hard_link" );
316370new_file .get (" my_hard_link" ).value ;
317- // Float64Array(4) [3.1, 4.1, 0, -1]
371+ // Float64Array(4) [3.1, 4.1, 0, -1]
318372
319373// create an external link:
320374new_file .create_external_link (" other_file.h5" , " other_dataset" , " my_external_link" );
@@ -326,7 +380,7 @@ new_file.create_group("links");
326380const links_group = new_file .get (" links" );
327381links_group .create_soft_link (" /entry/auto" , " soft_link" );
328382new_file .get (" /links/soft_link" ).value ;
329- // Float64Array(4) [3.1, 4.1, 0, -1]
383+ // Float64Array(4) [3.1, 4.1, 0, -1]
330384new_file .get_link (" /links/soft_link" );
331385// "/entry/auto"
332386new_file .get_link (" /entry/auto" );
@@ -335,14 +389,34 @@ new_file.get_link("/entry/auto");
335389new_file .close ()
336390` ` `
337391
338- ### Edit
339- One can also open an existing file and write to it:
392+ ## Library version bounds (libver)
393+
394+ HDF5 supports controlling the minimum and maximum library version used when writing objects to a file, via ` libver` . This can be set using the ` FileOptions` object when opening a file.
395+
396+ Valid ` libver` string values are: ` " earliest" ` , ` " v108" ` , ` " v110" ` , ` " v112" ` , ` " v114" ` , ` " v200" ` , and ` " latest" ` .
397+
398+ ### Single bound (low and high set to the same value)
340399` ` ` js
341- let f = new h5wasm.File (" myfile.h5" , " a" );
400+ // Require at least HDF5 v1.10 format features:
401+ const f = new h5wasm.File (" myfile.h5" , " w" , { libver: " v110" });
402+ f .create_dataset ({ name: " data" , data: new Float32Array ([1 , 2 , 3 ]) });
403+ f .close ();
342404
343- f .create_attribute (" new_attr" , " something wicked this way comes" );
344- f .close ()
405+ // Read back the actual bounds stored in the file:
406+ const f_read = new h5wasm.File (" myfile.h5" , " r" );
407+ f_read .libver ;
408+ // ["v110", "v110"]
409+ f_read .close ();
410+ ` ` `
411+
412+ ### Asymmetric bounds (different low and high)
413+ ` ` ` js
414+ // Allow any format from v1.10 up to the latest supported version:
415+ const f = new h5wasm.File (" myfile.h5" , " w" , { libver: [" v110" , " latest" ] });
416+ f .create_dataset ({ name: " data" , data: new Float32Array ([1 , 2 , 3 ]) });
417+ f .close ();
345418` ` `
419+
346420## Web Helpers
347421Optional, to support uploads and downloads
348422
@@ -379,31 +453,3 @@ FS.syncfs(true, (e) => {console.log(e)});
379453// to push all current files in /home/web_user to IndexedDB, e.g. when closing your application:
380454FS .syncfs (false , (e ) => {console .log (e)})
381455` ` `
382-
383- ## Library version bounds (libver)
384-
385- HDF5 supports controlling the minimum and maximum library version used when writing objects to a file, via ` libver ` . This can be set using the ` FileOptions ` object when opening a file.
386-
387- Valid ` libver ` string values are: ` "earliest" ` , ` "v108" ` , ` "v110" ` , ` "v112" ` , ` "v114" ` , ` "v200" ` , and ` "latest" ` .
388-
389- ### Single bound (low and high set to the same value)
390- ``` js
391- // Require at least HDF5 v1.10 format features:
392- const f = new h5wasm.File (" myfile.h5" , " w" , { libver: " v110" });
393- f .create_dataset ({ name: " data" , data: new Float32Array ([1 , 2 , 3 ]) });
394- f .close ();
395-
396- // Read back the actual bounds stored in the file:
397- const f_read = new h5wasm.File (" myfile.h5" , " r" );
398- f_read .libver ;
399- // ["v110", "v110"]
400- f_read .close ();
401- ```
402-
403- ### Asymmetric bounds (different low and high)
404- ``` js
405- // Allow any format from v1.10 up to the latest supported version:
406- const f = new h5wasm.File (" myfile.h5" , " w" , { libver: [" v110" , " latest" ] });
407- f .create_dataset ({ name: " data" , data: new Float32Array ([1 , 2 , 3 ]) });
408- f .close ();
409- ```
0 commit comments