-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwrite_attribute.nim
35 lines (32 loc) · 1.12 KB
/
write_attribute.nim
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
import nimhdf5
import std / strutils
proc writeAttr(h5o: H5Dataset | H5Group, key, val: string, dtype: string) =
template wr(fn): untyped {.dirty.} =
let dval = fn val
h5o.attrs[key] = dval
case dtype
of "int", "i", "d", "int64": wr(parseInt)
of "float", "f", "float64": wr(parseFloat)
of "string", "str": h5o.attrs[key] = val
of "bool", "b": wr(parseBool)
else:
doAssert false, "Unsupported data type : " & $dtype
proc main(f, obj, key, val: string, dtype: string) =
## Writes the attribute `key` with `val` to the HDF5 object `obj`
## in the file `f`
withH5(f, "rw"):
if obj in h5f:
if h5f.isGroup(obj):
var grp = h5f[obj.grp_str]
echo "[INFO] Writing attribute to group: ", grp.name
grp.writeAttr(key, val, dtype)
else: # must be a dataset
doAssert h5f.isDataset(obj)
var dset = h5f[obj.dset_str]
echo "[INFO] Writing attribute to dataset: ", dset.name
dset.writeAttr(key, val, dtype)
else:
echo "[INFO] No group or dataset of name: ", obj, " in file ", f
when isMainModule:
import cligen
dispatch main